forked from Firepup650/python-talk
More S2S work, it might finally be done!
This commit is contained in:
parent
272fa88527
commit
baaa9e811e
1 changed files with 72 additions and 27 deletions
99
server.py
99
server.py
|
@ -1,4 +1,5 @@
|
||||||
import os, sys, asyncio, re, signal
|
import os, sys, asyncio, re, signal
|
||||||
|
from traceback import format_exc
|
||||||
from firepup650 import console
|
from firepup650 import console
|
||||||
from logs import log
|
from logs import log
|
||||||
|
|
||||||
|
@ -21,7 +22,7 @@ G.killList = {}
|
||||||
G.outboundLinks = []
|
G.outboundLinks = []
|
||||||
G.S2SLogs = []
|
G.S2SLogs = []
|
||||||
saveLogs = True
|
saveLogs = True
|
||||||
address = '0.0.0.0'
|
address = "0.0.0.0"
|
||||||
# Try to load a message log, if one exists
|
# Try to load a message log, if one exists
|
||||||
try:
|
try:
|
||||||
G.msgs = __import__("cache").msgs
|
G.msgs = __import__("cache").msgs
|
||||||
|
@ -48,9 +49,9 @@ try:
|
||||||
log("Explicitly disabling saving of logs!")
|
log("Explicitly disabling saving of logs!")
|
||||||
saveLogs = False
|
saveLogs = False
|
||||||
elif arg.startswith("--link"):
|
elif arg.startswith("--link"):
|
||||||
G.outboundLinks.append((arg[6:], int(arg.split(":")[1])))
|
G.outboundLinks.append((arg[7:].split(":")[0], int(arg.split(":")[1])))
|
||||||
elif arg.startswith("--hostname"):
|
elif arg.startswith("--hostname"):
|
||||||
G.remoteID = arg[10:]
|
G.remoteID = arg[11:]
|
||||||
elif arg.startswith("--address"):
|
elif arg.startswith("--address"):
|
||||||
address = arg[9:]
|
address = arg[9:]
|
||||||
else:
|
else:
|
||||||
|
@ -117,7 +118,7 @@ async def handleClient(reader, writer):
|
||||||
response = None
|
response = None
|
||||||
if request.startswith("/mes "):
|
if request.startswith("/mes "):
|
||||||
response = log(f"* {name}'s {request[5:]}")
|
response = log(f"* {name}'s {request[5:]}")
|
||||||
G.S2SLogs.append(("A", (name+"'s", request[5:]), G.remoteID))
|
G.S2SLogs.append(("A", (name + "'s", request[5:]), G.remoteID))
|
||||||
elif request.startswith("/me "):
|
elif request.startswith("/me "):
|
||||||
response = log(f"* {name} {request[4:]}")
|
response = log(f"* {name} {request[4:]}")
|
||||||
G.S2SLogs.append(("A", (name, request[4:]), G.remoteID))
|
G.S2SLogs.append(("A", (name, request[4:]), G.remoteID))
|
||||||
|
@ -129,7 +130,9 @@ async def handleClient(reader, writer):
|
||||||
elif request.startswith("/afk"):
|
elif request.startswith("/afk"):
|
||||||
if len(request) > 5:
|
if len(request) > 5:
|
||||||
response = log(f"* {name} is afk to {request[5:]}")
|
response = log(f"* {name} is afk to {request[5:]}")
|
||||||
G.S2SLogs.append(("A", (name, f"is afk to {request[5:]}"), G.remoteID))
|
G.S2SLogs.append(
|
||||||
|
("A", (name, f"is afk to {request[5:]}"), G.remoteID)
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
response = log(f"* {name} is afk")
|
response = log(f"* {name} is afk")
|
||||||
G.S2SLogs.append(("A", (name, "is afk"), G.remoteID))
|
G.S2SLogs.append(("A", (name, "is afk"), G.remoteID))
|
||||||
|
@ -160,7 +163,7 @@ async def handleClient(reader, writer):
|
||||||
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
|
||||||
log(f"Server link! Link from {sName}")
|
G.msgs.append(log(f"{sName} has linked to the network"))
|
||||||
G.serverLinks += 1
|
G.serverLinks += 1
|
||||||
G.servers[sName] = []
|
G.servers[sName] = []
|
||||||
msgIndex = 0
|
msgIndex = 0
|
||||||
|
@ -169,7 +172,10 @@ async def handleClient(reader, writer):
|
||||||
client = raw((await reader.read(1024)).decode("utf8"))
|
client = raw((await reader.read(1024)).decode("utf8"))
|
||||||
if client == f"END OF CLIENT LISTING FROM {sName}":
|
if client == f"END OF CLIENT LISTING FROM {sName}":
|
||||||
break
|
break
|
||||||
if client.lower() in G.servers[sName] or client.lower() in G.clientsConnected:
|
if (
|
||||||
|
client.lower() in G.servers[sName]
|
||||||
|
or client.lower() in G.clientsConnected
|
||||||
|
):
|
||||||
writer.write(b"K Client rejected: Already exists\n")
|
writer.write(b"K Client rejected: Already exists\n")
|
||||||
await writer.drain()
|
await writer.drain()
|
||||||
continue
|
continue
|
||||||
|
@ -224,7 +230,7 @@ async def handleClient(reader, writer):
|
||||||
cName = buffer[2:].split("|", 1)[0]
|
cName = buffer[2:].split("|", 1)[0]
|
||||||
message = buffer[2:].split("|", 1)[1]
|
message = buffer[2:].split("|", 1)[1]
|
||||||
G.msgs.append(log(f" {cName}: {message}"))
|
G.msgs.append(log(f" {cName}: {message}"))
|
||||||
G.S2SLogs(("M", (cName, message), sName))
|
G.S2SLogs.append(("M", (cName, message), sName))
|
||||||
writer.write(
|
writer.write(
|
||||||
b"I Get these damn heretic ghost clients out of my store so i can buy my cult candles in peace.\n"
|
b"I Get these damn heretic ghost clients out of my store so i can buy my cult candles in peace.\n"
|
||||||
)
|
)
|
||||||
|
@ -232,7 +238,7 @@ async def handleClient(reader, writer):
|
||||||
cName = buffer[2:].split("|", 1)[0]
|
cName = buffer[2:].split("|", 1)[0]
|
||||||
message = buffer[2:].split("|", 1)[1]
|
message = buffer[2:].split("|", 1)[1]
|
||||||
G.msgs.append(log(f"* {cName} {message}"))
|
G.msgs.append(log(f"* {cName} {message}"))
|
||||||
G.S2SLogs(("A", (cName, message), sName))
|
G.S2SLogs.append(("A", (cName, message), sName))
|
||||||
writer.write(b"I Mmm... Strawberries\n")
|
writer.write(b"I Mmm... Strawberries\n")
|
||||||
case "Q":
|
case "Q":
|
||||||
break
|
break
|
||||||
|
@ -242,7 +248,9 @@ async def handleClient(reader, writer):
|
||||||
writer.write(b"I Mmm... Blood\n")
|
writer.write(b"I Mmm... Blood\n")
|
||||||
case _:
|
case _:
|
||||||
writer.write(
|
writer.write(
|
||||||
f"S Your server is doing drugs over here, sending me bullshit messages man - {G.remoteID}, A fellow server\n".encode("utf8")
|
f"S Your server is doing drugs over here, sending me bullshit messages man - {G.remoteID}, A fellow server\n".encode(
|
||||||
|
"utf8"
|
||||||
|
)
|
||||||
)
|
)
|
||||||
await writer.drain()
|
await writer.drain()
|
||||||
except TimeoutError:
|
except TimeoutError:
|
||||||
|
@ -253,9 +261,15 @@ async def handleClient(reader, writer):
|
||||||
if server != sName:
|
if server != sName:
|
||||||
match type:
|
match type:
|
||||||
case "A":
|
case "A":
|
||||||
|
nick, msg = data
|
||||||
|
writer.write(f"A {nick}|{msg}\n".encode("utf8"))
|
||||||
case "M":
|
case "M":
|
||||||
|
nick, msg = data
|
||||||
|
writer.write(f"M {nick}|{msg}\n".encode("utf8"))
|
||||||
case "+":
|
case "+":
|
||||||
|
writer.write(f"+ {data}\n".encode("utf8"))
|
||||||
case "-":
|
case "-":
|
||||||
|
writer.write(f"- {data}\n".encode("utf8"))
|
||||||
case _:
|
case _:
|
||||||
pass
|
pass
|
||||||
await writer.drain()
|
await writer.drain()
|
||||||
|
@ -270,7 +284,11 @@ async def handleClient(reader, writer):
|
||||||
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"))
|
||||||
except (ConnectionResetError, BrokenPipeError):
|
except (
|
||||||
|
ConnectionResetError,
|
||||||
|
BrokenPipeError,
|
||||||
|
IndexError,
|
||||||
|
): # Don't ask. IndexError needs to be caught here too.
|
||||||
if not name.startswith("S2S-"):
|
if not name.startswith("S2S-"):
|
||||||
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."))
|
||||||
|
@ -281,7 +299,7 @@ async def handleClient(reader, writer):
|
||||||
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())
|
G.clientsConnected.remove(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
|
||||||
G.serverLinks -= 1
|
G.serverLinks -= 1
|
||||||
|
@ -290,6 +308,7 @@ async def handleClient(reader, writer):
|
||||||
|
|
||||||
|
|
||||||
async def connectServer(hostname: str, port: int):
|
async def connectServer(hostname: str, port: int):
|
||||||
|
global G
|
||||||
reader, writer = await asyncio.open_connection(hostname, port)
|
reader, writer = await asyncio.open_connection(hostname, port)
|
||||||
await reader.read(1024)
|
await reader.read(1024)
|
||||||
writer.write(f"S2S-{G.remoteID}\n".encode("utf8"))
|
writer.write(f"S2S-{G.remoteID}\n".encode("utf8"))
|
||||||
|
@ -304,8 +323,9 @@ async def connectServer(hostname: str, port: int):
|
||||||
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(1024)).decode("utf8"))
|
rID = raw((await reader.read(1024)).decode("utf8"))
|
||||||
|
G.msgs.append(log(f"{rID} has linked to the network"))
|
||||||
G.serverLinks += 1
|
G.serverLinks += 1
|
||||||
G.servers.append(rID)
|
G.servers[rID] = []
|
||||||
writer.write(b"I recieved your remote ID, now awaiting client listing\n")
|
writer.write(b"I recieved your remote ID, now awaiting client listing\n")
|
||||||
await writer.drain()
|
await writer.drain()
|
||||||
# recieve client list from the other server
|
# recieve client list from the other server
|
||||||
|
@ -322,6 +342,7 @@ async def connectServer(hostname: str, port: int):
|
||||||
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].append(client.lower())
|
||||||
G.clientsConnected.extend(G.servers[rID])
|
G.clientsConnected.extend(G.servers[rID])
|
||||||
|
msgInd = len(G.S2SLogs)
|
||||||
try:
|
try:
|
||||||
while 1:
|
while 1:
|
||||||
try:
|
try:
|
||||||
|
@ -337,6 +358,7 @@ async def connectServer(hostname: str, port: int):
|
||||||
cName = buffer[2:]
|
cName = buffer[2:]
|
||||||
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.servers[rID].append(cName.lower())
|
G.servers[rID].append(cName.lower())
|
||||||
G.clientsConnected.append(cName.lower())
|
G.clientsConnected.append(cName.lower())
|
||||||
writer.write(b"I Mmm... Pineapples\n")
|
writer.write(b"I Mmm... Pineapples\n")
|
||||||
|
@ -344,19 +366,22 @@ async def connectServer(hostname: str, port: int):
|
||||||
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}"))
|
G.msgs.append(log(f"{cName} has disconnected from {rID}"))
|
||||||
G.servers[rID].remove(cName.lower())
|
G.servers[rID].remove(cName.lower())
|
||||||
G.clientsConnected.remove(cName.lower())
|
G.clientsConnected.remove(cName.lower())
|
||||||
|
G.S2SLogs.append(("-", cName, rID))
|
||||||
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]
|
||||||
G.msgs.append(log(f" {cName}: {message}"))
|
G.msgs.append(log(f" {cName}: {message}"))
|
||||||
|
G.S2SLogs.append(("M", (cName, message), rID))
|
||||||
writer.write(
|
writer.write(
|
||||||
b"I Get these damn heretic ghost clients out of my store so i can buy my cult candles in peace.\n"
|
b"I Get these damn heretic ghost clients out of my store so i can buy my cult candles in peace.\n"
|
||||||
)
|
)
|
||||||
case "A":
|
case "A":
|
||||||
cName = buffer[2:].split("|", 1)[0]
|
cName = buffer[2:].split("|", 1)[0]
|
||||||
message = buffer[2:].split("|", 1)[1]
|
message = buffer[2:].split("|", 1)[1]
|
||||||
|
G.S2SLogs.append(("A", (cName, message), rID))
|
||||||
G.msgs.append(log(f"* {cName} {message}"))
|
G.msgs.append(log(f"* {cName} {message}"))
|
||||||
writer.write(b"I Mmm... Strawberries\n")
|
writer.write(b"I Mmm... Strawberries\n")
|
||||||
case "Q":
|
case "Q":
|
||||||
|
@ -374,16 +399,34 @@ async def connectServer(hostname: str, port: int):
|
||||||
await writer.drain()
|
await writer.drain()
|
||||||
except TimeoutError:
|
except TimeoutError:
|
||||||
pass
|
pass
|
||||||
await writer.drain()
|
while msgInd < len(G.S2SLogs):
|
||||||
writer.close()
|
type, data, server = G.S2SLogs[msgInd]
|
||||||
await writer.wait_closed()
|
if server != rID:
|
||||||
for cName in G.servers[rID]:
|
match type:
|
||||||
G.msgs.append(log(f"{cName}'s server is going down"))
|
case "A":
|
||||||
G.clientsConnected.remove(cName.lower())
|
nick, msg = data
|
||||||
G.serverLinks -= 1
|
writer.write(f"A {nick}|{msg}\n".encode("utf8"))
|
||||||
G.servers.remove(rID)
|
case "M":
|
||||||
G.msgs.append(log(f"{rID} has de-linked from the network"))
|
nick, msg = data
|
||||||
except (ConnectionResetError, BrokenPipeError):
|
writer.write(f"M {nick}|{msg}\n".encode("utf8"))
|
||||||
|
case "+":
|
||||||
|
writer.write(f"+ {data}\n".encode("utf8"))
|
||||||
|
case "-":
|
||||||
|
writer.write(f"- {data}\n".encode("utf8"))
|
||||||
|
case _:
|
||||||
|
pass
|
||||||
|
await writer.drain()
|
||||||
|
msgInd += 1
|
||||||
|
await writer.drain()
|
||||||
|
writer.close()
|
||||||
|
await writer.wait_closed()
|
||||||
|
for cName in G.servers[rID]:
|
||||||
|
G.msgs.append(log(f"{cName}'s server is going down"))
|
||||||
|
G.clientsConnected.remove(cName.lower())
|
||||||
|
G.serverLinks -= 1
|
||||||
|
G.servers.remove(rID)
|
||||||
|
G.msgs.append(log(f"{rID} has de-linked from the network"))
|
||||||
|
except (ConnectionResetError, BrokenPipeError, IndexError):
|
||||||
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:
|
||||||
|
@ -409,9 +452,11 @@ async def runServer(address: str, port: int):
|
||||||
await asyncio.gather(*links)
|
await asyncio.gather(*links)
|
||||||
except Exception:
|
except Exception:
|
||||||
crash = True
|
crash = True
|
||||||
G.msgs.append(log("Server crash", level="FATAL")[1:])
|
G.msgs.append(log("Server crash", "FATAL")[1:])
|
||||||
log("Shutting down from Exception")
|
log("Shutting down from Exception", "FATAL")
|
||||||
# TODO: Add format_exc here
|
Err = format_exc()
|
||||||
|
for line in Err.split("\n"):
|
||||||
|
log(line, "ERROR")
|
||||||
finally:
|
finally:
|
||||||
if not crash:
|
if not crash:
|
||||||
G.msgs.append(log("Server shutdown"))
|
G.msgs.append(log("Server shutdown"))
|
||||||
|
|
Loading…
Reference in a new issue