-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
app.py
64 lines (46 loc) · 2.79 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import os
import CloudFlare
import waitress
import flask
app = flask.Flask(__name__)
@app.route('/', methods=['GET'])
def main():
token = flask.request.args.get('token')
zone = flask.request.args.get('zone')
record = flask.request.args.get('record')
ipv4 = flask.request.args.get('ipv4')
ipv6 = flask.request.args.get('ipv6')
cf = CloudFlare.CloudFlare(token=token)
if not token:
return flask.jsonify({'status': 'error', 'message': 'Missing token URL parameter.'}), 400
if not zone:
return flask.jsonify({'status': 'error', 'message': 'Missing zone URL parameter.'}), 400
if not ipv4 and not ipv6:
return flask.jsonify({'status': 'error', 'message': 'Missing ipv4 or ipv6 URL parameter.'}), 400
try:
zones = cf.zones.get(params={'name': zone})
if not zones:
return flask.jsonify({'status': 'error', 'message': 'Zone {} does not exist.'.format(zone)}), 404
record_zone_concat = '{}.{}'.format(record, zone) if record is not None else zone
a_record = cf.zones.dns_records.get(zones[0]['id'], params={
'name': record_zone_concat, 'match': 'all', 'type': 'A'})
aaaa_record = cf.zones.dns_records.get(zones[0]['id'], params={
'name': record_zone_concat, 'match': 'all', 'type': 'AAAA'})
if ipv4 is not None and not a_record:
return flask.jsonify({'status': 'error', 'message': f'A record for {record_zone_concat} does not exist.'}), 404
if ipv6 is not None and not aaaa_record:
return flask.jsonify({'status': 'error', 'message': f'AAAA record for {record_zone_concat} does not exist.'}), 404
if ipv4 is not None and a_record[0]['content'] != ipv4:
cf.zones.dns_records.put(zones[0]['id'], a_record[0]['id'], data={
'name': a_record[0]['name'], 'type': 'A', 'content': ipv4, 'proxied': a_record[0]['proxied'], 'ttl': a_record[0]['ttl']})
if ipv6 is not None and aaaa_record[0]['content'] != ipv6:
cf.zones.dns_records.put(zones[0]['id'], aaaa_record[0]['id'], data={
'name': aaaa_record[0]['name'], 'type': 'AAAA', 'content': ipv6, 'proxied': aaaa_record[0]['proxied'], 'ttl': aaaa_record[0]['ttl']})
except CloudFlare.exceptions.CloudFlareAPIError as e:
return flask.jsonify({'status': 'error', 'message': str(e)}), 500
return flask.jsonify({'status': 'success', 'message': 'Update successful.'}), 200
@app.route('/healthz', methods=['GET'])
def healthz():
return flask.jsonify({'status': 'success', 'message': 'OK'}), 200
app.secret_key = os.urandom(24)
waitress.serve(app, host='0.0.0.0', port=80)