-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcloudns_updater.py
128 lines (99 loc) · 3.92 KB
/
cloudns_updater.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
128
# -*- coding: utf-8 -*-
""" ClouDNS Updater """
__version__ = "1.0.0"
__author__ = "Jonathan Gonzalez"
__email__ = "j@0x30.io"
__date__ = "2023-02-20"
# pylint: disable=W0703,C0301
# - Standard libraries
from typing import Any, Dict, List
# - Third-party libraries
import requests
# - ---------------------------------------------------------------------------
# - PLEASE, CONFIGURE THE FOLLOWING FOUR (4) VARIABLES WITH YOUR OWN DETAILS
# - ---------------------------------------------------------------------------
# - Variables
_CLOUDNS_AUTH_ID: str = "NNNN"
_CLOUDNS_AUTH_PASSWORD: str = "aaaabbbbccccddddeeeeffffgggghhhhiiiijjjjkkkkllll"
_CLOUDNS_DOMAIN_NAME: str = "domain.tld"
_CLOUDNS_HOSTNAME: str = "hostname"
# - ---------------------------------------------------------------------------
# - WARNING: DO NOT CHANGE ANYTHING BELOW THIS POINT
# - ---------------------------------------------------------------------------
def get_current_ip() -> str:
"""Obtain currently configured IP
Returns:
str: IP address currently configured in ClouDNS servers
"""
response: str = ""
payload: Dict[str, str] = {"auth-id": _CLOUDNS_AUTH_ID, "auth-password": _CLOUDNS_AUTH_PASSWORD}
try:
resp = requests.get("https://api.cloudns.net/ip/get-my-ip.json", params=payload, timeout=60)
response = dict(resp.json())["ip"]
except requests.ConnectionError as conn_err:
print(f"Error: {conn_err}")
except Exception as broad_err:
print(f"Error: {broad_err}")
return response
def get_dns_record() -> List[str]:
"""Obtain DNS record for the domain in use
Returns:
List[str]: Obtains the ClouDNS record for an specific domain/host
"""
response: List[str] = []
payload: Dict[str, str] = {
"auth-id": _CLOUDNS_AUTH_ID,
"auth-password": _CLOUDNS_AUTH_PASSWORD,
"domain-name": _CLOUDNS_DOMAIN_NAME,
"host": _CLOUDNS_HOSTNAME,
}
try:
resp = requests.get("https://api.cloudns.net/dns/records.json", params=payload, timeout=60)
zone_record_id: str = list(resp.json())[0]
dns_data: Dict[Any, Any] = resp.json()
record_data: Any = dns_data[zone_record_id]
dns_ip: str = record_data.get("record")
response = [zone_record_id, dns_ip]
except requests.ConnectionError as conn_err:
print(f"Error: {conn_err}")
except Exception as broad_err:
print(f"Error: {broad_err}")
return response
def update_dns_record(*args) -> str:
"""Update the DNS record with the new IP detected
Returns: a string indicating actions performed in ClouDNS records
"""
response: str = ""
payload: Dict[str, str] = {
"auth-id": _CLOUDNS_AUTH_ID,
"auth-password": _CLOUDNS_AUTH_PASSWORD,
"domain-name": _CLOUDNS_DOMAIN_NAME,
"host": _CLOUDNS_HOSTNAME,
"record-id": args[0], # this is the Record-ID
"record": args[1], # this is the IP Address
"ttl": "60",
}
try:
resp = requests.post("https://api.cloudns.net/dns/mod-record.json", params=payload, timeout=60)
response = resp.text
except requests.ConnectionError as conn_err:
print(f"Error: {conn_err}")
except Exception as broad_err:
print(f"Error: {broad_err}")
return response
def main() -> None:
"""Main loop
Returns: None
"""
_current_ip: str = get_current_ip()
_dns_record: List[str] = get_dns_record()
print(f"Current IP: {_current_ip}")
print(f"IP found in DNS: {_dns_record[1]}")
if _dns_record[1] != _current_ip:
print("-> Updaing IP in DNS.")
response: str = update_dns_record(_dns_record[0], _current_ip)
print(f"-> ClouDNS response data: {response}")
else:
print("-> Same IP, nothing to change.")
if __name__ == "__main__":
main()