-
Notifications
You must be signed in to change notification settings - Fork 0
/
pyNameCheap.py
83 lines (67 loc) · 3.49 KB
/
pyNameCheap.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
import os
import time
import logging
import ipaddress
import requests
logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s")
logger = logging.getLogger()
app_name_text = os.path.basename(__file__).replace(".py", "")
release_version = os.environ.get("RELEASE_VERSION", "unknown")
logger.info(f"{'*' * 50}\n")
logger.info(f"{app_name_text} Version: {release_version}\n")
logger.info(f"{'*' * 50}")
api_endpoints = ["https://api.ipify.org", "http://wtfismyip.com/text"]
domain = os.environ.get("domain", "domain.com")
hosts = os.environ.get("hosts", "a,b,c")
ddns_password = os.environ.get("ddns_password", "password")
refresh_interval = float(os.environ.get("refresh_interval", 600))
def get_public_ip(api_endpoint):
try:
ip = requests.get(api_endpoint, timeout=5).text.replace("\n", "")
ipaddress.ip_address(ip)
logger.info(f"Found IP Address: {ip} from {api_endpoint}")
return ip.strip()
except Exception as e:
logger.error(f"Error getting public IP from {api_endpoint}, Error: {e}")
return None
def update_dynamic_dns(ip):
for host in hosts.split(","):
try:
url_template = "https://dynamicdns.park-your-domain.com/update?host={}&domain={}&password={}&ip={}"
url = url_template.format(host.strip(), domain, ddns_password, ip or "")
req = requests.get(url, timeout=5)
if req.status_code == 200:
try:
start_index = req.text.find("<ErrCount>")
end_index = req.text.find("</ErrCount>", start_index)
err_count = int(req.text[start_index + len("<ErrCount>") : end_index])
if err_count > 0:
start_index = req.text.find("<ResponseString>")
end_index = req.text.find("</ResponseString>", start_index)
response_string = req.text[start_index + len("<ResponseString>") : end_index]
if "A record not Found" in response_string:
error_string = "Wrong Sub Domain Name"
elif "password" in response_string:
error_string = "Wrong DDNS Password"
elif "domain name(s)" in response_string:
error_string = "Wrong Domain Name"
else:
error_string = "Unknown Error"
logger.error(f"{error_string} for {host}.{domain}")
else:
logger.info(f"Dynamic Address update successful for {host}.{domain}")
except Exception as e:
logger.warning(f"Failed to parse XML response: {e}")
logger.info(f"Dynamic Address update successful for {host}.{domain}")
else:
logger.error(f"Failed to update Dynamic Address for {host}.{domain}, HTTP Status Code: {req.status_code}")
except Exception as e:
logger.error(f"Error updating Dynamic Address for {host}.{domain}: {e}")
while True:
logger.info("Getting IP Address")
ip = next((ip for api in api_endpoints if (ip := get_public_ip(api)) is not None), None)
if not ip:
logger.warning("No valid IP Address Found, letting it be obtained automatically")
update_dynamic_dns(ip)
logger.info(f"Sleeping for {refresh_interval} seconds")
time.sleep(refresh_interval)