-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
569 lines (469 loc) · 17.2 KB
/
app.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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
#!/usr/bin/env python3
"""
Magi REST API © MIT licensed
https://magi.duinocoin.com | https://m-core.org
https://github.com/revoxhere/magi-rest-api
Duino-Coin Team & Community 2019-2021
"""
import gevent.monkey
gevent.monkey.patch_all()
import sys
import os
from flask_cors import CORS
from flask_caching import Cache
from flask import Flask, request, jsonify, render_template
from flask_limiter import Limiter
from flask_limiter.util import get_remote_address
from flask_ipban import IpBan
from bcrypt import hashpw, gensalt, checkpw
import threading
from datetime import datetime
import requests
from re import sub, match
from time import sleep, time
from sqlite3 import connect as sqlconn
from json import load
import traceback
from magilib import *
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import smtplib
import ssl
from dotenv import load_dotenv
load_dotenv()
CAPTCHA_SECRET_KEY = os.getenv('CAPTCHA_KEY')
MAGI_PASS = os.getenv('MAGI_PASS')
DUCO_EMAIL = os.getenv('MAGI_MAIL')
magi_rpc_user = os.getenv('MAGI_RPC_USER')
magi_rpc_pass = os.getenv('MAGI_RPC_PASS')
DATABASE = 'magi-db.db'
BCRYPT_ROUNDS = 6
DB_TIMEOUT = 3
SAVE_TIME = 30
config = {
"DEBUG": False,
"CACHE_TYPE": "redis",
"CACHE_REDIS_URL": "redis://localhost:6379/0",
"CACHE_DEFAULT_TIMEOUT": SAVE_TIME,
"JSONIFY_PRETTYPRINT_REGULAR": False}
def forwarded_ip_check():
return request.environ.get('HTTP_X_REAL_IP', request.remote_addr)
app = Flask(__name__, template_folder='config/error_pages')
app.config.from_mapping(config)
cache = Cache(app)
CORS(app)
limiter = Limiter(
key_func=forwarded_ip_check,
default_limits=["5000 per day", "1 per 1 second"])
limiter.init_app(app)
ip_ban = IpBan(ban_seconds=60*60, ban_count=10,
persist=True, record_dir="config/ipbans/",
ipc=True, secret_key=MAGI_PASS)
ip_ban.init_app(app)
overrides = [MAGI_PASS]
banlist, observations = [], {}
magi = rvxMagi(magi_rpc_user, magi_rpc_pass)
print(magi.get_balance(), "XMG total")
with open('register_email.html', 'r') as file:
html = file.read()
def _success(result, code=200):
return jsonify(success=True, result=result), code
def _error(string, code=200):
ip_addr = request.environ.get('HTTP_X_REAL_IP', request.remote_addr)
ip_ban.add(ip=ip_addr)
dbg("Error", string, ip_addr)
return jsonify(success=False, message=string), code
def dbg(*message):
print(*message)
def send_registration_email(username, email):
message = MIMEMultipart("alternative")
message["Subject"] = (u"\U0001F44B" +
" Welcome on the Coin Magi network, "
+ str(username)
+ "!")
try:
message["From"] = DUCO_EMAIL
message["To"] = email
email_body = html.replace("{user}", str(username))
part = MIMEText(email_body, "html")
message.attach(part)
context = ssl.create_default_context()
with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=context) as smtp:
smtp.login(
DUCO_EMAIL, MAGI_PASS)
smtp.sendmail(
DUCO_EMAIL, email, message.as_string())
return True
except Exception as e:
print(traceback.format_exc())
return False
@app.errorhandler(429)
def error429(e):
global observations
ip_addr = request.environ.get('HTTP_X_REAL_IP', request.remote_addr)
ip_ban.add(ip=ip_addr)
try:
observations[ip_addr] += 1
except:
observations[ip_addr] = 1
if observations[ip_addr] > 20:
dbg("Too many observations", ip_addr)
if not ip_addr in whitelist:
ip_ban.block(ip_addr)
return render_template('403.html'), 403
else:
limit_err = str(e).replace("429 Too Many Requests: ", "")
dbg("Error 429", ip_addr, limit_err, os.getpid())
return render_template('429.html', limit=limit_err), 429
@app.errorhandler(404)
def error404(e):
ip_addr = request.environ.get('HTTP_X_REAL_IP', request.remote_addr)
page_name = str(request.url)
ip_ban.add(ip=ip_addr)
dbg("Error 404", ip_addr, page_name)
return render_template('404.html', page_name=page_name), 404
@app.errorhandler(500)
def error500(e):
ip_addr = request.environ.get('HTTP_X_REAL_IP', request.remote_addr)
dbg("Error 500", ip_addr)
return render_template('500.html'), 500
@app.errorhandler(403)
def error403(e):
ip_addr = request.environ.get('HTTP_X_REAL_IP', request.remote_addr)
ip_ban.add(ip=ip_addr)
ip_ban.block(ip_addr)
dbg("Error 403", ip_addr)
try:
observations[ip_addr] += 1
except:
observations[ip_addr] = 1
if observations[ip_addr] > 40:
dbg("Too many observations - banning", ip_addr)
if not ip_addr in whitelist:
ip_addr_ban(ip_addr)
return render_template('403.html'), 403
@app.route("/balances/<username>")
@limiter.limit("30 per minute")
@cache.cached(timeout=SAVE_TIME)
def get_account_data(username):
global magi
ip_addr = request.environ.get('HTTP_X_REAL_IP', request.remote_addr)
dbg("/GET/balances/"+str(username), ip_addr)
try:
while true:
try:
return _success(magi.account_data(username))
except Exception as e:
magi = rvxMagi(magi_rpc_user, magi_rpc_pass)
except Exception as e:
return _error(f"This user doesn't exist: {e}")
@app.route("/users/<username>")
@limiter.limit("30 per minute")
@cache.cached(timeout=SAVE_TIME)
def get_user_data(username):
global magi
try:
ip_addr = request.environ.get('HTTP_X_REAL_IP', request.remote_addr)
limit = int(request.args.get('limit', 10))
except Exception as e:
return _error(f"Incorrect data: {e}")
dbg("/GET/users/"+str(username), ip_addr)
try:
while True:
try:
return _success(magi.user_data(username, limit))
except Exception as e:
magi = rvxMagi(magi_rpc_user, magi_rpc_pass)
except Exception as e:
return _error(f"This user doesn't exist: {e}")
@app.route("/user_transactions/<username>")
@cache.cached(timeout=SAVE_TIME)
def get_transaction_for_user(username: str):
global magi
try:
ip_addr = request.environ.get('HTTP_X_REAL_IP', request.remote_addr)
limit = int(request.args.get('limit', 10))
except Exception as e:
return _error(f"Incorrect data: {e}")
dbg("/GET/user_transactions/"+str(username), ip_addr)
try:
while True:
try:
transactions = magi.get_transactions(username, limit)
except Exception as e:
if str(e) != "Request-sent":
raise
else:
magi = rvxMagi(magi_rpc_user, magi_rpc_pass)
transactions_prep = []
for transaction in transactions:
transactions_prep.append(magi.transaction_data(transaction))
return _success(transactions_prep)
except Exception as e:
return _error(f"No transactions found: {e}")
@app.route("/transactions/<txid>")
@cache.cached(timeout=SAVE_TIME)
def get_transaction_by_txid(txid: str):
global magi
ip_addr = request.environ.get('HTTP_X_REAL_IP', request.remote_addr)
dbg("/GET/transactions/"+str(txid), ip_addr)
try:
while True:
try:
transaction = magi.transaction_by_txid(txid)
return _success(transaction)
except Exception as e:
if str(e) != "Request-sent":
print(traceback.format_exc())
raise
else:
magi = rvxMagi(magi_rpc_user, magi_rpc_pass)
except Exception as e:
return _error(f"No transactions found: {e}")
@app.route("/statistics")
@cache.cached(timeout=SAVE_TIME)
def get_stats():
global magi
ip_addr = request.environ.get('HTTP_X_REAL_IP', request.remote_addr)
dbg("/GET/statistics", ip_addr)
try:
with sqlconn(DATABASE, timeout=DB_TIMEOUT) as conn:
datab = conn.cursor()
datab.execute(
"""SELECT *
FROM Users""")
users = datab.fetchall()
while True:
try:
to_return = magi.statistics()
to_return["users"] = len(users)
return _success(to_return)
except Exception as e:
if str(e) != "Request-sent":
raise
else:
magi = rvxMagi(magi_rpc_user, magi_rpc_pass)
except Exception as e:
return _error(f"Error fetching stats: {e}")
@app.route("/all_balances")
@cache.cached(timeout=SAVE_TIME)
def all_balances():
global magi
ip_addr = request.environ.get('HTTP_X_REAL_IP', request.remote_addr)
dbg("/GET/all_balances", ip_addr)
try:
to_return = {}
for wallet in magi.get_wallets():
if (wallet["account"]
and not wallet["account"] in to_return
and wallet["amount"] != 0):
to_return[str(wallet["account"])] = magi.account_data(
wallet["account"])
return _success(to_return)
except Exception as e:
return _error(f"Error fetching stats: {e}")
@app.route("/transaction/")
@limiter.limit("2 per minute")
def api_transaction():
global magi
try:
ip_addr = request.environ.get('HTTP_X_REAL_IP', request.remote_addr)
username = request.args.get('username', None)
unhashed_pass = request.args.get('password', None)
recipient = request.args.get('recipient', None)
amount = float(request.args.get('amount', None))
memo = request.args.get('memo', None)[0:50]
memo = sub(r'[^A-Za-z0-9 .()-:/!#_+-]+', ' ', str(memo))
if len(recipient) != 34:
try:
while True:
try:
recipient = magi.get_account_address(recipient)
break
except Exception as e:
if str(e) != "Request-sent":
raise
else:
magi = rvxMagi(magi_rpc_user, magi_rpc_pass)
except:
return _error("NO,Recipient doesnt exist")
except Exception as e:
return _error(f"NO,Incorrect data: {e}")
dbg("/GET/transaction", username, amount, recipient, memo, ip_addr)
if not memo or memo == "-" or memo == "":
memo = "none"
if round(amount, 5) <= 0:
return _error("NO,Incorrect amount")
if not unhashed_pass in overrides:
login_protocol = login(username, unhashed_pass.encode('utf-8'))
if not login_protocol[0]:
return _error(login_protocol[1])
try:
if str(recipient) == str(username):
return _error("NO,You\'re sending funds to yourself")
if str(amount) == "" or float(amount) <= 0:
return _error("NO,Incorrect amount")
while True:
try:
balance = magi.get_balance(username)
if float(balance) < float(amount):
return _error("NO,Incorrect amount")
break
except Exception as e:
magi = rvxMagi(magi_rpc_user, magi_rpc_pass)
if float(balance) >= float(amount):
while True:
try:
global_last_block_hash_cp = magi.send(
username, recipient, amount, memo)
break
except Exception as e:
if str(e) != "Request-sent":
raise
else:
magi = rvxMagi(magi_rpc_user, magi_rpc_pass)
dbg("Successfully transferred", amount, "from",
username, "to", recipient, global_last_block_hash_cp)
cache.clear()
return _success("OK,Successfully transferred funds,"
+ str(global_last_block_hash_cp))
except Exception as e:
print(traceback.format_exc())
return _error(f"NO,Internal server error: {e}")
def login(username: str, unhashed_pass: str):
if not match(r"^[A-Za-z0-9_-]*$", username):
return (False, "Incorrect username")
try:
with sqlconn(DATABASE, timeout=DB_TIMEOUT) as conn:
datab = conn.cursor()
datab.execute(
"""SELECT *
FROM Users
WHERE username = ?""",
(str(username),))
data = datab.fetchone()
if data:
stored_password = data[1]
else:
return (False, "No user found")
try:
if checkpw(unhashed_pass, stored_password):
return (True, "Correct password")
return (False, "Invalid password")
except Exception:
if checkpw(unhashed_pass, stored_password.encode('utf-8')):
return (True, "Correct password")
return (False, "Invalid password")
except Exception as e:
return (False, "DB Err: " + str(e))
def email_exists(email: str):
try:
with sqlconn(DATABASE, timeout=DB_TIMEOUT) as conn:
datab = conn.cursor()
datab.execute(
"""SELECT *
FROM Users
WHERE email = ?""",
(str(email),))
data = datab.fetchone()
if data:
return True
return False
except Exception as e:
print(e)
return True
@app.route("/auth/<username>")
@limiter.limit("6 per minute")
def api_auth(username=None):
try:
ip_addr = request.environ.get('HTTP_X_REAL_IP', request.remote_addr)
unhashed_pass = request.args.get('password', None)
if not unhashed_pass:
raise Exception("No password specified")
unhashed_pass = unhashed_pass.encode('utf-8')
except Exception as e:
return _error(f"Invalid data: {e}")
dbg("/GET/auth", username)
if unhashed_pass.decode() in overrides:
return _success("Correct password")
if username in banlist:
ip_addr_ban(ip_addr)
return _error("User banned")
login_protocol = login(username, unhashed_pass)
if login_protocol[0] == True:
return _success(login_protocol[1])
else:
return _error(login_protocol[1])
@app.route("/register/")
@limiter.limit("5 per hour")
def register():
global magi
try:
username = str(request.args.get('username', None))
unhashed_pass = str(request.args.get('password', None)).encode('utf-8')
email = str(request.args.get('email', None))
captcha = request.args.get('captcha', None)
ip_addr = request.environ.get('HTTP_X_REAL_IP', request.remote_addr)
postdata = {'secret': CAPTCHA_SECRET_KEY,
'response': captcha}
except Exception as e:
return _error(f"Invalid data: {e}")
if len(username) > 64 or len(unhashed_pass) > 128 or len(email) > 64:
return _error("Submited data is too long")
if not match(r"^[A-Za-z0-9_-]*$", username):
return _error("You have used unallowed characters in the username")
if not "@" in email or not "." in email:
return _error("You have provided an invalid e-mail address")
if email_exists(email):
return _error("This e-mail was already used")
try:
captcha_data = requests.post(
'https://hcaptcha.com/siteverify', data=postdata).json()
if not captcha_data["success"]:
return _error("Incorrect captcha")
except Exception as e:
return _error("Captcha error: "+str(e))
while True:
try:
if magi.wallet_exists(username):
return _error("This username is already registered")
break
except Exception as e:
if str(e) != "Request-sent":
raise
else:
magi = rvxMagi(magi_rpc_user, magi_rpc_pass)
try:
password = hashpw(unhashed_pass, gensalt(rounds=BCRYPT_ROUNDS))
except Exception as e:
return _error("Bcrypt error: " +
str(e) + ", plase try using a different password")
try:
threading.Thread(
target=send_registration_email,
args=[username, email]).start()
created = str(datetime.now().strftime("%d/%m/%Y %H:%M:%S"))
with sqlconn(DATABASE, timeout=DB_TIMEOUT) as conn:
datab = conn.cursor()
datab.execute(
"""INSERT INTO Users
(username, password, email, balance, created, tbd)
VALUES(?, ?, ?, ?, ?, ?)""",
(username, password, email, 0.0, created, ""))
conn.commit()
while True:
try:
acc = magi.create_wallet(username)
break
except Exception as e:
if str(e) != "Request-sent":
raise
else:
magi = rvxMagi(magi_rpc_user, magi_rpc_pass)
result = {
"address": acc[0],
"account": acc[1]}
dbg(f"Success: registered {username} ({email})")
return _success(result)
except Exception as e:
return _error(f"Error registering new account: {e}")