2024-07-23 10:03:30 +10:00
|
|
|
from flask import (
|
|
|
|
Flask,
|
|
|
|
make_response,
|
|
|
|
redirect,
|
|
|
|
request,
|
|
|
|
jsonify,
|
|
|
|
render_template,
|
|
|
|
send_from_directory,
|
|
|
|
)
|
2023-11-07 23:04:54 +11:00
|
|
|
import os
|
|
|
|
import dotenv
|
|
|
|
import requests
|
|
|
|
import gift
|
2023-11-08 12:48:17 +11:00
|
|
|
import json
|
2023-11-08 14:01:08 +11:00
|
|
|
import schedule
|
|
|
|
import time
|
2023-11-08 14:49:08 +11:00
|
|
|
from email_validator import validate_email, EmailNotValidError
|
2023-11-08 14:01:08 +11:00
|
|
|
|
2023-11-07 23:04:54 +11:00
|
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
|
dotenv.load_dotenv()
|
2024-07-23 10:03:30 +10:00
|
|
|
address = "hs1qr7d0xqsyatls47jf28gvm97twe8k606gspfpsz"
|
2023-11-07 23:04:54 +11:00
|
|
|
|
2024-07-23 10:03:30 +10:00
|
|
|
if os.getenv("address") != None:
|
|
|
|
address = os.getenv("address")
|
2023-11-07 23:04:54 +11:00
|
|
|
|
|
|
|
|
2024-07-23 10:03:30 +10:00
|
|
|
# Assets routes
|
|
|
|
@app.route("/assets/<path:path>")
|
|
|
|
def send_report(path):
|
|
|
|
return send_from_directory("templates/assets", path)
|
2023-11-07 23:04:54 +11:00
|
|
|
|
|
|
|
|
2024-07-23 10:03:30 +10:00
|
|
|
@app.route("/")
|
2023-11-07 23:04:54 +11:00
|
|
|
def index():
|
|
|
|
params = request.args
|
2024-07-23 10:03:30 +10:00
|
|
|
if "r" in request.cookies:
|
|
|
|
print("Referer: " + request.cookies["r"])
|
|
|
|
return render_template(
|
|
|
|
"index.html", hidden=request.cookies["r"], address=address
|
|
|
|
)
|
|
|
|
|
|
|
|
if "r" in params:
|
|
|
|
print("Referer: " + params["r"])
|
2023-11-08 13:33:17 +11:00
|
|
|
# Set cookie
|
2024-07-23 10:03:30 +10:00
|
|
|
resp = make_response(
|
|
|
|
render_template("index.html", hidden=params["r"], address=address)
|
|
|
|
)
|
|
|
|
resp.set_cookie("r", params["r"], max_age=60 * 60 * 24)
|
2023-11-08 13:33:17 +11:00
|
|
|
return resp
|
2024-07-23 10:03:30 +10:00
|
|
|
return render_template("index.html", address=address)
|
2023-11-07 23:04:54 +11:00
|
|
|
|
|
|
|
|
2024-07-23 10:03:30 +10:00
|
|
|
@app.route("/", methods=["POST"])
|
2023-11-07 23:04:54 +11:00
|
|
|
def submit():
|
2024-07-23 10:03:30 +10:00
|
|
|
name = request.form["name"]
|
|
|
|
email = request.form["email"]
|
|
|
|
hidden = request.form["hi"]
|
2023-11-07 23:04:54 +11:00
|
|
|
ip = request.remote_addr
|
2023-11-07 23:14:36 +11:00
|
|
|
|
2024-07-23 10:03:30 +10:00
|
|
|
if "X-REAL-IP" in request.headers:
|
|
|
|
ip = request.headers["X-REAL-IP"]
|
2023-11-07 23:14:36 +11:00
|
|
|
|
2024-07-23 10:03:30 +10:00
|
|
|
if "X-Real-Ip" in request.headers:
|
|
|
|
ip = request.headers["X-Real-Ip"]
|
2023-11-07 23:14:36 +11:00
|
|
|
|
2024-07-23 10:03:30 +10:00
|
|
|
if hidden == "":
|
|
|
|
hidden = "None"
|
2023-11-07 23:04:54 +11:00
|
|
|
|
2024-07-23 10:03:30 +10:00
|
|
|
if "r" in request.cookies:
|
|
|
|
hidden = request.cookies["r"]
|
2023-11-08 13:33:17 +11:00
|
|
|
|
2023-11-08 14:49:08 +11:00
|
|
|
# Validate email
|
|
|
|
try:
|
|
|
|
emailinfo = validate_email(email, check_deliverability=False)
|
|
|
|
email = emailinfo.normalized
|
|
|
|
except EmailNotValidError as e:
|
2024-07-23 10:03:30 +10:00
|
|
|
return render_template(
|
|
|
|
"error.html", error="Invalid email address", address=address
|
|
|
|
)
|
2023-11-08 14:49:08 +11:00
|
|
|
|
2023-11-07 23:04:54 +11:00
|
|
|
status = gift.gift(name, email, hidden, ip)
|
2024-07-23 10:03:30 +10:00
|
|
|
print(status, flush=True)
|
2023-11-07 23:11:08 +11:00
|
|
|
|
2023-11-07 23:04:54 +11:00
|
|
|
if status == True:
|
2024-07-23 10:03:30 +10:00
|
|
|
return render_template("success.html", address=address)
|
2023-11-07 23:04:54 +11:00
|
|
|
else:
|
2024-07-23 10:03:30 +10:00
|
|
|
return render_template("error.html", error=status, address=address)
|
|
|
|
|
2023-11-07 23:04:54 +11:00
|
|
|
|
2024-07-23 10:03:30 +10:00
|
|
|
@app.route("/api", methods=["POST"])
|
2023-11-08 15:25:47 +11:00
|
|
|
def api():
|
|
|
|
# Get from params
|
|
|
|
params = request.args
|
2024-07-23 10:03:30 +10:00
|
|
|
name = params["name"]
|
|
|
|
email = params["email"]
|
|
|
|
key = params["key"]
|
2023-11-08 15:25:47 +11:00
|
|
|
|
2024-07-23 10:03:30 +10:00
|
|
|
if key != os.getenv("api_key"):
|
|
|
|
return jsonify({"error": "Invalid API key", "success": False})
|
2023-11-08 15:25:47 +11:00
|
|
|
|
2024-07-23 10:03:30 +10:00
|
|
|
if "X-REAL-IP" in request.headers:
|
|
|
|
ip = request.headers["X-REAL-IP"]
|
2023-11-08 15:25:47 +11:00
|
|
|
|
2024-07-23 10:03:30 +10:00
|
|
|
if "X-Real-Ip" in request.headers:
|
|
|
|
ip = request.headers["X-Real-Ip"]
|
2023-11-08 15:25:47 +11:00
|
|
|
|
|
|
|
# Validate email
|
|
|
|
try:
|
|
|
|
emailinfo = validate_email(email, check_deliverability=False)
|
|
|
|
email = emailinfo.normalized
|
|
|
|
except EmailNotValidError as e:
|
2024-07-23 10:03:30 +10:00
|
|
|
return jsonify({"error": "Invalid email address", "success": False})
|
2023-11-08 15:25:47 +11:00
|
|
|
|
2024-07-23 10:03:30 +10:00
|
|
|
status = gift.gift(name, email, "api", ip, True)
|
|
|
|
print(status, flush=True)
|
2023-11-08 15:25:47 +11:00
|
|
|
|
|
|
|
if status == True:
|
2024-07-23 10:03:30 +10:00
|
|
|
return jsonify({"success": True})
|
2023-11-08 15:25:47 +11:00
|
|
|
else:
|
2024-07-23 10:03:30 +10:00
|
|
|
return jsonify({"error": status, "success": False})
|
2023-11-08 15:25:47 +11:00
|
|
|
|
|
|
|
|
2023-11-07 23:04:54 +11:00
|
|
|
# Special routes
|
2024-07-23 10:03:30 +10:00
|
|
|
@app.route("/.well-known/wallets/<token>")
|
2023-11-07 23:04:54 +11:00
|
|
|
def send_wallet(token):
|
2024-07-23 10:03:30 +10:00
|
|
|
address = requests.get(
|
|
|
|
"https://nathan.woodburn.au/.well-known/wallets/" + token
|
|
|
|
).text
|
|
|
|
return make_response(address, 200, {"Content-Type": "text/plain"})
|
2023-11-07 23:04:54 +11:00
|
|
|
|
2024-07-23 10:03:30 +10:00
|
|
|
|
|
|
|
@app.route("/stats")
|
2023-11-08 12:48:17 +11:00
|
|
|
def stats():
|
|
|
|
# Read the file
|
2024-07-23 10:03:30 +10:00
|
|
|
path = "/data/gifts.json"
|
|
|
|
if os.getenv("local") == "true":
|
|
|
|
path = "./gifts.json"
|
2023-11-08 12:48:17 +11:00
|
|
|
|
|
|
|
# Load file
|
|
|
|
gifts = []
|
|
|
|
if os.path.isfile(path):
|
2024-07-23 10:03:30 +10:00
|
|
|
with open(path, "r") as f:
|
|
|
|
gifts = json.load(f)
|
2023-11-08 12:48:17 +11:00
|
|
|
|
|
|
|
# Loop through gifts
|
|
|
|
referals = {}
|
2023-11-08 12:55:10 +11:00
|
|
|
for gift_item in gifts:
|
2024-07-23 10:03:30 +10:00
|
|
|
if gift_item["referer"] not in referals:
|
|
|
|
referals[gift_item["referer"]] = 1
|
2023-11-08 12:48:17 +11:00
|
|
|
else:
|
2024-07-23 10:03:30 +10:00
|
|
|
referals[gift_item["referer"]] += 1
|
|
|
|
|
|
|
|
statsHTML = "Total gifts: " + str(len(gifts)) + "<br><br>"
|
|
|
|
statsHTML += "Referals:<br>"
|
2023-11-08 12:48:17 +11:00
|
|
|
for referal in referals:
|
2024-07-23 10:03:30 +10:00
|
|
|
statsHTML += referal + ": " + str(referals[referal]) + "<br>"
|
2023-11-08 12:48:17 +11:00
|
|
|
|
2024-07-23 10:03:30 +10:00
|
|
|
statsHTML += "<br>Remaining balance: " + str(gift.balance()) + " HNS<br>"
|
2023-11-08 12:48:17 +11:00
|
|
|
|
2024-07-23 10:03:30 +10:00
|
|
|
return render_template("stats.html", address=address, stats=statsHTML)
|
2023-11-08 12:48:17 +11:00
|
|
|
|
2023-11-07 23:04:54 +11:00
|
|
|
|
2024-07-23 10:03:30 +10:00
|
|
|
@app.route("/<path:path>")
|
2023-11-07 23:04:54 +11:00
|
|
|
def catch_all(path):
|
2024-07-23 10:03:30 +10:00
|
|
|
return redirect("/?r=" + path)
|
|
|
|
|
2023-11-07 23:04:54 +11:00
|
|
|
|
|
|
|
# 404 catch all
|
|
|
|
@app.errorhandler(404)
|
|
|
|
def not_found(e):
|
2024-07-23 10:03:30 +10:00
|
|
|
return redirect("/")
|
|
|
|
|
2023-11-07 23:04:54 +11:00
|
|
|
|
2023-11-08 14:01:08 +11:00
|
|
|
def update_address():
|
|
|
|
global address
|
|
|
|
payload = {
|
|
|
|
"asset": "HNS",
|
|
|
|
"timestamp": int(round(time.time() * 1000)),
|
2024-07-23 10:03:30 +10:00
|
|
|
"receiveWindow": 20000,
|
2023-11-08 14:01:08 +11:00
|
|
|
}
|
2024-07-23 10:03:30 +10:00
|
|
|
nbcookie = os.getenv("cookie")
|
2023-11-08 14:01:08 +11:00
|
|
|
cookies = {"namebase-main": nbcookie}
|
|
|
|
headers = {"Accept": "application/json", "Content-Type": "application/json"}
|
2024-07-23 10:03:30 +10:00
|
|
|
r = requests.post(
|
|
|
|
"https://www.namebase.io/api/v0/deposit/address",
|
|
|
|
data=json.dumps(payload),
|
|
|
|
headers=headers,
|
|
|
|
cookies=cookies,
|
|
|
|
)
|
|
|
|
if "address" not in r.json():
|
|
|
|
print("Error: " + r.text, flush=True)
|
2023-12-12 15:10:31 +11:00
|
|
|
# Send alert via discord
|
2024-07-23 10:03:30 +10:00
|
|
|
url = os.getenv("discord_webhook")
|
2023-12-12 15:13:34 +11:00
|
|
|
if url == None:
|
|
|
|
return "No webhook set"
|
2024-07-23 10:03:30 +10:00
|
|
|
|
|
|
|
payload = {"content": "NB cookie has expired\n@everyone"}
|
|
|
|
response = requests.post(
|
|
|
|
url, data=json.dumps(payload), headers={"Content-Type": "application/json"}
|
|
|
|
)
|
2023-12-12 15:13:34 +11:00
|
|
|
|
|
|
|
# Check if the message was sent successfully
|
|
|
|
if response.status_code == 204:
|
|
|
|
print("Message sent successfully!")
|
|
|
|
else:
|
|
|
|
print(f"Failed to send message. Status code: {response.status_code}")
|
|
|
|
print(response.text)
|
2023-12-12 15:10:31 +11:00
|
|
|
return
|
|
|
|
|
2024-07-23 10:03:30 +10:00
|
|
|
address = r.json()["address"]
|
|
|
|
print("Address updated: " + address, flush=True)
|
2023-11-08 14:01:08 +11:00
|
|
|
|
|
|
|
|
|
|
|
update_address()
|
|
|
|
|
|
|
|
|
2024-07-23 10:03:30 +10:00
|
|
|
if __name__ == "__main__":
|
|
|
|
app.run(debug=False, port=5000, host="0.0.0.0")
|