This repository has been archived by the owner on May 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
doddns.py
71 lines (51 loc) · 1.51 KB
/
doddns.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
#!/usr/bin/python
from flask import Flask
from flask.ext.httpauth import HTTPBasicAuth
import requests
app = Flask(__name__)
auth = HTTPBasicAuth()
# IMPORTANT CHANGE THIS
USERNAME = "user"
PASSWORD = "password"
API_TOKEN = "token"
DOMAIN = "sub.domain.com"
users = {
USERNAME: PASSWORD
}
@auth.get_password
def get_pw(username):
if username in users:
return users.get(username)
return None
def get(url):
url = 'https://api.digitalocean.com/v2' + url
headers = {'Authorization': "Bearer %s" % API_TOKEN}
resp = requests.get(url, headers=headers, timeout=60)
return resp.json()
def put(url, params={}):
url = 'https://api.digitalocean.com/v2' + url
headers = {'Authorization': "Bearer %s" % API_TOKEN}
resp = requests.put(url, headers=headers, params=params, timeout=60)
return resp.json()
def all_records(domain):
json = get('/domains/%s/records/' % DOMAIN)
return json['domain_records']
def find_a_record_id(records):
for record in records:
if record['type'] == "A":
return record["id"]
def edit_record(domain, record_id, ip):
params = {}
params['type'] = "A"
params['name'] = "@"
params['data'] = ip
put('/domains/%s/records/%s' % (domain, record_id), params=params)
@app.route("/<ip>")
@auth.login_required
def update_ip(ip):
records = all_records(DOMAIN)
record = find_a_record_id(records)
edit_record(DOMAIN, record, ip)
return "Updated IP %s" % ip
if __name__ == "__main__":
app.run(port=8000)