Skip to content

Commit

Permalink
rejig usc to be keyed by username not IP
Browse files Browse the repository at this point in the history
  • Loading branch information
alyssaruth committed Nov 9, 2024
1 parent ed9ac8c commit f13e0c5
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 53 deletions.
54 changes: 17 additions & 37 deletions server/src/main/java/server/MessageHandlerRunnable.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ public class MessageHandlerRunnable implements ServerRunnable,
private Socket clientSocket = null;
private String messageStr = null;
private UserConnection usc = null;
private SecretKey symmetricKey = null;
private String ipAddress = null;
private SecretKey symmetricKey = LegacyConstants.INSTANCE.getSYMMETRIC_KEY();
private boolean notificationSocket = false;

public MessageHandlerRunnable(EntropyServer server, Socket clientSocket)
Expand All @@ -57,25 +56,25 @@ public void run()
osw = new OutputStreamWriter(os, "US-ASCII");

clientSocket.setSoTimeout(120 * 1000); //2 minutes
initVariablesFromSocket();

in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
encryptedMessage = in.readLine();

//Now get the response
Document response = getResponse(encryptedMessage);
processMessage(encryptedMessage);

//If messageStr is null here, just return out
if (messageStr == null)
{
return;
}


//Now get the response
Document message = XmlUtil.getDocumentFromXmlString(messageStr);
Element root = message.getDocumentElement();
name = root.getNodeName();
String username = root.getAttribute("Username");

usc = ServerGlobals.INSTANCE.getUscStore().find(username);

Document response = getResponse();
if (notificationSocket)
{
usc.replaceNotificationSocket(name, new NotificationSocket(clientSocket, os, osw, in));
Expand Down Expand Up @@ -138,62 +137,43 @@ public void run()
}
}

private void initVariablesFromSocket()
{
InetAddress address = clientSocket.getInetAddress();
ipAddress = address.getHostAddress();
usc = ServerGlobals.INSTANCE.getUscStore().find(ipAddress);

if (usc != null)
{
symmetricKey = LegacyConstants.INSTANCE.getSYMMETRIC_KEY();
}
}

/**
* Gets the response to be sent to the client. Returns null if there is an error.
*/
private Document getResponse(String encryptedMessage) throws Throwable
{
private void processMessage(String encryptedMessage) {
Document unencryptedDocument = XmlUtil.getDocumentFromXmlString(encryptedMessage, true);
if (unencryptedDocument != null)
{
//We've been sent an unencrypted XML message. Either this is the client agreeing a new
//symmetric key, or else it's someone using an old version of Entropy/sending crap.
messageStr = encryptedMessage;
return handleUnencryptedMessage(unencryptedDocument);
}

if (symmetricKey == null)
{
//This client hasn't agreed a symmetric key with us. Stack trace and return null.
//Don't blacklist for this. Whilst it shouldn't happen, I can replicate this just by connecting to the
//server then hibernating the PC until I've been kicked off. Still stack trace for now.
//server.incrementFunctionsReceivedAndHandledForMessage("NO_SYMMETRIC_KEY");
//server.addToBlacklist(ipAddress, "NO_SYMMETRIC_KEY");
Debug.stackTrace("Received non-XML message from an IP with no symmetric key: " + ipAddress + ". Message: " + encryptedMessage);
return null;
handleUnencryptedMessage(unencryptedDocument);
}

//Decrypt the message with the symmetric key
messageStr = EncryptionUtil.decrypt(encryptedMessage, symmetricKey);
if (messageStr == null)
{
Debug.append("Failed to decrypt message " + encryptedMessage + " from IP " + ipAddress);
Debug.append("Failed to decrypt message " + encryptedMessage);
}
}
private Document getResponse() throws Throwable
{
if (messageStr == null)
{
return null;
}

return getResponseForMessage();
}

private Document handleUnencryptedMessage(Document message)
private void handleUnencryptedMessage(Document message)
{
Element root = message.getDocumentElement();
String name = root.getNodeName();

CoreGlobals.logger.info("unencrypted.message", "Received unencrypted " + name + " message. Probably using out of date version? "
+ "Message: " + messageStr);
return null;
}

private Document getResponseForMessage() throws Throwable
Expand Down
6 changes: 3 additions & 3 deletions server/src/main/kotlin/auth/UserConnection.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ private val ALL_SOCKETS =
XmlConstants.SOCKET_NAME_LOBBY
)

