hs-anyone-web/server.py

199 lines
5.7 KiB
Python
Raw Normal View History

2024-10-03 13:01:10 +10:00
from functools import cache
import json
from flask import (
Flask,
make_response,
redirect,
request,
jsonify,
render_template,
send_from_directory,
send_file,
)
import os
import json
import requests
from datetime import datetime
import dotenv
2024-10-03 14:06:42 +10:00
import re
2024-10-03 13:01:10 +10:00
dotenv.load_dotenv()
app = Flask(__name__)
2024-10-03 14:06:42 +10:00
errors = {
"no_domain": "No domain provided",
"invalid_domain": "Invalid domain provided (make sure the domain is just the TLD)",
"api_error": "API error",
"not_anyone": "The domain is not owned by an anyone can renew script",
}
allowed_owners = [
"hs1qu3nrzrjkd783ftpk7l4hvpa96aazx5dddw66hgs2zuukckcchrqsw3f8kc"
]
2024-10-03 13:01:10 +10:00
def find(name, path):
for root, dirs, files in os.walk(path):
if name in files:
return os.path.join(root, name)
# Assets routes
@app.route("/assets/<path:path>")
def send_assets(path):
if path.endswith(".json"):
return send_from_directory(
"templates/assets", path, mimetype="application/json"
)
if os.path.isfile("templates/assets/" + path):
return send_from_directory("templates/assets", path)
# Try looking in one of the directories
filename: str = path.split("/")[-1]
if (
filename.endswith(".png")
or filename.endswith(".jpg")
or filename.endswith(".jpeg")
or filename.endswith(".svg")
):
if os.path.isfile("templates/assets/img/" + filename):
return send_from_directory("templates/assets/img", filename)
if os.path.isfile("templates/assets/img/favicon/" + filename):
return send_from_directory("templates/assets/img/favicon", filename)
return render_template("404.html"), 404
# region Special routes
@app.route("/favicon.png")
def faviconPNG():
return send_from_directory("templates/assets/img", "favicon.png")
@app.route("/.well-known/<path:path>")
def wellknown(path):
# Try to proxy to https://nathan.woodburn.au/.well-known/
req = requests.get(f"https://nathan.woodburn.au/.well-known/{path}")
return make_response(
req.content, 200, {"Content-Type": req.headers["Content-Type"]}
)
# endregion
# region Main routes
@app.route("/")
def index():
2024-10-03 14:06:42 +10:00
if request.args.get("error"):
return render_template("index.html", error=errors[request.args.get("error")])
2024-10-03 14:17:50 +10:00
if request.args.get("message"):
return render_template("index.html", message=request.args.get("message"))
2024-10-03 13:01:10 +10:00
return render_template("index.html")
2024-10-03 14:06:42 +10:00
@app.route("/renew", methods=["POST"])
def renew():
domain = request.form.get("domain")
if not domain:
return redirect("/?error=no_domain")
# Double check the domain is valid
domain = domain.lower()
domain = domain.removeprefix(".")
domain = domain.removesuffix("/")
if domain.count(".") > 0 or domain.count("/") > 0:
return redirect("/?error=invalid_domain")
# Regex to check if the TLD is valid (only letters and numbers (or xn--...)
if not re.match(r"^[a-zA-Z0-9-]+$", domain):
return redirect("/?error=invalid_domain")
# Check the owner is correct
req = requests.get(f"https://api.niami.io/hsd/{domain}")
if req.status_code != 200:
return redirect("/?error=api_error")
req = req.json()
if req["success"] != True:
return redirect("/?error=api_error")
if req["data"]["owner_tx_data"]["address"] not in allowed_owners:
return redirect("/?error=not_anyone")
2024-10-03 15:33:08 +10:00
return redirect(f"https://pay.hns.au/p/renew?amount=10&data={domain}&redirect=https://renew.hns.au/?message=Pending:%20The%20domain%20will%20renew%20when%20the%20payment%20has%20arrived")
2024-10-03 14:06:42 +10:00
@app.route("/renew/<path:path>", methods=["POST"])
def renew_path(path: str):
# Read path from env
renew_path = os.getenv("RENEW_PATH")
# Verify path
if renew_path != path:
return jsonify({"error": "Invalid path"}), 400
# get post data
data = request.get_json()
if not data:
return jsonify({"error": "No data provided"}), 400
# Get amount
amount = data["amount"]
if not amount:
return jsonify({"error": "No amount provided"}), 400
if amount < 10:
return jsonify({"error": "Amount too low"}), 400
# GET HS-anyone api route
api = os.getenv("API")
req = requests.post(api, json={"domain": data["data"]})
if req.status_code != 200:
return jsonify({"error": "API error"}), 400
req = req.json()
2024-10-03 14:10:45 +10:00
output = {"success": True,"message": f'Renewing {data["data"]}',"output":req}
2024-10-03 14:06:42 +10:00
# Send discord webhook
webhook = os.getenv("DISCORD")
if webhook:
2024-10-03 15:33:08 +10:00
# Parse output
2024-10-03 16:01:54 +10:00
message = f'Renewing {data["data"]}'
2024-10-03 15:59:05 +10:00
if req["error"] != "":
message += "\n\nError: " + req["error"]
message += "\n\nTX: https://hns.cymon.de/tx/" + req["output"].split("'")[1]
2024-10-03 15:33:08 +10:00
2024-10-03 16:01:54 +10:00
requests.post(webhook, json={"content": message})
2024-10-03 14:06:42 +10:00
return jsonify(output)
2024-10-03 13:01:10 +10:00
@app.route("/<path:path>")
def catch_all(path: str):
if os.path.isfile("templates/" + path):
return render_template(path)
# Try with .html
if os.path.isfile("templates/" + path + ".html"):
return render_template(path + ".html")
if os.path.isfile("templates/" + path.strip("/") + ".html"):
return render_template(path.strip("/") + ".html")
# Try to find a file matching
if path.count("/") < 1:
# Try to find a file matching
filename = find(path, "templates")
if filename:
return send_file(filename)
return render_template("404.html"), 404
# endregion
# region Error Catching
# 404 catch all
@app.errorhandler(404)
def not_found(e):
return render_template("404.html"), 404
# endregion
if __name__ == "__main__":
app.run(debug=True, port=5000, host="0.0.0.0")