Compare commits

...

2 commits

View file

@ -1,5 +1,5 @@
#!/usr/bin/python3 #!/usr/bin/python3
import os, sys, asyncio, re, signal import os, sys, asyncio, re, signal, socket
from platform import uname from platform import uname
from traceback import format_exc from traceback import format_exc
from logs import log from logs import log
@ -36,8 +36,9 @@ G.killList = {}
G.outboundLinks = [] G.outboundLinks = []
G.S2SLogs = [] G.S2SLogs = []
G.cwlgd = False G.cwlgd = False
G.NUL = "\x00"
saveLogs = True saveLogs = True
address = "0.0.0.0" address = "::"
# 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
@ -239,7 +240,7 @@ Please note that this is not network level statistics.\r\n""".encode(
writer.close() writer.close()
await writer.wait_closed() await writer.wait_closed()
return # Server is already "linked", drop the connection return # Server is already "linked", drop the connection
if G.remoteID == sName: # Hey! you can't *also* be ***me***! if G.remoteID == sName or not sName: # Hey! you can't *also* be ***me***!
writer.close() writer.close()
await writer.wait_closed() await writer.wait_closed()
return # drop "us" return # drop "us"
@ -319,16 +320,16 @@ Please note that this is not network level statistics.\r\n""".encode(
) )
) )
case "M": case "M":
cName = buffer[2:].split("|", 1)[0] cName = buffer[2:].split(G.NUL, 1)[0]
message = buffer[2:].split("|", 1)[1] message = buffer[2:].split(G.NUL, 1)[1]
G.msgs.append(log(f" {cName}: {message}")) G.msgs.append(log(f" {cName}: {message}"))
G.S2SLogs.append(("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.\r\n" b"I Get these damn heretic ghost clients out of my store so i can buy my cult candles in peace.\r\n"
) )
case "A": case "A":
cName = buffer[2:].split("|", 1)[0] cName = buffer[2:].split(G.NUL, 1)[0]
message = buffer[2:].split("|", 1)[1] message = buffer[2:].split(G.NUL, 1)[1]
G.msgs.append(log(f"* {cName} {message}")) G.msgs.append(log(f"* {cName} {message}"))
G.S2SLogs.append(("A", (cName, message), sName)) G.S2SLogs.append(("A", (cName, message), sName))
writer.write(b"I Mmm... Strawberries\r\n") writer.write(b"I Mmm... Strawberries\r\n")
@ -371,10 +372,10 @@ Please note that this is not network level statistics.\r\n""".encode(
match type: match type:
case "A": case "A":
nick, msg = data nick, msg = data
writer.write(f"A {nick}|{msg}\r\n".encode("utf8")) writer.write(f"A {nick}{G.NUL}{msg}\r\n".encode("utf8"))
case "M": case "M":
nick, msg = data nick, msg = data
writer.write(f"M {nick}|{msg}\r\n".encode("utf8")) writer.write(f"M {nick}{G.NUL}{msg}\r\n".encode("utf8"))
case "+": case "+":
writer.write(f"+ {data}\r\n".encode("utf8")) writer.write(f"+ {data}\r\n".encode("utf8"))
case "-": case "-":
@ -444,7 +445,7 @@ async def connectServer(hostname: str, port: int):
writer.close() writer.close()
await writer.wait_closed() await writer.wait_closed()
return return
if G.remoteID == rID: if G.remoteID == rID or not rID:
writer.close() writer.close()
await writer.wait_closed() await writer.wait_closed()
return return
@ -505,16 +506,16 @@ async def connectServer(hostname: str, port: int):
) )
) )
case "M": case "M":
cName = buffer[2:].split("|", 1)[0] cName = buffer[2:].split(G.NUL, 1)[0]
message = buffer[2:].split("|", 1)[1] message = buffer[2:].split(G.NUL, 1)[1]
G.msgs.append(log(f" {cName}: {message}")) G.msgs.append(log(f" {cName}: {message}"))
G.S2SLogs.append(("M", (cName, message), rID)) 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.\r\n" b"I Get these damn heretic ghost clients out of my store so i can buy my cult candles in peace.\r\n"
) )
case "A": case "A":
cName = buffer[2:].split("|", 1)[0] cName = buffer[2:].split(G.NUL, 1)[0]
message = buffer[2:].split("|", 1)[1] message = buffer[2:].split(G.NUL, 1)[1]
G.S2SLogs.append(("A", (cName, message), rID)) 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\r\n") writer.write(b"I Mmm... Strawberries\r\n")
@ -556,10 +557,10 @@ async def connectServer(hostname: str, port: int):
match type: match type:
case "A": case "A":
nick, msg = data nick, msg = data
writer.write(f"A {nick}|{msg}\r\n".encode("utf8")) writer.write(f"A {nick}{G.NUL}{msg}\r\n".encode("utf8"))
case "M": case "M":
nick, msg = data nick, msg = data
writer.write(f"M {nick}|{msg}\r\n".encode("utf8")) writer.write(f"M {nick}{G.NUL}{msg}\r\n".encode("utf8"))
case "+": case "+":
writer.write(f"+ {data}\r\n".encode("utf8")) writer.write(f"+ {data}\r\n".encode("utf8"))
case "-": case "-":
@ -602,7 +603,13 @@ async def connectServer(hostname: str, port: int):
async def runServer(address: str, port: int): async def runServer(address: str, port: int):
global G global G
server = await asyncio.start_server(handleClient, address, port) if ":" in address:
INET = socket.AF_INET6
else:
INET = socket.AF_INET
sock = socket.socket(INET, socket.SOCK_STREAM)
sock.bind((address, port))
server = await asyncio.start_server(handleClient, sock=sock)
log(f"Listening on port {port}...") log(f"Listening on port {port}...")
G.msgs.append(log("Server startup")) G.msgs.append(log("Server startup"))
links = [] links = []