From 859c88fb97e21ba152b2a59630e8080452095a2f Mon Sep 17 00:00:00 2001 From: Nathan Woodburn Date: Thu, 3 Oct 2024 14:06:42 +1000 Subject: [PATCH] feat: Add web server --- server.py | 77 ++++++++++++++++++++++++++++++++++++++++++++ templates/index.html | 12 +++++-- 2 files changed, 87 insertions(+), 2 deletions(-) diff --git a/server.py b/server.py index 240a1a8..855cb83 100644 --- a/server.py +++ b/server.py @@ -15,11 +15,21 @@ import json import requests from datetime import datetime import dotenv +import re dotenv.load_dotenv() app = Flask(__name__) +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" +] def find(name, path): for root, dirs, files in os.walk(path): @@ -74,8 +84,75 @@ def wellknown(path): # region Main routes @app.route("/") def index(): + if request.args.get("error"): + return render_template("index.html", error=errors[request.args.get("error")]) + return render_template("index.html") +@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") + + return redirect("https://pay.hns.au/p/renew?amount=10&data={domain}&redirect=https://renew.hns.au") + +@app.route("/renew/", 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() + + output = {"success": True,"message": f"Renewing {data["data"]}","output":req} + # Send discord webhook + webhook = os.getenv("DISCORD") + if webhook: + requests.post(webhook, json={"content": json.dumps(output)}) + + return jsonify(output) @app.route("/") def catch_all(path: str): diff --git a/templates/index.html b/templates/index.html index bb349f8..478d58f 100644 --- a/templates/index.html +++ b/templates/index.html @@ -4,7 +4,7 @@ - Nathan.Woodburn/ + HS-Anyone | Nathan.Woodburn/ @@ -12,7 +12,15 @@
-

Nathan.Woodburn/

+

{{error}}

+ +

Renew an anyone can renew domain

+

This is a service to help anyone renew a domain name.

+

To renew a domain name, you will need to enter the domain below. You will be redirected to pay 10 HNS to cover the cost of the renewal.

+
+ + +