diff --git a/sp-relay/SharedWorldRelay/SharedWorldRelay-Latest.jar b/sp-relay/SharedWorldRelay/SharedWorldRelay-Latest.jar index 1363573..a84f6f5 100644 Binary files a/sp-relay/SharedWorldRelay/SharedWorldRelay-Latest.jar and b/sp-relay/SharedWorldRelay/SharedWorldRelay-Latest.jar differ diff --git a/sp-relay/SharedWorldRelay/src/main/java/net/lax1dude/eaglercraft/v1_8/sp/relay/server/Constants.java b/sp-relay/SharedWorldRelay/src/main/java/net/lax1dude/eaglercraft/v1_8/sp/relay/server/Constants.java index d443fcb..122a5ca 100644 --- a/sp-relay/SharedWorldRelay/src/main/java/net/lax1dude/eaglercraft/v1_8/sp/relay/server/Constants.java +++ b/sp-relay/SharedWorldRelay/src/main/java/net/lax1dude/eaglercraft/v1_8/sp/relay/server/Constants.java @@ -17,7 +17,7 @@ package net.lax1dude.eaglercraft.v1_8.sp.relay.server; */ public class Constants { - public static final String versionName = "1.1"; + public static final String versionName = "1.2"; public static final String versionBrand = "lax1dude"; public static final int protocolVersion = 1; diff --git a/sp-relay/SharedWorldRelay/src/main/java/net/lax1dude/eaglercraft/v1_8/sp/relay/server/DebugLogger.java b/sp-relay/SharedWorldRelay/src/main/java/net/lax1dude/eaglercraft/v1_8/sp/relay/server/DebugLogger.java index 13be9d1..b30b987 100644 --- a/sp-relay/SharedWorldRelay/src/main/java/net/lax1dude/eaglercraft/v1_8/sp/relay/server/DebugLogger.java +++ b/sp-relay/SharedWorldRelay/src/main/java/net/lax1dude/eaglercraft/v1_8/sp/relay/server/DebugLogger.java @@ -43,7 +43,7 @@ public class DebugLogger implements IRelayLogger { return debugLoggingLevel != Level.NONE; } - private static final Map loggers = new HashMap(); + private static final Map loggers = new HashMap<>(); public static DebugLogger getLogger(String name) { synchronized(loggers) { @@ -155,7 +155,6 @@ public class DebugLogger implements IRelayLogger { } private static final SimpleDateFormat fmt = new SimpleDateFormat("hh:mm:ss+SSS"); - private final Date dateInstance = new Date(); public static String formatParams(String msg, Object... params) { if(params.length > 0) { @@ -182,10 +181,12 @@ public class DebugLogger implements IRelayLogger { public void log(Level lvl, String msg, Object... params) { if(debugLoggingLevel.level >= lvl.level) { + Date d = new Date(); + d.setTime(System.currentTimeMillis()); + String str = "[" + fmt.format(d) + "][" + Thread.currentThread().getName() + "/" + lvl.label + "][" + name + "]: " + + (params.length == 0 ? msg : formatParams(msg, params)); synchronized(this) { - dateInstance.setTime(System.currentTimeMillis()); - System.out.println("[" + fmt.format(dateInstance) + "][" + Thread.currentThread().getName() + "/" + lvl.label + "][" + name + "]: " + - (params.length == 0 ? msg : formatParams(msg, params))); + System.out.println(str); } } } diff --git a/sp-relay/SharedWorldRelay/src/main/java/net/lax1dude/eaglercraft/v1_8/sp/relay/server/EaglerSPClient.java b/sp-relay/SharedWorldRelay/src/main/java/net/lax1dude/eaglercraft/v1_8/sp/relay/server/EaglerSPClient.java index bc758d6..f62b6ea 100644 --- a/sp-relay/SharedWorldRelay/src/main/java/net/lax1dude/eaglercraft/v1_8/sp/relay/server/EaglerSPClient.java +++ b/sp-relay/SharedWorldRelay/src/main/java/net/lax1dude/eaglercraft/v1_8/sp/relay/server/EaglerSPClient.java @@ -36,7 +36,7 @@ public class EaglerSPClient { this.socket = sock; this.server = srv; this.id = id; - this.createdOn = System.currentTimeMillis(); + this.createdOn = Util.millis(); this.address = addr; } @@ -45,13 +45,13 @@ public class EaglerSPClient { try { socket.send(RelayPacket.writePacket(packet, EaglerSPRelay.logger)); }catch(IOException ex) { - EaglerSPRelay.logger.debug("Error sending data to {}", socket.getAttachment()); + EaglerSPRelay.logger.debug("Error sending data to {}", (Object) socket.getAttachment()); EaglerSPRelay.logger.debug(ex); disconnect(RelayPacketFEDisconnectClient.TYPE_INTERNAL_ERROR, "Internal Server Error"); socket.close(); } }else { - EaglerSPRelay.logger.debug("WARNING: Tried to send data to {} after the connection closed.", socket.getAttachment()); + EaglerSPRelay.logger.debug("WARNING: Tried to send data to {} after the connection closed.", (Object) socket.getAttachment()); } } @@ -60,21 +60,21 @@ public class EaglerSPClient { if(LoginState.assertEquals(this, LoginState.RECIEVED_DESCRIPTION)) { state = LoginState.SENT_ICE_CANDIDATE; server.handleClientICECandidate(this, (RelayPacket03ICECandidate)packet); - EaglerSPRelay.logger.debug("[{}][Client -> Relay -> Server] PKT 0x03: ICECandidate", socket.getAttachment()); + EaglerSPRelay.logger.debug("[{}][Client -> Relay -> Server] PKT 0x03: ICECandidate", (Object) socket.getAttachment()); } return true; }else if(packet instanceof RelayPacket04Description) { if(LoginState.assertEquals(this, LoginState.INIT)) { state = LoginState.SENT_DESCRIPTION; server.handleClientDescription(this, (RelayPacket04Description)packet); - EaglerSPRelay.logger.debug("[{}][Client -> Relay -> Server] PKT 0x04: Description", socket.getAttachment()); + EaglerSPRelay.logger.debug("[{}][Client -> Relay -> Server] PKT 0x04: Description", (Object) socket.getAttachment()); } return true; }else if(packet instanceof RelayPacket05ClientSuccess) { if(LoginState.assertEquals(this, LoginState.RECIEVED_ICE_CANIDATE)) { state = LoginState.FINISHED; server.handleClientSuccess(this, (RelayPacket05ClientSuccess)packet); - EaglerSPRelay.logger.debug("[{}][Client -> Relay -> Server] PKT 0x05: ClientSuccess", socket.getAttachment()); + EaglerSPRelay.logger.debug("[{}][Client -> Relay -> Server] PKT 0x05: ClientSuccess", (Object) socket.getAttachment()); disconnect(RelayPacketFEDisconnectClient.TYPE_FINISHED_SUCCESS, "Successful connection"); } return true; @@ -82,7 +82,7 @@ public class EaglerSPClient { if(LoginState.assertEquals(this, LoginState.RECIEVED_ICE_CANIDATE)) { state = LoginState.FINISHED; server.handleClientFailure(this, (RelayPacket06ClientFailure)packet); - EaglerSPRelay.logger.debug("[{}][Client -> Relay -> Server] PKT 0x05: ClientFailure", socket.getAttachment()); + EaglerSPRelay.logger.debug("[{}][Client -> Relay -> Server] PKT 0x05: ClientFailure", (Object) socket.getAttachment()); disconnect(RelayPacketFEDisconnectClient.TYPE_FINISHED_FAILED, "Failed connection"); } return true; @@ -113,7 +113,7 @@ public class EaglerSPClient { send(pkt); socket.close(); } - EaglerSPRelay.logger.debug("[{}][Relay -> Client] PKT 0xFE: #{} {}", socket.getAttachment(), code, reason); + EaglerSPRelay.logger.debug("[{}][Relay -> Client] PKT 0xFE: #{} {}", (Object) socket.getAttachment(), code, reason); } public static final int clientCodeLength = 16; diff --git a/sp-relay/SharedWorldRelay/src/main/java/net/lax1dude/eaglercraft/v1_8/sp/relay/server/EaglerSPRelay.java b/sp-relay/SharedWorldRelay/src/main/java/net/lax1dude/eaglercraft/v1_8/sp/relay/server/EaglerSPRelay.java index fc4a7b7..237e15d 100644 --- a/sp-relay/SharedWorldRelay/src/main/java/net/lax1dude/eaglercraft/v1_8/sp/relay/server/EaglerSPRelay.java +++ b/sp-relay/SharedWorldRelay/src/main/java/net/lax1dude/eaglercraft/v1_8/sp/relay/server/EaglerSPRelay.java @@ -10,6 +10,7 @@ import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; +import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -79,15 +80,19 @@ public class EaglerSPRelay extends WebSocketServer { Thread tickThread = new Thread((() -> { int rateLimitUpdateCounter = 0; + final List pendingToClose = new LinkedList<>(); + final List clientToClose = new LinkedList<>(); while(true) { try { - long millis = System.currentTimeMillis(); + long millis = Util.millis(); + pendingToClose.clear(); + clientToClose.clear(); synchronized(pendingConnections) { Iterator> itr = pendingConnections.entrySet().iterator(); while(itr.hasNext()) { Entry etr = itr.next(); if(millis - etr.getValue().openTime > 500l) { - etr.getKey().close(); + pendingToClose.add(etr.getKey()); itr.remove(); } } @@ -97,10 +102,22 @@ public class EaglerSPRelay extends WebSocketServer { while(itr.hasNext()) { EaglerSPClient cl = itr.next(); if(millis - cl.createdOn > 10000l) { - cl.disconnect(RelayPacketFEDisconnectClient.TYPE_TIMEOUT, "Took too long to connect!"); + clientToClose.add(cl); } } } + if(!pendingToClose.isEmpty()) { + for(WebSocket cl : pendingToClose) { + cl.close(); + } + pendingToClose.clear(); + } + if(!clientToClose.isEmpty()) { + for(EaglerSPClient cl : clientToClose) { + cl.disconnect(RelayPacketFEDisconnectClient.TYPE_TIMEOUT, "Took too long to connect!"); + } + clientToClose.clear(); + } if(++rateLimitUpdateCounter > 300) { if(pingRateLimiter != null) { pingRateLimiter.update(); @@ -156,13 +173,13 @@ public class EaglerSPRelay extends WebSocketServer { } - private static final Map pendingConnections = new HashMap(); - private static final Map clientIds = new HashMap(); - private static final Map clientConnections = new HashMap(); - private static final Map serverCodes = new HashMap(); - private static final Map serverConnections = new HashMap(); - private static final Map> clientAddressSets = new HashMap(); - private static final Map> serverAddressSets = new HashMap(); + private static final Map pendingConnections = new HashMap<>(); + private static final Map clientIds = new HashMap<>(); + private static final Map clientConnections = new HashMap<>(); + private static final Map serverCodes = new HashMap<>(); + private static final Map serverConnections = new HashMap<>(); + private static final Map> clientAddressSets = new HashMap<>(); + private static final Map> serverAddressSets = new HashMap<>(); @Override public void onStart() { @@ -178,7 +195,7 @@ public class EaglerSPRelay extends WebSocketServer { } String addr; - long millis = System.currentTimeMillis(); + long millis = Util.millis(); if(config.isEnableRealIpHeader() && arg1.hasFieldValue(config.getRealIPHeaderName())) { addr = arg1.getFieldValue(config.getRealIPHeaderName()).toLowerCase(); }else { @@ -202,7 +219,7 @@ public class EaglerSPRelay extends WebSocketServer { } if(totalCons >= config.getConnectionsPerIP()) { - logger.debug("[{}]: Too many connections are open on this address", arg0.getAttachment()); + logger.debug("[{}]: Too many connections are open on this address", (Object) arg0.getAttachment()); arg0.send(RelayPacketFEDisconnectClient.ratelimitPacketTooMany); arg0.close(); return; @@ -231,7 +248,7 @@ public class EaglerSPRelay extends WebSocketServer { RelayPacket00Handshake ipkt = (RelayPacket00Handshake)pkt; if(ipkt.connectionVersion != Constants.protocolVersion) { logger.debug("[{}]: Connected with unsupported protocol version: {} (supported " - + "version: {})", arg0.getAttachment(), ipkt.connectionVersion, Constants.protocolVersion); + + "version: {})", (Object) arg0.getAttachment(), ipkt.connectionVersion, Constants.protocolVersion); if(ipkt.connectionVersion < Constants.protocolVersion) { arg0.send(RelayPacket.writePacket(new RelayPacketFFErrorCode(RelayPacketFFErrorCode.TYPE_PROTOCOL_VERSION, "Outdated Client! (v" + Constants.protocolVersion + " req)"), EaglerSPRelay.logger)); @@ -244,63 +261,76 @@ public class EaglerSPRelay extends WebSocketServer { } if(ipkt.connectionType == 0x01) { if(!rateLimit(worldRateLimiter, arg0, waiting.address)) { - logger.debug("[{}]: Got world ratelimited", arg0.getAttachment()); + logger.debug("[{}]: Got world ratelimited", (Object) arg0.getAttachment()); return; } + boolean fuck = false; synchronized(serverAddressSets) { List lst = serverAddressSets.get(waiting.address); if(lst != null) { if(lst.size() >= config.getWorldsPerIP()) { - logger.debug("[{}]: Too many worlds are open on this address", arg0.getAttachment()); - arg0.send(RelayPacketFEDisconnectClient.ratelimitPacketTooMany); - arg0.close(); - return; + fuck = true; } } } - logger.debug("[{}]: Connected as a server", arg0.getAttachment()); - EaglerSPServer srv; - synchronized(serverCodes) { - int j = 0; - String code; - do { - if(++j > 100) { - logger.error("Error: relay is running out of codes!"); - logger.error("Closing connection to {}", arg0.getAttachment()); - arg0.send(RelayPacket.writePacket(new RelayPacketFFErrorCode(RelayPacketFFErrorCode.TYPE_INTERNAL_ERROR, - "Internal Server Error"), EaglerSPRelay.logger)); - arg0.close(); - return; - } - code = config.generateCode(); - }while(serverCodes.containsKey(code)); - srv = new EaglerSPServer(arg0, code, ipkt.connectionCode, waiting.address); - serverCodes.put(code, srv); - ipkt.connectionCode = code; - arg0.send(RelayPacket.writePacket(ipkt, EaglerSPRelay.logger)); - logger.debug("[{}][Relay -> Server] PKT 0x00: Assign join code: {}", arg0.getAttachment(), code); + if(fuck) { + logger.debug("[{}]: Too many worlds are open on this address", (Object) arg0.getAttachment()); + arg0.send(RelayPacketFEDisconnectClient.ratelimitPacketTooMany); + arg0.close(); + return; } + logger.debug("[{}]: Connected as a server", (Object) arg0.getAttachment()); + + EaglerSPServer srv = null; + String code = null; + synchronized(serverCodes) { + eagler: { + int j = 0; + do { + if(++j > 20) { + logger.error("Error: relay is running out of codes!"); + logger.error("Closing connection to {}", (Object) arg0.getAttachment()); + break eagler; + } + code = config.generateCode(); + }while(serverCodes.containsKey(code)); + srv = new EaglerSPServer(arg0, code, ipkt.connectionCode, waiting.address); + serverCodes.put(code, srv); + } + } + + if(srv == null) { + arg0.send(RelayPacket.writePacket(new RelayPacketFFErrorCode(RelayPacketFFErrorCode.TYPE_INTERNAL_ERROR, + "Internal Server Error"), EaglerSPRelay.logger)); + arg0.close(); + return; + } + + ipkt.connectionCode = code; + arg0.send(RelayPacket.writePacket(ipkt, EaglerSPRelay.logger)); + logger.debug("[{}][Relay -> Server] PKT 0x00: Assign join code: {}", (Object) arg0.getAttachment(), code); + synchronized(serverConnections) { serverConnections.put(arg0, srv); } synchronized(serverAddressSets) { List lst = serverAddressSets.get(srv.serverAddress); if(lst == null) { - lst = new ArrayList(); + lst = new ArrayList<>(); serverAddressSets.put(srv.serverAddress, lst); } lst.add(srv); } srv.send(new RelayPacket01ICEServers(EaglerSPRelayConfigRelayList.relayServers)); - logger.debug("[{}][Relay -> Server] PKT 0x01: Send ICE server list to server", arg0.getAttachment()); + logger.debug("[{}][Relay -> Server] PKT 0x01: Send ICE server list to server", (Object) arg0.getAttachment()); }else { if(!rateLimit(pingRateLimiter, arg0, waiting.address)) { - logger.debug("[{}]: Got ping ratelimited", arg0.getAttachment()); + logger.debug("[{}]: Got ping ratelimited", (Object) arg0.getAttachment()); return; } if(ipkt.connectionType == 0x02) { String code = ipkt.connectionCode; - logger.debug("[{}]: Connected as a client, requested server code: {}", arg0.getAttachment(), code); + logger.debug("[{}]: Connected as a client, requested server code: {}", (Object) arg0.getAttachment(), code); if(code.length() != config.getCodeLength()) { logger.debug("The code '{}' is invalid because it's the wrong length, disconnecting", code); arg0.send(RelayPacket.writePacket(new RelayPacketFFErrorCode(RelayPacketFFErrorCode.TYPE_CODE_LENGTH, @@ -323,36 +353,35 @@ public class EaglerSPRelay extends WebSocketServer { String id; EaglerSPClient cl; synchronized(clientIds) { - int j = 0; do { id = EaglerSPClient.generateClientId(); }while(clientIds.containsKey(id)); cl = new EaglerSPClient(arg0, srv, id, waiting.address); clientIds.put(id, cl); - ipkt.connectionCode = id; - arg0.send(RelayPacket.writePacket(ipkt, EaglerSPRelay.logger)); - srv.handleNewClient(cl); } + ipkt.connectionCode = id; + arg0.send(RelayPacket.writePacket(ipkt, EaglerSPRelay.logger)); + srv.handleNewClient(cl); synchronized(clientConnections) { clientConnections.put(arg0, cl); } synchronized(clientAddressSets) { List lst = clientAddressSets.get(cl.address); if(lst == null) { - lst = new ArrayList(); + lst = new ArrayList<>(); clientAddressSets.put(cl.address, lst); } lst.add(cl); } cl.send(new RelayPacket01ICEServers(EaglerSPRelayConfigRelayList.relayServers)); - logger.debug("[{}][Relay -> Client] PKT 0x01: Send ICE server list to client", arg0.getAttachment()); + logger.debug("[{}][Relay -> Client] PKT 0x01: Send ICE server list to client", (Object) arg0.getAttachment()); } }else if(ipkt.connectionType == 0x03) { - logger.debug("[{}]: Pinging the server", arg0.getAttachment()); + logger.debug("[{}]: Pinging the server", (Object) arg0.getAttachment()); arg0.send(RelayPacket.writePacket(new RelayPacket69Pong(Constants.protocolVersion, config.getComment(), Constants.versionBrand), EaglerSPRelay.logger)); arg0.close(); }else if(ipkt.connectionType == 0x04) { - logger.debug("[{}]: Polling the server for other worlds", arg0.getAttachment()); + logger.debug("[{}]: Polling the server for other worlds", (Object) arg0.getAttachment()); if(config.isEnableShowLocals()) { arg0.send(RelayPacket.writePacket(new RelayPacket07LocalWorlds(getLocalWorlds(waiting.address)), EaglerSPRelay.logger)); }else { @@ -360,7 +389,7 @@ public class EaglerSPRelay extends WebSocketServer { } arg0.close(); }else { - logger.debug("[{}]: Unknown connection type: {}", arg0.getAttachment(), ipkt.connectionType); + logger.debug("[{}]: Unknown connection type: {}", (Object) arg0.getAttachment(), ipkt.connectionType); arg0.send(RelayPacket.writePacket(new RelayPacketFFErrorCode(RelayPacketFFErrorCode.TYPE_ILLEGAL_OPERATION, "Unexpected Init Packet"), EaglerSPRelay.logger)); arg0.close(); @@ -368,7 +397,7 @@ public class EaglerSPRelay extends WebSocketServer { } }else { logger.debug("[{}]: Pending connection did not send a 0x00 packet to identify " - + "as a client or server", arg0.getAttachment()); + + "as a client or server", (Object) arg0.getAttachment()); arg0.send(RelayPacket.writePacket(new RelayPacketFFErrorCode(RelayPacketFFErrorCode.TYPE_ILLEGAL_OPERATION, "Unexpected Init Packet"), EaglerSPRelay.logger)); arg0.close(); @@ -380,7 +409,7 @@ public class EaglerSPRelay extends WebSocketServer { } if(srv != null) { if(!srv.handle(pkt)) { - logger.debug("[{}]: Server sent invalid packet: {}", arg0.getAttachment(), pkt.getClass().getSimpleName()); + logger.debug("[{}]: Server sent invalid packet: {}", (Object) arg0.getAttachment(), pkt.getClass().getSimpleName()); arg0.send(RelayPacket.writePacket(new RelayPacketFFErrorCode(RelayPacketFFErrorCode.TYPE_INVALID_PACKET, "Invalid Packet Recieved"), EaglerSPRelay.logger)); arg0.close(); @@ -392,13 +421,13 @@ public class EaglerSPRelay extends WebSocketServer { } if(cl != null) { if(!cl.handle(pkt)) { - logger.debug("[{}]: Client sent invalid packet: {}", arg0.getAttachment(), pkt.getClass().getSimpleName()); + logger.debug("[{}]: Client sent invalid packet: {}", (Object) arg0.getAttachment(), pkt.getClass().getSimpleName()); arg0.send(RelayPacket.writePacket(new RelayPacketFFErrorCode(RelayPacketFFErrorCode.TYPE_INVALID_PACKET, "Invalid Packet Recieved"), EaglerSPRelay.logger)); arg0.close(); } }else { - logger.debug("[{}]: Connection has no client/server attached to it!", arg0.getAttachment()); + logger.debug("[{}]: Connection has no client/server attached to it!", (Object) arg0.getAttachment()); arg0.send(RelayPacket.writePacket(new RelayPacketFFErrorCode(RelayPacketFFErrorCode.TYPE_ILLEGAL_OPERATION, "Internal Server Error"), EaglerSPRelay.logger)); arg0.close(); @@ -406,14 +435,14 @@ public class EaglerSPRelay extends WebSocketServer { } } }catch(Throwable t) { - logger.error("[{}]: Failed to handle binary frame: {}", arg0.getAttachment(), t); + logger.error("[{}]: Failed to handle binary frame: {}", (Object) arg0.getAttachment(), t); arg0.close(); } } @Override public void onMessage(WebSocket arg0, String arg1) { - logger.debug("[{}]: Sent a text frame, disconnecting", arg0.getAttachment()); + logger.debug("[{}]: Sent a text frame, disconnecting", (Object) arg0.getAttachment()); arg0.close(); } @@ -424,7 +453,7 @@ public class EaglerSPRelay extends WebSocketServer { srv = serverConnections.remove(arg0); } if(srv != null) { - logger.debug("[{}]: Server closed, code: {}", arg0.getAttachment(), srv.code); + logger.debug("[{}]: Server closed, code: {}", (Object) arg0.getAttachment(), srv.code); synchronized(serverCodes) { serverCodes.remove(srv.code); } @@ -439,13 +468,13 @@ public class EaglerSPRelay extends WebSocketServer { } ArrayList clientList; synchronized(clientConnections) { - clientList = new ArrayList(clientConnections.values()); + clientList = new ArrayList<>(clientConnections.values()); } Iterator itr = clientList.iterator(); while(itr.hasNext()) { EaglerSPClient cl = itr.next(); if(cl.server == srv) { - logger.debug("[{}]: Disconnecting client: {} (id: {})", cl.socket.getAttachment(), cl.id); + logger.debug("[{}]: Disconnecting client: {} (id: {})", (Object) cl.socket.getAttachment(), cl.id); cl.socket.close(); } } @@ -464,26 +493,26 @@ public class EaglerSPRelay extends WebSocketServer { } } } - logger.debug("[{}]: Client closed, id: {}", arg0.getAttachment(), cl.id); + logger.debug("[{}]: Client closed, id: {}", (Object) arg0.getAttachment(), cl.id); synchronized(clientIds) { clientIds.remove(cl.id); } cl.server.handleClientDisconnect(cl); }else { - logger.debug("[{}]: Connection Closed", arg0.getAttachment()); + logger.debug("[{}]: Connection Closed", (Object) arg0.getAttachment()); } } } @Override public void onError(WebSocket arg0, Exception arg1) { - logger.error("[{}]: Exception thrown: {}", (arg0 == null ? "SERVER" : arg0.getAttachment()), arg1.toString()); + logger.error("[{}]: Exception thrown: {}", (arg0 == null ? "SERVER" : (Object) arg0.getAttachment()), arg1.toString()); logger.debug(arg1); if(arg0 != null) arg0.close(); } private List getLocalWorlds(String addr) { - List lst = new ArrayList(); + List lst = new ArrayList<>(); synchronized(serverAddressSets) { List srvs = serverAddressSets.get(addr); if(srvs != null) { diff --git a/sp-relay/SharedWorldRelay/src/main/java/net/lax1dude/eaglercraft/v1_8/sp/relay/server/EaglerSPRelayConfig.java b/sp-relay/SharedWorldRelay/src/main/java/net/lax1dude/eaglercraft/v1_8/sp/relay/server/EaglerSPRelayConfig.java index ab00e8d..8ad0eb2 100644 --- a/sp-relay/SharedWorldRelay/src/main/java/net/lax1dude/eaglercraft/v1_8/sp/relay/server/EaglerSPRelayConfig.java +++ b/sp-relay/SharedWorldRelay/src/main/java/net/lax1dude/eaglercraft/v1_8/sp/relay/server/EaglerSPRelayConfig.java @@ -295,7 +295,7 @@ public class EaglerSPRelayConfig { save(conf); } String[] splitted = originWhitelist.split(";"); - List splittedList = new ArrayList(); + List splittedList = new ArrayList<>(); for(int i = 0; i < splitted.length; ++i) { splitted[i] = splitted[i].trim().toLowerCase(); if(splitted[i].length() > 0) { diff --git a/sp-relay/SharedWorldRelay/src/main/java/net/lax1dude/eaglercraft/v1_8/sp/relay/server/EaglerSPRelayConfigRelayList.java b/sp-relay/SharedWorldRelay/src/main/java/net/lax1dude/eaglercraft/v1_8/sp/relay/server/EaglerSPRelayConfigRelayList.java index d3c1b0f..09689b1 100644 --- a/sp-relay/SharedWorldRelay/src/main/java/net/lax1dude/eaglercraft/v1_8/sp/relay/server/EaglerSPRelayConfigRelayList.java +++ b/sp-relay/SharedWorldRelay/src/main/java/net/lax1dude/eaglercraft/v1_8/sp/relay/server/EaglerSPRelayConfigRelayList.java @@ -28,10 +28,10 @@ import net.lax1dude.eaglercraft.v1_8.sp.relay.pkt.RelayPacket01ICEServers; */ public class EaglerSPRelayConfigRelayList { - public static final Collection relayServers = new ArrayList(); + public static final Collection relayServers = new ArrayList<>(); public static void loadRelays(File list) throws IOException { - ArrayList loading = new ArrayList(); + ArrayList loading = new ArrayList<>(); if(!list.isFile()) { EaglerSPRelay.logger.info("Creating new {}...", list.getName()); diff --git a/sp-relay/SharedWorldRelay/src/main/java/net/lax1dude/eaglercraft/v1_8/sp/relay/server/EaglerSPServer.java b/sp-relay/SharedWorldRelay/src/main/java/net/lax1dude/eaglercraft/v1_8/sp/relay/server/EaglerSPServer.java index c019b21..75e4fb1 100644 --- a/sp-relay/SharedWorldRelay/src/main/java/net/lax1dude/eaglercraft/v1_8/sp/relay/server/EaglerSPServer.java +++ b/sp-relay/SharedWorldRelay/src/main/java/net/lax1dude/eaglercraft/v1_8/sp/relay/server/EaglerSPServer.java @@ -35,7 +35,7 @@ public class EaglerSPServer { EaglerSPServer(WebSocket sock, String code, String serverName, String serverAddress) { this.socket = sock; this.code = code; - this.clients = new HashMap(); + this.clients = new HashMap<>(); if(serverName.endsWith(";1")) { this.serverHidden = true; @@ -78,7 +78,7 @@ public class EaglerSPServer { if(LoginState.assertEquals(cl, LoginState.SENT_ICE_CANDIDATE)) { cl.state = LoginState.RECIEVED_ICE_CANIDATE; cl.handleServerICECandidate(packet); - EaglerSPRelay.logger.debug("[{}][Server -> Relay -> Client] PKT 0x03: ICECandidate", cl.socket.getAttachment()); + EaglerSPRelay.logger.debug("[{}][Server -> Relay -> Client] PKT 0x03: ICECandidate", (Object) cl.socket.getAttachment()); } }else { socket.send(RelayPacket.writePacket(new RelayPacketFFErrorCode(RelayPacketFFErrorCode.TYPE_UNKNOWN_CLIENT, @@ -92,7 +92,7 @@ public class EaglerSPServer { if(LoginState.assertEquals(cl, LoginState.SENT_DESCRIPTION)) { cl.state = LoginState.RECIEVED_DESCRIPTION; cl.handleServerDescription(packet); - EaglerSPRelay.logger.debug("[{}][Server -> Relay -> Client] PKT 0x04: Description", cl.socket.getAttachment()); + EaglerSPRelay.logger.debug("[{}][Server -> Relay -> Client] PKT 0x04: Description", (Object) cl.socket.getAttachment()); } }else { socket.send(RelayPacket.writePacket(new RelayPacketFFErrorCode(RelayPacketFFErrorCode.TYPE_UNKNOWN_CLIENT, @@ -104,7 +104,7 @@ public class EaglerSPServer { EaglerSPClient cl = clients.get(packet.clientId); if(cl != null) { cl.handleServerDisconnectClient(packet); - EaglerSPRelay.logger.debug("[{}][Server -> Relay -> Client] PKT 0xFE: Disconnect: {}: {}", cl.socket.getAttachment(), + EaglerSPRelay.logger.debug("[{}][Server -> Relay -> Client] PKT 0xFE: Disconnect: {}: {}", (Object) cl.socket.getAttachment(), packet.code, packet.reason); }else { socket.send(RelayPacket.writePacket(new RelayPacketFFErrorCode(RelayPacketFFErrorCode.TYPE_UNKNOWN_CLIENT, @@ -119,9 +119,9 @@ public class EaglerSPServer { public void handleNewClient(EaglerSPClient client) { synchronized(clients) { clients.put(client.id, client); - send(new RelayPacket02NewClient(client.id)); - EaglerSPRelay.logger.debug("[{}][Relay -> Server] PKT 0x02: Notify server of the client, id: {}", serverAddress, client.id); } + send(new RelayPacket02NewClient(client.id)); + EaglerSPRelay.logger.debug("[{}][Relay -> Server] PKT 0x02: Notify server of the client, id: {}", serverAddress, client.id); } public void handleClientDisconnect(EaglerSPClient client) { diff --git a/sp-relay/SharedWorldRelay/src/main/java/net/lax1dude/eaglercraft/v1_8/sp/relay/server/RateLimiter.java b/sp-relay/SharedWorldRelay/src/main/java/net/lax1dude/eaglercraft/v1_8/sp/relay/server/RateLimiter.java index 56ea8ae..fdbede4 100644 --- a/sp-relay/SharedWorldRelay/src/main/java/net/lax1dude/eaglercraft/v1_8/sp/relay/server/RateLimiter.java +++ b/sp-relay/SharedWorldRelay/src/main/java/net/lax1dude/eaglercraft/v1_8/sp/relay/server/RateLimiter.java @@ -34,14 +34,14 @@ public class RateLimiter { protected boolean locked; protected RateLimitEntry() { - timer = System.currentTimeMillis(); + timer = Util.millis(); count = 0; lockedTimer = 0l; locked = false; } protected void update() { - long millis = System.currentTimeMillis(); + long millis = Util.millis(); if(locked) { if(millis - lockedTimer > RateLimiter.this.lockoutDuration) { timer = millis; @@ -70,7 +70,7 @@ public class RateLimiter { NONE, LIMIT, LIMIT_NOW_LOCKOUT, LOCKOUT; } - private final Map limiters = new HashMap(); + private final Map limiters = new HashMap<>(); public RateLimiter(int period, int limit, int lockoutLimit, int lockoutDuration) { this.period = period; @@ -79,50 +79,44 @@ public class RateLimiter { this.lockoutDuration = lockoutDuration; } - public RateLimit limit(String addr) { - synchronized(this) { - RateLimitEntry etr = limiters.get(addr); - - if(etr == null) { - etr = new RateLimitEntry(); - limiters.put(addr, etr); - }else { - etr.update(); - } - - if(etr.locked) { - return RateLimit.LOCKOUT; - } - - ++etr.count; - if(etr.count >= lockoutLimit) { - etr.count = 0; - etr.locked = true; - etr.lockedTimer = System.currentTimeMillis(); - return RateLimit.LIMIT_NOW_LOCKOUT; - }else if(etr.count > limit) { - return RateLimit.LIMIT; - }else { - return RateLimit.NONE; + public synchronized RateLimit limit(String addr) { + RateLimitEntry etr = limiters.get(addr); + + if(etr == null) { + etr = new RateLimitEntry(); + limiters.put(addr, etr); + }else { + etr.update(); + } + + if(etr.locked) { + return RateLimit.LOCKOUT; + } + + ++etr.count; + if(etr.count >= lockoutLimit) { + etr.count = 0; + etr.locked = true; + etr.lockedTimer = Util.millis(); + return RateLimit.LIMIT_NOW_LOCKOUT; + }else if(etr.count > limit) { + return RateLimit.LIMIT; + }else { + return RateLimit.NONE; + } + } + + public synchronized void update() { + Iterator itr = limiters.values().iterator(); + while(itr.hasNext()) { + if(itr.next().count == 0) { + itr.remove(); } } } - public void update() { - synchronized(this) { - Iterator itr = limiters.values().iterator(); - while(itr.hasNext()) { - if(itr.next().count == 0) { - itr.remove(); - } - } - } - } - - public void reset() { - synchronized(this) { - limiters.clear(); - } + public synchronized void reset() { + limiters.clear(); } } diff --git a/sp-relay/SharedWorldRelay/src/main/java/net/lax1dude/eaglercraft/v1_8/sp/relay/server/Util.java b/sp-relay/SharedWorldRelay/src/main/java/net/lax1dude/eaglercraft/v1_8/sp/relay/server/Util.java index 7513712..4abd845 100644 --- a/sp-relay/SharedWorldRelay/src/main/java/net/lax1dude/eaglercraft/v1_8/sp/relay/server/Util.java +++ b/sp-relay/SharedWorldRelay/src/main/java/net/lax1dude/eaglercraft/v1_8/sp/relay/server/Util.java @@ -30,4 +30,7 @@ public class Util { return sock.getAddress().getHostAddress() + ":" + sock.getPort(); } + public static long millis() { + return System.nanoTime() / 1000000l; + } }