forked from samrushing/caesure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webadmin.py
309 lines (284 loc) · 11.4 KB
/
webadmin.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# -*- Mode: Python -*-
import re
import sys
import time
import zlib
import coro
from urllib import splitquery
from urlparse import parse_qs
from cgi import escape
favicon = (
'AAABAAEAEBAAAAEAIABoBAAAFgAAACgAAAAQAAAAIAAAAAEAIAAAAAAAAAQAAAAAAAAAAAAAAAAA'
'AAAAAAD///8A////AP///wD9/f0A2uXsKbTN3FVFqeHlQqfe6mqhva1bsuLKj8Pfhu/v7w////8A'
'////AP///wD///8A////AP///wD8/f0AabXfuTat7v1lrs26V7Hc0G242LSBxN2cSqvd4E2s3d2K'
'wNKNv9LYR/z8/AH///8A////AP///wDv8/YSk7zSfkir3uJpt9i5ldToh5XU6IeV1OiHldToh5XU'
'6IeV1OiHldToh5TU54esydNh+vr6A////wD///8AYLPgxUKo3uqV1OiHldToh5XU6IeV1OiHldTo'
'h5XU6IeV1OiHldToh5XU6IeV1OiHlNTnh7jP1k////8A/Pz8ATSg2vpqtdW1kM3gipLQ44mV1OiH'
'ldToh5TU54eQzeCKlNTnh5XU6IeV1OiHjcjbjYa/0ZKSzd+G5unqGY7E4ohqsc+0PVdfzQQFBvoE'
'Bgb6OFFY0JXU6IeGwNKSAAAA/5DN4IqV1OiHWX+KtQUGBvoJDQ73UXN+vbjR2VI5pOD2WrLcyz1X'
'X81FYmvHea29mwIDA/2U1OeHhsDSkgAAAP+QzeCKjsvdjAUGB/pql6WqlNPnh4O7zJScx9R1Xq3Y'
'xXnA26Q9V1/NGiYp6Sc3PN4rPkTbldToh4bA0pIAAAD/kM3ginquvpsCAwP9lNPmh5XU6IeV1OiH'
'j8LShmGs1cB9wtygPVdfzSw+RNs7VFvPLD9F25XU6IeGwNKSAAAA/5DN4IqDu8yUAAAA/YjC1JGV'
'1OiHldToh4/D04ZGquHjUK7c2T1XX80kNDjgLkNJ2SU0OeBlkZ6tOFBX0AAAAP87VV3OapinqCU1'
'OeAlNTrgTG14wFl/iracx9R1rdHlYlut08holaOqSmpzwk9xfL2BucmWbZupp0pqc8JKanPCSmpz'
'wnKhsaOLx9mOTG12wUJfZ8l8sMCbuNLZU////wBFn9DiXbHYxpXU6IeV1OiHldToh5XU6IeV1OiH'
'ldToh5XU6IeV1OiHldToh5XU6IeV1OiHk83ghuTn6Rr///8Ah8Likzat7v2GxdqUldToh5XU6IeV'
'1OiHldToh5XU6IeV1OiHldToh5XU6IeV1OiHlNTnh7fO1lD///8A////AP39/QGtydhdSKHO3lmx'
'2s2PzeKNldToh5XU6IeV1OiHldToh5XU6IeV1OiHlNTnh6rJ02P6+voD////AP///wD///8A////'
'AJXH4382quv8VanQzl+028dgtNvEisnekFux2spIq97je7jPnr3R10r6+voD////AP///wD///8A'
'////AP///wD///8A7/HxD7/P10dSruDVPqbg7mSdu7NKrOHecrrirejr7Rf///8A////AP///wD/'
'//8A/B8AAOAPAADgBwAAgAMAAIABAAAAAQAAAAEAAAAAAAAAAAAAAAEAAIABAACAAQAAgAMAAOAH'
'AADwDwAA/B8AAA=='
).decode ('base64')
from __main__ import *
# all this CSS magic is stolen from looking at the output from pident.artefact2.com
css = """
<style type="text/css">
body { font-family: monospace; }
table > tbody > tr:nth-child(odd) {
background-color:#f0f0f0;
}
table > tbody > tr:nth-child(even) {
background-color:#e0e0e0;
}
table { width:100%; }
tr.inrow { border:1px green; }
tr.plus > td { background-color:#80ff80; }
tr.minus > td { background-color:#ff8080; }
a.alert { color:#ff0000; }
</style>
"""
def shorten (s, w=20):
if len(s) > w:
return s[:w] + '…'
else:
return s
def shorthex (s):
return shorten (hexify (s))
# 20 Sep 2013 - recovered from https://github.com/runeksvendsen/brutus/blob/master/bitcoin.py
def parse_oscript (s):
if (ord(s[0]) == 118 and ord(s[1]) == 169 and ord(s[-2]) == 136 and ord(s[-1]) == 172):
size = ord(s[2])
addr = key_to_address (s[3:size+3])
assert (size+5 == len(s))
return addr
else:
return None
class handler:
def __init__ (self):
self.pending_send = []
def match (self, request):
return request.path.startswith ('/admin/')
safe_cmd = re.compile ('[a-z]+')
def handle_request (self, request):
parts = request.path.split ('/')[2:] # ignore ['', 'admin']
subcmd = parts[0]
if not subcmd:
subcmd = 'status'
method_name = 'cmd_%s' % (subcmd,)
if self.safe_cmd.match (subcmd) and hasattr (self, method_name):
request['content-type'] = 'text/html'
request.set_deflate()
method = getattr (self, method_name)
request.push (
'\r\n'.join ([
'<!DOCTYPE HTML><html><head><meta charset="UTF-8"><title>Caesure admin</title>',
css,
'</head><body>',
'<h1>caesure admin</h1>',
])
)
self.menu (request)
try:
method (request, parts)
except SystemExit:
raise
except:
request.push ('<strong>something went wrong</strong><br><br>')
request.push ('<pre>%r</pre>' % (coro.compact_traceback(),))
request.push ('<hr>')
self.menu (request)
request.push ('</body></html>')
request.done()
else:
request.error (400)
def menu (self, request):
request.push (
' <a href="/admin/reload">reload</a>'
' <a href="/admin/status">status</a>'
' <a href="/admin/block/">blocks</a>'
' <a href="/admin/send/">send</a>'
' <a href="/admin/connect/">connect</a>'
' <a href="/admin/shutdown/">shutdown</a>'
)
def cmd_status (self, request, parts):
db = the_block_db
RP = request.push
RP ('<h3>last block</h3>')
RP ('hash[es]: %s' % (escape (repr (db.num_block[db.last_block]))))
RP ('<br>num: %d' % (db.last_block,))
RP ('<h3>connections</h3>')
RP ('<table><thead><tr><th>packets</th><th>address</th><tr></thead>')
for conn in the_connection_list:
try:
addr, port = conn.conn.getpeername()
RP ('<tr><td>%d</td><td>%s:%d</td></tr>' % (conn.packet_count, addr, port))
except:
RP ('<br>dead connection</br>')
RP ('</table><hr>')
def dump_block (self, request, b, num, name):
RP = request.push
RP ('\r\n'.join ([
'<br>block: %d' % (num,),
'<br>name: %s' % (name,),
'<br>prev: %s' % (b.prev_block,),
'<br>merk: %s' % (hexify (b.merkle_root),),
'<br>time: %s (%s)' % (b.timestamp, time.ctime (b.timestamp)),
'<br>bits: %s' % (b.bits,),
'<br>nonce: %s' % (b.nonce,),
'<br><a href="https://blockexplorer.com/b/%d">block explorer</a>' % (num,),
'<br><a href="https://blockchain.info/block/%s">blockchain.info</a>' % (name,),
]))
#RP ('<pre>%d transactions\r\n' % len(b.transactions))
RP ('<table><thead><tr><th>num</th><th>ID</th><th>inputs</th><th>outputs</th></tr></thead>')
for i in range (len (b.transactions)):
self.dump_tx (request, b.transactions[i], i)
RP ('</table>')
#RP ('</pre>')
def cmd_block (self, request, parts):
db = the_block_db
RP = request.push
if len(parts) == 2 and len (parts[1]):
name = parts[1]
else:
name = list(db.num_block[db.last_block])[0]
if db.has_key (name):
b = db[name]
num = db.block_num[name]
RP ('<br> <a href="/admin/block/%s">First Block</a>' % (genesis_block_hash,))
RP (' <a href="/admin/block/">Last Block</a><br>')
if name != genesis_block_hash:
RP (' <a href="/admin/block/%s">Prev Block</a>' % (db.prev[name],))
else:
RP (' Prev Block<br>')
if db.next.has_key (name):
names = list (db.next[name])
if len(names) > 1:
longer, length = longest (names)
for i in range (len (names)):
if names[i] != longer:
descrip = "Next Block (Orphan Chain)"
aclass = ' class="alert" '
else:
descrip = "Next Block"
aclass = ''
RP (' <a href="/admin/block/%s" %s>%s</a>' % (names[i], aclass, descrip,))
else:
RP (' <a href="/admin/block/%s">Next Block</a>' % (names[0],))
RP ('<br>')
else:
RP (' Next Block<br>')
self.dump_block (request, b, num, name)
def dump_tx (self, request, tx, tx_num):
RP = request.push
RP ('<tr><td>%s</td><td>%s</td>\r\n' % (tx_num, shorthex (dhash (tx.raw))))
RP ('<td><table>')
for i in range (len (tx.inputs)):
(outpoint, index), script, sequence = tx.inputs[i]
RP ('<tr><td>%3d</td><td>%s:%d</td><td>%s</td></tr>' % (
i,
shorthex (outpoint),
index,
shorthex (script),
))
RP ('</table></td><td><table>')
for i in range (len (tx.outputs)):
value, pk_script = tx.outputs[i]
try:
kind, data = parse_script (pk_script)
except:
kind = 'address'
data = parse_oscript (pk_script)
col0, col1 = '', ''
tr_class = ''
if kind == 'address':
addr = data
# too noisy, simplify
kind = ''
elif kind == 'pubkey':
addr = key_to_address (rhash (data))
else:
kind = escape (repr (kind))
addr = hexify (pk_script)
RP ('<tr%s><td>%s</td><td>%s %s</td></tr>' % (tr_class, bcrepr (value), kind, addr))
# lock time seems to always be zero
#RP ('</table></td><td>%s</td></tr>' % tx.lock_time,)
RP ('</table></td></tr>')
def cmd_reload (self, request, parts):
new_hand = reload (sys.modules['webadmin'])
hl = sys.modules['__main__'].h.handlers
for i in range (len (hl)):
if hl[i] is self:
del hl[i]
h0 = new_hand.handler()
# copy over any pending send txs
h0.pending_send = self.pending_send
hl.append (h0)
break
request.push ('<h3>[reloaded]</h3>')
self.cmd_status (request, parts)
def match_form (self, qparts, names):
if len(qparts) != len(names):
return False
else:
for name in names:
if not qparts.has_key (name):
return False
return True
def cmd_connect (self, request, parts):
RP = request.push
if request.query:
qparts = parse_qs (request.query[1:])
if self.match_form (qparts, ['host']):
global bc
## if bc:
## bc.close()
bc = connection (qparts['host'][0])
RP ('<form>'
'IP Address: <input type="text" name="host" value="127.0.0.1"/><br/>'
'<input type="submit" value="Connect"/></form>')
def cmd_shutdown (self, request, parts):
request.push ('<h3>Shutting down...</h3>')
request.done()
coro.sleep_relative (1)
coro.set_exit()
def chain_gen (name):
db = the_block_db
while 1:
if db.next.has_key (name):
names = db.next[name]
if len(names) > 1:
for x in longest (names):
yield 1
else:
name = list(names)[0]
yield 1
else:
break
def longest (names):
gens = [ (name, chain_gen (name)) for name in list (names) ]
ng = len (gens)
left = ng
n = 0
while left > 1:
for i in range (ng):
if gens[i]:
name, gen = gens[i]
try:
gen.next()
except StopIteration:
gens[i] = None
left -= 1
n += 1
[(name, _)] = [x for x in gens if x is not None]
return name, n