Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.jaghax.server.protocol;
- import org.jaghax.Client;
- import org.jaghax.server.rs.net.ByteArrayPool;
- import org.jaghax.server.rs.net.ByteBufferPool;
- import org.jaghax.server.util.CacheFile;
- import org.jaghax.server.util.Global;
- import java.nio.ByteBuffer;
- import java.util.zip.CRC32;
- /**
- * {@code JaggrabProtocolHandler}
- *
- * @author James Lawrence
- * @version 1
- */
- public class JaggrabProtocolHandler implements ProtocolHandler {
- private static JaggrabProtocolHandler instance;
- private static int[] cache_crc;
- private static int cache_hash;
- /**
- * Returns the singleton of this ProtocolHandler
- *
- * @return the only instance of this protocol handler
- */
- public static JaggrabProtocolHandler getInstance() {
- return instance;
- }
- static {
- instance = new JaggrabProtocolHandler();
- // Initialise cache CRCs
- cache_crc = new int[9];
- CRC32 crc = new CRC32();
- for (int i = 0; i < 9; i++) {
- byte[] data = CacheFile.cache[0].readEntry(i);
- if (data == null) {
- System.err.println("cache[0][" + i + "] is null :S");
- continue;
- }
- crc.update(data, 0, data.length);
- cache_crc[i] = (int) crc.getValue();
- crc.reset();
- data = null;
- }
- crc = null;
- cache_hash = 1234;
- for (int i = 0; i < 9; i++)
- cache_hash = (cache_hash << 1) + cache_crc[i];
- }
- private JaggrabProtocolHandler() {
- }
- public int handleInput(Client c) {
- ByteBuffer input = c.getInputBuffer();
- // See if we have a whole request (peek for '\n\n')
- boolean gotN = false;
- int length = -1;
- for (int i = 0; i < input.limit(); i++) {
- byte b = input.get(i);
- if (b == '\n') {
- if (gotN) {
- length = i + 1;
- break;
- }
- gotN = true;
- } else
- gotN = false;
- }
- if (length != -1)
- return 0;
- //J A G G R A B ' '
- if (input.getLong() != 0x4a41474752414220L) {
- System.err.println("Invalid request received on JAGGRAB server (doesn't start with JAGGRAB)");
- c.close();
- return 0;
- }
- // Read the request
- byte[] str_data = ByteArrayPool.acquire(length - 10);
- input.get(str_data, 0, length - 10);
- String s = new String(str_data);
- System.out.println("JAGGRAB REQUEST: " + s);
- // Find the request string
- if(s.startsWith("crc")) { // CRC request...
- if(!s.endsWith(String.valueOf(Global.CLIENT_VERSION))) {
- System.err.println("CRC request from non-317 client, dropped.");
- c.close();
- return 0;
- }
- // Send the CRCs
- ByteBuffer b = ByteBufferPool.acquire(40);
- for(int i = 0; i < 9; i++)
- b.putInt(cache_crc[i]);
- b.putInt(cache_hash);
- c.write(b);
- return length;
- } else { // Find the archive from the cache...
- int id = -1;
- if(s.startsWith("title"))
- id = 1;
- else if(s.startsWith("config"))
- id = 2;
- else if(s.startsWith("interface"))
- id = 3;
- else if(s.startsWith("media"))
- id = 4;
- else if(s.startsWith("versionlist"))
- id = 5;
- else if(s.startsWith("textures"))
- id = 6;
- else if(s.startsWith("wordenc"))
- id = 7;
- else if(s.startsWith("sounds"))
- id = 8;
- else {
- System.err.println("Invalid JAGGRAB request: " + s + ", connection dropped.");
- c.close();
- return 0;
- }
- // Check the client requested with the correct CRC
- if(!s.endsWith(String.valueOf(cache_crc[id]))) {
- System.err.println("Valid request, but CRC for request was inconsistent, connection dropped.");
- c.close();
- return 0;
- }
- byte[] data = CacheFile.cache[0].readEntry(id);
- if(data == null)
- throw new RuntimeException("Error in JAGGRAB server, failed to serve request '" + s + "' (not found in cache!)");
- ByteBuffer b = ByteBufferPool.acquire(data.length);
- c.write(b);
- data = null;
- return length;
- }
- }
- }
Add Comment
Please, Sign In to add comment