2017-11-19 14:46:42 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
from twisted.internet import reactor
|
|
|
|
from twisted.internet.ssl import DefaultOpenSSLContextFactory
|
2018-02-23 22:05:40 +00:00
|
|
|
from twisted.internet.protocol import Protocol, Factory, ClientFactory
|
2017-11-23 20:37:00 +00:00
|
|
|
|
2017-12-24 21:04:48 +00:00
|
|
|
#from twisted.python import log
|
|
|
|
#from sys import stdout
|
|
|
|
#log.startLogging(stdout)
|
|
|
|
|
2018-02-23 22:05:40 +00:00
|
|
|
from core.main import (
|
|
|
|
numbers,
|
|
|
|
configPath,
|
|
|
|
certPath,
|
|
|
|
listener,
|
|
|
|
connections,
|
|
|
|
IRCPool,
|
|
|
|
ReactorPool,
|
|
|
|
FactoryPool,
|
|
|
|
MonitorPool,
|
|
|
|
saveConf,
|
|
|
|
loadConf,
|
|
|
|
initMain,
|
2018-02-24 12:42:27 +00:00
|
|
|
)
|
|
|
|
|
2018-02-23 22:05:40 +00:00
|
|
|
initMain()
|
|
|
|
|
|
|
|
from core.main import (
|
|
|
|
config,
|
|
|
|
keyconf,
|
|
|
|
pool,
|
|
|
|
help,
|
|
|
|
wholist,
|
2018-02-24 12:42:27 +00:00
|
|
|
counters,
|
|
|
|
)
|
2017-11-19 14:46:42 +00:00
|
|
|
|
2018-02-22 19:30:31 +00:00
|
|
|
from utils.logging.log import *
|
2018-02-23 22:05:40 +00:00
|
|
|
import modules.userinfo as userinfo
|
|
|
|
import core.helper as helper
|
|
|
|
from core.parser import parseCommand
|
2017-11-24 19:16:08 +00:00
|
|
|
|
2017-11-19 14:46:42 +00:00
|
|
|
class Base(Protocol):
|
|
|
|
def __init__(self, addr):
|
|
|
|
self.addr = addr
|
|
|
|
self.authed = False
|
2017-11-23 18:32:30 +00:00
|
|
|
if config["UsePassword"] == False:
|
|
|
|
self.authed = True
|
2017-11-19 14:46:42 +00:00
|
|
|
|
|
|
|
def send(self, data):
|
|
|
|
data += "\r\n"
|
|
|
|
data = data.encode("utf-8", "replace")
|
|
|
|
self.transport.write(data)
|
|
|
|
|
|
|
|
def dataReceived(self, data):
|
2017-11-20 21:40:04 +00:00
|
|
|
data = data.decode("utf-8", "replace")
|
2017-11-27 19:54:35 +00:00
|
|
|
#log("Data received from %s:%s -- %s" % (self.addr.host, self.addr.port, repr(data)))
|
2017-12-03 13:10:51 +00:00
|
|
|
if "\n" in data:
|
|
|
|
splitData = [x for x in data.split("\n") if x]
|
|
|
|
if "\n" in data:
|
|
|
|
for i in splitData:
|
2018-02-23 22:05:40 +00:00
|
|
|
parseCommand(self.addr, self.authed, i)
|
2017-12-03 13:10:51 +00:00
|
|
|
return
|
2018-02-23 22:05:40 +00:00
|
|
|
parseCommand(self.addr, self.authed, data)
|
2017-11-19 14:46:42 +00:00
|
|
|
|
|
|
|
def connectionMade(self):
|
|
|
|
log("Connection from %s:%s" % (self.addr.host, self.addr.port))
|
|
|
|
self.send("Hello.")
|
|
|
|
|
|
|
|
def connectionLost(self, reason):
|
2017-11-21 20:16:14 +00:00
|
|
|
self.authed = False
|
2017-11-20 21:40:04 +00:00
|
|
|
log("Connection lost from %s:%s -- %s" % (self.addr.host, self.addr.port, reason.getErrorMessage()))
|
2017-11-19 14:46:42 +00:00
|
|
|
if not listener == None:
|
|
|
|
if self.addr in connections.keys():
|
|
|
|
del connections[self.addr]
|
|
|
|
else:
|
2017-11-20 19:15:58 +00:00
|
|
|
warn("Tried to remove a non-existant connection.")
|
2017-11-19 14:46:42 +00:00
|
|
|
else:
|
2017-11-20 19:15:58 +00:00
|
|
|
warn("Tried to remove a connection from a listener that wasn't running.")
|
2017-12-24 21:04:48 +00:00
|
|
|
if self.addr in MonitorPool:
|
|
|
|
MonitorPool.remove(self.addr)
|
2017-11-19 14:46:42 +00:00
|
|
|
|
|
|
|
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__":
|
2017-11-23 20:37:00 +00:00
|
|
|
for i in pool.keys():
|
|
|
|
if pool[i]["enabled"] == True:
|
|
|
|
helper.addBot(i)
|
2017-11-19 14:46:42 +00:00
|
|
|
|
|
|
|
listener = BaseFactory()
|
2018-01-17 19:02:41 +00:00
|
|
|
if config["Listener"]["UseSSL"] == True:
|
|
|
|
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"]))
|
2017-11-19 14:46:42 +00:00
|
|
|
else:
|
2018-01-17 19:02:41 +00:00
|
|
|
reactor.listenTCP(config["Listener"]["Port"], listener, interface=config["Listener"]["Address"])
|
|
|
|
log("Threshold running on %s:%s" % (config["Listener"]["Address"], config["Listener"]["Port"]))
|
2017-11-19 14:46:42 +00:00
|
|
|
|
|
|
|
reactor.run()
|