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A-Fa-f]{1,4}|(?:(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))|(?:(?:[0-9A-Fa-f]{1,4}:){,3}[0-9A-Fa-f]{1,4})?::[0-9A-Fa-f]{1,4}:(?:[0-9A-Fa-f]{1,4}:[0-9A-Fa-f]{1,4}|(?:(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))|(?:(?:[0-9A-Fa-f]{1,4}:){,4}[0-9A-Fa-f]{1,4})?::(?:[0-9A-Fa-f]{1,4}:[0-9A-Fa-f]{1,4}|(?:(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))|(?:(?:[0-9A-Fa-f]{1,4}:){,5}[0-9A-Fa-f]{1,4})?::[0-9A-Fa-f]{1,4}|(?:(?:[0-9A-Fa-f]{1,4}:){,6}[0-9A-Fa-f]{1,4})?::)'
|
2021-10-02 09:17:41 +00:00
|
|
|
|
2021-08-02 15:25:59 +00:00
|
|
|
def clear_old_files(incoming):
|
2021-08-02 11:50:11 +00:00
|
|
|
try:
|
2021-08-02 14:00:05 +00:00
|
|
|
os.remove(incoming)
|
2021-08-02 11:50:11 +00:00
|
|
|
except OSError:
|
|
|
|
pass
|
2021-08-02 13:47:46 +00:00
|
|
|
|
2021-08-02 15:25:59 +00:00
|
|
|
def download_filters(url,incoming):
|
2021-07-28 14:03:52 +00:00
|
|
|
print("downloading: ",url)
|
|
|
|
|
|
|
|
get = requests.get(url)
|
|
|
|
if get.status_code == requests.codes.ok:
|
2021-08-02 14:00:05 +00:00
|
|
|
with open(incoming, 'ab') as f:
|
2021-07-28 14:03:52 +00:00
|
|
|
for data in get:
|
|
|
|
f.write(data)
|
|
|
|
return url
|
|
|
|
|
2021-10-01 08:01:29 +00:00
|
|
|
def download_group_filters(multi_url,incoming):
|
|
|
|
for url in multi_url:
|
|
|
|
print("downloading: ",url)
|
|
|
|
|
|
|
|
get = requests.get(url)
|
|
|
|
if get.status_code == requests.codes.ok:
|
|
|
|
with open(incoming, 'ab') as f:
|
|
|
|
for data in get:
|
|
|
|
f.write(data)
|
|
|
|
|
2021-07-28 14:03:52 +00:00
|
|
|
def filtering(filters_welcome):
|
2021-11-15 10:07:48 +00:00
|
|
|
unwanted = ['#',';','@','$',' NS',' NS','@@||','!','local-data:','-',' IN']
|
2021-07-28 14:03:52 +00:00
|
|
|
print("filtering . . .")
|
|
|
|
with open(filters_welcome, 'r') as f:
|
|
|
|
lines = f.read().splitlines()
|
|
|
|
with open(filters_welcome, 'w') as f:
|
|
|
|
for line in lines:
|
2021-08-11 05:21:31 +00:00
|
|
|
if not line.startswith((tuple(unwanted))) and line.strip():
|
2021-08-03 11:56:08 +00:00
|
|
|
f.write('\n'.join([line + '\n']))
|
2021-09-29 13:35:07 +00:00
|
|
|
print("Simple Filtering Completed!")
|
2021-09-30 05:40:43 +00:00
|
|
|
f.close()
|
2021-10-02 03:57:12 +00:00
|
|
|
|
2021-10-02 03:53:52 +00:00
|
|
|
def IP_URL_FILTERING(filters_welcome):
|
2021-09-30 05:40:43 +00:00
|
|
|
with open(filters_welcome) as f:
|
|
|
|
file = f.read().split('\n')
|
|
|
|
for i in range(len(file)):
|
|
|
|
file[i] = re.sub('/..+\Z', '', file[i])
|
|
|
|
file[i] = re.sub(':(\d\d\d\d\d|\d\d\d\d|\d\d\d|\d\d|\d)', '', file[i])
|
2021-10-02 09:33:55 +00:00
|
|
|
file[i] = re.sub('\A'+str(IP4)+'\Z', '', file[i])
|
|
|
|
file[i] = re.sub('\A(\[|)'+str(IP6)+'(\]|)\Z', '', file[i])
|
2021-10-02 09:50:33 +00:00
|
|
|
file[i] = re.sub('\A\[..*\]\Z', '', file[i])
|
2021-09-30 05:40:43 +00:00
|
|
|
with open(filters_welcome, 'w') as f1:
|
|
|
|
f1.writelines(["%s\n" % item for item in file])
|
|
|
|
print("Ip Address and Url Path filtering Operation Completed!")
|
|
|
|
f.close()
|
2021-07-28 14:03:52 +00:00
|
|
|
|
|
|
|
def filteringcon(filters_regex_one):
|
|
|
|
print("filtering . . .")
|
|
|
|
with open(filters_regex_one) as f:
|
|
|
|
file = f.read().split('\n')
|
|
|
|
for i in range(len(file)):
|
2021-08-08 06:44:45 +00:00
|
|
|
file[i] = re.sub('\s\s+#.*', '', file[i])
|
2021-07-28 14:03:52 +00:00
|
|
|
file[i] = re.sub(' CNAME .$', '', file[i])
|
|
|
|
file[i] = re.sub(' CNAME . $', '', file[i])
|
2021-08-12 04:13:21 +00:00
|
|
|
file[i] = re.sub('^\*.', '', file[i])
|
2021-09-30 05:40:43 +00:00
|
|
|
file[i] = re.sub('^\*', '', file[i])
|
2021-08-14 15:04:57 +00:00
|
|
|
file[i] = re.sub('\s\s+', ' ', file[i])
|
|
|
|
file[i] = re.sub('#..*', '', file[i])
|
|
|
|
file[i] = re.sub('CNAME . ;..*', '', file[i])
|
|
|
|
file[i] = re.sub(';..*', '', file[i])
|
2021-09-29 13:35:07 +00:00
|
|
|
file[i] = re.sub('\A^\.', '', file[i])
|
|
|
|
file[i] = re.sub('^\s+|\s+$', '', file[i])
|
|
|
|
file[i] = re.sub(' $', '', file[i])
|
2021-07-28 14:03:52 +00:00
|
|
|
with open(filters_regex_one, 'w') as f1:
|
|
|
|
f1.writelines(["%s\n" % item for item in file])
|
2021-09-29 13:35:07 +00:00
|
|
|
print("RPZ filtering operation completed!")
|
2021-07-28 14:03:52 +00:00
|
|
|
f.close()
|
2021-08-14 14:50:43 +00:00
|
|
|
|
|
|
|
with open(filters_regex_one) as f:
|
|
|
|
file = f.read().split('\n')
|
|
|
|
for i in range(len(file)):
|
2022-01-15 08:08:20 +00:00
|
|
|
file[i] = re.sub('(@@\|\|..*)|(\|\|..*(\/|\*(\-|\&|banner|..))..*)|(^\|http)|(^(_|\*|&|\-|\/|\.|:|@@|\?|\=|\;|\,|\$|\~)..*)|(..*(#|\$|\*)..*)|(^..*\$(app=|removeparam=|popup)..*)(\$..*)', '', file[i])
|
2021-09-29 13:35:07 +00:00
|
|
|
file[i] = re.sub('0\.0\.0\.0 0\.0\.0\.0\Z', '', file[i])
|
2021-10-02 09:33:55 +00:00
|
|
|
file[i] = re.sub('\A'+str(IP4)+' ', '', file[i])
|
|
|
|
file[i] = re.sub('\A'+str(IP6)+' ', '', file[i])
|
2021-08-14 14:50:43 +00:00
|
|
|
file[i] = re.sub('\A0 ', '', file[i])
|
|
|
|
file[i] = re.sub('\A:: ', '', file[i])
|
2021-09-29 13:35:07 +00:00
|
|
|
file[i] = re.sub('\A::1 ', '', file[i])
|
|
|
|
file[i] = re.sub('^\A\|\|', '', file[i])
|
|
|
|
file[i] = re.sub('\^$\Z', '', file[i])
|
|
|
|
file[i] = re.sub('^\|', '', file[i])
|
2021-08-14 14:50:43 +00:00
|
|
|
file[i] = re.sub(r'#', ';', file[i])
|
|
|
|
with open(filters_regex_one, 'w') as f1:
|
|
|
|
f1.writelines(["%s\n" % item for item in file])
|
2021-09-29 13:35:07 +00:00
|
|
|
print('Host and Adblock filtering Operation Completed!')
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
with open(filters_regex_one) as f:
|
|
|
|
file = f.read().split('\n')
|
|
|
|
for i in range(len(file)):
|
|
|
|
file[i] = re.sub('\Alocal-zone: "', '', file[i])
|
|
|
|
file[i] = re.sub('static\Z', '', file[i])
|
|
|
|
file[i] = re.sub('always_null\Z', '', file[i])
|
|
|
|
file[i] = re.sub('always_nxdomain\Z', '', file[i])
|
|
|
|
file[i] = re.sub('always_refuse\Z', '', file[i])
|
|
|
|
file[i] = re.sub('redirect\Z', '', file[i])
|
|
|
|
file[i] = re.sub('" $', '', file[i])
|
|
|
|
with open(filters_regex_one, 'w') as f1:
|
|
|
|
f1.writelines(["%s\n" % item for item in file])
|
|
|
|
print('unbound filtering Operation Completed!')
|
2021-08-14 14:50:43 +00:00
|
|
|
f.close()
|
2021-08-02 08:15:42 +00:00
|
|
|
|
2021-09-29 13:35:07 +00:00
|
|
|
with open(filters_regex_one) as f:
|
|
|
|
file = f.read().split('\n')
|
|
|
|
for i in range(len(file)):
|
|
|
|
file[i] = re.sub('\Aserver=/', '', file[i])
|
|
|
|
file[i] = re.sub('\Aaddress=/', '', file[i])
|
2021-10-02 09:33:55 +00:00
|
|
|
file[i] = re.sub(str(IP4)+'\Z', '', file[i])
|
|
|
|
file[i] = re.sub(str(IP6)+'\Z', '', file[i])
|
2021-09-29 13:35:07 +00:00
|
|
|
file[i] = re.sub('127\.0\.0\.1\Z', '', file[i])
|
|
|
|
file[i] = re.sub('0\.0\.0\.0\Z', '', file[i])
|
|
|
|
file[i] = re.sub('::\Z', '', file[i])
|
2021-09-29 17:21:32 +00:00
|
|
|
file[i] = re.sub('::1\Z', '', file[i])
|
2021-09-29 13:35:07 +00:00
|
|
|
file[i] = re.sub('/\Z', '', file[i])
|
|
|
|
file[i] = re.sub('/$', '', file[i])
|
|
|
|
with open(filters_regex_one, 'w') as f1:
|
|
|
|
f1.writelines(["%s\n" % item for item in file])
|
|
|
|
print('dnsmasq filtering Operation Completed!')
|
|
|
|
f.close()
|
|
|
|
|
2021-08-12 06:45:17 +00:00
|
|
|
remove_words = ['localhost','localhost.localdomain','local','broadcasthost','loopback','ip6-localnet','ip6-mcastprefix','ip6-allnodes','ip6-allrouters','ip6-allhosts','ip6-loopback',' CNAME rpz-passthru.']
|
2021-08-08 06:44:45 +00:00
|
|
|
|
2021-08-08 04:24:35 +00:00
|
|
|
with open(filters_regex_one, 'r') as f:
|
|
|
|
lines = f.read().splitlines()
|
|
|
|
with open(filters_regex_one, 'w') as f:
|
|
|
|
for line in lines:
|
2021-08-08 06:44:45 +00:00
|
|
|
if not line.endswith((tuple(remove_words))):
|
2021-08-08 04:24:35 +00:00
|
|
|
f.write('\n'.join([line + '\n']))
|
2021-08-18 05:40:34 +00:00
|
|
|
f.close()
|
2021-08-08 04:24:35 +00:00
|
|
|
|
2021-07-28 14:03:52 +00:00
|
|
|
def killingdup(duplicated_file):
|
|
|
|
print('Getting rid of duplicated line')
|
|
|
|
with open(duplicated_file, 'r') as f:
|
|
|
|
lines = set(f.readlines())
|
|
|
|
with open(duplicated_file, 'w') as f:
|
2021-08-08 06:44:45 +00:00
|
|
|
f.writelines(set(lines))
|
2021-07-28 14:03:52 +00:00
|
|
|
print("++ successful!")
|
|
|
|
f.close()
|
|
|
|
|
2021-08-02 15:37:53 +00:00
|
|
|
def excluded(excluded ,incoming):
|
2021-11-15 10:07:48 +00:00
|
|
|
exline = [';','$','@',' IN','#']
|
2021-08-02 15:30:49 +00:00
|
|
|
with open(excluded ,'r') as f:
|
|
|
|
exclude = f.read().split()
|
|
|
|
with open(incoming ,'r') as f:
|
|
|
|
lines = f.read().splitlines() # read lines
|
2021-08-02 15:34:51 +00:00
|
|
|
with open(incoming ,'w') as f:
|
2021-08-02 15:30:49 +00:00
|
|
|
for line in lines:
|
2021-11-15 10:07:48 +00:00
|
|
|
if line.strip() and not line in exclude:
|
2021-08-02 15:49:36 +00:00
|
|
|
f.write('\n'.join([line + '\n']))
|
2021-11-15 10:25:53 +00:00
|
|
|
else:
|
2021-08-02 15:30:49 +00:00
|
|
|
f.write('\n'.join([line + '\n']))
|
|
|
|
|
2021-08-08 06:44:45 +00:00
|
|
|
def blankremover(incoming):
|
|
|
|
with open(incoming ,'r') as f:
|
|
|
|
lines = f.read().split()
|
|
|
|
with open(incoming ,'w') as f:
|
|
|
|
for line in lines:
|
|
|
|
if line.strip():
|
|
|
|
f.write('\n'.join([line + '\n']))
|
|
|
|
|
2021-08-03 11:56:08 +00:00
|
|
|
def sort(incoming):
|
|
|
|
with open(incoming, 'r') as f:
|
|
|
|
lines = sorted(f.readlines())
|
|
|
|
with open(incoming, 'w') as f:
|
|
|
|
for line in lines:
|
2021-08-05 13:35:22 +00:00
|
|
|
f.write(line)
|