-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
213 lines (165 loc) · 6.95 KB
/
main.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/usr/bin/env python3
import datetime
import itertools
import json
import logging
import os
import re
import crontab
import requests
import bash
LOG_FILE = "/var/log/rpz-server"
BIND_DIR = "/etc/bind/"
NAMED_CONF = "/etc/bind/named.conf"
PROVIDERS = "resources/providers.json"
HEADER = "resources/rpz_header"
CUSTOM_NAMED_CONF = "resources/named.conf"
MASTER_ZONE_TEMPLATE = "resources/master_zone_template"
def main() -> None:
configure_logs()
if not check_cron():
logging.info("Cron job set.")
with open(HEADER) as file:
header = file.read().replace("{SERIAL}", datetime.datetime.now().strftime("%Y%m%d%H"))
logging.info("Retrieving new domains.")
domain_categories, ip_range_lists = providers_from_json(PROVIDERS)
domains_per_category = get_domains(domain_categories)
logging.info("Generating domain zones.")
domain_zones = generate_domain_zones(domain_categories, domains_per_category, header)
logging.info("Generating ip zone.")
ip_zone = generate_ip_zone(ip_range_lists, header)
zones = domain_zones + ip_zone
logging.info("Building named configuration.")
build_named_conf(zones)
logging.info("Loading new configuration.")
load()
logging.info("rpz-server script finished.")
exit(0)
def configure_logs() -> None:
if not os.path.isfile(LOG_FILE):
os.mknod(LOG_FILE)
# noinspection PyArgumentList
logging.basicConfig(
datefmt="%Y-%m-%d %H:%M:%S %Z",
filename=LOG_FILE,
filemode="a",
format="{asctime} - {levelname:8}: {message}",
level=logging.INFO,
style="{"
)
logging.info("rpz-server script started.")
def check_cron() -> bool:
cron = crontab.CronTab(user="root")
for command in cron.find_comment("DNS-RPZ administration"):
return True
else:
# Set this script as cron job every day at 04:00
own_path = os.path.realpath(__file__)
dirname = os.path.dirname(own_path)
filename = os.path.basename(own_path)
python_path = bash.call("which python3").strip()
job = cron.new(command="cd {0} && {1} {2} >> {3}".format(dirname, python_path, filename, LOG_FILE + ".cron"),
comment="DNS-RPZ administration")
job.minute.on(0)
job.hour.every(2)
cron.write()
return False
def providers_from_json(filename: str) -> (dict, list):
with open(filename) as file:
content = json.load(file)
categories = content["domain_categories"]
ip_range_lists = content["ip_range_lists"]
return categories, ip_range_lists
def get_domains(domain_categories: dict) -> dict:
domains_per_category = {}
for category_id, data in domain_categories.items():
domains_per_category[category_id] = set()
for provider in data["providers"]:
domains = requests.get(provider).iter_lines()
for domain in domains:
domain, ok = _format_domain(domain)
if ok:
domains_per_category[category_id].add(domain)
return domains_per_category
def generate_domain_zones(domain_categories: dict, domains_per_category: dict, header: str) -> (str, str):
zones = ""
max_category_id = int(max(list(domain_categories)))
with open(MASTER_ZONE_TEMPLATE) as file:
master_zone_template = file.read()
for category_combination in _category_combinations(max_category_id):
combination_id = sum(map(lambda x: 2 ** x, category_combination))
description_list = map(lambda x: domain_categories[str(x)]["description"], category_combination)
zone_name = "db.combination.{:02d}".format(combination_id)
filename = BIND_DIR + zone_name
zones += master_zone_template.replace("{NAME}", zone_name).replace("{FILE}", filename)
combination_header = header.replace("{ZONE}", "block.{0}: Combination of {1}".format(combination_id, ", ".join(
description_list)))
with open(filename, "w") as file:
file.write(combination_header)
for category_id in category_combination:
file.write("\n".join(domains_per_category[str(category_id)]) + "\n")
return zones
def generate_ip_zone(ip_range_lists, header) -> (str, str):
ip_range_header = header.replace("{ZONE}", "block.ip_range")
zone_name = "db.ip"
filename = BIND_DIR + zone_name
with open(filename, "w") as file:
file.write(ip_range_header)
for ip_range_list in ip_range_lists:
ip_ranges = requests.get(ip_range_list).iter_lines()
format_ip = _format_ipv6 if "v6" in ip_range_list else _format_ipv4 # Pick correct format function
for ip_range in ip_ranges:
ip_range, ok = format_ip(ip_range)
if ok:
file.write(ip_range + "\n")
with open(MASTER_ZONE_TEMPLATE) as file:
master_zone_template = file.read()
return master_zone_template.replace("{NAME}", zone_name).replace("{FILE}", filename)
def build_named_conf(zones: str) -> None:
with open(CUSTOM_NAMED_CONF) as file:
custom_named_conf = file.read()
custom_named_conf = custom_named_conf.replace("{ZONES}", zones)
with open(NAMED_CONF, "w") as named_conf:
named_conf.write(custom_named_conf)
def load() -> None:
output = bash.call("systemctl is-active bind9").strip()
if output == "active":
# Reload via rndc
print(bash.call("sudo rndc reload"))
else:
# Start via systemctl
print(bash.call("sudo systemctl start bind9"))
def _category_combinations(n: int) -> list:
categories = range(n + 1)
combinations = list(
itertools.chain.from_iterable(itertools.combinations(categories, r) for r in range(len(categories) + 1)))
return sorted(combinations, key=sum)[1:]
def _format_domain(domain: bytes) -> (str, bool):
domain = str(domain, "utf-8").strip()
if domain.startswith("#") or len(domain) == 0:
return domain, False
parts = domain.split()
if len(parts) >= 2:
if parts[0].startswith("0.0.0.0") or parts[0].startswith("127.0.0.1"):
domain = parts[1]
else:
return domain, False
domain = domain.split("#")[0] # Before comment
domain += "\tCNAME . "
return domain, True
def _format_ipv4(ip_range: bytes) -> (str, bool):
ip_range = str(ip_range, "utf-8").strip()
if ip_range.startswith(";"):
return ip_range, False
o1, o2, o3, o4, m = re.split(r"(\d+)\.(\d+)\.(\d+)\.(\d+)/(\d+)", ip_range)[1:6]
return "{0}.{1}.{2}.{3}.{4}.rpz-ip.\tCNAME . ".format(m, o4, o3, o2, o1), True
def _format_ipv6(ip_range: bytes) -> (str, bool):
ip_range = str(ip_range, "utf-8")
if ip_range.startswith(";"):
return ip_range, False
ip_range = ip_range.split(";")[0].strip()
ip_range = ip_range.replace("::", ":zz:").replace(":/", ":").replace("/", ":").split(":")
ip_range.reverse()
return "{0}.rpz-ip\tCNAME . ".format(".".join(ip_range)), True
if __name__ == '__main__':
main()