forked from henrypp/simplewall
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild_profile_blocklist.py
92 lines (66 loc) · 2.69 KB
/
build_profile_blocklist.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
82
83
84
85
86
87
88
89
90
91
92
import os
import math
import re
import xml.dom.minidom
def natural_sort (list, key=lambda s:s):
def get_alphanum_key_func (key):
convert = lambda text: int (text) if text.isdigit () else text
return lambda s: [convert (c) for c in re.split ('([0-9]+)', key (s))]
list.sort (key=get_alphanum_key_func (key))
CURRENT_DIRECTORY = os.path.dirname (os.path.abspath (__file__))
RULES_DIR = os.path.join (CURRENT_DIRECTORY, '..', '3rd-party', 'WindowsSpyBlocker', 'data', 'firewall')
RULES_FILE = os.path.join (CURRENT_DIRECTORY, 'bin', 'profile_internal.xml')
if not os.path.isdir (RULES_DIR):
raise Exception ('Rules directory not found: ' + RULES_DIR)
if not os.path.isfile (RULES_FILE):
raise Exception ('Profile internal not found: ' + RULES_FILE)
# Open profile xml
with open (RULES_FILE, 'r', newline='') as f:
data = f.read ()
if not data:
raise Exception ('File reading failure: ' + RULES_FILE)
# toprettyxml() hack
data = data.replace ('\n', '')
data = data.replace ('\t', '')
xml_doc = xml.dom.minidom.parseString (data)
xml_root = xml_doc.getElementsByTagName ("root")
f.close ()
# Store timestamp
timestamp = int (xml_root[0].getAttribute ("timestamp"))
# Cleanup xml
for node in xml_doc.getElementsByTagName ("rules_blocklist"):
parent = node.parentNode
parent.removeChild (node)
xml_root[0].appendChild (xml_doc.createElement ("rules_blocklist"))
xml_section = xml_doc.getElementsByTagName ("rules_blocklist")
if not xml_section:
raise Exception ('Parse xml failure.')
# Enumerate Windows Spy Blocker spy/extra and update rules
for file_name in os.listdir (RULES_DIR):
module_name = os.path.splitext (file_name)[0]
module_path = os.path.join (RULES_DIR, file_name)
print ('Parsing ' + module_name + '...')
lastmod = int (os.path.getmtime (module_path));
if lastmod > timestamp:
timestamp = lastmod;
with open (module_path, 'r') as f:
rows = f.readlines ()
natural_sort (rows)
f.close ()
for string in rows:
line = string.strip ("\n\r\t ")
if line and not line.startswith ("#") and not line.startswith ("<") and not line.startswith (">") and not line.startswith ("="):
new_item = xml_doc.createElement ("item")
new_item.setAttribute ("name", module_name + "_" + line)
new_item.setAttribute ("rule", line)
xml_section[0].appendChild (new_item)
# Set new rule timestamp
xml_root[0].setAttribute ("timestamp", str (timestamp))
print ('\nBlocklist timetamp ' + str (timestamp))
# Save updated profile xml
data = xml_doc.toprettyxml (indent="\t", newl="\n")
if data:
data = data.replace ('/>', ' />')
with open (RULES_FILE, "w", newline='') as f:
f.write (data)
f.close ()