From 71c7683e0611ac7847cb64578f923f8cb2905180 Mon Sep 17 00:00:00 2001 From: Cmd-GZ Date: Wed, 1 Jul 2026 01:16:02 +0800 Subject: [PATCH] Fix: HTTP request to Global Server will be regarded as a player 1. Test player ID, If the number of line is >= 2 and there is a HTTP request prefix, then regard the `player` as HTTP request and reject it. --- src/GlobalServer/LobbyPlayer.java | 39 +++++++++++++++++++++++++++++-- src/GraphServer/Connection.java | 15 ++++++++++++ 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/src/GlobalServer/LobbyPlayer.java b/src/GlobalServer/LobbyPlayer.java index 606f002..7cfdd77 100644 --- a/src/GlobalServer/LobbyPlayer.java +++ b/src/GlobalServer/LobbyPlayer.java @@ -131,15 +131,50 @@ public void run() try { this.name = connection.readMessage(); - + System.out.println("New name: "+name); - + + if(name == null) + { + this.globalServer.removePlayer(this); + return; + } + if(name.compareTo(Constants.DUMMY_NAME) == 0) { this.dummy = true; } else { + boolean looksLikeHttp = name.startsWith("GET ") || name.startsWith("POST ") || + name.startsWith("HEAD ") || name.startsWith("PUT ") || + name.startsWith("DELETE ") || name.startsWith("OPTIONS ") || + name.startsWith("TRACE ") || name.startsWith("CONNECT ") || + name.startsWith("PATCH "); + + if (looksLikeHttp) + { + boolean hasSecondLine = false; + try + { + String secondLine = connection.readMessageWithTimeout(200); + if (secondLine != null) + { + hasSecondLine = true; + } + } + catch (java.io.IOException e) + { + } + + if (hasSecondLine) + { + System.out.println("Rejected HTTP request from " + getIpAddress() + ": " + name); + this.globalServer.removePlayer(this); + return; + } + } + this.dummy = false; } } diff --git a/src/GraphServer/Connection.java b/src/GraphServer/Connection.java index 7ba49f4..379232f 100644 --- a/src/GraphServer/Connection.java +++ b/src/GraphServer/Connection.java @@ -100,4 +100,19 @@ public String readMessage() throws IOException return line; } + + public String readMessageWithTimeout(int timeout) throws IOException + { + int oldTimeout = socket.getSoTimeout(); + socket.setSoTimeout(timeout); + + try + { + return in.readLine(); + } + finally + { + socket.setSoTimeout(oldTimeout); + } + } }