forked from yuguorui/aliyun-ddns-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathddns.py
84 lines (71 loc) · 3.64 KB
/
ddns.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
#!/usr/bin/env python
# coding=utf-8
"""
Copyright (C) 2010-2013, Ryan Fan
Modified by Guorui Yu.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Library General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
"""
from utils import DDNSUtils
from config import DDNSConfig
from record import DDNSDomainRecordManager
from socket import AF_INET, AF_INET6
def main():
"""
Main routine
"""
config = DDNSConfig()
record_manager = DDNSDomainRecordManager(config)
for local_record in record_manager.local_record_list:
family = AF_INET if local_record.type == 'A' else AF_INET6
interface = local_record.interface
if interface is None:
current_public_ip = {AF_INET: DDNSUtils.get_current_public_ip(),
AF_INET6: DDNSUtils.get_current_public_ipv6()}
else:
current_public_ip = {AF_INET: DDNSUtils.get_interface_address(interface),
AF_INET6: DDNSUtils.get_interface_address(interface, AF_INET6)}
if not current_public_ip:
DDNSUtils.info(
"Unable to get current IP for [{rec.subdomain}.{rec.domainname}.{rec.type}]".format(rec=local_record))
continue
dns_resolved_ip = DDNSUtils.get_dns_resolved_ip(local_record.subdomain,
local_record.domainname,
family)
if current_public_ip[family] == dns_resolved_ip:
DDNSUtils.info("Skipped as no changes for DomainRecord" \
"[{rec.subdomain}.{rec.domainname}.{rec.type}]".format(rec=local_record))
continue
# If current public IP doesn't equal to current DNS resolved ip, only in three cases:
# 1. The new synced IP for remote record in Aliyun doesn't take effect yet
# 2. remote record's IP in Aliyun server has changed
# 3. current public IP is changed
remote_record = record_manager.fetch_remote_record(local_record)
if not remote_record:
DDNSUtils.err("Failed finding remote DomainRecord" \
"[{rec.subdomain}.{rec.domainname}]".format(rec=local_record))
continue
if current_public_ip[family] == remote_record.value:
DDNSUtils.info("Skipped as we already updated DomainRecord" \
"[{rec.subdomain}.{rec.domainname}]".format(rec=local_record))
continue
# if we can fetch remote record and record's value doesn't equal to public IP
record_type = 'A' if family == AF_INET else 'AAAA'
sync_result = record_manager.update(remote_record, current_public_ip[family], record_type)
if not sync_result:
DDNSUtils.err("Failed updating DomainRecord" \
"[{rec.subdomain}.{rec.domainname}]".format(rec=local_record))
else:
DDNSUtils.info("Successfully updated DomainRecord" \
"[{rec.subdomain}.{rec.domainname}]".format(rec=local_record))
if __name__ == "__main__":
main()