feat: Add acme server for HNSDoH
All checks were successful
Build Docker / Build Image (push) Successful in 47s

This commit is contained in:
Nathan Woodburn 2023-12-22 15:07:24 +11:00
parent d67da4ad06
commit 9305442c4b
Signed by: nathanwoodburn
GPG Key ID: 203B000478AD0EF1
2 changed files with 36 additions and 1 deletions

View File

@ -1,4 +1,5 @@
flask
python-dotenv
gunicorn
requests
requests
cloudflare

View File

@ -2,6 +2,7 @@ from flask import Flask, make_response, redirect, request, jsonify, render_templ
import os
import dotenv
import requests
import CloudFlare
app = Flask(__name__)
dotenv.load_dotenv()
@ -152,6 +153,39 @@ def getAddress():
return address
@app.route('/hnsdoh-acme', methods=['POST'])
def hnsdoh_acme():
# Get the TXT record from the request
if not request.json:
return jsonify({'status': 'error', 'error': 'No JSON data provided'})
if 'txt' not in request.json or 'auth' not in request.json:
return jsonify({'status': 'error', 'error': 'Missing required data'})
txt = request.json['txt']
auth = request.json['auth']
if auth != os.getenv('CF_AUTH'):
return jsonify({'status': 'error', 'error': 'Invalid auth'})
cf = CloudFlare.CloudFlare(token=os.getenv('CF_TOKEN'))
zone = cf.zones.get(params={'name': 'hnsdoh.com'})
zone_id = zone[0]['id']
existing_records = cf.zones.dns_records.get(zone_id, params={'type': 'TXT', 'name': '_acme-challenge.hnsdoh.com'})
# Delete existing TXT records
for record in existing_records:
print(record)
record_id = record['id']
cf.zones.dns_records.delete(zone_id, record_id)
record = cf.zones.dns_records.post(zone_id, data={'type': 'TXT', 'name': '_acme-challenge', 'content': txt})
print(record)
return jsonify({'status': 'success'})
# 404 catch all
@app.errorhandler(404)
def not_found(e):