2017-11-19 14:46:42 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
from twisted.internet import reactor
|
|
|
|
from twisted.internet.ssl import DefaultOpenSSLContextFactory
|
|
|
|
from twisted.internet.protocol import Protocol, Factory
|
2017-11-21 20:16:14 +00:00
|
|
|
from json import load, dump, loads
|
2017-11-19 14:46:42 +00:00
|
|
|
from sys import exit
|
|
|
|
|
|
|
|
listener = None
|
|
|
|
connections = {}
|
|
|
|
|
|
|
|
def log(data):
|
|
|
|
print("[LOG]", data)
|
|
|
|
|
|
|
|
def debug(data):
|
|
|
|
print("[DEBUG]", data)
|
|
|
|
|
2017-11-20 19:15:58 +00:00
|
|
|
def warn(data):
|
|
|
|
print("[WARNING]", data)
|
|
|
|
|
|
|
|
def error(data):
|
|
|
|
print("[ERROR]", data)
|
|
|
|
exit(1)
|
|
|
|
|
2017-11-20 21:40:04 +00:00
|
|
|
def sendData(addr, data):
|
|
|
|
connections[addr].send(data)
|
|
|
|
|
|
|
|
def sendSuccess(addr, data):
|
|
|
|
sendData(addr, "[y] " + data)
|
|
|
|
|
|
|
|
def sendFailure(addr, data):
|
|
|
|
sendData(addr, "[n] " + data)
|
|
|
|
|
|
|
|
def sendInfo(addr, data):
|
|
|
|
sendData(addr, "[i] " + data)
|
|
|
|
|
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")
|
|
|
|
log("Data received from %s:%s -- %s" % (self.addr.host, self.addr.port, repr(data)))
|
|
|
|
helper.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):
|
|
|
|
global connections
|
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-11-19 14:46:42 +00:00
|
|
|
|
|
|
|
class BaseFactory(Factory):
|
|
|
|
def buildProtocol(self, addr):
|
|
|
|
global connections
|
|
|
|
entry = Base(addr)
|
|
|
|
connections[addr] = entry
|
|
|
|
return entry
|
|
|
|
|
|
|
|
def send(self, addr, data):
|
|
|
|
global connections
|
|
|
|
if addr in connections.keys():
|
|
|
|
connection = connections[addr]
|
|
|
|
connection.send(data)
|
|
|
|
else:
|
|
|
|
return
|
|
|
|
|
2017-11-20 19:53:25 +00:00
|
|
|
class Helper(object):
|
|
|
|
def getConfig(self):
|
|
|
|
with open("config.json", "r") as f:
|
|
|
|
config = load(f)
|
2017-11-23 18:32:30 +00:00
|
|
|
if set(["Port", "BindAddress", "UseSSL", "UsePassword"]).issubset(set(config.keys())):
|
|
|
|
if config["UseSSL"] == True:
|
|
|
|
if not set(["ListenerKey", "ListenerCertificate"]).issubset(set(config.keys())):
|
2017-11-20 19:53:25 +00:00
|
|
|
error("SSL is on but certificate or key is not defined")
|
2017-11-23 18:32:30 +00:00
|
|
|
if config["UsePassword"] == True:
|
|
|
|
if not "Password" in config.keys():
|
2017-11-20 21:40:04 +00:00
|
|
|
error("Password authentication is on but password is not defined")
|
2017-11-20 19:53:25 +00:00
|
|
|
return config
|
|
|
|
else:
|
|
|
|
error("Mandatory values missing from config")
|
2017-11-19 14:46:42 +00:00
|
|
|
|
2017-11-21 20:16:14 +00:00
|
|
|
def getPool(self):
|
|
|
|
with open("pool.json", "r") as f:
|
|
|
|
data = f.read()
|
|
|
|
if not data == "":
|
|
|
|
pool = loads(data.strip())
|
|
|
|
return pool
|
|
|
|
else:
|
|
|
|
return {}
|
|
|
|
|
|
|
|
def savePool(self):
|
|
|
|
global pool
|
|
|
|
with open("pool.json", "w") as f:
|
|
|
|
dump(pool, f, indent=4)
|
|
|
|
return
|
|
|
|
|
|
|
|
def getHelp(self):
|
|
|
|
with open("help.json", "r") as f:
|
|
|
|
help = load(f)
|
|
|
|
return help
|
|
|
|
|
|
|
|
def incorrectUsage(self, addr, mode):
|
|
|
|
if mode == None:
|
|
|
|
sendFailure(addr, "Incorrect usage")
|
|
|
|
return
|
|
|
|
if mode in help.keys():
|
|
|
|
sendFailure(addr, "Usage: " + help[mode])
|
|
|
|
return
|
|
|
|
|
2017-11-20 21:40:04 +00:00
|
|
|
def parseCommand(self, addr, authed, data):
|
2017-11-21 20:16:14 +00:00
|
|
|
global pool
|
2017-11-20 21:40:04 +00:00
|
|
|
data = data.strip()
|
|
|
|
spl = data.split()
|
|
|
|
obj = connections[addr]
|
|
|
|
|
|
|
|
success = lambda data: sendSuccess(addr, data)
|
|
|
|
failure = lambda data: sendFailure(addr, data)
|
|
|
|
info = lambda data: sendInfo(addr, data)
|
|
|
|
|
2017-11-21 20:16:14 +00:00
|
|
|
incUsage = lambda mode: self.incorrectUsage(addr, mode)
|
2017-11-20 21:40:04 +00:00
|
|
|
length = len(spl)
|
|
|
|
if len(spl) > 0:
|
|
|
|
cmd = spl[0]
|
|
|
|
else:
|
2017-11-21 18:41:18 +00:00
|
|
|
failure("No text was sent")
|
2017-11-20 21:40:04 +00:00
|
|
|
return
|
2017-11-21 20:16:14 +00:00
|
|
|
|
2017-11-20 21:40:04 +00:00
|
|
|
if authed == True:
|
2017-11-21 20:16:14 +00:00
|
|
|
if cmd == "help":
|
|
|
|
helpMap = []
|
|
|
|
for i in help.keys():
|
|
|
|
helpMap.append("%s: %s" % (i, help[i]))
|
|
|
|
info("\n".join(helpMap))
|
|
|
|
return
|
|
|
|
|
|
|
|
elif cmd == "pass":
|
2017-11-20 21:40:04 +00:00
|
|
|
info("You are already authenticated")
|
|
|
|
return
|
2017-11-21 20:16:14 +00:00
|
|
|
|
2017-11-20 21:40:04 +00:00
|
|
|
elif cmd == "logout":
|
|
|
|
obj.authed = False
|
|
|
|
success("Logged out")
|
|
|
|
return
|
2017-11-21 20:16:14 +00:00
|
|
|
|
|
|
|
elif cmd == "list":
|
|
|
|
poolMap = []
|
|
|
|
for i in pool.keys():
|
|
|
|
poolMap.append("%s: %s" % (i, pool[i]))
|
|
|
|
info("\n".join(poolMap))
|
2017-11-20 21:40:04 +00:00
|
|
|
return
|
2017-11-21 20:16:14 +00:00
|
|
|
|
|
|
|
elif cmd == "connect":
|
|
|
|
if length == 6:
|
|
|
|
|
|
|
|
if spl[1] in pool.keys():
|
|
|
|
failure("Name already exists: %s" % spl[1])
|
2017-11-20 21:40:04 +00:00
|
|
|
return
|
2017-11-21 20:16:14 +00:00
|
|
|
|
|
|
|
protocol = spl[4].lower()
|
|
|
|
|
|
|
|
if not protocol in ["ssl", "plain"]:
|
|
|
|
incUsage("connect")
|
2017-11-20 21:40:04 +00:00
|
|
|
return
|
2017-11-21 20:16:14 +00:00
|
|
|
|
|
|
|
pool[spl[1]] = { "address": spl[2],
|
|
|
|
"port": spl[3],
|
|
|
|
"protocol": protocol,
|
|
|
|
"nickname": spl[5],
|
|
|
|
}
|
|
|
|
success("Successfully created bot")
|
|
|
|
self.savePool()
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
incUsage("connect")
|
|
|
|
return
|
|
|
|
|
|
|
|
else:
|
|
|
|
incUsage(None)
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
if cmd == "pass" and length == 2:
|
2017-11-23 18:32:30 +00:00
|
|
|
if spl[1] == config["Password"]:
|
2017-11-21 20:16:14 +00:00
|
|
|
success("Authenticated successfully")
|
|
|
|
obj.authed = True
|
|
|
|
return
|
2017-11-20 21:40:04 +00:00
|
|
|
else:
|
2017-11-21 20:16:14 +00:00
|
|
|
failure("Password incorrect")
|
|
|
|
obj.transport.loseConnection()
|
2017-11-20 21:40:04 +00:00
|
|
|
return
|
|
|
|
else:
|
2017-11-21 20:16:14 +00:00
|
|
|
incUsage(None)
|
2017-11-20 21:40:04 +00:00
|
|
|
return
|
|
|
|
|
2017-11-19 14:46:42 +00:00
|
|
|
if __name__ == "__main__":
|
2017-11-20 19:53:25 +00:00
|
|
|
helper = Helper()
|
|
|
|
config = helper.getConfig()
|
2017-11-21 20:16:14 +00:00
|
|
|
pool = helper.getPool()
|
|
|
|
help = helper.getHelp()
|
2017-11-19 14:46:42 +00:00
|
|
|
|
|
|
|
listener = BaseFactory()
|
2017-11-23 18:32:30 +00:00
|
|
|
if config["UseSSL"] == True:
|
|
|
|
reactor.listenSSL(config["Port"], listener, DefaultOpenSSLContextFactory(config["ListenerKey"], config["ListenerCertificate"]), interface=config["BindAddress"])
|
|
|
|
log("Threshold running with SSL on %s:%s" % (config["BindAddress"], config["Port"]))
|
2017-11-19 14:46:42 +00:00
|
|
|
else:
|
2017-11-23 18:32:30 +00:00
|
|
|
reactor.listenTCP(config["Port"], listener, interface=config["BindAddress"])
|
|
|
|
log("Threshold running on %s:%s" % (config["BindAddress"], config["Port"]))
|
2017-11-19 14:46:42 +00:00
|
|
|
|
|
|
|
reactor.run()
|