Guest User

Untitled

a guest
Jul 22nd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.66 KB | None | 0 0
  1. package org.jaghax.server.protocol;
  2.  
  3. import org.jaghax.Client;
  4. import org.jaghax.server.rs.net.ByteArrayPool;
  5. import org.jaghax.server.rs.net.ByteBufferPool;
  6. import org.jaghax.server.util.CacheFile;
  7. import org.jaghax.server.util.Global;
  8.  
  9. import java.nio.ByteBuffer;
  10. import java.util.zip.CRC32;
  11.  
  12. /**
  13. * {@code JaggrabProtocolHandler}
  14. *
  15. * @author James Lawrence
  16. * @version 1
  17. */
  18. public class JaggrabProtocolHandler implements ProtocolHandler {
  19. private static JaggrabProtocolHandler instance;
  20. private static int[] cache_crc;
  21. private static int cache_hash;
  22.  
  23. /**
  24. * Returns the singleton of this ProtocolHandler
  25. *
  26. * @return the only instance of this protocol handler
  27. */
  28. public static JaggrabProtocolHandler getInstance() {
  29. return instance;
  30. }
  31.  
  32. static {
  33. instance = new JaggrabProtocolHandler();
  34.  
  35. // Initialise cache CRCs
  36. cache_crc = new int[9];
  37. CRC32 crc = new CRC32();
  38. for (int i = 0; i < 9; i++) {
  39. byte[] data = CacheFile.cache[0].readEntry(i);
  40. if (data == null) {
  41. System.err.println("cache[0][" + i + "] is null :S");
  42. continue;
  43. }
  44.  
  45. crc.update(data, 0, data.length);
  46. cache_crc[i] = (int) crc.getValue();
  47. crc.reset();
  48. data = null;
  49. }
  50. crc = null;
  51.  
  52. cache_hash = 1234;
  53. for (int i = 0; i < 9; i++)
  54. cache_hash = (cache_hash << 1) + cache_crc[i];
  55. }
  56.  
  57. private JaggrabProtocolHandler() {
  58. }
  59.  
  60. public int handleInput(Client c) {
  61. ByteBuffer input = c.getInputBuffer();
  62.  
  63. // See if we have a whole request (peek for '\n\n')
  64. boolean gotN = false;
  65. int length = -1;
  66. for (int i = 0; i < input.limit(); i++) {
  67. byte b = input.get(i);
  68. if (b == '\n') {
  69. if (gotN) {
  70. length = i + 1;
  71. break;
  72. }
  73. gotN = true;
  74. } else
  75. gotN = false;
  76. }
  77.  
  78. if (length != -1)
  79. return 0;
  80.  
  81. //J A G G R A B ' '
  82. if (input.getLong() != 0x4a41474752414220L) {
  83. System.err.println("Invalid request received on JAGGRAB server (doesn't start with JAGGRAB)");
  84. c.close();
  85. return 0;
  86. }
  87.  
  88. // Read the request
  89. byte[] str_data = ByteArrayPool.acquire(length - 10);
  90. input.get(str_data, 0, length - 10);
  91. String s = new String(str_data);
  92. System.out.println("JAGGRAB REQUEST: " + s);
  93.  
  94. // Find the request string
  95. if(s.startsWith("crc")) { // CRC request...
  96. if(!s.endsWith(String.valueOf(Global.CLIENT_VERSION))) {
  97. System.err.println("CRC request from non-317 client, dropped.");
  98. c.close();
  99. return 0;
  100. }
  101.  
  102. // Send the CRCs
  103. ByteBuffer b = ByteBufferPool.acquire(40);
  104. for(int i = 0; i < 9; i++)
  105. b.putInt(cache_crc[i]);
  106. b.putInt(cache_hash);
  107. c.write(b);
  108. return length;
  109. } else { // Find the archive from the cache...
  110. int id = -1;
  111. if(s.startsWith("title"))
  112. id = 1;
  113. else if(s.startsWith("config"))
  114. id = 2;
  115. else if(s.startsWith("interface"))
  116. id = 3;
  117. else if(s.startsWith("media"))
  118. id = 4;
  119. else if(s.startsWith("versionlist"))
  120. id = 5;
  121. else if(s.startsWith("textures"))
  122. id = 6;
  123. else if(s.startsWith("wordenc"))
  124. id = 7;
  125. else if(s.startsWith("sounds"))
  126. id = 8;
  127. else {
  128. System.err.println("Invalid JAGGRAB request: " + s + ", connection dropped.");
  129. c.close();
  130. return 0;
  131. }
  132.  
  133. // Check the client requested with the correct CRC
  134. if(!s.endsWith(String.valueOf(cache_crc[id]))) {
  135. System.err.println("Valid request, but CRC for request was inconsistent, connection dropped.");
  136. c.close();
  137. return 0;
  138. }
  139.  
  140. byte[] data = CacheFile.cache[0].readEntry(id);
  141. if(data == null)
  142. throw new RuntimeException("Error in JAGGRAB server, failed to serve request '" + s + "' (not found in cache!)");
  143. ByteBuffer b = ByteBufferPool.acquire(data.length);
  144. c.write(b);
  145. data = null;
  146.  
  147. return length;
  148. }
  149. }
  150. }
Add Comment
Please, Sign In to add comment