A BUNCH of little fixes to how clients are handled

This commit is contained in:
Firepup Sixfifty 2024-07-16 16:16:17 +00:00
parent 9e892ccb9d
commit 99ea5d7994
Signed by: Firepup650
SSH key fingerprint: SHA256:U0Zp8EhEe3CMqFSrC79CqatzaEiL4sjta80/RSX2XrY

View file

@ -11,7 +11,7 @@ G = Globals()
G.uniqueClients = 0 G.uniqueClients = 0
G.serverLinks = 0 G.serverLinks = 0
G.servers = {} G.servers = {}
G.clientsConnected = [] G.clientsConnected = {}
port = 65048 port = 65048
G.msgs = [] G.msgs = []
G.remoteID = uname().node G.remoteID = uname().node
@ -131,7 +131,7 @@ async def handleClient(reader, writer):
await writer.wait_closed() await writer.wait_closed()
return return
if not name.startswith("S2S-"): if not name.startswith("S2S-"):
G.clientsConnected.append(name.lower()) G.clientsConnected[name.lower()] = G.remoteID
msgIndex = 0 msgIndex = 0
G.uniqueClients += 1 G.uniqueClients += 1
G.msgs.extend([log(f"{name} has connected to the server.")]) G.msgs.extend([log(f"{name} has connected to the server.")])
@ -192,7 +192,7 @@ async def handleClient(reader, writer):
await writer.wait_closed() await writer.wait_closed()
G.uniqueClients -= 1 G.uniqueClients -= 1
G.msgs.append(log(f"{name} has disconnected from the server.")) G.msgs.append(log(f"{name} has disconnected from the server."))
G.clientsConnected.remove(name.lower()) del G.clientsConnected[name.lower()]
G.S2SLogs.append(("-", name, G.remoteID)) G.S2SLogs.append(("-", name, G.remoteID))
else: # This is... probably a server? else: # This is... probably a server?
sName = name[4:] # Trim off the S2S label sName = name[4:] # Trim off the S2S label
@ -225,7 +225,8 @@ async def handleClient(reader, writer):
writer.write(b"I added that client.\n") writer.write(b"I added that client.\n")
await writer.drain() await writer.drain()
G.msgs.append(log(f"{client} has connected from {sName}")) G.msgs.append(log(f"{client} has connected from {sName}"))
G.servers[sName].append(client.lower()) G.servers[sName][client.lower()] = False
G.clientsConnected[client.lower()] = sName
G.S2SLogs.append(("+", client, sName)) G.S2SLogs.append(("+", client, sName))
writer.write(f"{G.remoteID}\n".encode("utf8")) writer.write(f"{G.remoteID}\n".encode("utf8"))
await writer.drain() await writer.drain()
@ -235,10 +236,12 @@ async def handleClient(reader, writer):
await writer.drain() await writer.drain()
resp = raw((await reader.read(1024)).decode("utf8")) resp = raw((await reader.read(1024)).decode("utf8"))
if resp.startswith("K"): if resp.startswith("K"):
G.killList[client.lower()] = True if G.clientsConnected[client] == G.remoteID:
G.killList[client] = True
else:
G.servers[G.clientsConnected[client]] = True
writer.write(f"END OF CLIENT LISTING FROM {G.remoteID}\n".encode("utf8")) writer.write(f"END OF CLIENT LISTING FROM {G.remoteID}\n".encode("utf8"))
await writer.drain() await writer.drain()
G.clientsConnected.extend(G.servers[sName])
msgInd = len(G.S2SLogs) msgInd = len(G.S2SLogs)
while 1: while 1:
try: try:
@ -256,19 +259,28 @@ async def handleClient(reader, writer):
G.msgs.append( G.msgs.append(
log(f"{cName} has connected from {sName}") log(f"{cName} has connected from {sName}")
) )
G.servers[sName].append(cName.lower()) G.servers[sName][cName.lower()] = False
G.clientsConnected.append(cName.lower()) G.clientsConnected[cName.lower()] = sName
G.S2SLogs.append(("+", cName, sName)) G.S2SLogs.append(("+", cName, sName))
writer.write(b"I Mmm... Pineapples\n") writer.write(b"I Mmm... Pineapples\n")
else: else:
writer.write(f"K {cName}\n".encode("utf8")) writer.write(f"K {cName}\n".encode("utf8"))
case "-": case "-":
cName = buffer[2:] cName = buffer[2:]
G.msgs.append(log(f"{cName} has disconnected from {sName}")) if G.clientsConnected.get(cName, None) == sName:
G.servers[sName].remove(cName.lower()) G.msgs.append(
G.clientsConnected.remove(cName.lower()) log(f"{cName} has disconnected from {sName}")
)
del G.servers[sName][cName.lower()]
del G.clientsConnected[cName.lower()]
G.S2SLogs.append(("-", cName, sName)) G.S2SLogs.append(("-", cName, sName))
writer.write(b"I Mmm... Bananas\n") writer.write(b"I Mmm... Bananas\n")
else:
writer.write(
f"Your server is LYING about who is connected to it. - {G.remoteID}, a fellow server\n".encode(
"utf8"
)
)
case "M": case "M":
cName = buffer[2:].split("|", 1)[0] cName = buffer[2:].split("|", 1)[0]
message = buffer[2:].split("|", 1)[1] message = buffer[2:].split("|", 1)[1]
@ -298,6 +310,12 @@ async def handleClient(reader, writer):
await writer.drain() await writer.drain()
except TimeoutError: except TimeoutError:
pass pass
if any(G.servers[sName].values()):
for name in G.servers[sName]:
if G.servers[sName][name]:
writer.write(f"K {name}\n".encode("utf8"))
await writer.drain()
G.servers[sName][name] = False
while msgInd < len(G.S2SLogs): while msgInd < len(G.S2SLogs):
type, data, server = G.S2SLogs[msgInd] type, data, server = G.S2SLogs[msgInd]
# match-case on type # match-case on type
@ -323,7 +341,7 @@ async def handleClient(reader, writer):
for cName in G.servers[sName]: for cName in G.servers[sName]:
G.msgs.append(log(f"{cName}'s server is going down")) G.msgs.append(log(f"{cName}'s server is going down"))
G.S2SLogs.append(("-", cName, sName)) G.S2SLogs.append(("-", cName, sName))
G.clientsConnected.remove(cName.lower()) del G.clientsConnected[cName.lower()]
G.serverLinks -= 1 G.serverLinks -= 1
del G.servers[sName] del G.servers[sName]
G.msgs.append(log(f"{sName} has de-linked from the network")) G.msgs.append(log(f"{sName} has de-linked from the network"))
@ -336,12 +354,12 @@ async def handleClient(reader, writer):
G.uniqueClients -= 1 G.uniqueClients -= 1
G.msgs.append(log(f"{name} has disconnected from the server.")) G.msgs.append(log(f"{name} has disconnected from the server."))
G.S2SLogs.append(("-", name, G.remoteID)) G.S2SLogs.append(("-", name, G.remoteID))
G.clientsConnected.remove(name.lower()) del G.clientsConnected[name.lower()]
else: else:
for cName in G.servers[name[4:]]: for cName in G.servers[name[4:]]:
G.msgs.append(log(f"{cName}'s server is going down")) G.msgs.append(log(f"{cName}'s server is going down"))
try: try:
G.clientsConnected.remove(cName.lower()) del G.clientsConnected[cName.lower()]
G.S2SLogs.append(("-", cName, name[4:])) G.S2SLogs.append(("-", cName, name[4:]))
except Exception: # Crash during connection sequence? except Exception: # Crash during connection sequence?
pass pass
@ -362,7 +380,10 @@ async def connectServer(hostname: str, port: int):
await writer.drain() await writer.drain()
resp = raw((await reader.read(1024)).decode("utf8")) resp = raw((await reader.read(1024)).decode("utf8"))
if resp.startswith("K"): if resp.startswith("K"):
G.killList[client.lower()] = True if G.clientsConnected[client] == G.remoteID:
G.killList[client] = True
else:
G.servers[G.clientsConnected[client]] = True
writer.write(f"END OF CLIENT LISTING FROM {G.remoteID}\n".encode("utf8")) writer.write(f"END OF CLIENT LISTING FROM {G.remoteID}\n".encode("utf8"))
await writer.drain() await writer.drain()
rID = raw((await reader.read(16)).decode("utf8")) rID = raw((await reader.read(16)).decode("utf8"))
@ -395,8 +416,9 @@ async def connectServer(hostname: str, port: int):
writer.write(b"I added that client.\n") writer.write(b"I added that client.\n")
await writer.drain() await writer.drain()
G.msgs.append(log(f"{client} has connected from {rID}")) G.msgs.append(log(f"{client} has connected from {rID}"))
G.servers[rID].append(client.lower()) G.servers[rID][client.lower()] = False
G.clientsConnected.extend(G.servers[rID]) for client in G.servers[rID]:
G.clientsConnected[client] = rID
msgInd = len(G.S2SLogs) msgInd = len(G.S2SLogs)
try: try:
while 1: while 1:
@ -414,17 +436,24 @@ async def connectServer(hostname: str, port: int):
if cName.lower() not in G.clientsConnected: if cName.lower() not in G.clientsConnected:
G.msgs.append(log(f"{cName} has connected from {rID}")) G.msgs.append(log(f"{cName} has connected from {rID}"))
G.S2SLogs.append(("+", cName, rID)) G.S2SLogs.append(("+", cName, rID))
G.servers[rID].append(cName.lower()) G.servers[rID][cName.lower()] = False
G.clientsConnected.append(cName.lower()) G.clientsConnected[cName.lower()] = rID
writer.write(b"I Mmm... Pineapples\n") writer.write(b"I Mmm... Pineapples\n")
else: else:
writer.write(f"K {cName}\n".encode("utf8")) writer.write(f"K {cName}\n".encode("utf8"))
case "-": case "-":
cName = buffer[2:] cName = buffer[2:]
if G.clientsConnected.get(cName, None) == rID:
G.msgs.append(log(f"{cName} has disconnected from {rID}")) G.msgs.append(log(f"{cName} has disconnected from {rID}"))
G.servers[rID].remove(cName.lower()) del G.servers[rID][cName.lower()]
G.clientsConnected.remove(cName.lower()) del G.clientsConnected[cName.lower()]
G.S2SLogs.append(("-", cName, rID)) G.S2SLogs.append(("-", cName, rID))
else:
writer.write(
f"Your server is LYING about who is connected to it. - {G.remoteID}, a fellow server\n".encode(
"utf8"
)
)
case "M": case "M":
cName = buffer[2:].split("|", 1)[0] cName = buffer[2:].split("|", 1)[0]
message = buffer[2:].split("|", 1)[1] message = buffer[2:].split("|", 1)[1]
@ -454,6 +483,12 @@ async def connectServer(hostname: str, port: int):
await writer.drain() await writer.drain()
except TimeoutError: except TimeoutError:
pass pass
if any(G.servers[rID].values()):
for name in G.servers[rID]:
if G.servers[rID][name]:
writer.write(f"K {name}\n".encode("utf8"))
await writer.drain()
G.servers[rID][name] = False
while msgInd < len(G.S2SLogs): while msgInd < len(G.S2SLogs):
type, data, server = G.S2SLogs[msgInd] type, data, server = G.S2SLogs[msgInd]
if server != rID: if server != rID:
@ -477,7 +512,7 @@ async def connectServer(hostname: str, port: int):
await writer.wait_closed() await writer.wait_closed()
for cName in G.servers[rID]: for cName in G.servers[rID]:
G.msgs.append(log(f"{cName}'s server is going down")) G.msgs.append(log(f"{cName}'s server is going down"))
G.clientsConnected.remove(cName.lower()) del G.clientsConnected[cName.lower()]
G.serverLinks -= 1 G.serverLinks -= 1
del G.servers[rID] del G.servers[rID]
G.msgs.append(log(f"{rID} has de-linked from the network")) G.msgs.append(log(f"{rID} has de-linked from the network"))
@ -485,7 +520,7 @@ async def connectServer(hostname: str, port: int):
for cName in G.servers[rID]: for cName in G.servers[rID]:
G.msgs.append(log(f"{cName}'s server is going down")) G.msgs.append(log(f"{cName}'s server is going down"))
try: try:
G.clientsConnected.remove(cName.lower()) del G.clientsConnected[cName.lower()]
except Exception: except Exception:
pass pass
G.serverLinks -= 1 G.serverLinks -= 1