Implement connection information storage and parsing
This commit is contained in:
parent
21ed410717
commit
acad766dea
14
example.json
14
example.json
|
@ -1,9 +1,9 @@
|
||||||
{
|
{
|
||||||
"port":13867,
|
"port": 13867,
|
||||||
"bind":"127.0.0.1",
|
"bind": "127.0.0.1",
|
||||||
"usessl":true,
|
"usessl": true,
|
||||||
"key":"key.pem",
|
"key": "key.pem",
|
||||||
"cert":"cert.pem",
|
"cert": "cert.pem",
|
||||||
"usepassword":true,
|
"usepassword": true,
|
||||||
"password":"example"
|
"password": "example"
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,6 @@
|
||||||
|
{
|
||||||
|
"pass": "pass [password]",
|
||||||
|
"logout": "logout",
|
||||||
|
"connect": "connect [name] [address] [port] [ssl|plain] [nickname]",
|
||||||
|
"list": "list"
|
||||||
|
}
|
92
threshold
92
threshold
|
@ -2,7 +2,7 @@
|
||||||
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
|
from twisted.internet.protocol import Protocol, Factory
|
||||||
from json import load, dump
|
from json import load, dump, loads
|
||||||
from sys import exit
|
from sys import exit
|
||||||
|
|
||||||
listener = None
|
listener = None
|
||||||
|
@ -54,6 +54,7 @@ class Base(Protocol):
|
||||||
|
|
||||||
def connectionLost(self, reason):
|
def connectionLost(self, reason):
|
||||||
global connections
|
global connections
|
||||||
|
self.authed = False
|
||||||
log("Connection lost from %s:%s -- %s" % (self.addr.host, self.addr.port, reason.getErrorMessage()))
|
log("Connection lost from %s:%s -- %s" % (self.addr.host, self.addr.port, reason.getErrorMessage()))
|
||||||
if not listener == None:
|
if not listener == None:
|
||||||
if self.addr in connections.keys():
|
if self.addr in connections.keys():
|
||||||
|
@ -93,7 +94,36 @@ class Helper(object):
|
||||||
else:
|
else:
|
||||||
error("Mandatory values missing from config")
|
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):
|
def parseCommand(self, addr, authed, data):
|
||||||
|
global pool
|
||||||
data = data.strip()
|
data = data.strip()
|
||||||
spl = data.split()
|
spl = data.split()
|
||||||
obj = connections[addr]
|
obj = connections[addr]
|
||||||
|
@ -102,27 +132,68 @@ class Helper(object):
|
||||||
failure = lambda data: sendFailure(addr, data)
|
failure = lambda data: sendFailure(addr, data)
|
||||||
info = lambda data: sendInfo(addr, data)
|
info = lambda data: sendInfo(addr, data)
|
||||||
|
|
||||||
incUsage = lambda: sendFailure(addr, "Incorrect usage")
|
incUsage = lambda mode: self.incorrectUsage(addr, mode)
|
||||||
length = len(spl)
|
length = len(spl)
|
||||||
if len(spl) > 0:
|
if len(spl) > 0:
|
||||||
cmd = spl[0]
|
cmd = spl[0]
|
||||||
else:
|
else:
|
||||||
failure("No text was sent")
|
failure("No text was sent")
|
||||||
return
|
return
|
||||||
|
|
||||||
if authed == True:
|
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")
|
info("You are already authenticated")
|
||||||
return
|
return
|
||||||
|
|
||||||
elif cmd == "logout":
|
elif cmd == "logout":
|
||||||
obj.authed = False
|
obj.authed = False
|
||||||
success("Logged out")
|
success("Logged out")
|
||||||
return
|
return
|
||||||
else:
|
|
||||||
incUsage()
|
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
|
return
|
||||||
else:
|
else:
|
||||||
if cmd == "pass":
|
incUsage("connect")
|
||||||
if length == 2:
|
return
|
||||||
|
|
||||||
|
else:
|
||||||
|
incUsage(None)
|
||||||
|
return
|
||||||
|
else:
|
||||||
|
if cmd == "pass" and length == 2:
|
||||||
if spl[1] == config["password"]:
|
if spl[1] == config["password"]:
|
||||||
success("Authenticated successfully")
|
success("Authenticated successfully")
|
||||||
obj.authed = True
|
obj.authed = True
|
||||||
|
@ -132,15 +203,14 @@ class Helper(object):
|
||||||
obj.transport.loseConnection()
|
obj.transport.loseConnection()
|
||||||
return
|
return
|
||||||
else:
|
else:
|
||||||
incUsage()
|
incUsage(None)
|
||||||
return
|
|
||||||
else:
|
|
||||||
incUsage()
|
|
||||||
return
|
return
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
helper = Helper()
|
helper = Helper()
|
||||||
config = helper.getConfig()
|
config = helper.getConfig()
|
||||||
|
pool = helper.getPool()
|
||||||
|
help = helper.getHelp()
|
||||||
|
|
||||||
listener = BaseFactory()
|
listener = BaseFactory()
|
||||||
if config["usessl"] == True:
|
if config["usessl"] == True:
|
||||||
|
|
Loading…
Reference in New Issue