monolith/main.py

90 lines
2.4 KiB
Python
Raw Normal View History

import json
import pickle
from redis import StrictRedis
from string import digits
from os import urandom
from utils.logging.log import *
configPath = "conf/"
certPath = "cert/"
filemap = {
# JSON configs
"config": ["config.json", "configuration", "json"],
"help": ["help.json", "command help", "json"],
"counters": ["counters.json", "counters file", "json"],
"monitor": ["monitor.json", "monitoring database", "json"],
"tokens": ["tokens.json", "authentication tokens", "json"],
"aliasdata": ["aliasdata.json", "data for alias generation", "json"],
"alias": ["alias.json", "provisioned alias data", "json"],
# Binary (pickle) configs
"network": ["network.dat", "network list", "pickle"]
}
connections = {}
relayConnections = {}
IRCPool = {}
ReactorPool = {}
FactoryPool = {}
TempChan = {}
MonitorPool = []
CommandMap = {}
runningSample = 0
lastMinuteSample = 0
# Generate 16-byte hex key for message checksums
hashKey = urandom(16)
lastEvents = {}
def liveNets():
networks = set()
for i in IRCPool.keys():
networks.add("".join([x for x in i if not x in digits]))
return networks
def saveConf(var):
if filemap[var][2] == "json":
with open(configPath+filemap[var][0], "w") as f:
json.dump(globals()[var], f, indent=4)
elif filemap[var][2] == "pickle":
with open(configPath+filemap[var][0], "wb") as f:
pickle.dump(globals()[var], f)
else:
raise Exception("invalid format")
def loadConf(var):
if filemap[var][2] == "json":
with open(configPath+filemap[var][0], "r") as f:
globals()[var] = json.load(f)
if var == "alias":
# This is a hack to convert all the keys into integers since JSON
# turns them into strings...
# Dammit Jason!
global alias
alias = {int(x):y for x, y in alias.items()}
elif filemap[var][2] == "pickle":
try:
with open(configPath+filemap[var][0], "rb") as f:
globals()[var] = pickle.load(f)
except FileNotFoundError:
globals()[var] = {}
else:
raise Exception("invalid format")
def initConf():
for i in filemap.keys():
loadConf(i)
def initMain():
global r, g
initConf()
r = StrictRedis(unix_socket_path=config["RedisSocket"], db=0) # Ephemeral - flushed on quit
g = StrictRedis(unix_socket_path=config["RedisSocket"], db=1) # Persistent
2018-08-27 19:52:39 +00:00