feat: Add web server
All checks were successful
Build Docker / BuildImage (push) Successful in 30s

This commit is contained in:
Nathan Woodburn 2024-10-03 14:06:42 +10:00
parent 3bf9291cf2
commit 859c88fb97
Signed by: nathanwoodburn
GPG Key ID: 203B000478AD0EF1
2 changed files with 87 additions and 2 deletions

View File

@ -15,11 +15,21 @@ import json
import requests import requests
from datetime import datetime from datetime import datetime
import dotenv import dotenv
import re
dotenv.load_dotenv() dotenv.load_dotenv()
app = Flask(__name__) 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): def find(name, path):
for root, dirs, files in os.walk(path): for root, dirs, files in os.walk(path):
@ -74,8 +84,75 @@ def wellknown(path):
# region Main routes # region Main routes
@app.route("/") @app.route("/")
def index(): def index():
if request.args.get("error"):
return render_template("index.html", error=errors[request.args.get("error")])
return render_template("index.html") 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/<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()
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("/<path:path>") @app.route("/<path:path>")
def catch_all(path: str): def catch_all(path: str):

View File

@ -4,7 +4,7 @@
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Nathan.Woodburn/</title> <title>HS-Anyone | Nathan.Woodburn/</title>
<link rel="icon" href="/assets/img/favicon.png" type="image/png"> <link rel="icon" href="/assets/img/favicon.png" type="image/png">
<link rel="stylesheet" href="/assets/css/index.css"> <link rel="stylesheet" href="/assets/css/index.css">
</head> </head>
@ -12,7 +12,15 @@
<body> <body>
<div class="spacer"></div> <div class="spacer"></div>
<div class="centre"> <div class="centre">
<h1>Nathan.Woodburn/</h1> <h2 style="color: red;">{{error}}</h2>
<h1>Renew an anyone can renew domain</h1>
<p>This is a service to help anyone renew a domain name.</p>
<p>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.</p>
<form action="/renew" method="POST">
<input type="text" name="domain" placeholder="exampledomain">
<button type="submit">Renew</button>
</form>
</div> </div>
</body> </body>