This repository has been archived by the owner on May 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
app.py
77 lines (71 loc) · 3.36 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
from twisted.internet import reactor, ssl
from twisted.internet.endpoints import serverFromString
from twisted.python import log
from txircd.ircd import IRCD, default_options
from txircd.server import ServerFactory
from txircd.utils import resolveEndpointDescription
from OpenSSL import SSL
import yaml, collections, sys, signal
# A direct copy of DefaultOpenSSLContext factory as of Twisted 12.2.0
# The only difference is using ctx.use_certificate_chain_file instead of ctx.use_certificate_file
# This code remains unchanged in the newer Twisted 13.0.0
class ChainedOpenSSLContextFactory(ssl.DefaultOpenSSLContextFactory):
def cacheContext(self):
if self._context is None:
ctx = self._contextFactory(self.sslmethod)
ctx.set_options(SSL.OP_NO_SSLv2)
ctx.use_certificate_chain_file(self.certificateFileName)
ctx.use_privatekey_file(self.privateKeyFileName)
self._context = ctx
def createHangupHandler(ircd):
return lambda signal, stack: ircd.rehash()
def addClientPortToIRCd(port, ircd, desc):
ircd.saveClientPort(desc, port)
def addServerPortToIRCd(port, ircd, desc):
ircd.saveServerPort(desc, port)
def logPortNotBound(error):
log.msg("An error occurred: {}".format(error))
if __name__ == "__main__":
# Copy the defaults
options = default_options.copy()
# Parse command line
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--config", default="txircd.yaml")
parser.add_argument("-v", "--verbose", dest="verbose", action="store_true")
parser.add_argument("-l", "--log-file", dest="log_file", type=argparse.FileType('a'), default=sys.stdout)
args = parser.parse_args()
# Load config file
try:
with open(args.config) as f:
options.update(yaml.safe_load(f))
except:
print "Error: Loading configuration file" # Oh well
if options["app_verbose"] or args.verbose:
log.startLogging(args.log_file)
ssl_cert = ChainedOpenSSLContextFactory(options["app_ssl_key"],options["app_ssl_pem"])
ssl_cert.getContext().set_verify(SSL.VERIFY_PEER, lambda connection, x509, errnum, errdepth, ok: True) # We can ignore the validity of certs to get what we need
ircd = IRCD(args.config, options, ssl_cert)
serverlink_factory = ServerFactory(ircd)
for portstring in options["server_client_ports"]:
try:
endpoint = serverFromString(reactor, resolveEndpointDescription(portstring))
except ValueError as e:
log.msg("Could not bind {}: not a valid description ({})".format(portstring, e))
continue
listenDeferred = endpoint.listen(ircd)
listenDeferred.addCallback(addClientPortToIRCd, ircd, portstring)
listenDeferred.addErrback(logPortNotBound)
for portstring in options["server_link_ports"]:
try:
endpoint = serverFromString(reactor, resolveEndpointDescription(portstring))
except ValueError as e:
log.msg("Could not bind {}: not a valid description ({})".format(portstring, e))
continue
listenDeferred = endpoint.listen(serverlink_factory)
listenDeferred.addCallback(addServerPortToIRCd, ircd, portstring)
listenDeferred.addErrback(logPortNotBound)
# Bind SIGHUP to rehash
signal.signal(signal.SIGHUP, createHangupHandler(ircd))
# And start up the reactor
reactor.run()