forked from krruzic/TurtleFaucet
-
Notifications
You must be signed in to change notification settings - Fork 2
/
faucet.py
executable file
·181 lines (149 loc) · 5.64 KB
/
faucet.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
from flask import Flask, render_template, request, g
from flask_sqlalchemy import SQLAlchemy
from wtforms.validators import DataRequired
from flask_wtf import FlaskForm
from flask_wtf.recaptcha import RecaptchaField
from wtforms import StringField, HiddenField
from flask_wtf.csrf import CSRFProtect
from datetime import datetime
from ratelimit import ratelimit, get_view_rate_limit
import requests
import json
import os
import binascii
ADDRESS = os.environ.get("FAUCET_ADDR")
RPC_URL = "http://127.0.0.1:8070/json_rpc"
HEADERS = {'content-type': 'application/json'}
RECAPTCHA_PUBLIC_KEY = os.environ.get("RECAPTCHA_PUBLIC_KEY")
RECAPTCHA_PRIVATE_KEY = os.environ.get("RECAPTCHA_PRIVATE_KEY")
RECAPTCHA_DATA_ATTRS = {'theme': 'dark'}
csrf = CSRFProtect()
app = Flask(__name__, static_url_path='/static')
app.config.from_object(__name__)
app.config.update(dict(
SECRET_KEY=os.environ.get("SECRET_KEY"),
WTF_CSRF_SECRET_KEY=os.environ.get("WTF_CSRF_SECRET_KEY"),
SQLALCHEMY_DATABASE_URI='sqlite:///faucet.db',
SQLALCHEMY_TRACK_MODIFICATIONS=False
))
csrf.init_app(app)
db = SQLAlchemy(app)
class Transfer(db.Model):
id = db.Column(db.Integer, primary_key=True)
destination = db.Column(db.String(99), nullable=False)
payment_id = db.Column(db.String(128), nullable=False)
amount = db.Column(db.Integer, nullable=False)
transfer_time = db.Column(db.DateTime, nullable=False,
default=datetime.utcnow)
status = db.Column(db.String(50), nullable=False)
tx_hash = db.Column(db.String(128), nullable=False)
ip = db.Column(db.String(50), nullable=False)
fp = db.Column(db.String(32), nullable=False)
def __repr__(self):
return '<Transfer %r: %d sent to %s>' % (self.tx_hash,self.amount,self.destination)
class FaucetForm(FlaskForm):
recaptcha = RecaptchaField()
address = StringField('address', validators=[DataRequired()])
fingerprint = HiddenField('fingerprint')
@app.after_request
def inject_x_rate_headers(response):
limit = get_view_rate_limit()
if limit and limit.send_x_headers:
h = response.headers
h.add('X-RateLimit-Remaining', str(limit.remaining_ip))
h.add('X-RateLimit-Limit', str(limit.limit))
h.add('X-RateLimit-Reset', str(limit.reset))
return response
@app.errorhandler(500)
def internal_error(error):
return json.dumps({'status':'Fail',
'reason':'Server Error'})
@app.route("/")
def index(form=None):
shells = json.loads(shell_balance())
if form is None:
form = FaucetForm()
return render_template("index.html",shells=shells['available'],form=form,addr=ADDRESS)
@app.route("/transfers", methods=["GET"])
def get_transfers():
transfers = db.session.query(Transfer).order_by(Transfer.id.desc()).limit(10).all()
return render_template("transfers.html",transfers=transfers)
@app.route("/pour", methods=["POST"])
@ratelimit(limit=4, per=60*60*24)
def get_shells():
form = FaucetForm()
if form.address.data==ADDRESS:
return json.dumps({'status':'Fail',
'reason':'The faucet cannot send to itself'}),403
if form.validate_on_submit():
resp = do_send(form.address.data,request)
if "reason" in json.loads(resp):
return resp,500
return json.dumps({'status':'OK'}),200
return json.dumps({'status':'Fail',
'reason':'Make sure the captcha and address fields are filled'}),400
## code modified from https://moneroexamples.github.io/python-json-rpc/
@app.route("/balance", methods=["GET"])
def shell_balance():
rpc_input = {
"method": "getBalance"
}
# add standard rpc values
rpc_input.update({"jsonrpc": "2.0", "id": "0"})
# execute the rpc request
response = requests.post(
RPC_URL,
data=json.dumps(rpc_input),
headers=HEADERS)
data = response.json()
app.logger.info("balance_rpc: "+str(data))
av = float(data['result']['availableBalance'])
lck = float(data['result']['lockedAmount'])
return json.dumps({"available": str((av)/100),"locked": str((lck)/100)})
def do_send(address,r):
avail = json.loads(shell_balance())['available']
int_amount = 300
recipents = [{"address": address,
"amount": int_amount}]
# get some random payment_id
payment_id = get_payment_id()
# simplewallet' procedure/method to call
rpc_input = {
"method": "sendTransaction",
"params": {"anonymity":1,
"transfers": recipents,
"unlockTime": 0,
"fee": 5,
"paymentId": payment_id}
}
# add standard rpc values
rpc_input.update({"jsonrpc": "2.0", "id": "0"})
# execute the rpc request
response = requests.post(
RPC_URL,
data=json.dumps(rpc_input),
headers=HEADERS)
# pretty print json output
app.logger.info(json.dumps(response.json(), indent=4))
app.logger.info("FROM IP: "+r.environ['REMOTE_ADDR'])
if "error" in response.json():
return json.dumps({"status": "Fail", "reason": response.json()["error"]["message"]})
tx_hash = response.json()['result']['transactionHash']
transfer = Transfer(destination=address,
payment_id=payment_id,
amount = int_amount,
transfer_time=datetime.utcnow(),
status="Sent",
tx_hash=tx_hash,
ip=r.environ['REMOTE_ADDR'],
fp=r.form.get('fingerprint')
)
db.session.add(transfer)
db.session.commit()
return json.dumps({"status": "OK"})
def get_payment_id():
random_32_bytes = os.urandom(32)
payment_id = "".join(map(chr, binascii.hexlify(random_32_bytes)))
return payment_id
if __name__ == "__main__":
app.run()