-
Notifications
You must be signed in to change notification settings - Fork 308
/
seeder.py
executable file
·283 lines (256 loc) · 9.38 KB
/
seeder.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# seeder.py - Exports reachable nodes into DNS zone files for DNS seeder.
#
# Copyright (c) Addy Yeow <[email protected]>
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
Exports reachable nodes into DNS zone files for DNS seeder.
"""
import glob
import json
import logging
import operator
import os
import random
import sys
import time
from collections import defaultdict
from configparser import ConfigParser
from utils import new_redis_conn
CONF = {}
class Seeder(object):
"""
Implements seeding mechanic by exporting reachable nodes as A and AAAA
records into DNS zone files. A separate DNS server software is expected to
consume and serve the zone files to the public.
"""
def __init__(self, redis_conn=None):
self.redis_conn = redis_conn
self.dump = None
self.nodes = []
self.addresses = defaultdict(list)
self.now = 0
def export_nodes(self, dump):
"""
Exports nodes to generate A and AAAA records from the latest snapshot.
"""
self.now = int(time.time())
if dump != self.dump:
try:
self.nodes = json.loads(open(dump, "r").read())
except ValueError:
logging.warning("Write pending")
return
if len(self.nodes) == 0:
logging.warning(f"len(self.nodes): {len(self.nodes)}")
return
self.addresses = defaultdict(list)
for address, services in self.filter_nodes():
self.addresses[services].append(address)
self.dump = dump
self.save_zone_files()
def save_zone_files(self):
"""
Saves A and AAAA records in DNS zone files.
"""
zone_dir = os.path.dirname(CONF["zone_file"])
default_zone = os.path.basename(CONF["zone_file"])
# Default zone followed by services-based zones (max. 100 zones)
zone_keys = sorted(set([0] + list(self.addresses.keys())))[:100]
for i in zone_keys:
if i == 0:
# Default zone should include all nodes that have at least
# NODE_NETWORK service bit set.
zone_file = CONF["zone_file"]
addresses = []
for services, addrs in self.addresses.items():
if services & 1 == 1: # NODE_NETWORK
addresses.extend(addrs)
else:
zone_file = os.path.join(zone_dir, "x%x.%s" % (i, default_zone))
addresses = self.addresses[i]
records = self.get_records(addresses)
self.save_zone_file(zone_file, records)
def save_zone_file(self, zone_file, records):
"""
Saves filtered addresses in formatted records in the DNS zone file.
"""
logging.debug(f"Zone file: {zone_file}")
serial = str(self.now)
logging.debug(f"Serial: {serial}")
origin = os.path.basename(zone_file).replace("zone", "")
logging.debug(f"Origin: {origin}")
template = (
open(CONF["template"], "r")
.read()
.replace("1501826735", serial)
.replace("seed.bitnodes.io.", origin)
)
content = (
"".join(
[
template,
"\n",
records,
]
).strip()
+ "\n"
)
open(zone_file, "w").write(content)
def get_records(self, addresses):
"""
Returns addresses formatted in A, AAAA, TXT records for a zone file.
"""
a_records = []
aaaa_records = []
txt_records = []
for address in addresses:
if address.endswith(".onion"):
txt_records.append(f"@\tIN\tTXT\t{address}")
elif ":" in address:
aaaa_records.append(f"@\tIN\tAAAA\t{address}")
else:
a_records.append(f"@\tIN\tA\t{address}")
logging.debug(
f"A records: {len(a_records)}",
)
logging.debug(f"AAAA records: {len(aaaa_records)}")
logging.debug(f"TXT records: {len(txt_records)}")
random.shuffle(a_records)
random.shuffle(aaaa_records)
random.shuffle(txt_records)
records = "".join(
[
"\n".join(a_records[: CONF["a_records"]]),
"\n",
"\n".join(aaaa_records[: CONF["aaaa_records"]]),
"\n",
"\n".join(txt_records[: CONF["txt_records"]]),
]
)
return records
def filter_nodes(self):
"""
Returns nodes that satisfy the minimum requirements listed below:
1) Height must be at most 2 blocks away from the consensus height
2) Uptime must be equal or greater than the configured min. age
3) Max. one node per ASN
4) Uses default port
"""
consensus_height = self.get_consensus_height()
min_age = self.get_min_age()
asns = set()
for node in self.nodes:
address = node[0]
port = node[1]
age = self.now - node[4]
services = node[5]
height = node[6]
asn = node[13]
if port != CONF["port"] or asn is None or age < min_age:
continue
if consensus_height and abs(consensus_height - height) > 2:
continue
if asn in asns and not address.endswith(".onion"):
continue
yield address, services
asns.add(asn)
def get_consensus_height(self):
"""
Returns the most common height from Redis.
"""
height = self.redis_conn.get("height")
if height:
height = int(height)
logging.info(f"Consensus height: {height}")
return height
def get_min_age(self):
"""
Returns the minimum required uptime. If the oldest node cannot satisfy
the configured value, use a fallback value of max. 1 percent away from
the uptime of the oldest node.
"""
min_age = CONF["min_age"]
oldest = self.now - min(self.nodes, key=operator.itemgetter(4))[4]
logging.info(f"Longest uptime: {oldest}")
if oldest < min_age:
min_age = oldest - (0.01 * oldest) # Max. 1% newer than oldest
logging.info(f"Min. age: {min_age}")
return min_age
def cron():
"""
Periodically fetches latest snapshot to sample nodes for DNS zone files.
"""
redis_conn = new_redis_conn(db=CONF["db"])
seeder = Seeder(redis_conn=redis_conn)
while True:
time.sleep(5)
try:
dump = max(glob.iglob(f"{CONF['export_dir']}/*.json"))
except ValueError as err:
logging.warning(err)
continue
logging.info(f"Dump: {dump}")
seeder.export_nodes(dump)
def init_conf(config):
"""
Populates CONF with key-value pairs from configuration file.
"""
conf = ConfigParser()
conf.read(config)
CONF["logfile"] = conf.get("seeder", "logfile")
CONF["port"] = conf.getint("seeder", "port")
CONF["db"] = conf.getint("seeder", "db")
CONF["debug"] = conf.getboolean("seeder", "debug")
CONF["export_dir"] = conf.get("seeder", "export_dir")
CONF["min_age"] = conf.getint("seeder", "min_age")
CONF["zone_file"] = conf.get("seeder", "zone_file")
CONF["template"] = conf.get("seeder", "template")
CONF["a_records"] = conf.getint("seeder", "a_records")
CONF["aaaa_records"] = conf.getint("seeder", "aaaa_records")
CONF["txt_records"] = conf.getint("seeder", "txt_records")
zone_dir = os.path.dirname(CONF["zone_file"])
if not os.path.exists(zone_dir):
os.makedirs(zone_dir)
def main(argv):
if len(argv) < 2 or not os.path.exists(argv[1]):
print("Usage: seeder.py [config]")
return 1
# Initialize global conf.
init_conf(argv[1])
# Initialize logger.
loglevel = logging.INFO
if CONF["debug"]:
loglevel = logging.DEBUG
logformat = (
"[%(process)d] %(asctime)s,%(msecs)05.1f %(levelname)s "
"(%(funcName)s) %(message)s"
)
logging.basicConfig(
level=loglevel, format=logformat, filename=CONF["logfile"], filemode="w"
)
print(f"Log: {CONF['logfile']}, press CTRL+C to terminate..")
cron()
return 0
if __name__ == "__main__":
sys.exit(main(sys.argv))