forked from jgarzik/bc-hashfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashfs-server.py
379 lines (291 loc) · 9.5 KB
/
hashfs-server.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
import re
import os
import json
import binascii
import hashlib
import time
import base58
from datetime import datetime
import logging
import apsw
import pprint
pp = pprint.PrettyPrinter(indent=2)
# import flask web microframework
from flask import Flask
from flask import request
from flask import abort
# import from the 21 Developer Library
from two1.lib.wallet import Wallet
from two1.lib.bitserv.flask import Payment
app = Flask(__name__)
wallet = Wallet()
payment = Payment(app, wallet)
HASHFS_ROOT_DIR = "hashroot/"
HASHFS_MAX_GB = 2
HASHFS_DB = apsw.Connection("hashfs.sqlite3")
blank_re = re.compile('^\s*$')
SQLS_HASH_QUERY = "SELECT size,time_create,time_expire,content_type FROM metadata WHERE hash = ?"
SQLS_HASH_INSERT = "INSERT INTO metadata(hash,size,time_create,time_expire,content_type,pubkey_addr) VALUES(?, ?, ?, ?, ?, ?)"
SQLS_TOTAL_SIZE = "SELECT SUM(size) FROM metadata"
SQLS_EXPIRED = "SELECT hash,size FROM metadata WHERE time_expire < ? ORDER BY time_expire"
SQLS_EXPIRE_LIST = "DELETE FROM metadata WHERE "
SQLS_HASH_SIZE = "SELECT size FROM metadata WHERE hash = ?"
def httpdate(dt):
"""Return a string representation of a date according to RFC 1123
(HTTP/1.1).
The supplied date must be in UTC.
"""
weekday = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"][dt.weekday()]
month = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep",
"Oct", "Nov", "Dec"][dt.month - 1]
return "%s, %02d %s %04d %02d:%02d:%02d GMT" % (weekday, dt.day, month,
dt.year, dt.hour, dt.minute, dt.second)
def make_hashfs_fn(hexstr, make_dirs=False):
dir1 = hexstr[:3]
dir2 = hexstr[3:6]
dir1_pn = "%s%s" % (HASHFS_ROOT_DIR, dir1)
dir2_pn = "%s%s/%s" % (HASHFS_ROOT_DIR, dir1, dir2)
fn = "%s%s/%s/%s" % (HASHFS_ROOT_DIR, dir1, dir2, hexstr)
if not make_dirs:
return fn
try:
if not os.path.isdir(dir2_pn):
if not os.path.isdir(dir1_pn):
os.mkdir(dir1_pn)
os.mkdir(dir2_pn)
except OSError:
return False
return fn
def hashfs_total_size(cursor):
row = cursor.execute(SQLS_TOTAL_SIZE).fetchone()
if row is None or row[0] is None:
return 0
return int(row[0])
def hashfs_free_space(cursor):
max_size = HASHFS_MAX_GB * 1000 * 1000 * 1000
return max_size - hashfs_total_size(cursor)
def hashfs_expired(cursor):
curtime = int(time.time())
rows = []
for md_hash,md_size in cursor.execute(SQLS_EXPIRED, (curtime,)):
row = (md_hash, int(md_size))
rows.append(row)
return rows
def hashfs_expired_size(rows):
total = 0
for row in rows:
total = total + row[1]
return total
def hashfs_expire_data(cursor, goal):
# list all expired records
rows = hashfs_expired(cursor)
exp_size = hashfs_expired_size(rows)
# is it possible to meet the goal? if not, exit now.
if goal > exp_size:
return
# build list of data to expire
exp_total = 0
exp_rows = []
for row in rows:
exp_total = exp_total + row[1]
exp_rows.append(row)
if exp_toal >= goal:
break
# pass 1: remove metadata
# dynamically build SQL statement listing all hashes to be removed
sqls = SQLS_EXPIRE_LIST
in_first = True
for row in exp_rows:
if not in_first:
sqls += " OR "
sqls += "hash='%s'" % (row[0],)
in_first = False
# execute large sql stmt
cursor.execute(sqls)
# pass 2: remove data from OS filesystem
for row in exp_rows:
fn = make_hashfs_fn(row[0])
try:
os.remove(fn)
except OSError:
app.logger.error("Failed to remove " + fn)
def hashfs_hash_size(cursor, hash):
row = cursor.execute(SQLS_HASH_SIZE, (hash,)).fetchone()
if row is None or row[0] is None:
return None
return int(row[0])
@app.route('/')
def home():
# export API endpoint metadata
home_obj = [
{
"name": "hashfs/1", # service 'hashfs', version '1'
"pricing-type": "per-rpc", # indicates layout of "pricing"
"pricing" : [
{
"rpc": "get",
"per-req": 1, # 1 satoshi per request
"per-mb": 2, # 2 satoshi per 1000000 bytes
},
{
"rpc": "put",
"per-req": 1, # 1 satoshi per request
"per-kb": 10, # 10 satoshis per 1000 bytes
"per-hour": 2, # 2 satoshis per hour to keep alive
},
# default pricing, if no specific match
{
"rpc": True, # True = indicates default
"per-req": 1, # 1 satoshi per request
},
]
}
]
body = json.dumps(home_obj)
return (body, 200, {
'Content-length': len(body),
'Content-type': 'application/json',
})
def hashfs_price_get(request):
# re-parse path, as we are denied access to urls.py tokens
path = request.path
sl_pos = path.rfind('/')
hexstr = path[sl_pos+1:]
# lookup size of $hash's data (if present)
connection = HASHFS_DB
cursor = connection.cursor()
val_size = hashfs_hash_size(cursor, hexstr)
if val_size is None:
app.logger.warning("returning 2 zero price for " + request.path)
return 0
# build pricing structure
mb = int(val_size / 1000000)
if mb == 0:
mb = 1
price = 1 # 1 sat - base per-request price
price = price + (mb * 2) # 2 sat/MB bandwidth price
app.logger.info("returning price " + str(price) + " for " + request.path)
return price
@app.route('/hashfs/1/get/<hexstr>')
@payment.required(hashfs_price_get)
def hashfs_get(hexstr):
# decode hex string param
hexstr = hexstr.lower()
try:
hash = binascii.unhexlify(hexstr)
except TypeError:
abort(400)
if len(hash) != 32:
abort(400)
# get sqlite handle
connection = HASHFS_DB
cursor = connection.cursor()
# query for metadata
md = {}
row = cursor.execute(SQLS_HASH_QUERY, (hexstr,)).fetchone()
if row is None:
abort(404)
md['size'] = int(row[0])
md['created'] = int(row[1])
md['expires'] = int(row[2])
md['content_type'] = row[3]
# set up FileWrapper to return data
filename = make_hashfs_fn(hexstr)
try:
body = open(filename, 'rb').read()
except:
app.logger.error("failed read " + filename)
abort(500)
if len(body) != md['size']:
abort(500)
dt = datetime.fromtimestamp(md['created'])
last_mod = httpdate(dt)
return (body, 200, {
'Content-Length': md['size'],
'Content-Type': md['content_type'],
'ETag': hexstr,
'Last-Modified': last_mod,
})
@app.route('/hashfs/1/put/<hexstr>', methods=['PUT'])
@payment.required(1)
def hashfs_put(hexstr):
# decode hex string param
hexstr = hexstr.lower()
try:
hash = binascii.unhexlify(hexstr)
except TypeError:
abort(400)
if len(hash) != 32:
abort(400)
# get sqlite handle
connection = HASHFS_DB
cursor = connection.cursor()
# get content-length
clen_str = request.headers.get('content-length')
if clen_str is None:
abort(400)
clen = int(request.headers.get('content-length'))
if clen < 1 or clen > (100 * 1000 * 1000):
abort(400)
# do we have room for this new data?
free_space = hashfs_free_space(cursor)
if free_space < clen:
# attempt to remove old, expired data (if any)
hashfs_expire_data(cursor, clen)
# do we have room for this new data, pass #2
free_space = hashfs_free_space(cursor)
# TODO: is there a better HTTP status?
if free_space < clen:
abort(500)
# get content-type
ctype = request.headers.get('content-type')
if blank_re.match(ctype):
ctype = 'application/octet-stream'
# note public key hash, if provided
pkh = request.headers.get('x-hashfs-pkh')
if not pkh is None:
if len(pkh) < 32 or len(pkh) > 35:
abort(400)
try:
base58.b58decode_check(pkh)
except:
abort(400)
# check file existence; if it exists, no need to proceed further
# create dir1/dir2 hierarchy if need be
filename = make_hashfs_fn(hexstr, True)
if filename is None:
abort(500)
if os.path.isfile(filename):
abort(400)
# get data in memory, up to 100M (limit set in nginx config)
body = request.data
body_len = len(body)
# verify content-length matches provided
if clen != body_len:
abort(400)
# hash data
h = hashlib.new('sha256')
h.update(body)
# verify hash matches provided
if h.hexdigest() != hexstr:
abort(400)
# write to filesystem
try:
outf = open(filename, 'wb')
outf.write(body)
outf.close()
except OSError:
abort(500)
body = None
# Create, expiration times
tm_creat = int(time.time())
tm_expire = tm_creat + (24 * 60 * 60)
# Add hash metadata to db
# TODO: test for errors, unlink file if so
cursor.execute(SQLS_HASH_INSERT, (hexstr, body_len, tm_creat, tm_expire, ctype, pkh))
return ("true\n", 200, {
'Content-length': body_len,
'Content-type': 'application/json',
})
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8001)