diff --git a/account.py b/account.py index 1370721..76fbc96 100644 --- a/account.py +++ b/account.py @@ -201,6 +201,46 @@ def getDNS(domain: str): return response['result']['records'] +def setDNS(account,domain,records): + account_name = check_account(account) + password = ":".join(account.split(":")[1:]) + + if account_name == False: + return { + "error": "Invalid account" + } + + records = json.loads(records) + newRecords = [] + TXTRecords = [] + for record in records: + if record['type'] == 'TXT': + TXTRecords.append(record['value']) + elif record['type'] == 'NS': + newRecords.append({ + 'type': 'NS', + 'ns': record['value'] + }) + elif record['type'] in ['GLUE4','GLUE6',"SYNTH4","SYNTH6"]: + newRecords.append({ + 'type': record['type'], + 'ns': str(record['value']).split(' ')[0], + 'address': str(record['value']).split(' ')[1] + }) + else: + newRecords.append(record) + + if len(TXTRecords) > 0: + newRecords.append({ + 'type': 'TXT', + 'txt': TXTRecords + }) + + data = '{"records":'+str(newRecords).replace("'","\"")+'}' + response = hsw.sendUPDATE(account_name,password,domain,data) + return response + + def getNodeSync(): response = hsd.getInfo() sync = response['chain']['progress']*100 diff --git a/main.py b/main.py index 1526fa8..71f3585 100644 --- a/main.py +++ b/main.py @@ -1,3 +1,4 @@ +import json from flask import Flask, make_response, redirect, request, jsonify, render_template, send_from_directory,send_file import os import dotenv @@ -7,6 +8,7 @@ import render import re from flask_qrcode import QRcode import domainLookup +import urllib.parse dotenv.load_dotenv() @@ -317,7 +319,7 @@ def search(): dns=dns, txs=txs) @app.route('/manage/') -def manage(domain): +def manage(domain: str): # Check if the user is logged in if request.cookies.get("account") is None: return redirect("/login") @@ -341,15 +343,16 @@ def manage(domain): expiry = domain_info['info']['stats']['daysUntilExpire'] dns = account_module.getDNS(domain) + raw_dns = str(dns).replace("'",'"') dns = render.dns(dns) return render_template("manage.html", account=account, sync=account_module.getNodeSync(), - domain=domain,expiry=expiry, dns=dns) + domain=domain,expiry=expiry, dns=dns,raw_dns=urllib.parse.quote(raw_dns)) @app.route('/manage//renew') -def renew(domain): +def renew(domain: str): # Check if the user is logged in if request.cookies.get("account") is None: return redirect("/login") @@ -363,6 +366,90 @@ def renew(domain): return redirect("/success?tx=" + response['hash']) +@app.route('/manage//edit') +def editPage(domain: str): + # Check if the user is logged in + if request.cookies.get("account") is None: + return redirect("/login") + + account = account_module.check_account(request.cookies.get("account")) + if not account: + return redirect("/logout") + + domain = domain.lower() + + own_domains = account_module.getDomains(account) + own_domains = [x['name'] for x in own_domains] + own_domains = [x.lower() for x in own_domains] + if domain not in own_domains: + return redirect("/search?q=" + domain) + + + user_edits = request.args.get("dns") + if user_edits != None: + dns = urllib.parse.unquote(user_edits) + else: + dns = account_module.getDNS(domain) + + dns = json.loads(dns) + + # Check if new records have been added + dnsType = request.args.get("type") + dnsValue = request.args.get("value") + if dnsType != None and dnsValue != None: + if dnsType != "DS": + dns.append({"type": dnsType, "value": dnsValue}) + else: + # Verify the DS record + ds = dnsValue.split(" ") + if len(ds) != 4: + raw_dns = str(dns).replace("'",'"') + return redirect("/manage/" + domain + "/edit?dns=" + urllib.parse.quote(str(raw_dns)) + "&error=Invalid DS record") + + try: + ds[0] = int(ds[0]) + ds[1] = int(ds[1]) + ds[2] = int(ds[2]) + except: + raw_dns = str(dns).replace("'",'"') + return redirect("/manage/" + domain + "/edit?dns=" + urllib.parse.quote(str(raw_dns)) + "&error=Invalid DS record") + finally: + dns.append({"type": dnsType, "keyTag": ds[0], "algorithm": ds[1], "digestType": ds[2], "digest": ds[3]}) + + dns = json.dumps(dns).replace("'",'"') + return redirect("/manage/" + domain + "/edit?dns=" + urllib.parse.quote(dns)) + + raw_dns = str(dns).replace("'",'"') + dns = render.dns(dns,True) + errorMessage = request.args.get("error") + if errorMessage == None: + errorMessage = "" + + + return render_template("edit.html", account=account, sync=account_module.getNodeSync(), + domain=domain, error=errorMessage, + dns=dns,raw_dns=urllib.parse.quote(raw_dns)) + + +@app.route('/manage//edit/save') +def editSave(domain: str): + # Check if the user is logged in + if request.cookies.get("account") is None: + return redirect("/login") + + + if not account_module.check_account(request.cookies.get("account")): + return redirect("/logout") + + domain = domain.lower() + dns = request.args.get("dns") + raw_dns = dns + dns = urllib.parse.unquote(dns) + response = account_module.setDNS(request.cookies.get("account"),domain,dns) + if 'error' in response: + print(response) + return redirect("/manage/" + domain + "/edit?dns="+raw_dns+"&error=" + str(response['error'])) + return redirect("/success?tx=" + response['hash']) @app.route('/auction/') def auction(domain): diff --git a/render.py b/render.py index 6e2e768..f0b83df 100644 --- a/render.py +++ b/render.py @@ -1,6 +1,6 @@ import datetime import json - +import urllib.parse def domains(domains): html = '' @@ -71,9 +71,9 @@ def transactions(txs): return html -def dns(data): +def dns(data, edit=False): html_output = "" - + index = 0 for entry in data: html_output += f"{entry['type']}\n" @@ -101,7 +101,14 @@ def dns(data): value += str(val) + " " html_output += f"{value}\n" + if edit: + # Remove the current entry from the list + keptRecords = str(data[:index] + data[index+1:]).replace("'", '"') + keptRecords = urllib.parse.quote(keptRecords) + html_output += f"Remove\n" + html_output += " \n" + index += 1 return html_output def txs(data): diff --git a/templates/edit.html b/templates/edit.html new file mode 100644 index 0000000..4a01671 --- /dev/null +++ b/templates/edit.html @@ -0,0 +1,132 @@ + + + + + + + Manage - FireWallet + + + + + + + + + + + + + +
+ +
+
+ +

{{error}}

+
+
+
+

{{domain}}/

+
+
+
+
+
+
+

DNS

+ + + + + + + + + + {{dns | safe}} + +
+
+
+
+
+ + + + + + + + + + + + + + + +
TypeValue
+
+
+
Save DNS +
+
+
+
+
+
+ +
+
+
+
+ + + + + \ No newline at end of file diff --git a/templates/manage.html b/templates/manage.html index b868531..7ee36f4 100644 --- a/templates/manage.html +++ b/templates/manage.html @@ -71,7 +71,7 @@
-

DNS

Edit
+

DNS

Edit