Verified Commit 882dc715 authored by Martin Petráček's avatar Martin Petráček
Browse files

domains_reapply script

parent 71c6cf8d
#!/usr/bin/env python3
import os
import sys
import time
import datetime
import time
import sqlite3
import signal
import errno
import re
import json
import glob
#TODO: remove duplicate code (shared with monitor.py). Maybe create a package?
class MultiReplace:
"perform replacements specified by regex and adict all at once"
" The regex is constructed such that it matches the whole string (.* in the beginnin and end),"
" the actual key from adict is the first group of match (ignoring possible prefix and suffix)."
" The whole string is then replaced (the replacement is specified by adict)"
def __init__(self, adict):
self.setup(adict)
def setup(self, adict):
self.adict = adict
self.rx = re.compile("^.*("+'|'.join(map(re.escape, adict))+").*$")
def replace(self, text):
def one_xlat(match):
return self.adict[match.group(1)]
return self.rx.sub(one_xlat, text)
def load_replaces():
adict={}
try:
for fn in glob.glob("/usr/share/pakon-light/domains_replace/*.conf"):
with open(fn) as f:
for line in f:
line=line.strip()
if not line:
continue
match = re.match('"([^"]+)"\s*:\s*"([^"]+)"', line)
if not match:
print("invalid line: "+line)
continue
adict[match.group(1)]=match.group(2)
except IOError:
print("can't load domains_services file")
return adict
def replace(db, multiple_replace):
con = sqlite3.connect(db)
c = con.cursor()
replaced = 0
for row in c.execute('SELECT DISTINCT(app_hostname) FROM traffic WHERE app_hostname IS NOT NULL'):
name = multiple_replace.replace(row[0])
if name!=row[0]:
t = con.cursor()
t.execute("UPDATE traffic SET app_hostname = ? WHERE app_hostname = ?", (name, row[0]))
replaced += t.rowcount
con.commit()
con.close()
print("Replaced "+str(replaced)+" hostnames in "+db)
def main():
adict = load_replaces()
if not adict:
print("empty dictionary of replacements, nothing to do")
sys.exit(1)
multiple_replace = MultiReplace(adict)
replace('/var/lib/pakon.db', multiple_replace)
replace('/srv/pakon/pakon-archive.db', multiple_replace)
if __name__ == "__main__":
main()
......@@ -78,16 +78,17 @@ def get_dns_hostname(src_ip, dest_ip):
name_ = dns_cache.get(src_ip, name)
return name
def load_domain_replaces():
def load_replaces():
adict={}
try:
for fn in glob.glob("/usr/share/pakon-light/domains_replace/*.conf"):
with open(fn) as f:
for line in f:
match = re.match('\s*"([^"]+)"\s*:\s*"([^"]+)"\s*', line)
line=line.strip()
if not line:
continue
match = re.match('"([^"]+)"\s*:\s*"([^"]+)"', line)
if not match:
if re.match('\s*', line): #ignore empty lines
continue
print("invalid line: "+line)
continue
adict[match.group(1)]=match.group(2)
......@@ -178,7 +179,8 @@ def exit_gracefully(signum, frame):
def reload_replaces(signum, frame):
global domain_replace
domain_replace.set(load_domain_replaces())
logging.info("reloading domain replaces")
domain_replace.setup(load_replaces())
if not os.path.isfile('/var/lib/pakon.db'):
subprocess.call(['/usr/bin/python3', '/usr/libexec/pakon-light/create_db.py'])
......@@ -193,7 +195,7 @@ except:
logging.debug('Error cleaning flow_id')
dns_cache = DNSCache()
domain_replace = MultiReplace(load_domain_replaces())
domain_replace = MultiReplace(load_replaces())
conntrack=None
try:
devnull = open(os.devnull, 'w')
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment