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-08-11 20:58:14 +00:00
|
|
|
import modules.alias as alias
|
|
|
|
from twisted.internet import reactor
|
|
|
|
from core.bot import IRCBot, IRCBotFactory
|
|
|
|
import main
|
|
|
|
from utils.logging.log import *
|
2019-08-25 20:29:11 +00:00
|
|
|
from utils.getrelay 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-08-11 20:58:14 +00:00
|
|
|
"enabled": False,
|
2019-08-25 20:29:11 +00:00
|
|
|
"net": self.net,
|
|
|
|
"id": num
|
2019-08-11 20:58:14 +00:00
|
|
|
}
|
2019-08-25 20:29:11 +00:00
|
|
|
self.aliases[num] = alias.generate_alias()
|
|
|
|
return num, self.aliases[num]["nick"]
|
2019-08-11 20:58:14 +00:00
|
|
|
|
|
|
|
def delete_relay(self, id):
|
|
|
|
del self.relays[id]
|
|
|
|
del self.aliases[id]
|
|
|
|
|
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)
|