forked from 217heidai/adblockfilters
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresolver.py
81 lines (74 loc) · 2.97 KB
/
resolver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import os
import re
class Resolver(object):
def __init__(self, fileName):
self.__fileName = fileName
def Resolve(self):
def match(pattern, string):
matchObj = re.match(pattern, string)
if matchObj:
#print(matchObj.group())
return True
return False
blockList = []
unblockList = []
if not os.path.exists(self.__fileName):
return blockList,unblockList
with open(self.__fileName, "r") as f:
for line in f:
# 去掉换行符
line = line.replace('\r', '').replace('\n', '').strip()
# 去掉空行
if len(line) < 1:
continue
# 跳过注释! #
if match('^!.*', line) or match('^#.*', line):
continue
# 跳过注释[]
if line.find('[') == 0 and line.rfind(']')== len(line)-1:
continue
# ||
if match('^\|\|.*', line):
#print(line)
blockList.append(line)
continue
# /REGEX/
if match('^/.*', line):
#print(line)
blockList.append(line)
continue
# @@
if match('^@@.*', line):
#print(line)
unblockList.append(line)
continue
# @ 注释
if match('^@.*', line):
#print(line)
continue
# host 模式
if line.find('0.0.0.0')==0 or line.find('127.0.0.1') == 0:
row = line.split(' ')
row = list(map(lambda x: x.strip(), row)) # 字段去空格
for i in range(len(row)-1):
if len(row[i]) == 0:
row.pop(i)
domain = row[1]
if domain in ['localhost', 'localhost.localdomain', 'local', '0.0.0.0']:
continue
domain = '||%s^'%(domain)
blockList.append(domain)
continue
# 过滤无效的hosts
if line.replace(' ', '') in ['::1localhost','255.255.255.255broadcasthost','::1ip6-localhost','::1ip6-loopback','fe80::1%lo0localhost','ff00::0ip6-localnet','ff00::0ip6-mcastprefix','ff02::1ip6-allnodes','ff02::2ip6-allrouters','ff02::3ip6-allhosts','255.255.255.255\tbroadcasthost']:
continue
blockList.append(line)
pass
return blockList,unblockList
if __name__ == '__main__':
pwd = os.getcwd()
file = pwd + '/rules/Hblock_Filters.txt'
resolver = Resolver(file)
blockList, unblockList = resolver.Resolve()
print('blockList: %s'%(len(blockList)))
print('unblockList: %s'%(len(unblockList)))