2022-07-21 12:40:05 +00:00
|
|
|
from twisted.internet.protocol import Protocol, Factory
|
2022-07-21 12:39:59 +00:00
|
|
|
import main
|
2022-07-21 12:40:05 +00:00
|
|
|
from utils.logging.log import log, warn
|
2018-03-04 13:26:53 +00:00
|
|
|
|
2022-07-21 12:40:01 +00:00
|
|
|
from core.parser import parseCommand
|
|
|
|
|
2022-07-21 12:39:41 +00:00
|
|
|
|
2018-03-04 13:26:53 +00:00
|
|
|
class Server(Protocol):
|
|
|
|
def __init__(self, addr):
|
|
|
|
self.addr = addr
|
|
|
|
self.authed = False
|
2022-07-21 12:40:05 +00:00
|
|
|
if main.config["UsePassword"] is False:
|
2018-03-04 13:26:53 +00:00
|
|
|
self.authed = True
|
|
|
|
|
|
|
|
def send(self, data):
|
|
|
|
data += "\r\n"
|
|
|
|
data = data.encode("utf-8", "replace")
|
|
|
|
self.transport.write(data)
|
|
|
|
|
|
|
|
def dataReceived(self, data):
|
|
|
|
data = data.decode("utf-8", "replace")
|
2022-07-21 12:39:41 +00:00
|
|
|
# log("Data received from %s:%s -- %s" % (self.addr.host, self.addr.port, repr(data)))
|
2018-03-04 13:26:53 +00:00
|
|
|
if "\n" in data:
|
|
|
|
splitData = [x for x in data.split("\n") if x]
|
|
|
|
if "\n" in data:
|
|
|
|
for i in splitData:
|
|
|
|
parseCommand(self.addr, self.authed, i)
|
|
|
|
return
|
|
|
|
parseCommand(self.addr, self.authed, data)
|
|
|
|
|
|
|
|
def connectionMade(self):
|
|
|
|
log("Connection from %s:%s" % (self.addr.host, self.addr.port))
|
2019-02-01 23:26:01 +00:00
|
|
|
self.send("Greetings.")
|
2018-03-04 13:26:53 +00:00
|
|
|
|
|
|
|
def connectionLost(self, reason):
|
|
|
|
self.authed = False
|
2022-07-21 12:40:01 +00:00
|
|
|
log("Connection lost from %s:%s -- %s" % (self.addr.host, self.addr.port, reason.getErrorMessage()))
|
2018-03-14 20:13:40 +00:00
|
|
|
if self.addr in main.connections.keys():
|
|
|
|
del main.connections[self.addr]
|
2018-03-04 13:26:53 +00:00
|
|
|
else:
|
2018-03-10 14:01:43 +00:00
|
|
|
warn("Tried to remove a non-existant connection.")
|
2018-03-04 13:26:53 +00:00
|
|
|
|
2022-07-21 12:39:41 +00:00
|
|
|
|
2018-03-04 13:26:53 +00:00
|
|
|
class ServerFactory(Factory):
|
|
|
|
def buildProtocol(self, addr):
|
|
|
|
entry = Server(addr)
|
2018-03-14 20:13:40 +00:00
|
|
|
main.connections[addr] = entry
|
2018-03-04 13:26:53 +00:00
|
|
|
return entry
|
|
|
|
|
|
|
|
def send(self, addr, data):
|
2018-03-14 20:13:40 +00:00
|
|
|
if addr in main.connections.keys():
|
|
|
|
connection = main.connections[addr]
|
2018-03-04 13:26:53 +00:00
|
|
|
connection.send(data)
|
|
|
|
else:
|
|
|
|
return
|