Implement the backend for automatically provisioning relays
parent
6046329a83
commit
4efea3f535
@ -1,80 +0,0 @@
|
|||||||
import main
|
|
||||||
|
|
||||||
class Default:
|
|
||||||
def __init__(self, register):
|
|
||||||
register("default", self.default)
|
|
||||||
|
|
||||||
def default(self, addr, authed, data, obj, spl, success, failure, info, incUsage, length):
|
|
||||||
if authed:
|
|
||||||
toUnset = False
|
|
||||||
if length == 1:
|
|
||||||
optionMap = ["Viewing defaults"]
|
|
||||||
for i in main.config["Default"].keys():
|
|
||||||
optionMap.append("%s: %s" % (i, main.config["Default"][i]))
|
|
||||||
info("\n".join(optionMap))
|
|
||||||
return
|
|
||||||
elif length == 2:
|
|
||||||
if not spl[1] in main.config["Default"].keys():
|
|
||||||
failure("No such key: %s" % spl[1])
|
|
||||||
return
|
|
||||||
info("%s: %s" % (spl[1], main.config["Default"][spl[1]]))
|
|
||||||
return
|
|
||||||
elif length == 3:
|
|
||||||
if not spl[1] in main.config["Default"].keys():
|
|
||||||
failure("No such key: %s" % spl[1])
|
|
||||||
return
|
|
||||||
|
|
||||||
if spl[2].lower() in ["none", "nil"]:
|
|
||||||
spl[2] = None
|
|
||||||
toUnset = True
|
|
||||||
|
|
||||||
if spl[1] in ["port", "timeout", "maxdelay"]:
|
|
||||||
try:
|
|
||||||
spl[2] = int(spl[2])
|
|
||||||
except:
|
|
||||||
failure("Value must be an integer, not %s" % spl[2])
|
|
||||||
return
|
|
||||||
|
|
||||||
if spl[2] in ["initialdelay", "factor", "jitter"]:
|
|
||||||
try:
|
|
||||||
spl[3] = float(spl[3])
|
|
||||||
except:
|
|
||||||
failure("Value must be a floating point integer, not %s" % spl[3])
|
|
||||||
return
|
|
||||||
|
|
||||||
if spl[1] == "protocol":
|
|
||||||
if not toUnset:
|
|
||||||
if not spl[2] in ["ssl", "plain"]:
|
|
||||||
failure("Protocol must be ssl or plain, not %s" % spl[2])
|
|
||||||
return
|
|
||||||
|
|
||||||
if spl[2] == main.config["Default"][spl[1]]:
|
|
||||||
failure("Value already exists: %s" % spl[2])
|
|
||||||
return
|
|
||||||
|
|
||||||
if spl[1] == "authtype":
|
|
||||||
if not toUnset:
|
|
||||||
if not spl[2] in ["sp", "ns"]:
|
|
||||||
failure("Authtype must be sp or ns, not %s" % spl[2])
|
|
||||||
return
|
|
||||||
if spl[1] == "enabled":
|
|
||||||
failure("Use the ConnectOnCreate main.config parameter to set this")
|
|
||||||
return
|
|
||||||
if spl[1] == "autojoin":
|
|
||||||
if not toUnset:
|
|
||||||
spl[2] = spl[2].split(",")
|
|
||||||
else:
|
|
||||||
spl[2] = []
|
|
||||||
|
|
||||||
main.config["Default"][spl[1]] = spl[2]
|
|
||||||
main.saveConf("config")
|
|
||||||
if toUnset:
|
|
||||||
success("Successfully unset key %s" % spl[1])
|
|
||||||
else:
|
|
||||||
success("Successfully set key %s to %s" % (spl[1], spl[2]))
|
|
||||||
return
|
|
||||||
else:
|
|
||||||
incUsage("default")
|
|
||||||
return
|
|
||||||
else:
|
|
||||||
incUsage(None)
|
|
@ -0,0 +1,60 @@
|
|||||||
|
import main
|
||||||
|
from yaml import dump
|
||||||
|
|
||||||
|
class Network:
|
||||||
|
def __init__(self, register):
|
||||||
|
register("network", self.network)
|
||||||
|
|
||||||
|
def network(self, addr, authed, data, obj, spl, success, failure, info, incUsage, length):
|
||||||
|
if authed:
|
||||||
|
if length == 7:
|
||||||
|
if spl[1] == "add":
|
||||||
|
if spl[2] in main.network.keys():
|
||||||
|
failure("Network already exists: %s" % spl[2])
|
||||||
|
return
|
||||||
|
if not spl[4].isdigit():
|
||||||
|
failure("Port must be an integer, not %s" % spl[4])
|
||||||
|
return
|
||||||
|
if not spl[5].lower() in ["ssl", "plain"]:
|
||||||
|
failure("Security must be ssl or plain, not %s" % spl[5])
|
||||||
|
return
|
||||||
|
if not spl[6].lower() in ["sasl", "ns", "none"]:
|
||||||
|
failure("Auth must be sasl, ns or none, not %s" % spl[5])
|
||||||
|
return
|
||||||
|
else:
|
||||||
|
main.network[spl[2]] = {"host": spl[3],
|
||||||
|
"port": spl[4],
|
||||||
|
"security": spl[5].lower(),
|
||||||
|
"auth": spl[6].lower()}
|
||||||
|
success("Successfully created network: %s" % spl[2])
|
||||||
|
main.saveConf("network")
|
||||||
|
return
|
||||||
|
else:
|
||||||
|
incUsage("network")
|
||||||
|
return
|
||||||
|
|
||||||
|
elif length == 3:
|
||||||
|
if spl[1] == "del":
|
||||||
|
if spl[2] in main.network.keys():
|
||||||
|
del main.network[spl[2]]
|
||||||
|
success("Successfully removed network: %s" % spl[2])
|
||||||
|
main.saveConf("network")
|
||||||
|
return
|
||||||
|
else:
|
||||||
|
failure("No such network: %s" % spl[2])
|
||||||
|
return
|
||||||
|
else:
|
||||||
|
incUsage("network")
|
||||||
|
return
|
||||||
|
elif length == 2:
|
||||||
|
if spl[1] == "list":
|
||||||
|
info(dump(main.network))
|
||||||
|
return
|
||||||
|
else:
|
||||||
|
incUsage("network")
|
||||||
|
return
|
||||||
|
else:
|
||||||
|
incUsage("network")
|
||||||
|
return
|
||||||
|
else:
|
||||||
|
incUsage(None)
|
@ -0,0 +1,59 @@
|
|||||||
|
import main
|
||||||
|
from modules import provision
|
||||||
|
|
||||||
|
class Provision:
|
||||||
|
def __init__(self, register):
|
||||||
|
register("provision", self.provision)
|
||||||
|
|
||||||
|
def provision(self, addr, authed, data, obj, spl, success, failure, info, incUsage, length):
|
||||||
|
if authed:
|
||||||
|
if length == 4 or length == 3:
|
||||||
|
if not spl[1] in main.relay.keys():
|
||||||
|
failure("No such relay: %s" % spl[1])
|
||||||
|
return
|
||||||
|
if not spl[2] in main.alias.keys():
|
||||||
|
failure("No such alias: %s" % spl[2])
|
||||||
|
return
|
||||||
|
if length == 4: # provision for relay, alias and network
|
||||||
|
if not spl[3] in main.network.keys():
|
||||||
|
failure("No such network: %s" % spl[3])
|
||||||
|
return
|
||||||
|
|
||||||
|
if "users" in main.relay[spl[1]]:
|
||||||
|
if not spl[2] in main.relay[spl[1]]["users"]:
|
||||||
|
failure("Relay %s not provisioned for alias %s" % (spl[1], spl[2]))
|
||||||
|
return
|
||||||
|
else:
|
||||||
|
failure("Relay %s not provisioned for alias %s" % (spl[1], spl[2]))
|
||||||
|
return
|
||||||
|
|
||||||
|
rtrn = provision.provisionRelayForNetwork(spl[1], spl[2], spl[3])
|
||||||
|
if rtrn == "PROVISIONED":
|
||||||
|
failure("Relay %s already provisioned for network %s" % (spl[1], spl[3]))
|
||||||
|
return
|
||||||
|
elif rtrn == "DUPLICATE":
|
||||||
|
failure("Instance with relay %s and alias %s already exists for network %s" % (spl[1], spl[2], spl[3]))
|
||||||
|
elif rtrn:
|
||||||
|
success("Started provisioning network %s on relay %s for alias %s" % (spl[3], spl[1], spl[2]))
|
||||||
|
info("Instance name is %s" % rtrn)
|
||||||
|
return
|
||||||
|
else:
|
||||||
|
failure("Failure while provisioning relay %s" % spl[1])
|
||||||
|
return
|
||||||
|
if length == 3: # provision for relay and alias only
|
||||||
|
rtrn = provision.provisionRelayForAlias(spl[1], spl[2])
|
||||||
|
if rtrn == "PROVISIONED":
|
||||||
|
failure("Relay %s already provisioned for alias %s" % (spl[1], spl[2]))
|
||||||
|
return
|
||||||
|
elif rtrn:
|
||||||
|
success("Started provisioning relay %s for alias %s" % (spl[1], spl[2]))
|
||||||
|
return
|
||||||
|
else:
|
||||||
|
failure("Failure while provisioning relay %s" % spl[1])
|
||||||
|
return
|
||||||
|
|
||||||
|
else:
|
||||||
|
incUsage("provision")
|
||||||
|
return
|
||||||
|
else:
|
||||||
|
incUsage(None)
|
@ -0,0 +1 @@
|
|||||||
|
{}
|
@ -0,0 +1,87 @@
|
|||||||
|
import main
|
||||||
|
from core.bot import deliverRelayCommands
|
||||||
|
from utils.logging.log import *
|
||||||
|
|
||||||
|
def provisionUserData(relay, alias, nick, altnick, ident, realname, password):
|
||||||
|
commands = {}
|
||||||
|
commands["controlpanel"] = []
|
||||||
|
commands["controlpanel"].append("AddUser %s %s" % (alias, password))
|
||||||
|
commands["controlpanel"].append("Set Nick %s %s" % (alias, nick))
|
||||||
|
commands["controlpanel"].append("Set Altnick %s %s" % (alias, altnick))
|
||||||
|
commands["controlpanel"].append("Set Ident %s %s" % (alias, ident))
|
||||||
|
commands["controlpanel"].append("Set RealName %s %s" % (alias, realname))
|
||||||
|
deliverRelayCommands(relay, commands)
|
||||||
|
return
|
||||||
|
|
||||||
|
def provisionNetworkData(relay, alias, network, host, port, security, auth, password):
|
||||||
|
commands = {}
|
||||||
|
stage2commands = {}
|
||||||
|
commands["controlpanel"] = []
|
||||||
|
commands["controlpanel"].append("AddNetwork %s %s" % (alias, network))
|
||||||
|
if security == "ssl":
|
||||||
|
commands["controlpanel"].append("SetNetwork TrustAllCerts %s %s true" % (alias, network)) # Don't judge me
|
||||||
|
commands["controlpanel"].append("AddServer %s %s %s +%s" % (alias, network, host, port))
|
||||||
|
elif security == "plain":
|
||||||
|
commands["controlpanel"].append("AddServer %s %s %s %s" % (alias, network, host, port))
|
||||||
|
if auth == "sasl":
|
||||||
|
stage2commands["status"] = []
|
||||||
|
stage2commands["sasl"] = []
|
||||||
|
stage2commands["status"].append("LoadMod sasl")
|
||||||
|
stage2commands["sasl"].append("Mechanism plain")
|
||||||
|
stage2commands["sasl"].append("Set %s %s" % (alias, password))
|
||||||
|
elif auth == "ns":
|
||||||
|
stage2commands["status"] = []
|
||||||
|
stage2commands["nickserv"] = []
|
||||||
|
stage2commands["status"].append("LoadMod NickServ")
|
||||||
|
stage2commands["nickserv"].append("Set %s" % password)
|
||||||
|
deliverRelayCommands(relay, commands, stage2=[[alias+"/"+network, stage2commands]])
|
||||||
|
return
|
||||||
|
|
||||||
|
def provisionRelayForAlias(relay, alias):
|
||||||
|
if "users" in main.relay[relay].keys():
|
||||||
|
if alias in main.relay[relay]["users"]:
|
||||||
|
return "PROVISIONED"
|
||||||
|
else:
|
||||||
|
main.relay[relay]["users"] = []
|
||||||
|
main.relay[relay]["users"].append(alias)
|
||||||
|
provisionUserData(relay, alias, main.alias[alias]["nick"],
|
||||||
|
main.alias[alias]["altnick"],
|
||||||
|
main.alias[alias]["ident"],
|
||||||
|
main.alias[alias]["realname"],
|
||||||
|
main.relay[relay]["password"])
|
||||||
|
main.saveConf("relay")
|
||||||
|
return True
|
||||||
|
|
||||||
|
def provisionRelayForNetwork(relay, alias, network):
|
||||||
|
if "networks" in main.relay[relay].keys():
|
||||||
|
if network in main.relay[relay]["networks"]:
|
||||||
|
return "PROVISIONED"
|
||||||
|
else:
|
||||||
|
main.relay[relay]["networks"] = []
|
||||||
|
main.relay[relay]["networks"].append(network)
|
||||||
|
provisionNetworkData(relay, alias, network,
|
||||||
|
main.network[network]["host"],
|
||||||
|
main.network[network]["port"],
|
||||||
|
main.network[network]["security"],
|
||||||
|
main.network[network]["auth"],
|
||||||
|
main.alias[alias]["password"])
|
||||||
|
main.saveConf("relay")
|
||||||
|
storedNetwork = False
|
||||||
|
num = 1
|
||||||
|
while not storedNetwork:
|
||||||
|
i = str(num)
|
||||||
|
if num == 1000:
|
||||||
|
error("Too many iterations in while trying to choose name for r: %s a: %s n: %s" % (relay, alias, network))
|
||||||
|
return False
|
||||||
|
|
||||||
|
if network+i in main.pool.keys():
|
||||||
|
if main.pool[network+i]["alias"] == alias and main.pool[network+i]["relay"] == relay:
|
||||||
|
return "DUPLICATE"
|
||||||
|
num += 1
|
||||||
|
else:
|
||||||
|
main.pool[network+i] = {"relay": relay,
|
||||||
|
"alias": alias,
|
||||||
|
"network": network}
|
||||||
|
main.saveConf("pool")
|
||||||
|
storedNetwork = True
|
||||||
|
return network+i
|
Loading…
Reference in New Issue