Move server functions to a separate file
This commit is contained in:
parent
badfe46694
commit
eae4315562
|
@ -1,6 +1,4 @@
|
||||||
{
|
{
|
||||||
"Keywords": [
|
"Keywords": [],
|
||||||
"example"
|
|
||||||
],
|
|
||||||
"KeywordsExcept": {}
|
"KeywordsExcept": {}
|
||||||
}
|
}
|
|
@ -0,0 +1,58 @@
|
||||||
|
from twisted.internet.protocol import Protocol, Factory, ClientFactory
|
||||||
|
from core.main import *
|
||||||
|
from utils.logging.log import *
|
||||||
|
|
||||||
|
from core.parser import parseCommand
|
||||||
|
|
||||||
|
class Server(Protocol):
|
||||||
|
def __init__(self, addr):
|
||||||
|
self.addr = addr
|
||||||
|
self.authed = False
|
||||||
|
if config["UsePassword"] == False:
|
||||||
|
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")
|
||||||
|
#log("Data received from %s:%s -- %s" % (self.addr.host, self.addr.port, repr(data)))
|
||||||
|
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))
|
||||||
|
self.send("Hello.")
|
||||||
|
|
||||||
|
def connectionLost(self, reason):
|
||||||
|
self.authed = False
|
||||||
|
log("Connection lost from %s:%s -- %s" % (self.addr.host, self.addr.port, reason.getErrorMessage()))
|
||||||
|
if not listener == None:
|
||||||
|
if self.addr in connections.keys():
|
||||||
|
del connections[self.addr]
|
||||||
|
else:
|
||||||
|
warn("Tried to remove a non-existant connection.")
|
||||||
|
else:
|
||||||
|
warn("Tried to remove a connection from a listener that wasn't running.")
|
||||||
|
if self.addr in MonitorPool:
|
||||||
|
MonitorPool.remove(self.addr)
|
||||||
|
|
||||||
|
class ServerFactory(Factory):
|
||||||
|
def buildProtocol(self, addr):
|
||||||
|
entry = Server(addr)
|
||||||
|
connections[addr] = entry
|
||||||
|
return entry
|
||||||
|
|
||||||
|
def send(self, addr, data):
|
||||||
|
if addr in connections.keys():
|
||||||
|
connection = connections[addr]
|
||||||
|
connection.send(data)
|
||||||
|
else:
|
||||||
|
return
|
57
threshold
57
threshold
|
@ -1,7 +1,6 @@
|
||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
from twisted.internet import reactor
|
from twisted.internet import reactor
|
||||||
from twisted.internet.ssl import DefaultOpenSSLContextFactory
|
from twisted.internet.ssl import DefaultOpenSSLContextFactory
|
||||||
from twisted.internet.protocol import Protocol, Factory, ClientFactory
|
|
||||||
|
|
||||||
#from twisted.python import log
|
#from twisted.python import log
|
||||||
#from sys import stdout
|
#from sys import stdout
|
||||||
|
@ -36,67 +35,15 @@ from core.main import (
|
||||||
from utils.logging.log import *
|
from utils.logging.log import *
|
||||||
import modules.userinfo as userinfo
|
import modules.userinfo as userinfo
|
||||||
import core.helper as helper
|
import core.helper as helper
|
||||||
from core.parser import parseCommand
|
from core.server import Server, ServerFactory
|
||||||
|
|
||||||
class Base(Protocol):
|
|
||||||
def __init__(self, addr):
|
|
||||||
self.addr = addr
|
|
||||||
self.authed = False
|
|
||||||
if config["UsePassword"] == False:
|
|
||||||
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")
|
|
||||||
#log("Data received from %s:%s -- %s" % (self.addr.host, self.addr.port, repr(data)))
|
|
||||||
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))
|
|
||||||
self.send("Hello.")
|
|
||||||
|
|
||||||
def connectionLost(self, reason):
|
|
||||||
self.authed = False
|
|
||||||
log("Connection lost from %s:%s -- %s" % (self.addr.host, self.addr.port, reason.getErrorMessage()))
|
|
||||||
if not listener == None:
|
|
||||||
if self.addr in connections.keys():
|
|
||||||
del connections[self.addr]
|
|
||||||
else:
|
|
||||||
warn("Tried to remove a non-existant connection.")
|
|
||||||
else:
|
|
||||||
warn("Tried to remove a connection from a listener that wasn't running.")
|
|
||||||
if self.addr in MonitorPool:
|
|
||||||
MonitorPool.remove(self.addr)
|
|
||||||
|
|
||||||
class BaseFactory(Factory):
|
|
||||||
def buildProtocol(self, addr):
|
|
||||||
entry = Base(addr)
|
|
||||||
connections[addr] = entry
|
|
||||||
return entry
|
|
||||||
|
|
||||||
def send(self, addr, data):
|
|
||||||
if addr in connections.keys():
|
|
||||||
connection = connections[addr]
|
|
||||||
connection.send(data)
|
|
||||||
else:
|
|
||||||
return
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
for i in pool.keys():
|
for i in pool.keys():
|
||||||
if pool[i]["enabled"] == True:
|
if pool[i]["enabled"] == True:
|
||||||
helper.addBot(i)
|
helper.addBot(i)
|
||||||
|
|
||||||
listener = BaseFactory()
|
listener = ServerFactory()
|
||||||
if config["Listener"]["UseSSL"] == True:
|
if config["Listener"]["UseSSL"] == True:
|
||||||
reactor.listenSSL(config["Listener"]["Port"], listener, DefaultOpenSSLContextFactory(certPath+config["Listener"]["Key"], certPath+config["Listener"]["Certificate"]), interface=config["Listener"]["Address"])
|
reactor.listenSSL(config["Listener"]["Port"], listener, DefaultOpenSSLContextFactory(certPath+config["Listener"]["Key"], certPath+config["Listener"]["Certificate"]), interface=config["Listener"]["Address"])
|
||||||
log("Threshold running with SSL on %s:%s" % (config["Listener"]["Address"], config["Listener"]["Port"]))
|
log("Threshold running with SSL on %s:%s" % (config["Listener"]["Address"], config["Listener"]["Port"]))
|
||||||
|
|
Loading…
Reference in New Issue