-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
127 lines (106 loc) · 4.25 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/python3
import os
import CloudFlare
from flask import Flask, request
from flask_httpauth import HTTPBasicAuth
from werkzeug.security import generate_password_hash, check_password_hash
app = Flask(__name__)
auth = HTTPBasicAuth()
# Set variables from environment variables
auth_user = os.environ.get('AUTH_USER', None)
auth_pass = os.environ.get('AUTH_PASS', None)
api_token = os.environ.get('API_TOKEN', None)
record_type = os.environ.get('RECORD_TYPE', 'A')
record_ttl = os.environ.get('RECORD_TTL', 60)
users = {
auth_user: generate_password_hash(auth_pass)
}
@auth.verify_password
def verify_password(username, password): # pylint: disable=inconsistent-return-statements
if username in users and check_password_hash(users.get(username), password):
return username
@auth.error_handler
def unauthorized():
log_msg('Authentication failed')
return "badauth"
@app.route('/update')
@app.route('/nic/update')
@auth.login_required
def main():
# Set hostname variable
if 'hostname' in request.args:
hostname = request.args.get('hostname')
else:
hostname = 'blank'
# Set ip variable
if 'myip' in request.args:
ip = request.args.get('myip')
else:
ip = 'blank'
# Test inputs to determine the next step
if hostname == 'blank':
log_msg('Incoming request did not contain an IP')
response = "nohost"
elif ip == 'blank':
log_msg('Incoming request did not contain an IP')
response = "noip"
else:
log_msg('Received update request for ' + hostname + ' (' + ip + ')')
response = check_cloudflare(hostname, ip)
return response
# Cloudflare Functions
def check_cloudflare(hostname, ip):
# Check if API token is set appropriately
if api_token not in (None, ''):
record_name = hostname
# Determine the DNS zone from the supplied hostname
hostname_split = hostname.split('.')
zone_name = '.'.join(hostname_split[1:])
# Initialize the Cloudflare API
cf = CloudFlare.CloudFlare(token=api_token)
# Get the DNS zone ID
try:
zones = cf.zones.get(params={'name': zone_name})
if len(zones) == 0:
log_msg('Zone ' + zone_name + ' not found')
zone_id = zones[0]['id']
except CloudFlare.exceptions.CloudFlareAPIError as e:
log_msg('/zones.get %d %s' % (e, e)) # pylint: disable=bad-string-format-type, consider-using-f-string
# Get the DNS record ID
try:
dns_records = cf.zones.dns_records.get(zone_id, params={'name': record_name, 'type': record_type})
if len(dns_records) == 0:
log_msg('DNS record ' + record_name + ' not found')
record_id = dns_records[0]['id']
record_content = dns_records[0]['content']
record_ttl_current = dns_records[0]['ttl']
except CloudFlare.exceptions.CloudFlareAPIError as e:
log_msg('/zones.dns_records.get %d %s' % (e, e)) # pylint: disable=bad-string-format-type, consider-using-f-string
# Test if the record needs updating
if record_content != ip or record_ttl_current != record_ttl:
log_msg("A DNS record update is needed for " + record_name)
# Update the DNS record
dns_record = {
'type': record_type,
'name': record_name,
'content': ip,
'ttl': record_ttl
}
try:
cf.zones.dns_records.put(zone_id, record_id, data=dns_record)
log_msg('DNS record updated successfully: ' + record_name + ' (' + ip + ')')
response = "good " + ip
except CloudFlare.exceptions.CloudFlareAPIError as e:
log_msg('/zones.dns_records.put %d %s' % (e, e)) # pylint: disable=bad-string-format-type, consider-using-f-string
response = "dnserr"
else:
log_msg('No update needed for ' + hostname + ' (' + ip + ')')
response = "nochg " + ip
else:
log_msg('No api token has been configured for Cloudflare')
response = "noapitoken"
return response
def log_msg(msg):
print(msg)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080, debug=True)