Skip to content

Commit 301c2b1

Browse files
committed
contrib: makeseeds: Improve logging and filtering
- Change regular expression to cover recent versions, as well as subversions with custom uacomment, and improve readability. - Vary uptime requirements per network (onions are allowed to have less uptime, to make sure we get enough of them) - Add deduplication step (to allow simple concatentation of multiple seeds files). - Log of number of nodes (per network) after every step.
1 parent c3a8e09 commit 301c2b1

File tree

1 file changed

+44
-4
lines changed

1 file changed

+44
-4
lines changed

contrib/seeds/makeseeds.py

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,15 @@
3030
PATTERN_IPV4 = re.compile(r"^((\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})):(\d+)$")
3131
PATTERN_IPV6 = re.compile(r"^\[([0-9a-z:]+)\]:(\d+)$")
3232
PATTERN_ONION = re.compile(r"^([abcdefghijklmnopqrstuvwxyz234567]{16}\.onion):(\d+)$")
33-
PATTERN_AGENT = re.compile(r"^(/Satoshi:0.14.(0|1|2|99)/|/Satoshi:0.15.(0|1|2|99)|/Satoshi:0.16.(0|1|2|99)/)$")
33+
PATTERN_AGENT = re.compile(
34+
r"^/Satoshi:("
35+
r"0.14.(0|1|2|3|99)|"
36+
r"0.15.(0|1|2|99)|"
37+
r"0.16.(0|1|2|3|99)|"
38+
r"0.17.(0|0.1|1|2|99)|"
39+
r"0.18.(0|1|99)|"
40+
r"0.19.99"
41+
r")")
3442

3543
def parseline(line):
3644
sline = line.split()
@@ -99,6 +107,13 @@ def parseline(line):
99107
'sortkey': sortkey,
100108
}
101109

110+
def dedup(ips):
111+
'''deduplicate by address'''
112+
d = {}
113+
for ip in ips:
114+
d[ip['ip']] = ip
115+
return list(d.values())
116+
102117
def filtermultiport(ips):
103118
'''Filter out hosts with more nodes per IP'''
104119
hist = collections.defaultdict(list)
@@ -146,29 +161,54 @@ def filterbyasn(ips, max_per_asn, max_total):
146161
result.extend(ips_onion)
147162
return result
148163

164+
def ip_stats(ips):
165+
hist = collections.defaultdict(int)
166+
for ip in ips:
167+
if ip is not None:
168+
hist[ip['net']] += 1
169+
170+
return 'IPv4 %d, IPv6 %d, Onion %d' % (hist['ipv4'], hist['ipv6'], hist['onion'])
171+
149172
def main():
150173
lines = sys.stdin.readlines()
151174
ips = [parseline(line) for line in lines]
152175

153-
# Skip entries with valid address.
176+
print('Initial: %s' % (ip_stats(ips)), file=sys.stderr)
177+
# Skip entries with invalid address.
154178
ips = [ip for ip in ips if ip is not None]
179+
print('Skip entries with invalid address: %s' % (ip_stats(ips)), file=sys.stderr)
180+
# Skip duplicattes (in case multiple seeds files were concatenated)
181+
ips = dedup(ips)
182+
print('After removing duplicates: %s' % (ip_stats(ips)), file=sys.stderr)
155183
# Skip entries from suspicious hosts.
156184
ips = [ip for ip in ips if ip['ip'] not in SUSPICIOUS_HOSTS]
185+
print('Skip entries from suspicious hosts: %s' % (ip_stats(ips)), file=sys.stderr)
157186
# Enforce minimal number of blocks.
158187
ips = [ip for ip in ips if ip['blocks'] >= MIN_BLOCKS]
188+
print('Enforce minimal number of blocks: %s' % (ip_stats(ips)), file=sys.stderr)
159189
# Require service bit 1.
160190
ips = [ip for ip in ips if (ip['service'] & 1) == 1]
161-
# Require at least 50% 30-day uptime.
162-
ips = [ip for ip in ips if ip['uptime'] > 50]
191+
print('Require service bit 1: %s' % (ip_stats(ips)), file=sys.stderr)
192+
# Require at least 50% 30-day uptime for clearnet, 10% for onion.
193+
req_uptime = {
194+
'ipv4': 50,
195+
'ipv6': 50,
196+
'onion': 10,
197+
}
198+
ips = [ip for ip in ips if ip['uptime'] > req_uptime[ip['net']]]
199+
print('Require minimum uptime: %s' % (ip_stats(ips)), file=sys.stderr)
163200
# Require a known and recent user agent.
164201
ips = [ip for ip in ips if PATTERN_AGENT.match(ip['agent'])]
202+
print('Require a known and recent user agent: %s' % (ip_stats(ips)), file=sys.stderr)
165203
# Sort by availability (and use last success as tie breaker)
166204
ips.sort(key=lambda x: (x['uptime'], x['lastsuccess'], x['ip']), reverse=True)
167205
# Filter out hosts with multiple bitcoin ports, these are likely abusive
168206
ips = filtermultiport(ips)
207+
print('Filter out hosts with multiple bitcoin ports: %s' % (ip_stats(ips)), file=sys.stderr)
169208
# Look up ASNs and limit results, both per ASN and globally.
170209
ips = filterbyasn(ips, MAX_SEEDS_PER_ASN, NSEEDS)
171210
# Sort the results by IP address (for deterministic output).
211+
print('Look up ASNs and limit results, both per ASN and globally: %s' % (ip_stats(ips)), file=sys.stderr)
172212
ips.sort(key=lambda x: (x['net'], x['sortkey']))
173213

174214
for ip in ips:

0 commit comments

Comments
 (0)