data class UserConnection(val ipAddress: String, val name: String) : IHasId<String> {
override val id = ipAddress
data class UserConnection(val name: String) : IHasId<String> {
override val id = name

val colour: String = ColourGenerator.generateNextColour()
var lastActive: Long = -1
Expand Down Expand Up @@ -61,7 +61,7 @@ data class UserConnection(val ipAddress: String, val name: String) : IHasId<Stri
return hmSocketBySocketName[socketType]
}

override fun toString() = "$name @ $ipAddress"
override fun toString() = name

fun sendNotificationInWorkerPool(
message: String?,
Expand Down
6 changes: 3 additions & 3 deletions server/src/main/kotlin/routes/session/SessionService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class SessionService(
}

// Also populate legacy user connection
val usc = UserConnection(ip, name)
val usc = UserConnection(name)
usc.setLastActiveNow()
uscStore.put(usc)
ServerGlobals.lobbyService.lobbyChanged(usc)
Expand All @@ -75,7 +75,7 @@ class SessionService(
}

fun finishSession(session: Session) {
val usc = uscStore.get(session.ip)
val usc = uscStore.get(session.name)
usc.destroyNotificationSockets()

val rooms: List<Room> = ServerGlobals.server.getRooms()
Expand All @@ -84,7 +84,7 @@ class SessionService(
room.removePlayer(usc.name, false)
}

uscStore.remove(session.ip)
uscStore.remove(session.name)
sessionStore.remove(session.id)

logger.info("finishSession", "Session ended for ${usc.name}")
Expand Down
8 changes: 4 additions & 4 deletions server/src/test/kotlin/routes/lobby/LobbyServiceTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class LobbyServiceTest : AbstractTest() {
fun `Should do nothing if told to exclude the only user`() {
val server = mockk<EntropyServer>(relaxed = true)
val (service, _, _, uscStore) = makeService(server = server)
val usc = UserConnection("1.2.3.4", "Alyssa")
val usc = UserConnection("Alyssa")
uscStore.put(usc)
service.lobbyChanged(usc)

Expand All @@ -68,9 +68,9 @@ class LobbyServiceTest : AbstractTest() {
fun `Should notify the correct users`() {
val server = mockk<EntropyServer>(relaxed = true)
val (service, _, _, uscStore) = makeService(server = server)
val uscA = UserConnection("1.2.3.4", "Alyssa")
val uscB = UserConnection("5.6.7.8", "Bob")
val uscC = UserConnection("0.0.0.0", "Cat")
val uscA = UserConnection("Alyssa")
val uscB = UserConnection("Bob")
val uscC = UserConnection("Cat")
uscStore.putAll(uscA, uscB, uscC)

service.lobbyChanged(uscA)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ class SessionServiceTest : AbstractTest() {

val usc = uscStore.getAll().only()
usc.name shouldBe "Alyssa"
usc.ipAddress shouldBe "1.2.3.4"
}

@Test
Expand Down
8 changes: 4 additions & 4 deletions server/src/test/kotlin/store/MemoryUserConnectionStoreTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import auth.UserConnection
class MemoryUserConnectionStoreTest : AbstractStoreTest<String, UserConnection>() {
override fun makeStore() = MemoryUserConnectionStore()

override fun makeIdA() = "1.2.3.A"
override fun makeIdA() = "Alyssa"

override fun makeIdB() = "4.5.6.B"
override fun makeIdB() = "Leah"

override fun makeItemA(id: String) = UserConnection(id, "Alyssa")
override fun makeItemA(id: String) = UserConnection(id)

override fun makeItemB(id: String) = UserConnection(id, "Leah")
override fun makeItemB(id: String) = UserConnection(id)
}
2 changes: 1 addition & 1 deletion server/src/test/kotlin/util/TestFactory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fun makeSession(
apiVersion: Int = OnlineConstants.API_VERSION,
) = Session(id, name, ip, achievementCount, apiVersion)

fun makeUserConnection(session: Session) = UserConnection(session.ip, session.name)
fun makeUserConnection(session: Session) = UserConnection(session.name)

fun makeGameSettings(
mode: GameMode = GameMode.Entropy,
Expand Down

0 comments on commit f13e0c5

Please sign in to comment.