| import os, sys, time, dns |
|
|
| os.chdir('/www/server/panel') |
| sys.path.append('class/') |
| import public |
| try: |
| import dns.resolver |
| except: |
| if os.path.exists('/www/server/panel/pyenv'): |
| public.ExecShell('/www/server/panel/pyenv/bin/pip install dnspython') |
| else: |
| public.ExecShell('pip install dnspython') |
| import dns.resolver |
|
|
|
|
| def cron_shell(): |
| from safeModel import firewallModel |
| firewallobj = firewallModel.main() |
| conf = public.M('firewall_domain').select() |
| domain_name_dict = {} |
| old_a_ip_dict = {} |
| |
| for ii in conf: |
| domain_name = ii['domain'].split('|')[0] |
| if domain_name not in domain_name_dict.keys(): |
| domain_name_dict[domain_name] = [] |
| old_a_ip_dict[domain_name] = [] |
| domain_name_dict[domain_name].append(ii) |
| old_a_ip_dict[domain_name].append(ii['address']) |
| is_reload = False |
| |
| for i3 in domain_name_dict.keys(): |
| a_ip = firewallobj.get_a_ip(i3) |
| |
| if a_ip and len(a_ip) < 2 and public.is_domain(a_ip[0]): |
| |
| a_ip = [firewallobj.check_a_ip(a_ip[0])] |
| |
| if not a_ip: continue |
| |
| a_ip_num = len(a_ip) |
| old_num = len(domain_name_dict[i3]) |
| |
| intersection_list = list(set(old_a_ip_dict[i3]).intersection(a_ip)) |
| |
| |
| a_ip_difference = list(set(a_ip).difference(old_a_ip_dict[i3])) |
| |
| i3_difference = list(set(old_a_ip_dict[i3]).difference(a_ip)) |
| |
| if a_ip_num < old_num: |
| pass |
| |
| elif a_ip_num > old_num: |
| pass |
| |
| else: |
| |
| if not intersection_list: |
| for i4 in domain_name_dict[i3]: |
| |
| is_reload = True |
| args = public.dict_obj() |
| args.id = i4['sid'] |
| args.domain = i4['domain'] |
| |
| for i7 in a_ip: |
| if i7 not in intersection_list: |
| args.address = i7 |
| a_ip.remove(i7) |
| break |
| if args.domain.split('|')[1] != args.address: |
| args.domain = args.domain.split( |
| '|')[0] + '|' + args.address |
| args.types = i4['types'] |
| args.brief = i4['brief'] |
| args.sid = i4['id'] |
| |
| |
| if i4['port']: |
| args.protocol = i4['protocol'] |
| args.ports = i4['port'] |
| args.choose = 'point' |
| args.source = args.address |
| firewallobj.modify_rules(args) |
| else: |
| |
| |
| firewallobj.modify_ip_rules(args) |
| |
| if i4['address'] and i4['address'] != args.address: |
| public.M('firewall_domain').where( |
| "id=?", (i4['id'], )).save('address', |
| (args.address, )) |
| |
| elif intersection_list and intersection_list != old_a_ip_dict[i3]: |
| for i5 in domain_name_dict[i3]: |
| if i5['address'] not in intersection_list: |
| is_reload = True |
| args = public.dict_obj() |
| args.id = i5['sid'] |
| args.domain = i5['domain'] |
| if args.domain.split('|')[1] != args.address: |
| args.domain = args.domain.split( |
| '|')[0] + '|' + args.address |
| |
| for i6 in a_ip: |
| if i6 not in intersection_list: |
| args.address = i6 |
| a_ip.remove(i6) |
| break |
| |
| args.types = i5['types'] |
| args.brief = i5['brief'] |
| args.sid = i5['id'] |
| |
| |
| if i5['port']: |
| args.protocol = i5['protocol'] |
| args.ports = i5['port'] |
| args.choose = 'point' |
| args.source = args.address |
| firewallobj.modify_rules(args) |
| else: |
| |
| |
| firewallobj.modify_ip_rules(args) |
| |
| if i5['address'] and i5['address'] != args.address: |
| public.M('firewall_domain').where( |
| "id=?", |
| (i4['id'], )).save('address', (args.address, )) |
|
|
| if is_reload: firewallobj.FirewallReload() |
|
|
|
|
| cron_shell() |
|
|