2024-06-15 03:14:45 +00:00
|
|
|
import os, sys, asyncio, re, signal
|
|
|
|
from firepup650 import console
|
|
|
|
from logs import log
|
|
|
|
|
|
|
|
|
|
|
|
class Globals: ...
|
|
|
|
|
|
|
|
|
|
|
|
G = Globals()
|
|
|
|
G.uniqueClients = 0
|
2024-07-05 05:31:47 +00:00
|
|
|
G.serverLinks = 0
|
|
|
|
G.servers = {}
|
|
|
|
G.clientsConnected = []
|
2024-06-15 03:14:45 +00:00
|
|
|
port = 65048
|
|
|
|
G.msgs = []
|
2024-07-14 05:17:40 +00:00
|
|
|
G.remoteID = "pyTalkServ"
|
2024-06-15 03:14:45 +00:00
|
|
|
G.event = asyncio.Event()
|
|
|
|
G.loop = asyncio.get_event_loop()
|
|
|
|
G.interruptCount = 0
|
2024-07-13 10:16:30 +00:00
|
|
|
G.killList = {}
|
|
|
|
G.outboundLinks = []
|
|
|
|
G.S2SLogs = []
|
2024-07-13 08:28:37 +00:00
|
|
|
saveLogs = True
|
2024-07-14 05:17:40 +00:00
|
|
|
address = '0.0.0.0'
|
2024-06-15 03:14:45 +00:00
|
|
|
# Try to load a message log, if one exists
|
|
|
|
try:
|
|
|
|
G.msgs = __import__("cache").msgs
|
|
|
|
log(f"Got {len(G.msgs)} lines from message cache")
|
|
|
|
except ImportError:
|
|
|
|
log("No server message cache.", level="WARN")
|
|
|
|
except Exception:
|
|
|
|
log(
|
|
|
|
"Abnormal state! Failed to load cache even though the file exists!",
|
|
|
|
level="WARN",
|
|
|
|
)
|
|
|
|
try:
|
2024-07-14 05:17:40 +00:00
|
|
|
sys.argv.pop(0) # Ignore the filename
|
2024-06-15 03:14:45 +00:00
|
|
|
for arg in sys.argv:
|
2024-07-13 10:16:30 +00:00
|
|
|
if arg.startswith("--port") or arg.startswith("-p"):
|
|
|
|
port = int(arg.lstrip("-port="))
|
2024-06-15 03:14:45 +00:00
|
|
|
elif arg in ["-n", "--no-cache"]:
|
|
|
|
log("Explicitly erasing cached messages")
|
|
|
|
G.msgs = []
|
|
|
|
elif arg in ["-?", "-h", "--help"]:
|
|
|
|
print("TODO: Help menu soon")
|
|
|
|
exit(0)
|
2024-07-13 08:28:37 +00:00
|
|
|
elif arg in ["-l", "--no-logs"]:
|
2024-07-13 10:16:30 +00:00
|
|
|
log("Explicitly disabling saving of logs!")
|
2024-07-13 08:28:37 +00:00
|
|
|
saveLogs = False
|
2024-07-13 10:16:30 +00:00
|
|
|
elif arg.startswith("--link"):
|
|
|
|
G.outboundLinks.append((arg[6:], int(arg.split(":")[1])))
|
2024-07-14 05:17:40 +00:00
|
|
|
elif arg.startswith("--hostname"):
|
|
|
|
G.remoteID = arg[10:]
|
|
|
|
elif arg.startswith("--address"):
|
|
|
|
address = arg[9:]
|
2024-07-13 10:16:30 +00:00
|
|
|
else:
|
|
|
|
log(f"Unrecognized argument {arg}!", "WARN")
|
2024-06-15 03:14:45 +00:00
|
|
|
except Exception:
|
|
|
|
sys.tracebacklimit = 0
|
|
|
|
raise ValueError("Invalid arguments. Please refer to -? for usage.") from None
|
|
|
|
|
|
|
|
|
2024-07-05 05:31:47 +00:00
|
|
|
def raw(string: str) -> str:
|
|
|
|
s = string.strip()
|
2024-07-13 08:28:37 +00:00
|
|
|
s = f"{s!r}"[1:-1].replace("\\\\", "\\")
|
|
|
|
if '"' in s:
|
|
|
|
return s.replace("\\'", "'")
|
|
|
|
else:
|
|
|
|
return s
|
|
|
|
|
2024-07-05 05:31:47 +00:00
|
|
|
|
|
|
|
def fmt(msg: str, name: str = "", action: bool = False) -> str:
|
|
|
|
if action:
|
|
|
|
return f"* {name}{' '*(20-len(name))} {msg}"
|
|
|
|
else:
|
|
|
|
return f" {name}{' '*(20-len(name))}: {msg}"
|
|
|
|
|
2024-07-13 08:28:37 +00:00
|
|
|
|
2024-07-13 11:28:41 +00:00
|
|
|
async def handleClient(reader, writer):
|
2024-06-15 03:14:45 +00:00
|
|
|
try:
|
|
|
|
global G
|
2024-07-05 05:31:47 +00:00
|
|
|
writer.write(b"Please identify yourself. Nick limit is 20 chars.\n")
|
2024-06-15 03:14:45 +00:00
|
|
|
await writer.drain()
|
2024-07-05 05:31:47 +00:00
|
|
|
name = raw((await reader.read(20)).decode("utf8"))
|
2024-06-15 03:14:45 +00:00
|
|
|
if len(name) > 20:
|
|
|
|
name = name[
|
|
|
|
:19
|
|
|
|
] # Really this is only possible if someone passes raw unicode as a nick, but let's clean it up anyways.
|
|
|
|
try:
|
|
|
|
await asyncio.wait_for(
|
|
|
|
reader.read(), 0.01
|
2024-07-13 08:28:37 +00:00
|
|
|
) # Silently consume the excess username data
|
2024-06-15 03:14:45 +00:00
|
|
|
except asyncio.TimeoutError:
|
|
|
|
pass
|
|
|
|
if not name:
|
|
|
|
writer.write(b"Nice try. Actually set a nick.\n")
|
|
|
|
await writer.drain()
|
|
|
|
writer.close()
|
|
|
|
await writer.wait_closed()
|
|
|
|
return
|
2024-07-05 05:31:47 +00:00
|
|
|
if name in G.clientsConnected:
|
2024-06-15 03:14:45 +00:00
|
|
|
writer.write(f"Nick ({name}) in use\n".encode("utf8"))
|
|
|
|
await writer.drain()
|
|
|
|
writer.close()
|
|
|
|
await writer.wait_closed()
|
|
|
|
return
|
2024-07-05 05:31:47 +00:00
|
|
|
if not name.startswith("S2S-"):
|
2024-07-14 05:17:40 +00:00
|
|
|
G.clientsConnected.append(name.lower())
|
2024-07-05 05:31:47 +00:00
|
|
|
msgIndex = 0
|
|
|
|
G.uniqueClients += 1
|
|
|
|
G.msgs.extend([log(f"{name} has connected to the server.")])
|
|
|
|
while 1:
|
|
|
|
try:
|
|
|
|
buffer = await asyncio.wait_for(reader.read(967), 0.1)
|
|
|
|
request = raw(buffer.decode("utf8"))
|
|
|
|
response = None
|
|
|
|
if request.startswith("/mes "):
|
|
|
|
response = log(f"* {name}'s {request[5:]}")
|
|
|
|
elif request.startswith("/me "):
|
|
|
|
response = log(f"* {name} {request[4:]}")
|
|
|
|
elif request.startswith("/h"):
|
|
|
|
writer.write(b"TODO: Command listing\n")
|
|
|
|
await writer.drain()
|
|
|
|
elif request.startswith("/quit"):
|
|
|
|
break
|
2024-07-13 08:28:37 +00:00
|
|
|
elif request.startswith("/afk"):
|
|
|
|
if len(request) > 5:
|
|
|
|
response = log(f"* {name} is afk to {request[4:]}")
|
|
|
|
else:
|
|
|
|
response = log(f"* {name} is afk")
|
|
|
|
elif request.startswith("/back"):
|
|
|
|
response = log(f"* {name} is back")
|
2024-07-05 05:31:47 +00:00
|
|
|
elif request:
|
|
|
|
response = log(f" {name}: {request}")
|
|
|
|
if response:
|
|
|
|
G.msgs.extend([response])
|
|
|
|
except asyncio.TimeoutError:
|
|
|
|
pass
|
|
|
|
if msgIndex < len(G.msgs):
|
|
|
|
writer.writelines(G.msgs[msgIndex:])
|
2024-07-13 08:28:37 +00:00
|
|
|
await writer.drain()
|
2024-07-05 05:31:47 +00:00
|
|
|
msgIndex = len(G.msgs)
|
2024-07-14 05:17:40 +00:00
|
|
|
if name.lower() in G.killList and G.killList[name.lower()]:
|
2024-07-13 08:28:37 +00:00
|
|
|
writer.write(b"Your client has been killed by the server\n")
|
2024-07-14 05:17:40 +00:00
|
|
|
G.killList[name.lower()] = False
|
2024-07-13 08:28:37 +00:00
|
|
|
break
|
2024-06-15 03:14:45 +00:00
|
|
|
await writer.drain()
|
2024-07-05 05:31:47 +00:00
|
|
|
writer.close()
|
|
|
|
await writer.wait_closed()
|
|
|
|
G.uniqueClients -= 1
|
2024-07-13 08:28:37 +00:00
|
|
|
G.msgs.append(log(f"{name} has disconnected from the server."))
|
2024-07-14 05:17:40 +00:00
|
|
|
G.clientsConnected.remove(name.lower())
|
2024-07-05 05:31:47 +00:00
|
|
|
else: # This is... probably a server?
|
|
|
|
sName = name[4:] # Trim off the S2S label
|
|
|
|
log(f"Server link! Link from {sName}")
|
|
|
|
G.serverLinks += 1
|
|
|
|
G.servers[sName] = []
|
|
|
|
msgIndex = 0
|
2024-07-13 11:28:41 +00:00
|
|
|
writer.write("I am awaiting your client listing.\n")
|
2024-07-05 05:31:47 +00:00
|
|
|
while 1:
|
2024-07-13 11:28:41 +00:00
|
|
|
client = raw((await reader.read(1024)).decode("utf8"))
|
2024-07-05 05:31:47 +00:00
|
|
|
if client == f"END OF CLIENT LISTING FROM {sName}":
|
|
|
|
break
|
2024-07-14 05:17:40 +00:00
|
|
|
if client.lower() in G.servers[sName] or client.lower() in G.clientsConnected:
|
2024-07-13 08:28:37 +00:00
|
|
|
writer.write(b"K Client rejected: Already exists\n")
|
|
|
|
await writer.drain()
|
2024-07-13 10:16:30 +00:00
|
|
|
continue
|
2024-07-13 11:28:41 +00:00
|
|
|
writer.write(b"I added that client.\n")
|
2024-07-13 08:28:37 +00:00
|
|
|
await writer.drain()
|
|
|
|
G.msgs.append(log(f"{client} has connected from {sName}"))
|
2024-07-14 05:17:40 +00:00
|
|
|
G.servers[sName].append(client.lower())
|
2024-07-13 11:28:41 +00:00
|
|
|
writer.write(f"{G.remoteID}\n".encode("utf8"))
|
|
|
|
await writer.drain()
|
|
|
|
await reader.read()
|
|
|
|
for client in G.clientsConnected:
|
|
|
|
writer.write(f"{client}\n".encode("utf8"))
|
|
|
|
resp = raw((await reader.read(1024)).decode("utf8"))
|
|
|
|
if resp.startswith("K"):
|
2024-07-14 05:17:40 +00:00
|
|
|
G.killList[client.lower()] = True
|
2024-07-13 11:28:41 +00:00
|
|
|
writer.write(f"END OF CLIENT LISTING FROM {G.remoteID}\n".encode("utf8"))
|
2024-07-05 05:31:47 +00:00
|
|
|
G.clientsConnected.extend(G.servers[sName])
|
|
|
|
while 1:
|
2024-07-05 06:44:35 +00:00
|
|
|
try:
|
|
|
|
rawMsg = await asyncio.wait_for(reader.read(967), 0.1)
|
|
|
|
buffer = raw(rawMsg.decode("utf8"))
|
|
|
|
match buffer[0]:
|
|
|
|
case "S": # Server notice
|
|
|
|
G.msgs.extend([log(buffer[2:])])
|
|
|
|
writer.write(b"I Mmm... Blueberries\n")
|
|
|
|
case "I":
|
|
|
|
pass
|
|
|
|
case "+":
|
|
|
|
cName = buffer[2:]
|
2024-07-14 05:17:40 +00:00
|
|
|
if cName.lower() not in G.clientsConnected:
|
2024-07-13 08:28:37 +00:00
|
|
|
G.msgs.append(
|
|
|
|
log(f"{cName} has connected from {sName}")
|
|
|
|
)
|
2024-07-14 05:17:40 +00:00
|
|
|
G.servers[sName].append(cName.lower())
|
|
|
|
G.clientsConnected.append(cName.lower())
|
2024-07-13 08:28:37 +00:00
|
|
|
writer.write(b"I Mmm... Pineapples\n")
|
|
|
|
else:
|
2024-07-14 05:17:40 +00:00
|
|
|
writer.write(f"K {cName}\n".encode("utf8"))
|
2024-07-05 06:44:35 +00:00
|
|
|
case "-":
|
|
|
|
cName = buffer[2:]
|
2024-07-13 08:28:37 +00:00
|
|
|
G.msgs.append(log(f"{cName} has disconnected from {sName}"))
|
2024-07-14 05:17:40 +00:00
|
|
|
G.servers[sName].remove(cName.lower())
|
|
|
|
G.clientsConnected.remove(cName.lower())
|
2024-07-05 06:44:35 +00:00
|
|
|
writer.write(b"I Mmm... Bananas\n")
|
|
|
|
case "M":
|
|
|
|
cName = buffer[2:].split("|", 1)[0]
|
|
|
|
message = buffer[2:].split("|", 1)[1]
|
2024-07-13 08:28:37 +00:00
|
|
|
G.msgs.append(log(f" {cName}: {message}"))
|
|
|
|
writer.write(
|
|
|
|
b"I Get these damn heretic ghost clients out of my store so i can buy my cult candles in peace."
|
|
|
|
)
|
2024-07-05 06:44:35 +00:00
|
|
|
case "A":
|
|
|
|
cName = buffer[2:].split("|", 1)[0]
|
|
|
|
message = buffer[2:].split("|", 1)[1]
|
2024-07-13 08:28:37 +00:00
|
|
|
G.msgs.append(log(f"* {cName} {message}"))
|
|
|
|
writer.write(b"I Mmm... Strawberries\n")
|
2024-07-05 06:44:35 +00:00
|
|
|
case "Q":
|
|
|
|
break
|
2024-07-13 08:28:37 +00:00
|
|
|
case "K":
|
|
|
|
cName = buffer[2:]
|
|
|
|
G.killList[cName] = True
|
|
|
|
writer.write(b"I Mmm... Blood\n")
|
2024-07-05 06:44:35 +00:00
|
|
|
case _:
|
2024-07-13 08:28:37 +00:00
|
|
|
writer.write(
|
2024-07-14 04:12:40 +00:00
|
|
|
f"S Your server is doing drugs over here, sending me bullshit messages man - {G.remoteID}, A fellow server\n".encode("utf8")
|
2024-07-13 08:28:37 +00:00
|
|
|
)
|
2024-07-13 10:16:30 +00:00
|
|
|
await writer.drain()
|
2024-07-05 06:44:35 +00:00
|
|
|
except TimeoutError:
|
|
|
|
pass
|
2024-07-13 10:16:30 +00:00
|
|
|
await writer.drain()
|
|
|
|
writer.close()
|
|
|
|
await writer.wait_closed()
|
|
|
|
for cName in G.servers[sName]:
|
2024-07-13 08:28:37 +00:00
|
|
|
G.msgs.append(log(f"{cName}'s server is going down"))
|
2024-07-14 05:17:40 +00:00
|
|
|
G.clientsConnected.remove(cName.lower())
|
2024-07-05 06:44:35 +00:00
|
|
|
G.serverLinks -= 1
|
|
|
|
G.servers.remove(sName)
|
2024-07-13 08:28:37 +00:00
|
|
|
G.msgs.append(log(f"{sName} has de-linked from the network"))
|
2024-07-13 10:16:30 +00:00
|
|
|
except (ConnectionResetError, BrokenPipeError):
|
2024-07-05 05:31:47 +00:00
|
|
|
if not name.startswith("S2S-"):
|
2024-07-05 06:44:35 +00:00
|
|
|
G.uniqueClients -= 1
|
2024-07-13 08:28:37 +00:00
|
|
|
G.msgs.append(log(f"{name} has disconnected from the server."))
|
2024-07-14 05:17:40 +00:00
|
|
|
G.clientsConnected.remove(name.lower())
|
2024-07-05 05:31:47 +00:00
|
|
|
else:
|
2024-07-13 10:16:30 +00:00
|
|
|
for cName in G.servers[name[4:]]:
|
2024-07-13 08:28:37 +00:00
|
|
|
G.msgs.append(log(f"{cName}'s server is going down"))
|
|
|
|
try:
|
2024-07-14 05:17:40 +00:00
|
|
|
G.clientsConnected.remove(cName.lower())
|
2024-07-13 08:28:37 +00:00
|
|
|
except Exception: # Crash during connection sequence?
|
|
|
|
pass
|
2024-07-05 05:31:47 +00:00
|
|
|
G.serverLinks -= 1
|
|
|
|
G.servers.remove(name[4:])
|
2024-07-13 08:28:37 +00:00
|
|
|
G.msgs.append(log(f"{name[4:]} has de-linked from the network"))
|
2024-06-15 03:14:45 +00:00
|
|
|
|
|
|
|
|
2024-07-13 10:16:30 +00:00
|
|
|
async def connectServer(hostname: str, port: int):
|
|
|
|
reader, writer = await asyncio.open_connection(hostname, port)
|
|
|
|
await reader.read(1024)
|
|
|
|
writer.write(f"S2S-{G.remoteID}\n".encode("utf8"))
|
|
|
|
await writer.drain()
|
|
|
|
await reader.read(1024)
|
|
|
|
for client in G.clientsConnected:
|
|
|
|
writer.write(f"{client}\n".encode("utf8"))
|
|
|
|
await writer.drain()
|
2024-07-13 11:28:41 +00:00
|
|
|
resp = raw((await reader.read(1024)).decode("utf8"))
|
|
|
|
if resp.startswith("K"):
|
2024-07-14 05:17:40 +00:00
|
|
|
G.killList[client.lower()] = True
|
2024-07-13 11:28:41 +00:00
|
|
|
writer.write(f"END OF CLIENT LISTING FROM {G.remoteID}\n".encode("utf8"))
|
2024-07-13 10:16:30 +00:00
|
|
|
await writer.drain()
|
2024-07-13 11:28:41 +00:00
|
|
|
rID = raw((await reader.read(1024)).decode("utf8"))
|
|
|
|
G.serverLinks += 1
|
|
|
|
G.servers.append(rID)
|
|
|
|
writer.write(b"I recieved your remote ID, now awaiting client listing\n")
|
2024-07-13 10:16:30 +00:00
|
|
|
# recieve client list from the other server
|
2024-07-13 11:28:41 +00:00
|
|
|
while 1:
|
|
|
|
client = raw((await reader.read(1024)).decode("utf8"))
|
|
|
|
if client == f"END OF CLIENT LISTING FROM {rID}":
|
|
|
|
break
|
2024-07-14 05:17:40 +00:00
|
|
|
if client.lower() in G.servers[rID] or client.lower() in G.clientsConnected:
|
2024-07-13 11:28:41 +00:00
|
|
|
writer.write(b"K Client rejected: Already exists\n")
|
|
|
|
await writer.drain()
|
|
|
|
continue
|
|
|
|
writer.write(b"I added that client.\n")
|
|
|
|
await writer.drain()
|
|
|
|
G.msgs.append(log(f"{client} has connected from {rID}"))
|
2024-07-14 05:17:40 +00:00
|
|
|
G.servers[rID].append(client.lower())
|
2024-07-13 11:28:41 +00:00
|
|
|
G.clientsConnected.extend(G.servers[rID])
|
2024-07-14 04:12:40 +00:00
|
|
|
try:
|
|
|
|
while 1:
|
|
|
|
try:
|
|
|
|
rawMsg = await asyncio.wait_for(reader.read(967), 0.1)
|
|
|
|
buffer = raw(rawMsg.decode("utf8"))
|
|
|
|
match buffer[0]:
|
|
|
|
case "S":
|
|
|
|
G.msgs.extend([log(buffer[2:])])
|
|
|
|
writer.write(b"I Mmm... Blueberries\n")
|
|
|
|
case "I":
|
|
|
|
pass
|
|
|
|
case "+":
|
|
|
|
cName = buffer[2:]
|
2024-07-14 05:17:40 +00:00
|
|
|
if cName.lower() not in G.clientsConnected:
|
2024-07-14 04:12:40 +00:00
|
|
|
G.msgs.append(log(f"{cName} has connected from {rID}"))
|
2024-07-14 05:17:40 +00:00
|
|
|
G.servers[rID].append(cName.lower())
|
|
|
|
G.clientsConnected.append(cName.lower())
|
2024-07-14 04:12:40 +00:00
|
|
|
writer.write(b"I Mmm... Pineapples\n")
|
|
|
|
else:
|
|
|
|
writer.write(f"K {cName}\n".encode("utf8"))
|
|
|
|
case "-":
|
|
|
|
cName = buffer[2:]
|
|
|
|
G.msgs.append(log(f"{cName} has disconnected from {sName}"))
|
2024-07-14 05:17:40 +00:00
|
|
|
G.servers[rID].remove(cName.lower())
|
|
|
|
G.clientsConnected.remove(cName.lower())
|
2024-07-14 04:12:40 +00:00
|
|
|
case "M":
|
|
|
|
cName = buffer[2:].split("|", 1)[0]
|
|
|
|
message = buffer[2:].split("|", 1)[1]
|
|
|
|
G.msgs.append(log(f" {cName}: {message}"))
|
|
|
|
writer.write(
|
|
|
|
b"I Get these damn heretic ghost clients out of my store so i can buy my cult candles in peace."
|
|
|
|
)
|
|
|
|
case "A":
|
|
|
|
cName = buffer[2:].split("|", 1)[0]
|
|
|
|
message = buffer[2:].split("|", 1)[1]
|
|
|
|
G.msgs.append(log(f"* {cName} {message}"))
|
|
|
|
writer.write(b"I Mmm... Strawberries\n")
|
|
|
|
case "Q":
|
|
|
|
break
|
|
|
|
case "K":
|
|
|
|
cName = buffer[2:]
|
2024-07-14 05:17:40 +00:00
|
|
|
G.killList[cName.lower()] = True
|
2024-07-14 04:12:40 +00:00
|
|
|
writer.write(b"I Mmm... Blood\n")
|
|
|
|
case _:
|
|
|
|
writer.write(
|
|
|
|
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()
|
|
|
|
except TimeoutError:
|
|
|
|
pass
|
|
|
|
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"))
|
2024-07-14 05:17:40 +00:00
|
|
|
G.clientsConnected.remove(cName.lower())
|
2024-07-14 04:12:40 +00:00
|
|
|
G.serverLinks -= 1
|
|
|
|
G.servers.remove(rID)
|
|
|
|
G.msgs.append(log(f"{rID} has de-linked from the network"))
|
|
|
|
except (ConnectionResetError, BrokenPipeError):
|
|
|
|
for cName in G.servers[rID]:
|
|
|
|
G.msgs.append(log(f"{cName}'s server is going down"))
|
|
|
|
try:
|
2024-07-14 05:17:40 +00:00
|
|
|
G.clientsConnected.remove(cName.lower())
|
2024-07-14 04:12:40 +00:00
|
|
|
except Exception:
|
|
|
|
pass
|
|
|
|
G.serverLinks -= 1
|
|
|
|
G.servers.remove(rID)
|
|
|
|
G.msgs.append(log(f"{rID} has de-linked from the network"))
|
2024-07-13 10:16:30 +00:00
|
|
|
|
|
|
|
|
2024-07-14 05:17:40 +00:00
|
|
|
async def runServer(address: str, port: int):
|
2024-06-15 03:14:45 +00:00
|
|
|
global G
|
2024-07-14 05:17:40 +00:00
|
|
|
server = await asyncio.start_server(handleClient, address, port)
|
2024-06-15 03:14:45 +00:00
|
|
|
log(f"Listening on port {port}...")
|
2024-07-13 08:28:37 +00:00
|
|
|
G.msgs.append(log("Server startup"))
|
2024-07-13 10:16:30 +00:00
|
|
|
links = []
|
|
|
|
for hostname, portNum in G.outboundLinks:
|
|
|
|
links.append(connectServer(hostname, portNum))
|
2024-06-15 03:14:45 +00:00
|
|
|
crash = False
|
|
|
|
try:
|
2024-07-13 10:16:30 +00:00
|
|
|
links.append(G.event.wait())
|
|
|
|
await asyncio.gather(*links)
|
2024-06-15 03:14:45 +00:00
|
|
|
except Exception:
|
|
|
|
crash = True
|
2024-07-13 10:16:30 +00:00
|
|
|
G.msgs.append(log("Server crash", level="FATAL")[1:])
|
2024-06-15 03:14:45 +00:00
|
|
|
log("Shutting down from Exception")
|
|
|
|
# TODO: Add format_exc here
|
|
|
|
finally:
|
|
|
|
if not crash:
|
2024-07-13 10:16:30 +00:00
|
|
|
G.msgs.append(log("Server shutdown"))
|
2024-06-15 03:14:45 +00:00
|
|
|
log("Kicking all clients as we go down")
|
|
|
|
server.close()
|
|
|
|
# server.abort_clients()
|
2024-07-13 08:28:37 +00:00
|
|
|
if saveLogs:
|
|
|
|
with open("cache.py", "w") as cache:
|
|
|
|
cache.write(f"msgs = {G.msgs}\n")
|
|
|
|
log("Saved logs, exiting now.")
|
|
|
|
else:
|
|
|
|
log("Not saving logs, exiting now.")
|
2024-06-15 03:14:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ServerInterruptException(KeyboardInterrupt): ...
|
|
|
|
|
|
|
|
|
|
|
|
def interruptCatch(s, f):
|
|
|
|
global G
|
|
|
|
print()
|
|
|
|
G.loop.call_soon_threadsafe(G.event.set)
|
|
|
|
G.interruptCount += 1
|
|
|
|
sys.tracebacklimit = 0
|
|
|
|
raise ServerInterruptException from None
|
|
|
|
|
|
|
|
|
|
|
|
signal.signal(signal.SIGINT, interruptCatch)
|
2024-07-14 05:17:40 +00:00
|
|
|
asyncio.run(runServer(address, port))
|