2019-08-25 20:29:11 +00:00
|
|
|
from twisted.internet.ssl import DefaultOpenSSLContextFactory
|
2019-08-11 20:58:14 +00:00
|
|
|
import json
|
2019-08-25 20:29:11 +00:00
|
|
|
|
2019-10-02 19:26:05 +00:00
|
|
|
from modules import alias
|
2019-08-11 20:58:14 +00:00
|
|
|
from twisted.internet import reactor
|
|
|
|
from core.bot import IRCBot, IRCBotFactory
|
|
|
|
import main
|
|
|
|
from utils.logging.log import *
|
2019-09-29 21:45:16 +00:00
|
|
|
from utils.get import getRelay
|
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
|
|
|
|
|
|
|
|
self.last = 0
|
|
|
|
self.relays = {}
|
|
|
|
self.aliases = {}
|
|
|
|
|
2019-08-25 20:29:11 +00:00
|
|
|
def add_relay(self, num=None):
|
|
|
|
if not num:
|
|
|
|
self.last += 1
|
|
|
|
num = self.last
|
|
|
|
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,
|
|
|
|
"id": num
|
2019-08-11 20:58:14 +00:00
|
|
|
}
|
2019-09-29 21:45:16 +00:00
|
|
|
password = alias.generate_password()
|
|
|
|
if not num in main.alias.keys():
|
|
|
|
main.alias[num] = alias.generate_alias()
|
|
|
|
main.saveConf("alias")
|
|
|
|
self.aliases[num] = {"password": password}
|
|
|
|
#if main.config["ConnectOnCreate"]: -- Done in provision
|
|
|
|
# self.start_bot(num)
|
|
|
|
return num, main.alias[num]["nick"]
|
2019-08-11 20:58:14 +00:00
|
|
|
|
2019-10-02 19:45:28 +00:00
|
|
|
def killAliases(self, aliasList):
|
|
|
|
for i in aliasList:
|
2019-10-02 19:26:05 +00:00
|
|
|
name = self.net+str(i)
|
|
|
|
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]
|
|
|
|
#del main.alias[id] - Aliases are global per num, so don't delete them!
|
|
|
|
self.killAliases([id])
|
|
|
|
|
|
|
|
def seppuku(self):
|
|
|
|
# Removes all bots in preperation for deletion
|
|
|
|
self.killAliases(self.relay.keys())
|
|
|
|
|
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
|
|
|
|
keyFN = main.certPath+main.config["Key"]
|
|
|
|
certFN = main.certPath+main.config["Certificate"]
|
|
|
|
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)
|
|
|
|
#host, port = self.relays[num]["host"], self.relays[num]["port"]
|
|
|
|
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)
|