{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 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;

View File

@ -43,7 +43,7 @@ public class DebugLogger implements IRelayLogger {
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) {
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);
}
}
}

View File

@ -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;

View File

@ -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<WebSocket> pendingToClose = new LinkedList<>();
final List<EaglerSPClient> clientToClose = new LinkedList<>();
while(true) {
try {
long millis = System.currentTimeMillis();
long millis = Util.millis();
pendingToClose.clear();
clientToClose.clear();
synchronized(pendingConnections) {
Iterator<Entry<WebSocket,PendingConnection>> itr = pendingConnections.entrySet().iterator();
while(itr.hasNext()) {
Entry<WebSocket,PendingConnection> etr = itr.next();
if(millis - etr.getValue().openTime > 500l) {
etr.getKey().close();
pendingToClose.add(etr.getKey());
itr.remove();
}
}
@ -97,9 +102,21 @@ public class EaglerSPRelay extends WebSocketServer {
while(itr.hasNext()) {
EaglerSPClient cl = itr.next();
if(millis - cl.createdOn > 10000l) {
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) {
@ -156,13 +173,13 @@ public class EaglerSPRelay extends WebSocketServer {
}
private static final Map<WebSocket,PendingConnection> pendingConnections = new HashMap();
private static final Map<String,EaglerSPClient> clientIds = new HashMap();
private static final Map<WebSocket,EaglerSPClient> clientConnections = new HashMap();
private static final Map<String,EaglerSPServer> serverCodes = 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<EaglerSPServer>> serverAddressSets = new HashMap();
private static final Map<WebSocket,PendingConnection> pendingConnections = new HashMap<>();
private static final Map<String,EaglerSPClient> clientIds = new HashMap<>();
private static final Map<WebSocket,EaglerSPClient> clientConnections = new HashMap<>();
private static final Map<String,EaglerSPServer> serverCodes = 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<EaglerSPServer>> 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<EaglerSPServer> 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());
fuck = true;
}
}
}
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", arg0.getAttachment());
EaglerSPServer srv;
logger.debug("[{}]: Connected as a server", (Object) arg0.getAttachment());
EaglerSPServer srv = null;
String code = null;
synchronized(serverCodes) {
eagler: {
int j = 0;
String code;
do {
if(++j > 100) {
if(++j > 20) {
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;
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: {}", arg0.getAttachment(), code);
}
logger.debug("[{}][Relay -> Server] PKT 0x00: Assign join code: {}", (Object) arg0.getAttachment(), code);
synchronized(serverConnections) {
serverConnections.put(arg0, srv);
}
synchronized(serverAddressSets) {
List<EaglerSPServer> 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);
}
synchronized(clientConnections) {
clientConnections.put(arg0, cl);
}
synchronized(clientAddressSets) {
List<EaglerSPClient> 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<EaglerSPClient> clientList;
synchronized(clientConnections) {
clientList = new ArrayList(clientConnections.values());
clientList = new ArrayList<>(clientConnections.values());
}
Iterator<EaglerSPClient> 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<RelayPacket07LocalWorlds.LocalWorld> getLocalWorlds(String addr) {
List<RelayPacket07LocalWorlds.LocalWorld> lst = new ArrayList();
List<RelayPacket07LocalWorlds.LocalWorld> lst = new ArrayList<>();
synchronized(serverAddressSets) {
List<EaglerSPServer> srvs = serverAddressSets.get(addr);
if(srvs != null) {

View File

@ -295,7 +295,7 @@ public class EaglerSPRelayConfig {
save(conf);
}
String[] splitted = originWhitelist.split(";");
List<String> splittedList = new ArrayList();
List<String> splittedList = new ArrayList<>();
for(int i = 0; i < splitted.length; ++i) {
splitted[i] = splitted[i].trim().toLowerCase();
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 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 {
ArrayList<RelayPacket01ICEServers.RelayServer> loading = new ArrayList();
ArrayList<RelayPacket01ICEServers.RelayServer> loading = new ArrayList<>();
if(!list.isFile()) {
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) {
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,10 +119,10 @@ 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);
}
}
public void handleClientDisconnect(EaglerSPClient client) {
synchronized(clients) {

View File

@ -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<String, RateLimitEntry> limiters = new HashMap();
private final Map<String, RateLimitEntry> limiters = new HashMap<>();
public RateLimiter(int period, int limit, int lockoutLimit, int lockoutDuration) {
this.period = period;
@ -79,8 +79,7 @@ public class RateLimiter {
this.lockoutDuration = lockoutDuration;
}
public RateLimit limit(String addr) {
synchronized(this) {
public synchronized RateLimit limit(String addr) {
RateLimitEntry etr = limiters.get(addr);
if(etr == null) {
@ -98,7 +97,7 @@ public class RateLimiter {
if(etr.count >= lockoutLimit) {
etr.count = 0;
etr.locked = true;
etr.lockedTimer = System.currentTimeMillis();
etr.lockedTimer = Util.millis();
return RateLimit.LIMIT_NOW_LOCKOUT;
}else if(etr.count > limit) {
return RateLimit.LIMIT;
@ -106,10 +105,8 @@ public class RateLimiter {
return RateLimit.NONE;
}
}
}
public void update() {
synchronized(this) {
public synchronized void update() {
Iterator<RateLimitEntry> itr = limiters.values().iterator();
while(itr.hasNext()) {
if(itr.next().count == 0) {
@ -117,12 +114,9 @@ public class RateLimiter {
}
}
}
}
public void reset() {
synchronized(this) {
public synchronized void reset() {
limiters.clear();
}
}
}

View File

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