From acad766dea4f4bdfe2ed1765f56b2aedbd3798e1 Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Tue, 21 Nov 2017 20:16:14 +0000 Subject: [PATCH] Implement connection information storage and parsing --- example.json | 14 +++---- help.json | 6 +++ threshold | 102 +++++++++++++++++++++++++++++++++++++++++++-------- 3 files changed, 99 insertions(+), 23 deletions(-) create mode 100644 help.json diff --git a/example.json b/example.json index c363f4e..07d4b3e 100644 --- a/example.json +++ b/example.json @@ -1,9 +1,9 @@ { - "port":13867, - "bind":"127.0.0.1", - "usessl":true, - "key":"key.pem", - "cert":"cert.pem", - "usepassword":true, - "password":"example" + "port": 13867, + "bind": "127.0.0.1", + "usessl": true, + "key": "key.pem", + "cert": "cert.pem", + "usepassword": true, + "password": "example" } diff --git a/help.json b/help.json new file mode 100644 index 0000000..984b16b --- /dev/null +++ b/help.json @@ -0,0 +1,6 @@ +{ + "pass": "pass [password]", + "logout": "logout", + "connect": "connect [name] [address] [port] [ssl|plain] [nickname]", + "list": "list" +} diff --git a/threshold b/threshold index 1a0142d..076c053 100755 --- a/threshold +++ b/threshold @@ -2,7 +2,7 @@ from twisted.internet import reactor from twisted.internet.ssl import DefaultOpenSSLContextFactory from twisted.internet.protocol import Protocol, Factory -from json import load, dump +from json import load, dump, loads from sys import exit listener = None @@ -54,6 +54,7 @@ class Base(Protocol): def connectionLost(self, reason): global connections + 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(): @@ -93,7 +94,36 @@ class Helper(object): else: error("Mandatory values missing from config") + 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 + def parseCommand(self, addr, authed, data): + global pool data = data.strip() spl = data.split() obj = connections[addr] @@ -102,45 +132,85 @@ class Helper(object): failure = lambda data: sendFailure(addr, data) info = lambda data: sendInfo(addr, data) - incUsage = lambda: sendFailure(addr, "Incorrect usage") + incUsage = lambda mode: self.incorrectUsage(addr, mode) length = len(spl) if len(spl) > 0: cmd = spl[0] else: failure("No text was sent") return + if authed == True: - if cmd == "pass": + if cmd == "help": + helpMap = [] + for i in help.keys(): + helpMap.append("%s: %s" % (i, help[i])) + info("\n".join(helpMap)) + return + + elif cmd == "pass": info("You are already authenticated") return + elif cmd == "logout": obj.authed = False success("Logged out") return + + elif cmd == "list": + poolMap = [] + for i in pool.keys(): + poolMap.append("%s: %s" % (i, pool[i])) + info("\n".join(poolMap)) + return + + elif cmd == "connect": + if length == 6: + + if spl[1] in pool.keys(): + failure("Name already exists: %s" % spl[1]) + return + + protocol = spl[4].lower() + + if not protocol in ["ssl", "plain"]: + incUsage("connect") + return + + 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() + incUsage(None) return else: - if cmd == "pass": - if length == 2: - if spl[1] == config["password"]: - success("Authenticated successfully") - obj.authed = True - return - else: - failure("Password incorrect") - obj.transport.loseConnection() - return + if cmd == "pass" and length == 2: + if spl[1] == config["password"]: + success("Authenticated successfully") + obj.authed = True + return else: - incUsage() + failure("Password incorrect") + obj.transport.loseConnection() return else: - incUsage() + incUsage(None) return if __name__ == "__main__": helper = Helper() config = helper.getConfig() + pool = helper.getPool() + help = helper.getHelp() listener = BaseFactory() if config["usessl"] == True: