forked from ayeowch/bitnodes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresolve.py
252 lines (210 loc) · 7.72 KB
/
resolve.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# resolve.py - Resolves hostname and GeoIP data for each reachable node.
#
# Copyright (c) 2014 Addy Yeow Chin Heng <[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.
"""
Resolves hostname and GeoIP data for each reachable node.
"""
from decimal import Decimal
from gevent import socket
import gevent
import logging
import os
import pygeoip
import random
import redis
import redis.connection
import sys
from ConfigParser import ConfigParser
redis.connection.socket = socket
# Redis connection setup
REDIS_SOCKET = os.environ.get('REDIS_SOCKET', "/tmp/redis.sock")
REDIS_PASSWORD = os.environ.get('REDIS_PASSWORD', None)
REDIS_CONN = redis.StrictRedis(unix_socket_path=REDIS_SOCKET,
password=REDIS_PASSWORD)
# MaxMind databases
GEOIP4 = pygeoip.GeoIP("geoip/GeoLiteCity.dat", pygeoip.MMAP_CACHE)
GEOIP6 = pygeoip.GeoIP("geoip/GeoLiteCityv6.dat", pygeoip.MMAP_CACHE)
ASN4 = pygeoip.GeoIP("geoip/GeoIPASNum.dat", pygeoip.MMAP_CACHE)
ASN6 = pygeoip.GeoIP("geoip/GeoIPASNumv6.dat", pygeoip.MMAP_CACHE)
# Worker (resolver) status
RESOLVED = 2
FAILED = 1 # Failed socket.gethostbyaddr()
SETTINGS = {}
def resolve_nodes(nodes):
"""
Spawns workers to resolve hostname and GeoIP data for all nodes.
"""
addresses_1 = [] # Resolve hostname
addresses_2 = [] # Resolve GeoIP data
idx = 0
for node in nodes:
node = eval(node)
address = node[0]
if not REDIS_CONN.hexists('resolve:{}'.format(address), 'hostname'):
if idx < 1000:
addresses_1.append(address)
idx += 1
if not REDIS_CONN.hexists('resolve:{}'.format(address), 'geoip'):
addresses_2.append(address)
logging.info("Hostname: {} addresses".format(len(addresses_1)))
workers = [gevent.spawn(set_hostname, address) for address in addresses_1]
gevent.joinall(workers, timeout=15)
(resolved, failed, aborted) = status(workers)
logging.info("Hostname: {} resolved, {} failed, {} aborted".format(
resolved, failed, aborted))
logging.info("GeoIP: {} addresses".format(len(addresses_2)))
workers = [gevent.spawn(set_geoip, address) for address in addresses_2]
gevent.joinall(workers, timeout=15)
(resolved, failed, aborted) = status(workers)
logging.info("GeoIP: {} resolved, {} failed, {} aborted".format(
resolved, failed, aborted))
def status(workers):
"""
Summarizes resolve status for the spawned workers after a set timeout.
"""
resolved = 0
failed = 0
aborted = 0 # Timed out
for worker in workers:
if worker.value == RESOLVED:
resolved += 1
elif worker.value == FAILED:
failed += 1
else:
aborted += 1
return (resolved, failed, aborted)
def set_data(address, field, value):
"""
Stores data for an address in Redis with a randomize TTL randomize to
distribute expiring keys across multiple times.
"""
ttl = random.randint(SETTINGS['min_ttl'], SETTINGS['max_ttl'])
redis_pipe = REDIS_CONN.pipeline()
redis_pipe.hset('resolve:{}'.format(address), field, value)
redis_pipe.expire('resolve:{}'.format(address), ttl)
redis_pipe.execute()
def set_hostname(address):
"""
Caches hostname for the specified address in Redis.
"""
hostname = raw_hostname(address)
set_data(address, 'hostname', hostname)
if hostname != address:
return RESOLVED
return FAILED
def raw_hostname(address):
"""
Resolves hostname for the specified address using reverse DNS resolution.
"""
hostname = address
try:
hostname = socket.gethostbyaddr(address)[0]
except (socket.gaierror, socket.herror) as err:
logging.debug("{}: {}".format(address, err))
return hostname
def set_geoip(address):
"""
Caches GeoIP data for the specified address in Redis.
"""
geoip = raw_geoip(address)
set_data(address, 'geoip', geoip)
return RESOLVED
def raw_geoip(address):
"""
Resolves GeoIP data for the specified address using MaxMind databases.
"""
city = None
country = None
latitude = 0.0
longitude = 0.0
timezone = None
asn = None
org = None
geoip_record = None
prec = Decimal('.000001')
if ":" in address:
geoip_record = GEOIP6.record_by_addr(address)
else:
geoip_record = GEOIP4.record_by_addr(address)
if geoip_record:
city = geoip_record['city']
country = geoip_record['country_code']
latitude = float(Decimal(geoip_record['latitude']).quantize(prec))
longitude = float(Decimal(geoip_record['longitude']).quantize(prec))
timezone = geoip_record['time_zone']
asn_record = None
if ":" in address:
asn_record = ASN6.org_by_addr(address)
else:
asn_record = ASN4.org_by_addr(address)
if asn_record:
data = asn_record.split(" ", 1)
asn = data[0]
if len(data) > 1:
org = data[1]
return (city, country, latitude, longitude, timezone, asn, org)
def init_settings(argv):
"""
Populates SETTINGS with key-value pairs from configuration file.
"""
conf = ConfigParser()
conf.read(argv[1])
SETTINGS['logfile'] = conf.get('resolve', 'logfile')
SETTINGS['debug'] = conf.getboolean('resolve', 'debug')
SETTINGS['min_ttl'] = conf.getint('resolve', 'min_ttl')
SETTINGS['max_ttl'] = conf.getint('resolve', 'max_ttl')
def main(argv):
if len(argv) < 2 or not os.path.exists(argv[1]):
print("Usage: resolve.py [config]")
return 1
# Initialize global settings
init_settings(argv)
# Initialize logger
loglevel = logging.INFO
if SETTINGS['debug']:
loglevel = logging.DEBUG
logformat = ("%(asctime)s,%(msecs)05.1f %(levelname)s (%(funcName)s) "
"%(message)s")
logging.basicConfig(level=loglevel,
format=logformat,
filename=SETTINGS['logfile'],
filemode='w')
print("Writing output to {}, press CTRL+C to terminate..".format(
SETTINGS['logfile']))
pubsub = REDIS_CONN.pubsub()
pubsub.subscribe('snapshot')
for msg in pubsub.listen():
# 'snapshot' message is published by ping.py after establishing
# connection with nodes from a new snapshot.
if msg['channel'] == 'snapshot' and msg['type'] == 'message':
timestamp = int(msg['data'])
logging.info("Timestamp: {}".format(timestamp))
nodes = REDIS_CONN.smembers('opendata')
logging.info("Nodes: {}".format(len(nodes)))
resolve_nodes(nodes)
REDIS_CONN.publish('resolve', timestamp)
return 0
if __name__ == '__main__':
sys.exit(main(sys.argv))