2019-08-11 20:58:14 +00:00
|
|
|
import json
|
|
|
|
import pickle
|
2019-01-26 18:58:21 +00:00
|
|
|
from redis import StrictRedis
|
2018-10-14 13:47:08 +00:00
|
|
|
from string import digits
|
2019-08-15 20:20:49 +00:00
|
|
|
from os import urandom
|
|
|
|
|
2018-02-23 22:05:40 +00:00
|
|
|
from utils.logging.log import *
|
|
|
|
|
|
|
|
configPath = "conf/"
|
|
|
|
certPath = "cert/"
|
|
|
|
|
|
|
|
filemap = {
|
2019-08-11 20:58:14 +00:00
|
|
|
# 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"],
|
2019-09-29 21:45:16 +00:00
|
|
|
"alias": ["alias.json", "provisioned alias data", "json"],
|
2019-08-11 20:58:14 +00:00
|
|
|
|
|
|
|
# Binary (pickle) configs
|
|
|
|
"network": ["network.dat", "network list", "pickle"]
|
2019-08-11 21:01:29 +00:00
|
|
|
}
|
2018-02-23 22:05:40 +00:00
|
|
|
|
|
|
|
connections = {}
|
2019-03-18 21:01:28 +00:00
|
|
|
relayConnections = {}
|
2018-02-23 22:05:40 +00:00
|
|
|
IRCPool = {}
|
|
|
|
ReactorPool = {}
|
|
|
|
FactoryPool = {}
|
2019-10-11 12:07:57 +00:00
|
|
|
TempChan = {}
|
2018-02-23 22:05:40 +00:00
|
|
|
|
|
|
|
MonitorPool = []
|
|
|
|
|
|
|
|
CommandMap = {}
|
|
|
|
|
2019-07-28 14:07:46 +00:00
|
|
|
runningSample = 0
|
|
|
|
lastMinuteSample = 0
|
|
|
|
|
2019-08-15 20:20:49 +00:00
|
|
|
# Generate 16-byte hex key for message checksums
|
|
|
|
hashKey = urandom(16)
|
|
|
|
lastEvents = {}
|
|
|
|
|
2018-10-21 16:14:50 +00:00
|
|
|
def liveNets():
|
|
|
|
networks = set()
|
|
|
|
for i in IRCPool.keys():
|
|
|
|
networks.add("".join([x for x in i if not x in digits]))
|
2018-10-14 13:47:08 +00:00
|
|
|
return networks
|
|
|
|
|
2018-02-23 22:05:40 +00:00
|
|
|
def saveConf(var):
|
2019-08-11 20:58:14 +00:00
|
|
|
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")
|
2018-02-23 22:05:40 +00:00
|
|
|
|
|
|
|
def loadConf(var):
|
2019-08-11 20:58:14 +00:00
|
|
|
if filemap[var][2] == "json":
|
|
|
|
with open(configPath+filemap[var][0], "r") as f:
|
|
|
|
globals()[var] = json.load(f)
|
2019-09-29 21:45:16 +00:00
|
|
|
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()}
|
2019-08-11 20:58:14 +00:00
|
|
|
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")
|
2018-02-23 22:05:40 +00:00
|
|
|
|
|
|
|
def initConf():
|
|
|
|
for i in filemap.keys():
|
|
|
|
loadConf(i)
|
|
|
|
|
|
|
|
def initMain():
|
2019-10-08 20:07:54 +00:00
|
|
|
global r, g
|
2018-02-23 22:05:40 +00:00
|
|
|
initConf()
|
2019-10-08 20:07:54 +00:00
|
|
|
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
|
|
|
|
|
|
|
|