forked from c00w/bitHopper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwork.py
259 lines (218 loc) · 10.1 KB
/
work.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
#License#
#bitHopper by Colin Rice is licensed under a Creative Commons
# Attribution-NonCommercial-ShareAlike 3.0 Unported License.
#Based on a work at github.com.
import json, base64, traceback, logging
import eventlet
httplib2 = eventlet.import_patched('httplib20_7_1')
from eventlet import pools
from eventlet.green import socket
from peak.util import plugins
# Global timeout for sockets in case something leaks
socket.setdefaulttimeout(900)
import webob
class Work():
def __init__(self, bitHopper):
self.bitHopper = bitHopper
self.i = 0
self.connect_pool = {}
#pools.Pool(min_size = 2, max_size = 10, create = lambda: httplib2.Http(disable_ssl_certificate_validation=True))
def get_http(self, address, timeout=0):
try:
configured_timeout = self.bitHopper.config.getfloat('main','work_request_timeout')
except:
configured_timeout = 2.5
pass
if timeout == 0:
timeout = configured_timeout
if address not in self.connect_pool:
self.connect_pool[address] = pools.Pool(min_size = 0, create = lambda: httplib2.Http(disable_ssl_certificate_validation=True, timeout=timeout))
return self.connect_pool[address].item()
def jsonrpc_lpcall(self, server, url, lp):
try:
#self.i += 1
#request = json.dumps({'method':'getwork', 'params':[], 'id':self.i}, ensure_ascii = True)
user, passw, error = self.user_substitution(server, None, None)
#Check if we are using {USER} or {PASSWORD}
if error:
return None
header = {'Authorization':"Basic " +base64.b64encode(user+ ":" + passw).replace('\n',''), 'user-agent': 'poclbm/20110709', 'Content-Type': 'application/json', 'connection': 'keep-alive'}
with self.get_http(url, timeout=15*60) as http:
try:
resp, content = http.request( url, 'GET', headers=header)#, body=request)[1] # Returns response dict and content str
except Exception, e:
logging.debug('Error with a jsonrpc_lpcall http request')
logging.debug(e)
content = None
lp.receive(content, server)
return None
except Exception, e:
logging.debug('Error in lpcall work.py')
logging.debug(e)
lp.receive(None, server)
return None
def get(self, url, useragent=None):
"""A utility method for getting webpages"""
if useragent == None:
try:
useragent = self.bitHopper.config.get('main', 'work_user_agent')
except:
useragent = 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)'
pass
#logging.debug('user-agent: ' + useragent + ' for ' + str(url) )
header = {'user-agent':useragent}
with self.get_http(url) as http:
try:
content = http.request( url, 'GET', headers=header)[1] # Returns response dict and content str
except Exception, e:
logging.debug('Error with a work.get() http request: ' + str(e))
content = ""
return content
def user_substitution(self, server, username, password):
info = self.bitHopper.pool.get_entry(server)
if '{USER}' in info['user'] and username is not None:
user = info['user'].replace('{USER}', username)
else:
user = info['user']
if '{PASSWORD}' in info['pass'] and password is not None:
passw = info['pass'].replace('{PASSWORD}', password)
else:
passw = info['pass']
if '{USER}' in info['user'] and username is None or '{PASSWORD}' in info['pass'] and password is None:
error = True
else:
error = False
return user, passw, error
def jsonrpc_call(self, server, data, client_header={}, username = None, password = None):
try:
request = json.dumps({'method':'getwork', 'params':data, 'id':self.i}, ensure_ascii = True)
self.i += 1
info = self.bitHopper.pool.get_entry(server)
user, passw, error = self.user_substitution(server, username, password)
header = {'Authorization':"Basic " +base64.b64encode(user + ":" + passw).replace('\n',''), 'connection': 'keep-alive'}
header['user-agent'] = 'poclbm/20110709'
for k,v in client_header.items():
#Ugly hack to deal with httplib trying to be smart and supplying its own user agent.
if k.lower() in [ 'user-agent', 'user_agent']:
header['user-agent'] = v
if k.lower() in ['x-mining-extensions', 'x-mining-hashrate']:
header[k] = v
url = "http://" + info['mine_address']
with self.get_http(url) as http:
try:
resp, content = http.request( url, 'POST', headers=header, body=request)
except Exception, e:
logging.debug('jsonrpc_call http error: ' + str(e))
return None, None
#Check for long polling header
lp = self.bitHopper.lp
if lp.check_lp(server):
#bitHopper.log_msg('Inside LP check')
for k,v in resp.items():
if k.lower() == 'x-long-polling':
lp.set_lp(v,server)
break
except Exception, e:
logging.debug('jsonrpc_call error: ' + str(e))
if self.bitHopper.options.debug:
traceback.print_exc()
return None, None
try:
message = json.loads(content)
value = message['result']
return value, resp
except Exception, e:
logging.debug( "Error in json decoding, Server probably down")
logging.debug(server)
logging.debug(content)
return None, None
def jsonrpc_getwork(self, server, data, headers={}, username = None, password = None):
tries = 0
work = None
while work == None:
if data == [] and tries > 1:
server = self.bitHopper.get_new_server(server)
elif data != [] and tries > 1:
self.bitHopper.get_new_server(server)
if tries >2:
return None, {}, 'No Server'
tries += 1
try:
work, server_headers = self.jsonrpc_call(server, data, headers, username, password)
except Exception, e:
logging.debug( 'caught, inner jsonrpc_call loop')
logging.debug(server)
logging.debug(e)
work = None
return work, server_headers, server
def handle(self, env, start_request):
request = webob.Request(env)
try:
rpc_request = json.loads(request.body)
except:
start_request('200 OK', {})
return ['Go to /stats']
client_headers = {}
for header in env:
if header[0:5] in 'HTTP_':
client_headers[header[5:].replace('_','-')] = env[header]
data = rpc_request['params']
j_id = rpc_request['id']
#check if they are sending a valid message
if rpc_request['method'] != "getwork":
response = json.dumps({"result":None, 'error':{'message':'Invalid method'}, 'id':j_id})
return [response]
if data != []:
server = self.bitHopper.getwork_store.get_server(data[0][72:136])
if data == [] or server == None:
server = self.bitHopper.pool.get_work_server()
auth_data = env.get('HTTP_AUTHORIZATION').split(None, 1)[1]
username, password = auth_data.decode('base64').split(':', 1)
work, server_headers, server = self.jsonrpc_getwork(server, data, client_headers, username, password)
to_delete = []
for header in server_headers:
if header.lower() not in ['x-roll-ntime','x-reject-reason']:
to_delete.append(header)
for item in to_delete:
del server_headers[item]
server_headers['X-Long-Polling'] = '/LP'
start_request('200 OK', server_headers.items())
if work == None:
response = json.dumps({"result":None, 'error':{'message':'Cannot get work unit'}, 'id':j_id})
else:
response = json.dumps({"result":work, 'error':None, 'id':j_id})
eventlet.spawn_n(self.handle_store, work, server, data, username, password, rpc_request)
return [response]
def handle_store(self, work, server, data, username, password, rpc_request):
#some reject callbacks and merkle root stores
if str(work).lower() == 'false':
self.bitHopper.reject_callback(server, data, username, password)
elif str(work).lower() != 'true':
merkle_root = work["data"][72:136]
self.bitHopper.getwork_store.add(server,merkle_root)
#Fancy display methods
hook = plugins.Hook('work.rpc.request')
hook.notify(rpc_request, data, server)
if data != []:
self.bitHopper.data_callback(server, data, username,password) #request.remote_password)
def handle_LP(self, env, start_response):
start_response('200 OK', [('Content-Type', 'text/json')])
request = webob.Request(env)
j_id = None
try:
rpc_request = json.loads(request.body)
j_id = rpc_request['id']
except Exception, e:
logging.debug('Error in json handle_LP')
logging.debug(e)
if not j_id:
j_id = 1
value = self.bitHopper.lp_callback.read()
try:
data = env.get('HTTP_AUTHORIZATION').split(None, 1)[1]
username = data.decode('base64').split(':', 1)[0] # Returns ['username', 'password']
except Exception,e:
username = ''
logging.info('LP Callback for miner: '+ username)
response = json.dumps({"result":value, 'error':None, 'id':j_id})
return [response]