{1.2} Fixed more deadlock issues with the shared world relay

This commit is contained in:
lax1dude 2024-11-13 20:50:39 -08:00
parent 70561d1694
commit fa63fc3676
10 changed files with 158 additions and 131 deletions

View File

@ -17,7 +17,7 @@ package net.lax1dude.eaglercraft.v1_8.sp.relay.server;
*/ */
public class Constants { 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 String versionBrand = "lax1dude";
public static final int protocolVersion = 1; public static final int protocolVersion = 1;

View File

@ -43,7 +43,7 @@ public class DebugLogger implements IRelayLogger {
return debugLoggingLevel != Level.NONE; return debugLoggingLevel != Level.NONE;
} }
private static final Map<String,DebugLogger> loggers = new HashMap(); private static final Map<String,DebugLogger> loggers = new HashMap<>();
public static DebugLogger getLogger(String name) { public static DebugLogger getLogger(String name) {
synchronized(loggers) { synchronized(loggers) {
@ -155,7 +155,6 @@ public class DebugLogger implements IRelayLogger {
} }
private static final SimpleDateFormat fmt = new SimpleDateFormat("hh:mm:ss+SSS"); 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) { public static String formatParams(String msg, Object... params) {
if(params.length > 0) { if(params.length > 0) {
@ -182,10 +181,12 @@ public class DebugLogger implements IRelayLogger {
public void log(Level lvl, String msg, Object... params) { public void log(Level lvl, String msg, Object... params) {
if(debugLoggingLevel.level >= lvl.level) { 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) { synchronized(this) {
dateInstance.setTime(System.currentTimeMillis()); System.out.println(str);
System.out.println("[" + fmt.format(dateInstance) + "][" + Thread.currentThread().getName() + "/" + lvl.label + "][" + name + "]: " +
(params.length == 0 ? msg : formatParams(msg, params)));
} }
} }
} }

View File

@ -36,7 +36,7 @@ public class EaglerSPClient {
this.socket = sock; this.socket = sock;
this.server = srv; this.server = srv;
this.id = id; this.id = id;
this.createdOn = System.currentTimeMillis(); this.createdOn = Util.millis();
this.address = addr; this.address = addr;
} }
@ -45,13 +45,13 @@ public class EaglerSPClient {
try { try {
socket.send(RelayPacket.writePacket(packet, EaglerSPRelay.logger)); socket.send(RelayPacket.writePacket(packet, EaglerSPRelay.logger));
}catch(IOException ex) { }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); EaglerSPRelay.logger.debug(ex);
disconnect(RelayPacketFEDisconnectClient.TYPE_INTERNAL_ERROR, "Internal Server Error"); disconnect(RelayPacketFEDisconnectClient.TYPE_INTERNAL_ERROR, "Internal Server Error");
socket.close(); socket.close();
} }
}else { }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)) { if(LoginState.assertEquals(this, LoginState.RECIEVED_DESCRIPTION)) {
state = LoginState.SENT_ICE_CANDIDATE; state = LoginState.SENT_ICE_CANDIDATE;
server.handleClientICECandidate(this, (RelayPacket03ICECandidate)packet); 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; return true;
}else if(packet instanceof RelayPacket04Description) { }else if(packet instanceof RelayPacket04Description) {
if(LoginState.assertEquals(this, LoginState.INIT)) { if(LoginState.assertEquals(this, LoginState.INIT)) {
state = LoginState.SENT_DESCRIPTION; state = LoginState.SENT_DESCRIPTION;
server.handleClientDescription(this, (RelayPacket04Description)packet); 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; return true;
}else if(packet instanceof RelayPacket05ClientSuccess) { }else if(packet instanceof RelayPacket05ClientSuccess) {
if(LoginState.assertEquals(this, LoginState.RECIEVED_ICE_CANIDATE)) { if(LoginState.assertEquals(this, LoginState.RECIEVED_ICE_CANIDATE)) {
state = LoginState.FINISHED; state = LoginState.FINISHED;
server.handleClientSuccess(this, (RelayPacket05ClientSuccess)packet); 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"); disconnect(RelayPacketFEDisconnectClient.TYPE_FINISHED_SUCCESS, "Successful connection");
} }
return true; return true;
@ -82,7 +82,7 @@ public class EaglerSPClient {
if(LoginState.assertEquals(this, LoginState.RECIEVED_ICE_CANIDATE)) { if(LoginState.assertEquals(this, LoginState.RECIEVED_ICE_CANIDATE)) {
state = LoginState.FINISHED; state = LoginState.FINISHED;
server.handleClientFailure(this, (RelayPacket06ClientFailure)packet); 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"); disconnect(RelayPacketFEDisconnectClient.TYPE_FINISHED_FAILED, "Failed connection");
} }
return true; return true;
@ -113,7 +113,7 @@ public class EaglerSPClient {
send(pkt); send(pkt);
socket.close(); 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; public static final int clientCodeLength = 16;

View File

@ -10,6 +10,7 @@ import java.nio.ByteBuffer;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
@ -79,15 +80,19 @@ public class EaglerSPRelay extends WebSocketServer {
Thread tickThread = new Thread((() -> { Thread tickThread = new Thread((() -> {
int rateLimitUpdateCounter = 0; int rateLimitUpdateCounter = 0;
final List<WebSocket> pendingToClose = new LinkedList<>();
final List<EaglerSPClient> clientToClose = new LinkedList<>();
while(true) { while(true) {
try { try {
long millis = System.currentTimeMillis(); long millis = Util.millis();
pendingToClose.clear();
clientToClose.clear();
synchronized(pendingConnections) { synchronized(pendingConnections) {
Iterator<Entry<WebSocket,PendingConnection>> itr = pendingConnections.entrySet().iterator(); Iterator<Entry<WebSocket,PendingConnection>> itr = pendingConnections.entrySet().iterator();
while(itr.hasNext()) { while(itr.hasNext()) {
Entry<WebSocket,PendingConnection> etr = itr.next(); Entry<WebSocket,PendingConnection> etr = itr.next();
if(millis - etr.getValue().openTime > 500l) { if(millis - etr.getValue().openTime > 500l) {
etr.getKey().close(); pendingToClose.add(etr.getKey());
itr.remove(); itr.remove();
} }
} }
@ -97,10 +102,22 @@ public class EaglerSPRelay extends WebSocketServer {
while(itr.hasNext()) { while(itr.hasNext()) {
EaglerSPClient cl = itr.next(); EaglerSPClient cl = itr.next();
if(millis - cl.createdOn > 10000l) { 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(++rateLimitUpdateCounter > 300) {
if(pingRateLimiter != null) { if(pingRateLimiter != null) {
pingRateLimiter.update(); pingRateLimiter.update();
@ -156,13 +173,13 @@ public class EaglerSPRelay extends WebSocketServer {
} }
private static final Map<WebSocket,PendingConnection> pendingConnections = new HashMap(); private static final Map<WebSocket,PendingConnection> pendingConnections = new HashMap<>();
private static final Map<String,EaglerSPClient> clientIds = new HashMap(); private static final Map<String,EaglerSPClient> clientIds = new HashMap<>();
private static final Map<WebSocket,EaglerSPClient> clientConnections = new HashMap(); private static final Map<WebSocket,EaglerSPClient> clientConnections = new HashMap<>();
private static final Map<String,EaglerSPServer> serverCodes = new HashMap(); private static final Map<String,EaglerSPServer> serverCodes = new HashMap<>();
private static final Map<WebSocket,EaglerSPServer> serverConnections = new HashMap(); private static final Map<WebSocket,EaglerSPServer> serverConnections = new HashMap<>();
private static final Map<String,List<EaglerSPClient>> clientAddressSets = new HashMap(); private static final Map<String,List<EaglerSPClient>> clientAddressSets = new HashMap<>();
private static final Map<String,List<EaglerSPServer>> serverAddressSets = new HashMap(); private static final Map<String,List<EaglerSPServer>> serverAddressSets = new HashMap<>();
@Override @Override
public void onStart() { public void onStart() {
@ -178,7 +195,7 @@ public class EaglerSPRelay extends WebSocketServer {
} }
String addr; String addr;
long millis = System.currentTimeMillis(); long millis = Util.millis();
if(config.isEnableRealIpHeader() && arg1.hasFieldValue(config.getRealIPHeaderName())) { if(config.isEnableRealIpHeader() && arg1.hasFieldValue(config.getRealIPHeaderName())) {
addr = arg1.getFieldValue(config.getRealIPHeaderName()).toLowerCase(); addr = arg1.getFieldValue(config.getRealIPHeaderName()).toLowerCase();
}else { }else {
@ -202,7 +219,7 @@ public class EaglerSPRelay extends WebSocketServer {
} }
if(totalCons >= config.getConnectionsPerIP()) { 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.send(RelayPacketFEDisconnectClient.ratelimitPacketTooMany);
arg0.close(); arg0.close();
return; return;
@ -231,7 +248,7 @@ public class EaglerSPRelay extends WebSocketServer {
RelayPacket00Handshake ipkt = (RelayPacket00Handshake)pkt; RelayPacket00Handshake ipkt = (RelayPacket00Handshake)pkt;
if(ipkt.connectionVersion != Constants.protocolVersion) { if(ipkt.connectionVersion != Constants.protocolVersion) {
logger.debug("[{}]: Connected with unsupported protocol version: {} (supported " 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) { if(ipkt.connectionVersion < Constants.protocolVersion) {
arg0.send(RelayPacket.writePacket(new RelayPacketFFErrorCode(RelayPacketFFErrorCode.TYPE_PROTOCOL_VERSION, arg0.send(RelayPacket.writePacket(new RelayPacketFFErrorCode(RelayPacketFFErrorCode.TYPE_PROTOCOL_VERSION,
"Outdated Client! (v" + Constants.protocolVersion + " req)"), EaglerSPRelay.logger)); "Outdated Client! (v" + Constants.protocolVersion + " req)"), EaglerSPRelay.logger));
@ -244,63 +261,76 @@ public class EaglerSPRelay extends WebSocketServer {
} }
if(ipkt.connectionType == 0x01) { if(ipkt.connectionType == 0x01) {
if(!rateLimit(worldRateLimiter, arg0, waiting.address)) { if(!rateLimit(worldRateLimiter, arg0, waiting.address)) {
logger.debug("[{}]: Got world ratelimited", arg0.getAttachment()); logger.debug("[{}]: Got world ratelimited", (Object) arg0.getAttachment());
return; return;
} }
boolean fuck = false;
synchronized(serverAddressSets) { synchronized(serverAddressSets) {
List<EaglerSPServer> lst = serverAddressSets.get(waiting.address); List<EaglerSPServer> lst = serverAddressSets.get(waiting.address);
if(lst != null) { if(lst != null) {
if(lst.size() >= config.getWorldsPerIP()) { if(lst.size() >= config.getWorldsPerIP()) {
logger.debug("[{}]: Too many worlds are open on this address", arg0.getAttachment()); fuck = true;
arg0.send(RelayPacketFEDisconnectClient.ratelimitPacketTooMany);
arg0.close();
return;
} }
} }
} }
logger.debug("[{}]: Connected as a server", arg0.getAttachment()); if(fuck) {
EaglerSPServer srv; logger.debug("[{}]: Too many worlds are open on this address", (Object) arg0.getAttachment());
synchronized(serverCodes) { arg0.send(RelayPacketFEDisconnectClient.ratelimitPacketTooMany);
int j = 0; arg0.close();
String code; return;
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);
} }
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) { synchronized(serverConnections) {
serverConnections.put(arg0, srv); serverConnections.put(arg0, srv);
} }
synchronized(serverAddressSets) { synchronized(serverAddressSets) {
List<EaglerSPServer> lst = serverAddressSets.get(srv.serverAddress); List<EaglerSPServer> lst = serverAddressSets.get(srv.serverAddress);
if(lst == null) { if(lst == null) {
lst = new ArrayList(); lst = new ArrayList<>();
serverAddressSets.put(srv.serverAddress, lst); serverAddressSets.put(srv.serverAddress, lst);
} }
lst.add(srv); lst.add(srv);
} }
srv.send(new RelayPacket01ICEServers(EaglerSPRelayConfigRelayList.relayServers)); 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 { }else {
if(!rateLimit(pingRateLimiter, arg0, waiting.address)) { if(!rateLimit(pingRateLimiter, arg0, waiting.address)) {
logger.debug("[{}]: Got ping ratelimited", arg0.getAttachment()); logger.debug("[{}]: Got ping ratelimited", (Object) arg0.getAttachment());
return; return;
} }
if(ipkt.connectionType == 0x02) { if(ipkt.connectionType == 0x02) {
String code = ipkt.connectionCode; 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()) { if(code.length() != config.getCodeLength()) {
logger.debug("The code '{}' is invalid because it's the wrong length, disconnecting", code); logger.debug("The code '{}' is invalid because it's the wrong length, disconnecting", code);
arg0.send(RelayPacket.writePacket(new RelayPacketFFErrorCode(RelayPacketFFErrorCode.TYPE_CODE_LENGTH, arg0.send(RelayPacket.writePacket(new RelayPacketFFErrorCode(RelayPacketFFErrorCode.TYPE_CODE_LENGTH,
@ -323,36 +353,35 @@ public class EaglerSPRelay extends WebSocketServer {
String id; String id;
EaglerSPClient cl; EaglerSPClient cl;
synchronized(clientIds) { synchronized(clientIds) {
int j = 0;
do { do {
id = EaglerSPClient.generateClientId(); id = EaglerSPClient.generateClientId();
}while(clientIds.containsKey(id)); }while(clientIds.containsKey(id));
cl = new EaglerSPClient(arg0, srv, id, waiting.address); cl = new EaglerSPClient(arg0, srv, id, waiting.address);
clientIds.put(id, cl); 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) { synchronized(clientConnections) {
clientConnections.put(arg0, cl); clientConnections.put(arg0, cl);
} }
synchronized(clientAddressSets) { synchronized(clientAddressSets) {
List<EaglerSPClient> lst = clientAddressSets.get(cl.address); List<EaglerSPClient> lst = clientAddressSets.get(cl.address);
if(lst == null) { if(lst == null) {
lst = new ArrayList(); lst = new ArrayList<>();
clientAddressSets.put(cl.address, lst); clientAddressSets.put(cl.address, lst);
} }
lst.add(cl); lst.add(cl);
} }
cl.send(new RelayPacket01ICEServers(EaglerSPRelayConfigRelayList.relayServers)); 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) { }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.send(RelayPacket.writePacket(new RelayPacket69Pong(Constants.protocolVersion, config.getComment(), Constants.versionBrand), EaglerSPRelay.logger));
arg0.close(); arg0.close();
}else if(ipkt.connectionType == 0x04) { }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()) { if(config.isEnableShowLocals()) {
arg0.send(RelayPacket.writePacket(new RelayPacket07LocalWorlds(getLocalWorlds(waiting.address)), EaglerSPRelay.logger)); arg0.send(RelayPacket.writePacket(new RelayPacket07LocalWorlds(getLocalWorlds(waiting.address)), EaglerSPRelay.logger));
}else { }else {
@ -360,7 +389,7 @@ public class EaglerSPRelay extends WebSocketServer {
} }
arg0.close(); arg0.close();
}else { }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, arg0.send(RelayPacket.writePacket(new RelayPacketFFErrorCode(RelayPacketFFErrorCode.TYPE_ILLEGAL_OPERATION,
"Unexpected Init Packet"), EaglerSPRelay.logger)); "Unexpected Init Packet"), EaglerSPRelay.logger));
arg0.close(); arg0.close();
@ -368,7 +397,7 @@ public class EaglerSPRelay extends WebSocketServer {
} }
}else { }else {
logger.debug("[{}]: Pending connection did not send a 0x00 packet to identify " 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, arg0.send(RelayPacket.writePacket(new RelayPacketFFErrorCode(RelayPacketFFErrorCode.TYPE_ILLEGAL_OPERATION,
"Unexpected Init Packet"), EaglerSPRelay.logger)); "Unexpected Init Packet"), EaglerSPRelay.logger));
arg0.close(); arg0.close();
@ -380,7 +409,7 @@ public class EaglerSPRelay extends WebSocketServer {
} }
if(srv != null) { if(srv != null) {
if(!srv.handle(pkt)) { 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, arg0.send(RelayPacket.writePacket(new RelayPacketFFErrorCode(RelayPacketFFErrorCode.TYPE_INVALID_PACKET,
"Invalid Packet Recieved"), EaglerSPRelay.logger)); "Invalid Packet Recieved"), EaglerSPRelay.logger));
arg0.close(); arg0.close();
@ -392,13 +421,13 @@ public class EaglerSPRelay extends WebSocketServer {
} }
if(cl != null) { if(cl != null) {
if(!cl.handle(pkt)) { 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, arg0.send(RelayPacket.writePacket(new RelayPacketFFErrorCode(RelayPacketFFErrorCode.TYPE_INVALID_PACKET,
"Invalid Packet Recieved"), EaglerSPRelay.logger)); "Invalid Packet Recieved"), EaglerSPRelay.logger));
arg0.close(); arg0.close();
} }
}else { }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, arg0.send(RelayPacket.writePacket(new RelayPacketFFErrorCode(RelayPacketFFErrorCode.TYPE_ILLEGAL_OPERATION,
"Internal Server Error"), EaglerSPRelay.logger)); "Internal Server Error"), EaglerSPRelay.logger));
arg0.close(); arg0.close();
@ -406,14 +435,14 @@ public class EaglerSPRelay extends WebSocketServer {
} }
} }
}catch(Throwable t) { }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(); arg0.close();
} }
} }
@Override @Override
public void onMessage(WebSocket arg0, String arg1) { 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(); arg0.close();
} }
@ -424,7 +453,7 @@ public class EaglerSPRelay extends WebSocketServer {
srv = serverConnections.remove(arg0); srv = serverConnections.remove(arg0);
} }
if(srv != null) { if(srv != null) {
logger.debug("[{}]: Server closed, code: {}", arg0.getAttachment(), srv.code); logger.debug("[{}]: Server closed, code: {}", (Object) arg0.getAttachment(), srv.code);
synchronized(serverCodes) { synchronized(serverCodes) {
serverCodes.remove(srv.code); serverCodes.remove(srv.code);
} }
@ -439,13 +468,13 @@ public class EaglerSPRelay extends WebSocketServer {
} }
ArrayList<EaglerSPClient> clientList; ArrayList<EaglerSPClient> clientList;
synchronized(clientConnections) { synchronized(clientConnections) {
clientList = new ArrayList(clientConnections.values()); clientList = new ArrayList<>(clientConnections.values());
} }
Iterator<EaglerSPClient> itr = clientList.iterator(); Iterator<EaglerSPClient> itr = clientList.iterator();
while(itr.hasNext()) { while(itr.hasNext()) {
EaglerSPClient cl = itr.next(); EaglerSPClient cl = itr.next();
if(cl.server == srv) { 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(); 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) { synchronized(clientIds) {
clientIds.remove(cl.id); clientIds.remove(cl.id);
} }
cl.server.handleClientDisconnect(cl); cl.server.handleClientDisconnect(cl);
}else { }else {
logger.debug("[{}]: Connection Closed", arg0.getAttachment()); logger.debug("[{}]: Connection Closed", (Object) arg0.getAttachment());
} }
} }
} }
@Override @Override
public void onError(WebSocket arg0, Exception arg1) { 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); logger.debug(arg1);
if(arg0 != null) arg0.close(); if(arg0 != null) arg0.close();
} }
private List<RelayPacket07LocalWorlds.LocalWorld> getLocalWorlds(String addr) { private List<RelayPacket07LocalWorlds.LocalWorld> getLocalWorlds(String addr) {
List<RelayPacket07LocalWorlds.LocalWorld> lst = new ArrayList(); List<RelayPacket07LocalWorlds.LocalWorld> lst = new ArrayList<>();
synchronized(serverAddressSets) { synchronized(serverAddressSets) {
List<EaglerSPServer> srvs = serverAddressSets.get(addr); List<EaglerSPServer> srvs = serverAddressSets.get(addr);
if(srvs != null) { if(srvs != null) {

View File

@ -295,7 +295,7 @@ public class EaglerSPRelayConfig {
save(conf); save(conf);
} }
String[] splitted = originWhitelist.split(";"); String[] splitted = originWhitelist.split(";");
List<String> splittedList = new ArrayList(); List<String> splittedList = new ArrayList<>();
for(int i = 0; i < splitted.length; ++i) { for(int i = 0; i < splitted.length; ++i) {
splitted[i] = splitted[i].trim().toLowerCase(); splitted[i] = splitted[i].trim().toLowerCase();
if(splitted[i].length() > 0) { if(splitted[i].length() > 0) {

View File

@ -28,10 +28,10 @@ import net.lax1dude.eaglercraft.v1_8.sp.relay.pkt.RelayPacket01ICEServers;
*/ */
public class EaglerSPRelayConfigRelayList { public class EaglerSPRelayConfigRelayList {
public static final Collection<RelayPacket01ICEServers.RelayServer> relayServers = new ArrayList(); public static final Collection<RelayPacket01ICEServers.RelayServer> relayServers = new ArrayList<>();
public static void loadRelays(File list) throws IOException { public static void loadRelays(File list) throws IOException {
ArrayList<RelayPacket01ICEServers.RelayServer> loading = new ArrayList(); ArrayList<RelayPacket01ICEServers.RelayServer> loading = new ArrayList<>();
if(!list.isFile()) { if(!list.isFile()) {
EaglerSPRelay.logger.info("Creating new {}...", list.getName()); EaglerSPRelay.logger.info("Creating new {}...", list.getName());

View File

@ -35,7 +35,7 @@ public class EaglerSPServer {
EaglerSPServer(WebSocket sock, String code, String serverName, String serverAddress) { EaglerSPServer(WebSocket sock, String code, String serverName, String serverAddress) {
this.socket = sock; this.socket = sock;
this.code = code; this.code = code;
this.clients = new HashMap(); this.clients = new HashMap<>();
if(serverName.endsWith(";1")) { if(serverName.endsWith(";1")) {
this.serverHidden = true; this.serverHidden = true;
@ -78,7 +78,7 @@ public class EaglerSPServer {
if(LoginState.assertEquals(cl, LoginState.SENT_ICE_CANDIDATE)) { if(LoginState.assertEquals(cl, LoginState.SENT_ICE_CANDIDATE)) {
cl.state = LoginState.RECIEVED_ICE_CANIDATE; cl.state = LoginState.RECIEVED_ICE_CANIDATE;
cl.handleServerICECandidate(packet); 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 { }else {
socket.send(RelayPacket.writePacket(new RelayPacketFFErrorCode(RelayPacketFFErrorCode.TYPE_UNKNOWN_CLIENT, socket.send(RelayPacket.writePacket(new RelayPacketFFErrorCode(RelayPacketFFErrorCode.TYPE_UNKNOWN_CLIENT,
@ -92,7 +92,7 @@ public class EaglerSPServer {
if(LoginState.assertEquals(cl, LoginState.SENT_DESCRIPTION)) { if(LoginState.assertEquals(cl, LoginState.SENT_DESCRIPTION)) {
cl.state = LoginState.RECIEVED_DESCRIPTION; cl.state = LoginState.RECIEVED_DESCRIPTION;
cl.handleServerDescription(packet); 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 { }else {
socket.send(RelayPacket.writePacket(new RelayPacketFFErrorCode(RelayPacketFFErrorCode.TYPE_UNKNOWN_CLIENT, socket.send(RelayPacket.writePacket(new RelayPacketFFErrorCode(RelayPacketFFErrorCode.TYPE_UNKNOWN_CLIENT,
@ -104,7 +104,7 @@ public class EaglerSPServer {
EaglerSPClient cl = clients.get(packet.clientId); EaglerSPClient cl = clients.get(packet.clientId);
if(cl != null) { if(cl != null) {
cl.handleServerDisconnectClient(packet); 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); packet.code, packet.reason);
}else { }else {
socket.send(RelayPacket.writePacket(new RelayPacketFFErrorCode(RelayPacketFFErrorCode.TYPE_UNKNOWN_CLIENT, socket.send(RelayPacket.writePacket(new RelayPacketFFErrorCode(RelayPacketFFErrorCode.TYPE_UNKNOWN_CLIENT,
@ -119,9 +119,9 @@ public class EaglerSPServer {
public void handleNewClient(EaglerSPClient client) { public void handleNewClient(EaglerSPClient client) {
synchronized(clients) { synchronized(clients) {
clients.put(client.id, client); 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) { public void handleClientDisconnect(EaglerSPClient client) {

View File

@ -34,14 +34,14 @@ public class RateLimiter {
protected boolean locked; protected boolean locked;
protected RateLimitEntry() { protected RateLimitEntry() {
timer = System.currentTimeMillis(); timer = Util.millis();
count = 0; count = 0;
lockedTimer = 0l; lockedTimer = 0l;
locked = false; locked = false;
} }
protected void update() { protected void update() {
long millis = System.currentTimeMillis(); long millis = Util.millis();
if(locked) { if(locked) {
if(millis - lockedTimer > RateLimiter.this.lockoutDuration) { if(millis - lockedTimer > RateLimiter.this.lockoutDuration) {
timer = millis; timer = millis;
@ -70,7 +70,7 @@ public class RateLimiter {
NONE, LIMIT, LIMIT_NOW_LOCKOUT, LOCKOUT; NONE, LIMIT, LIMIT_NOW_LOCKOUT, LOCKOUT;
} }
private final Map<String, RateLimitEntry> limiters = new HashMap(); private final Map<String, RateLimitEntry> limiters = new HashMap<>();
public RateLimiter(int period, int limit, int lockoutLimit, int lockoutDuration) { public RateLimiter(int period, int limit, int lockoutLimit, int lockoutDuration) {
this.period = period; this.period = period;
@ -79,50 +79,44 @@ public class RateLimiter {
this.lockoutDuration = lockoutDuration; this.lockoutDuration = lockoutDuration;
} }
public RateLimit limit(String addr) { public synchronized RateLimit limit(String addr) {
synchronized(this) { RateLimitEntry etr = limiters.get(addr);
RateLimitEntry etr = limiters.get(addr);
if(etr == null) { if(etr == null) {
etr = new RateLimitEntry(); etr = new RateLimitEntry();
limiters.put(addr, etr); limiters.put(addr, etr);
}else { }else {
etr.update(); etr.update();
} }
if(etr.locked) { if(etr.locked) {
return RateLimit.LOCKOUT; return RateLimit.LOCKOUT;
} }
++etr.count; ++etr.count;
if(etr.count >= lockoutLimit) { if(etr.count >= lockoutLimit) {
etr.count = 0; etr.count = 0;
etr.locked = true; etr.locked = true;
etr.lockedTimer = System.currentTimeMillis(); etr.lockedTimer = Util.millis();
return RateLimit.LIMIT_NOW_LOCKOUT; return RateLimit.LIMIT_NOW_LOCKOUT;
}else if(etr.count > limit) { }else if(etr.count > limit) {
return RateLimit.LIMIT; return RateLimit.LIMIT;
}else { }else {
return RateLimit.NONE; return RateLimit.NONE;
}
}
public synchronized void update() {
Iterator<RateLimitEntry> itr = limiters.values().iterator();
while(itr.hasNext()) {
if(itr.next().count == 0) {
itr.remove();
} }
} }
} }
public void update() { public synchronized void reset() {
synchronized(this) { limiters.clear();
Iterator<RateLimitEntry> itr = limiters.values().iterator();
while(itr.hasNext()) {
if(itr.next().count == 0) {
itr.remove();
}
}
}
}
public void reset() {
synchronized(this) {
limiters.clear();
}
} }
} }

View File

@ -30,4 +30,7 @@ public class Util {
return sock.getAddress().getHostAddress() + ":" + sock.getPort(); return sock.getAddress().getHostAddress() + ":" + sock.getPort();
} }
public static long millis() {
return System.nanoTime() / 1000000l;
}
} }