monolith/legacy/threshold

126 lines
3.9 KiB
Plaintext
Raw Permalink Normal View History

#!/usr/bin/env python
import sys
2022-07-21 12:40:09 +00:00
from codecs import getwriter
2022-07-29 16:28:19 +00:00
from os import getenv
2022-07-21 12:40:09 +00:00
from signal import SIGINT, signal
from sys import stderr, stdout
2022-07-21 12:39:41 +00:00
import main
2022-07-21 12:40:09 +00:00
import modules.counters
2022-07-21 12:40:11 +00:00
from api.views import API
2022-07-21 12:40:09 +00:00
from core.relay import RelayFactory
from core.server import ServerFactory
2022-09-05 06:20:30 +00:00
from twisted.internet import reactor
# Webapp stuff
from twisted.internet.protocol import Factory
from twisted.internet.ssl import DefaultOpenSSLContextFactory
from utils.cleanup import handler
2022-07-21 12:40:05 +00:00
from utils.loaders.command_loader import loadCommands
2022-07-21 12:40:09 +00:00
from utils.logging.log import log
2022-07-21 12:40:05 +00:00
2022-07-21 12:40:11 +00:00
Factory.noisy = False
2022-07-21 12:40:05 +00:00
main.initMain()
2022-07-21 12:39:41 +00:00
if "--debug" in sys.argv: # yes really
main.config["Debug"] = True
if "--trace" in sys.argv:
main.config["Trace"] = True
2022-08-12 22:32:00 +00:00
if "--migrate" in sys.argv:
from modules.network import migrate
2022-07-21 12:39:41 +00:00
2022-08-12 22:32:00 +00:00
migrate()
exit()
loadCommands()
2022-07-21 12:40:05 +00:00
2022-07-21 12:39:41 +00:00
# core.logstash.init_logstash()
2022-07-21 12:40:05 +00:00
signal(SIGINT, handler) # Handle Ctrl-C and run the cleanup routine
stdout = getwriter("utf8")(stdout) # this is a generic fix but we all know
stderr = getwriter("utf8")(stderr) # it's just for the retards on Rizon using
# unicode quit messages for no reason
2022-07-29 16:28:19 +00:00
trues = ("true", "1", "t", True)
# Main listener
listener_address = getenv("THRESHOLD_LISTENER_HOST", main.config["Listener"]["Address"])
listener_port = int(getenv("THRESHOLD_LISTENER_PORT", main.config["Listener"]["Port"]))
2022-09-05 06:20:30 +00:00
listener_ssl = (
getenv("THRESHOLD_LISTENER_SSL", main.config["Listener"]["UseSSL"]) in trues
)
# RelayAPI
2022-09-05 06:20:30 +00:00
relay_enabled = (
getenv("THRESHOLD_RELAY_ENABLED", main.config["RelayAPI"]["Enabled"]) in trues
)
relay_address = getenv("THRESHOLD_RELAY_HOST", main.config["RelayAPI"]["Address"])
relay_port = int(getenv("THRESHOLD_RELAY_PORT", main.config["RelayAPI"]["Port"]))
relay_ssl = getenv("THRESHOLD_RELAY_SSL", main.config["RelayAPI"]["UseSSL"]) in trues
# Web API
api_enabled = getenv("THRESHOLD_API_ENABLED", main.config["API"]["Enabled"]) in trues
api_address = getenv("THRESHOLD_API_HOST", main.config["API"]["Address"])
api_port = int(getenv("THRESHOLD_API_PORT", main.config["API"]["Port"]))
2022-08-11 19:09:01 +00:00
# Debugging
debug_enabled = getenv("THRESHOLD_DEBUG", main.config["Debug"]) in trues
trace_enabled = getenv("THRESHOLD_TRACE", main.config["Trace"]) in trues
if debug_enabled:
main.config["Debug"] = True
if trace_enabled:
main.config["Trace"] = True
if __name__ == "__main__":
listener = ServerFactory()
if listener_ssl is True:
2022-07-21 12:39:41 +00:00
reactor.listenSSL(
2022-07-29 21:22:22 +00:00
listener_port,
2022-07-21 12:39:41 +00:00
listener,
DefaultOpenSSLContextFactory(
main.certPath + main.config["Key"],
main.certPath + main.config["Certificate"],
),
interface=listener_address,
2022-07-21 12:39:41 +00:00
)
2022-07-29 16:28:19 +00:00
log("Threshold running with SSL on %s:%s" % (listener_address, listener_port))
else:
2022-07-21 12:39:41 +00:00
reactor.listenTCP(
listener_port,
2022-07-21 12:39:41 +00:00
listener,
interface=listener_address,
2022-07-21 12:39:41 +00:00
)
log("Threshold running on %s:%s" % (listener_address, listener_port))
if relay_enabled:
relay = RelayFactory()
if relay_ssl is True:
2022-07-21 12:39:41 +00:00
reactor.listenSSL(
relay_port,
2022-07-21 12:39:41 +00:00
relay,
DefaultOpenSSLContextFactory(
main.certPath + main.config["Key"],
main.certPath + main.config["Certificate"],
),
interface=relay_address,
2022-07-21 12:39:41 +00:00
)
2022-09-05 06:20:30 +00:00
log(
"Threshold relay running with SSL on %s:%s"
% (relay_address, relay_port)
)
else:
2022-07-21 12:39:41 +00:00
reactor.listenTCP(
relay_port,
2022-07-21 12:39:41 +00:00
relay,
interface=relay_address,
2022-07-21 12:39:41 +00:00
)
2022-07-29 16:28:19 +00:00
log("Threshold relay running on %s:%s" % (relay_address, relay_port))
for net in main.network.keys():
main.network[net].start_bots()
modules.counters.setupCounterLoop()
if api_enabled:
2022-07-21 12:40:11 +00:00
api = API()
api.app.run(api_address, api_port)
2022-07-21 12:40:11 +00:00
else:
reactor.run()