forked from NewFuture/DDNS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
executable file
·94 lines (79 loc) · 2.34 KB
/
run.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
#!/usr/bin/env python
#-*- coding:utf-8 -*-
"""
自动更新DNS
@author: New Future
"""
from __future__ import print_function
import argparse
import json
import time
import os
import tempfile
from dns import alidns, dnspod
from util import ip
from util.cache import Cache
CACHE_FILE = os.path.join(tempfile.gettempdir(), 'ddns.cache')
def get_config(key=None, default=None, path="config.json"):
"""
读取配置
"""
if not hasattr(get_config, "config"):
try:
with open(path) as configfile:
get_config.config = json.load(configfile)
get_config.time = os.stat(path).st_mtime
except Exception:
exit('fail to load config from file: %s' % path)
if key:
return get_config.config.get(key, default)
else:
return get_config.config
def update_ip(ip_type, cache, dns):
"""
更新IP
"""
ipname = 'ipv' + ip_type
domains = get_config(ipname)
if not domains:
return None
index = get_config('index' + ip_type) or "default"
if str(index).isdigit():
value = getattr(ip, "local_v" + ip_type)(index)
else:
value = getattr(ip, index + "_v" + ip_type)()
if value is None:
return False
elif cache and value == cache[ipname]:
print('.', end=" ")
else:
print ('update %s to: %s' % (ipname, value))
record_type = (ip_type == '4') and 'A' or 'AAAA'
for domain in domains:
print (dns.update_record(domain, value, record_type=record_type))
if cache is not False:
cache[ipname] = value
def main():
"""
更新
"""
parser = argparse.ArgumentParser()
parser.add_argument('-c', default="config.json")
get_config(path=parser.parse_args().c)
if get_config('dns', 'dnspod').startswith('ali'):
dns = alidns
else:
dns = dnspod
dns.ID, dns.TOKEN = get_config('id'), get_config('token')
dns.PROXY = get_config('proxy')
ip.DEBUG = get_config('debug')
cache = get_config('cache', True) and Cache(CACHE_FILE)
if cache is False:
print("Cache is disabled!")
elif len(cache) < 1 or get_config.time >= cache.time:
cache.clear()
print ("=" * 25, time.ctime(), "=" * 25, sep=' ')
update_ip('4', cache, dns)
update_ip('6', cache, dns)
if __name__ == '__main__':
main()