Implement connection information storage and parsing

This commit is contained in:
Mark Veidemanis 2017-11-21 20:16:14 +00:00
parent 21ed410717
commit acad766dea
3 changed files with 99 additions and 23 deletions

View File

@ -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"
}

6
help.json Normal file
View File

@ -0,0 +1,6 @@
{
"pass": "pass [password]",
"logout": "logout",
"connect": "connect [name] [address] [port] [ssl|plain] [nickname]",
"list": "list"
}

102
threshold
View File

@ -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: