forked from ayeowch/bitnodes
-
Notifications
You must be signed in to change notification settings - Fork 1
/
resolve.py
281 lines (235 loc) · 8.7 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
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# resolve.py - Resolves hostname and GeoIP data for each reachable node.
#
# Copyright (c) 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 gevent import monkey
monkey.patch_all()
import geoip2.database
import gevent
import gevent.pool
import logging
import os
import redis
import redis.connection
import socket
import sys
import time
from binascii import hexlify, unhexlify
from collections import defaultdict
from ConfigParser import ConfigParser
from decimal import Decimal
from geoip2.errors import AddressNotFoundError
from utils import new_redis_conn
redis.connection.socket = gevent.socket
REDIS_CONN = None
CONF = {}
# MaxMind databases
GEOIP_CITY = geoip2.database.Reader("geoip/GeoLite2-City.mmdb")
GEOIP_COUNTRY = geoip2.database.Reader("geoip/GeoLite2-Country.mmdb")
ASN = geoip2.database.Reader("geoip/GeoLite2-ASN.mmdb")
class Resolve(object):
"""
Implements hostname and GeoIP resolver.
"""
def __init__(self, addresses):
self.addresses = addresses
self.resolved = defaultdict(dict)
self.redis_pipe = REDIS_CONN.pipeline()
def resolve_addresses(self):
"""
Resolves hostname for up to 1000 new addresses and GeoIP data for all
addresses.
"""
start = time.time()
idx = 0
for address in self.addresses:
key = 'resolve:{}'.format(address)
ttl = REDIS_CONN.ttl(key)
expiring = False
if ttl < 0.1 * CONF['ttl']: # Less than 10% of initial TTL
expiring = True
if expiring and idx < 1000 and not address.endswith(".onion"):
self.resolved['hostname'][address] = None
idx += 1
self.resolved['geoip'][address] = None
logging.info("GeoIP: %d", len(self.resolved['geoip']))
self.resolve_geoip()
logging.info("Hostname: %d", len(self.resolved['hostname']))
self.resolve_hostname()
self.cache_resolved()
end = time.time()
elapsed = end - start
logging.info("Elapsed: %d", elapsed)
def cache_resolved(self):
"""
Caches resolved addresses in Redis.
"""
resolved = 0
for address, geoip in self.resolved['geoip'].iteritems():
if geoip[1] or geoip[5]:
resolved += 1 # country/asn is set
key = 'resolve:{}'.format(address)
self.redis_pipe.hset(key, 'geoip', geoip)
logging.debug("%s geoip: %s", key, geoip)
logging.info("GeoIP: %d resolved", resolved)
resolved = 0
for address, hostname in self.resolved['hostname'].iteritems():
if hostname != address:
resolved += 1
key = 'resolve:{}'.format(address)
self.redis_pipe.hset(key, 'hostname', hostname)
self.redis_pipe.expire(key, CONF['ttl'])
logging.debug("%s hostname: %s", key, hostname)
logging.info("Hostname: %d resolved", resolved)
self.redis_pipe.execute()
def resolve_geoip(self):
"""
Resolves GeoIP data for the unresolved addresses.
"""
for address in self.resolved['geoip']:
geoip = raw_geoip(address)
self.resolved['geoip'][address] = geoip
def resolve_hostname(self):
"""
Concurrently resolves hostname for the unresolved addresses.
"""
pool = gevent.pool.Pool(len(self.resolved['hostname']))
with gevent.Timeout(15, False):
for address in self.resolved['hostname']:
pool.spawn(self.set_hostname, address)
pool.join()
def set_hostname(self, address):
"""
Resolves hostname for the specified address.
"""
hostname = raw_hostname(address)
self.resolved['hostname'][address] = hostname
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("%s: %s", address, err)
return hostname
def raw_geoip(address):
"""
Resolves GeoIP data for the specified address using MaxMind databases.
"""
country = None
city = None
lat = 0.0
lng = 0.0
timezone = None
asn = None
org = None
prec = Decimal('.000001')
if not address.endswith(".onion"):
try:
gcountry = GEOIP_COUNTRY.country(address)
except AddressNotFoundError:
pass
else:
country = gcountry.country.iso_code
try:
gcity = GEOIP_CITY.city(address)
except AddressNotFoundError:
pass
else:
city = gcity.city.name
if gcity.location.latitude is not None and \
gcity.location.longitude is not None:
lat = float(Decimal(gcity.location.latitude).quantize(prec))
lng = float(Decimal(gcity.location.longitude).quantize(prec))
timezone = gcity.location.time_zone
if address.endswith(".onion"):
asn = "TOR"
org = "Tor network"
else:
try:
asn_record = ASN.asn(address)
except AddressNotFoundError:
pass
else:
asn = 'AS{}'.format(asn_record.autonomous_system_number)
org = asn_record.autonomous_system_organization
return (city, country, lat, lng, timezone, asn, org)
def init_conf(argv):
"""
Populates CONF with key-value pairs from configuration file.
"""
conf = ConfigParser()
conf.read(argv[1])
CONF['logfile'] = conf.get('resolve', 'logfile')
CONF['magic_number'] = unhexlify(conf.get('resolve', 'magic_number'))
CONF['db'] = conf.getint('resolve', 'db')
CONF['debug'] = conf.getboolean('resolve', 'debug')
CONF['ttl'] = conf.getint('resolve', 'ttl')
def main(argv):
if len(argv) < 2 or not os.path.exists(argv[1]):
print("Usage: resolve.py [config]")
return 1
# Initialize global conf
init_conf(argv)
# Initialize logger
loglevel = logging.INFO
if CONF['debug']:
loglevel = logging.DEBUG
logformat = ("%(asctime)s,%(msecs)05.1f %(levelname)s (%(funcName)s) "
"%(message)s")
logging.basicConfig(level=loglevel,
format=logformat,
filename=CONF['logfile'],
filemode='w')
print("Log: {}, press CTRL+C to terminate..".format(CONF['logfile']))
global REDIS_CONN
REDIS_CONN = new_redis_conn(db=CONF['db'])
subscribe_key = 'snapshot:{}'.format(hexlify(CONF['magic_number']))
publish_key = 'resolve:{}'.format(hexlify(CONF['magic_number']))
pubsub = REDIS_CONN.pubsub()
pubsub.subscribe(subscribe_key)
while True:
msg = pubsub.get_message()
if msg is None:
time.sleep(0.001) # 1 ms artificial intrinsic latency.
continue
# 'snapshot' message is published by ping.py after establishing
# connection with nodes from a new snapshot.
if msg['channel'] == subscribe_key and msg['type'] == 'message':
timestamp = int(msg['data'])
logging.info("Timestamp: %d", timestamp)
nodes = REDIS_CONN.smembers('opendata')
logging.info("Nodes: %d", len(nodes))
addresses = set([eval(node)[0] for node in nodes])
resolve = Resolve(addresses=addresses)
resolve.resolve_addresses()
REDIS_CONN.publish(publish_key, timestamp)
return 0
if __name__ == '__main__':
sys.exit(main(sys.argv))