diff --git a/frankenrouter.py b/frankenrouter.py index f151546..c7434e0 100644 --- a/frankenrouter.py +++ b/frankenrouter.py @@ -307,8 +307,8 @@ if __name__ == "__main__": python3 frankenrouter.py init --- setup the default firewall python3 frankenrouter.py allipsetup --- read the contents of /root/pubip.cache and setup all assigments. for startup. -python3 frankenrouter.py ipadd IP MASK VLAN --- add IP to VLAN - example: ipadd 87.120.110.120 24 142 +python3 frankenrouter.py ipadd IP VLAN --- add IP to VLAN + example: ipadd 87.120.110.120 142 python3 frankenrouter,py ipdel IP VLAN --- del IP from VLAN example: ipdel 87.120.110.120 142 @@ -328,7 +328,7 @@ python3 frankenrouter,py ipdel IP VLAN --- del IP from VLAN allipsetup('/root/pubip.cache', ip_mask) if sys.argv[1] == 'ipadd': - bashexec('ipadd-{}-{}-{}'.format(sys.argv[2], ip_mask, sys.argv[3]), assignip(sys.argv[2], ip_mask, sys.argv[3])) + bashexec('ipadd-{}-{}'.format(sys.argv[2], sys.argv[3]), assignip(sys.argv[2], ip_mask, sys.argv[3])) if sys.argv[1] == 'ipdel': bashexec('ipdel-{}-{}'.format(sys.argv[2], sys.argv[3]), removeip(sys.argv[2], sys.argv[3])) diff --git a/updateipcache.py b/updateipcache.py index 1a2bf0f..3742efb 100644 --- a/updateipcache.py +++ b/updateipcache.py @@ -4,12 +4,28 @@ import requests import json import sys import re +import subprocess -conffile = open('/root/frankenrouter/config.sh', 'r') -for line in conffile: - if re.search('LABEL', line): - slave_name = line.split('=', 1)[1].rstrip().replace('"', '') -conffile.close() +class DictDiffer(object): + """ + Calculate the difference between two dictionaries as: + (1) items added + (2) items removed + (3) keys same in both but changed values + (4) keys same in both and unchanged values + """ + def __init__(self, current_dict, past_dict): + self.current_dict, self.past_dict = current_dict, past_dict + self.set_current, self.set_past = set(current_dict.keys()), set(past_dict.keys()) + self.intersect = self.set_current.intersection(self.set_past) + def added(self): + return self.set_current - self.intersect + def removed(self): + return self.set_past - self.intersect + def changed(self): + return set(o for o in self.intersect if self.past_dict[o] != self.current_dict[o]) + def unchanged(self): + return set(o for o in self.intersect if self.past_dict[o] == self.current_dict[o]) conffile = open('/root/frankenrouter/config.sh', 'r') for line in conffile: @@ -19,6 +35,12 @@ conffile.close() api_url = 'https://' + str(api_host) + '/vmanager/slavetables' +conffile = open('/root/frankenrouter/config.sh', 'r') +for line in conffile: + if re.search('LABEL', line): + slave_name = line.split('=', 1)[1].rstrip().replace('"', '') +conffile.close() + ### try: @@ -26,15 +48,47 @@ try: apireq = requests.post(api_url, headers={'Content-Type': 'application/json'}, data=json.dumps(data), timeout=30) result = apireq.json() except: + print('can not connect') sys.exit() if result['status'] == 'ok': del result['status'] - wr = open('/root/pubip.cache', 'w') - #TODO: Sort cache and compare. - wr.write(json.dumps(result)) - wr.close() - print('public ip cache updated') + new_list = result + + # read cache to load the old values + r_ca = open('/root/pubip.cache', 'r') + current_list = json.loads(r_ca.read()) + r_ca.close() + + #compare the current value with the cache + newdataflag = False + difference = DictDiffer(new_list, current_list) + if len(difference.added()) is not 0: + for ipkey in difference.added(): + ip = ipkey + vlan = new_list[ipkey] + print('added {} to {}'.format(ip, vlan)) + newdataflag = True + subprocess.call('python3 /root/frankenrouter/frankenrouter.py ipadd {} {}'.format(ip, vlan), shell=True) + if len(difference.removed()) is not 0: + for ipkey in difference.removed(): + ip = ipkey + vlan = current_list[ipkey] + print('removed {} from {}'.format(ip, vlan)) + newdataflag = True + subprocess.call('python3 /root/frankenrouter/frankenrouter.py ipdel {} {}'.format(ip, vlan), shell=True) + + if newdataflag: + # move the old cache and write the new data + subprocess.call('mv /root/pubip.cache /root/pubip.cache.old', shell=True) + w_ca = open('/root/pubip.cache', 'w') + w_ca.write(json.dumps(new_list)) + w_ca.close() + print('public ip cache updated with the new data') + else: + pass else: - print('no data') + print('no data error') + +