2022-07-21 12:40:09 +00:00
|
|
|
from twisted.internet import reactor
|
2022-07-21 12:39:59 +00:00
|
|
|
from twisted.internet.ssl import DefaultOpenSSLContextFactory
|
|
|
|
|
2022-07-21 12:40:09 +00:00
|
|
|
import main
|
|
|
|
from core.bot import IRCBotFactory
|
2019-10-02 19:26:05 +00:00
|
|
|
from modules import alias
|
2019-10-08 20:10:42 +00:00
|
|
|
from modules.chankeep import nukeNetwork
|
2020-06-07 16:26:53 +00:00
|
|
|
from modules.regproc import needToRegister
|
2022-07-27 21:03:42 +00:00
|
|
|
from utils.deliver_relay_commands import deliverRelayCommands
|
2022-07-21 12:40:01 +00:00
|
|
|
from utils.get import getRelay
|
2022-07-21 12:40:09 +00:00
|
|
|
from utils.logging.log import log
|
2019-08-11 20:58:14 +00:00
|
|
|
|
2022-07-21 12:39:41 +00:00
|
|
|
|
2019-08-11 20:58:14 +00:00
|
|
|
class Network:
|
2019-08-25 20:29:11 +00:00
|
|
|
def __init__(self, net, host, port, security, auth):
|
|
|
|
self.net = net
|
2019-08-11 20:58:14 +00:00
|
|
|
self.host = host
|
|
|
|
self.port = port
|
|
|
|
self.security = security
|
|
|
|
self.auth = auth
|
|
|
|
|
2019-10-11 12:07:57 +00:00
|
|
|
self.last = 1
|
2019-08-11 20:58:14 +00:00
|
|
|
self.relays = {}
|
|
|
|
self.aliases = {}
|
|
|
|
|
2019-08-25 20:29:11 +00:00
|
|
|
def add_relay(self, num=None):
|
2022-07-29 16:28:09 +00:00
|
|
|
# Grrrrrrrrrr
|
|
|
|
self.last = int(self.last)
|
2019-08-25 20:29:11 +00:00
|
|
|
if not num:
|
|
|
|
num = self.last
|
2019-10-11 12:07:57 +00:00
|
|
|
self.last += 1
|
|
|
|
elif num == self.last:
|
|
|
|
self.last += 1
|
2020-05-30 20:40:10 +00:00
|
|
|
registered = False
|
2020-06-07 16:26:53 +00:00
|
|
|
if not needToRegister(self.net):
|
|
|
|
registered = True
|
|
|
|
# Don't need to register if it's been disabled in definitions,
|
|
|
|
# so we'll pretend we already did
|
2019-08-25 20:29:11 +00:00
|
|
|
self.relays[num] = {
|
2019-09-29 13:57:36 +00:00
|
|
|
"enabled": main.config["ConnectOnCreate"],
|
2019-08-25 20:29:11 +00:00
|
|
|
"net": self.net,
|
2020-05-30 20:40:10 +00:00
|
|
|
"id": num,
|
2022-07-21 12:39:41 +00:00
|
|
|
"registered": registered,
|
2019-08-11 20:58:14 +00:00
|
|
|
}
|
2019-09-29 21:45:16 +00:00
|
|
|
password = alias.generate_password()
|
2022-07-21 12:40:05 +00:00
|
|
|
if num not in main.alias.keys():
|
2019-09-29 21:45:16 +00:00
|
|
|
main.alias[num] = alias.generate_alias()
|
|
|
|
main.saveConf("alias")
|
|
|
|
self.aliases[num] = {"password": password}
|
2022-07-21 12:39:41 +00:00
|
|
|
# if main.config["ConnectOnCreate"]: -- Done in provision
|
2019-09-29 21:45:16 +00:00
|
|
|
# self.start_bot(num)
|
|
|
|
return num, main.alias[num]["nick"]
|
2019-08-11 20:58:14 +00:00
|
|
|
|
2022-07-27 21:03:42 +00:00
|
|
|
def enable_relay(self, num):
|
|
|
|
"""
|
|
|
|
Enable a relay for this network.
|
|
|
|
Send a command to ZNC to connect.
|
|
|
|
"""
|
|
|
|
self.relays[num]["enabled"] = True
|
|
|
|
user = main.alias[num]["nick"]
|
|
|
|
commands = {"status": ["Connect"]}
|
|
|
|
name = f"{self.net}{num}"
|
|
|
|
deliverRelayCommands(num, commands, user=user + "/" + self.net)
|
|
|
|
main.saveConf("network")
|
|
|
|
if name not in main.IRCPool.keys():
|
|
|
|
self.start_bot(num)
|
|
|
|
|
|
|
|
def disable_relay(self, num):
|
|
|
|
"""
|
|
|
|
Disable a relay for this network.
|
|
|
|
Send a command to ZNC to disconnect.
|
|
|
|
Stop trying to connect to the relay.
|
|
|
|
"""
|
|
|
|
self.relays[num]["enabled"] = False
|
|
|
|
user = main.alias[num]["nick"]
|
|
|
|
# relay = main.network[spl[1]].relays[relayNum]
|
|
|
|
commands = {"status": ["Disconnect"]}
|
|
|
|
name = f"{self.net}{num}"
|
|
|
|
deliverRelayCommands(num, commands, user=user + "/" + self.net)
|
|
|
|
main.saveConf("network")
|
|
|
|
if name in main.ReactorPool.keys():
|
|
|
|
if name in main.FactoryPool.keys():
|
|
|
|
main.FactoryPool[name].stopTrying()
|
|
|
|
main.ReactorPool[name].disconnect()
|
|
|
|
if name in main.IRCPool.keys():
|
|
|
|
del main.IRCPool[name]
|
|
|
|
del main.ReactorPool[name]
|
|
|
|
del main.FactoryPool[name]
|
|
|
|
|
2019-10-02 19:45:28 +00:00
|
|
|
def killAliases(self, aliasList):
|
|
|
|
for i in aliasList:
|
2022-07-21 12:39:41 +00:00
|
|
|
name = self.net + str(i)
|
2019-10-02 19:26:05 +00:00
|
|
|
if name in main.ReactorPool.keys():
|
|
|
|
if name in main.FactoryPool.keys():
|
|
|
|
main.FactoryPool[name].stopTrying()
|
|
|
|
main.ReactorPool[name].disconnect()
|
|
|
|
if name in main.IRCPool.keys():
|
|
|
|
del main.IRCPool[name]
|
|
|
|
del main.ReactorPool[name]
|
|
|
|
del main.FactoryPool[name]
|
|
|
|
|
2019-10-02 19:45:28 +00:00
|
|
|
def delete_relay(self, id):
|
|
|
|
del self.relays[id]
|
|
|
|
del self.aliases[id]
|
2022-07-21 12:39:41 +00:00
|
|
|
# del main.alias[id] - Aliases are global per num, so don't delete them!
|
2019-10-02 19:45:28 +00:00
|
|
|
self.killAliases([id])
|
|
|
|
|
|
|
|
def seppuku(self):
|
|
|
|
# Removes all bots in preperation for deletion
|
2019-10-02 20:25:15 +00:00
|
|
|
self.killAliases(self.relays.keys())
|
2019-10-08 20:10:42 +00:00
|
|
|
nukeNetwork(self.net)
|
2019-10-02 19:45:28 +00:00
|
|
|
|
2019-08-25 20:29:11 +00:00
|
|
|
def start_bot(self, num):
|
2019-08-11 20:58:14 +00:00
|
|
|
# a single name is given to relays in the backend
|
|
|
|
# e.g. freenode1 for the first relay on freenode network
|
2022-07-21 12:39:41 +00:00
|
|
|
keyFN = main.certPath + main.config["Key"]
|
|
|
|
certFN = main.certPath + main.config["Certificate"]
|
2022-07-21 12:40:03 +00:00
|
|
|
contextFactory = DefaultOpenSSLContextFactory(
|
|
|
|
keyFN.encode("utf-8", "replace"), certFN.encode("utf-8", "replace")
|
|
|
|
)
|
2019-08-25 20:29:11 +00:00
|
|
|
bot = IRCBotFactory(self.net, num)
|
2022-07-21 12:39:41 +00:00
|
|
|
# host, port = self.relays[num]["host"], self.relays[num]["port"]
|
2019-08-25 20:29:11 +00:00
|
|
|
host, port = getRelay(num)
|
|
|
|
rct = reactor.connectSSL(host, port, bot, contextFactory)
|
|
|
|
name = self.net + str(num)
|
2019-08-11 20:58:14 +00:00
|
|
|
main.ReactorPool[name] = rct
|
|
|
|
main.FactoryPool[name] = bot
|
|
|
|
|
2019-08-25 20:29:11 +00:00
|
|
|
log("Started bot on relay %s on %s" % (num, self.host))
|
2019-08-11 20:58:14 +00:00
|
|
|
|
|
|
|
def start_bots(self):
|
2019-08-25 20:29:11 +00:00
|
|
|
for num in self.relays.keys():
|
|
|
|
if self.relays[num]["enabled"]:
|
|
|
|
self.start_bot(num)
|