forked from ayeowch/bitnodes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ping.py
291 lines (234 loc) · 8.85 KB
/
ping.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
284
285
286
287
288
289
290
291
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# ping.py - Greenlets-based Bitcoin network pinger.
#
# Copyright (c) 2014 Addy Yeow Chin Heng <ayeowch@gmail.com>
#
# 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.
"""
Greenlets-based Bitcoin network pinger.
"""
from gevent import monkey
monkey.patch_all()
import gevent
import gevent.pool
import glob
import json
import logging
import os
import random
import redis
import redis.connection
import socket
import sys
import time
from ConfigParser import ConfigParser
from protocol import ProtocolError, ConnectionError, Connection
redis.connection.socket = gevent.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)
SETTINGS = {}
def keepalive(connection, version_msg):
"""
Periodically sends a ping message to the specified node to maintain open
connection. Open connections are tracked in open set with the associated
data stored in opendata set in Redis.
"""
node = connection.to_addr
version = version_msg.get('version', "")
user_agent = version_msg.get('user_agent', "")
now = int(time.time())
data = node + (version, user_agent, now)
REDIS_CONN.sadd('open', node)
REDIS_CONN.sadd('opendata', data)
redis_pipe = REDIS_CONN.pipeline()
last_ping = now
while True:
try:
wait = int(REDIS_CONN.get('elapsed'))
except TypeError as err:
wait = 60
if time.time() > last_ping + wait:
nonce = random.getrandbits(64)
try:
connection.ping(nonce=nonce)
except socket.error as err:
logging.debug("Closing {} ({})".format(node, err))
break
last_ping = time.time()
key = "ping:{}-{}:{}".format(node[0], node[1], nonce)
redis_pipe.lpush(key, int(last_ping * 1000)) # in ms
redis_pipe.expire(key, SETTINGS['ttl'])
redis_pipe.execute()
# Sink received messages to flush them off socket buffer
try:
connection.get_messages()
except socket.timeout as err:
pass
except (ProtocolError, ConnectionError, socket.error) as err:
logging.debug("Closing {} ({})".format(node, err))
break
gevent.sleep(0.3)
connection.close()
REDIS_CONN.srem('open', node)
REDIS_CONN.srem('opendata', data)
def task():
"""
Assigned to a worker to retrieve (pop) a node from the reachable set and
attempt to establish and maintain connection with the node.
"""
node = REDIS_CONN.spop('reachable')
if node is None:
return
(address, port, height) = eval(node)
handshake_msgs = []
connection = Connection((address, port),
socket_timeout=SETTINGS['socket_timeout'],
user_agent=SETTINGS['user_agent'],
height=height)
try:
connection.open()
handshake_msgs = connection.handshake()
except (ProtocolError, ConnectionError, socket.error) as err:
logging.debug("Closing {} ({})".format(connection.to_addr, err))
connection.close()
if len(handshake_msgs) == 0:
return
keepalive(connection, handshake_msgs[0])
def cron(pool):
"""
Assigned to a worker to perform the following tasks periodically to
maintain a continuous network-wide connections:
[Master]
1) Checks for a new snapshot
2) Loads new reachable nodes into the reachable set in Redis
3) Signals listener to get reachable nodes from opendata set
[Master/Slave]
1) Spawns workers to establish and maintain connection with reachable nodes
"""
snapshot = None
while True:
if SETTINGS['master']:
new_snapshot = get_snapshot()
if new_snapshot != snapshot:
nodes = get_nodes(new_snapshot)
if len(nodes) == 0:
continue
logging.info("New snapshot: {}".format(new_snapshot))
snapshot = new_snapshot
logging.info("Nodes: {}".format(len(nodes)))
reachable_nodes = set_reachable(nodes)
logging.info("New reachable nodes: {}".format(reachable_nodes))
# Allow connections to stabilize before publishing snapshot
gevent.sleep(SETTINGS['cron_delay'])
REDIS_CONN.publish('snapshot', int(time.time()))
connections = REDIS_CONN.scard('open')
logging.info("Connections: {}".format(connections))
for _ in xrange(min(REDIS_CONN.scard('reachable'), pool.free_count())):
pool.spawn(task)
workers = SETTINGS['workers'] - pool.free_count()
logging.info("Workers: {}".format(workers))
gevent.sleep(SETTINGS['cron_delay'])
def get_snapshot():
"""
Returns latest JSON file (based on creation date) containing a snapshot of
all reachable nodes from a completed crawl.
"""
snapshot = None
try:
snapshot = max(glob.iglob("{}/*.json".format(SETTINGS['crawl_dir'])))
except ValueError as err:
logging.warning(err)
return snapshot
def get_nodes(path):
"""
Returns all reachable nodes from a JSON file.
"""
nodes = []
text = open(path, 'r').read()
try:
nodes = json.loads(text)
except ValueError as err:
logging.warning(err)
return nodes
def set_reachable(nodes):
"""
Adds reachable nodes that are not already in the open set into the
reachable set in Redis. New workers can be spawned separately to establish
and maintain connection with these nodes.
"""
for node in nodes:
address = node[0]
port = node[1]
height = node[2]
if not REDIS_CONN.sismember('open', (address, port)):
REDIS_CONN.sadd('reachable', (address, port, height))
return REDIS_CONN.scard('reachable')
def init_settings(argv):
"""
Populates SETTINGS with key-value pairs from configuration file.
"""
conf = ConfigParser()
conf.read(argv[1])
SETTINGS['logfile'] = conf.get('ping', 'logfile')
SETTINGS['workers'] = conf.getint('ping', 'workers')
SETTINGS['debug'] = conf.getboolean('ping', 'debug')
SETTINGS['user_agent'] = conf.get('ping', 'user_agent')
SETTINGS['socket_timeout'] = conf.getint('ping', 'socket_timeout')
SETTINGS['cron_delay'] = conf.getint('ping', 'cron_delay')
SETTINGS['ttl'] = conf.getint('ping', 'ttl')
SETTINGS['crawl_dir'] = conf.get('ping', 'crawl_dir')
if not os.path.exists(SETTINGS['crawl_dir']):
os.makedirs(SETTINGS['crawl_dir'])
SETTINGS['master'] = argv[2] == "master"
def main(argv):
if len(argv) < 3 or not os.path.exists(argv[1]):
print("Usage: ping.py [config] [master|slave]")
return 1
# Initialize global settings
init_settings(argv)
# Initialize logger
loglevel = logging.INFO
if SETTINGS['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=SETTINGS['logfile'],
filemode='a')
print("Writing output to {}, press CTRL+C to terminate..".format(
SETTINGS['logfile']))
if SETTINGS['master']:
logging.info("Removing all keys")
REDIS_CONN.delete('reachable')
REDIS_CONN.delete('open')
REDIS_CONN.delete('opendata')
# Initialize a pool of workers (greenlets)
pool = gevent.pool.Pool(SETTINGS['workers'])
pool.spawn(cron, pool)
pool.join()
return 0
if __name__ == '__main__':
sys.exit(main(sys.argv))