mirror of
https://github.com/NewFuture/DDNS.git
synced 2022-04-19 20:24:44 +00:00
更新缓存读写和dns切换
This commit is contained in:
@@ -1,3 +1,7 @@
|
||||
config.*.json
|
||||
|
||||
.vscode
|
||||
|
||||
# Byte-compiled / optimized / DLL files
|
||||
__pycache__/
|
||||
*.py[cod]
|
||||
|
||||
@@ -18,7 +18,7 @@ DDNS
|
||||
* [x] 阿里DNS
|
||||
|
||||
### TODO:
|
||||
* [ ] 文件缓存(减少服务器IP请求)
|
||||
* [x] 文件缓存(减少服务器IP请求)
|
||||
* [ ] 同线路多记录支持
|
||||
* [ ] socks代理
|
||||
* [ ] 多代理自动切换
|
||||
@@ -61,6 +61,7 @@ python run.py -c /path/to/config.json
|
||||
{
|
||||
"id": "12345",
|
||||
"token": "mythokenkey",
|
||||
"dns": "dnspod 或者 alidns",
|
||||
"ipv4": [
|
||||
"dns.newfuture.xyz",
|
||||
"ipv4.dns.newfuture.xyz"
|
||||
|
||||
@@ -14,41 +14,40 @@ from dns import alidns, dnspod
|
||||
from util import ip
|
||||
from util.cache import Cache
|
||||
|
||||
DNS = dnspod
|
||||
CACHE_FILE = os.path.join(tempfile.gettempdir(), 'ddns.cache')
|
||||
|
||||
|
||||
def get_config(key=None, default=None, file="config.json"):
|
||||
def get_config(key=None, default=None, path="config.json"):
|
||||
"""
|
||||
读取配置
|
||||
"""
|
||||
if not hasattr(get_config, "config"):
|
||||
try:
|
||||
with open(file) as configfile:
|
||||
with open(path) as configfile:
|
||||
get_config.config = json.load(configfile)
|
||||
except:
|
||||
exit('fail to load config from file: %s' % file)
|
||||
except IOError:
|
||||
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(Type, cache):
|
||||
def update_ip(ip_type, cache, dns):
|
||||
"""
|
||||
更新IP
|
||||
"""
|
||||
ipname = 'ipv' + Type
|
||||
ipname = 'ipv' + ip_type
|
||||
domains = get_config(ipname)
|
||||
if not domains:
|
||||
return None
|
||||
|
||||
index = get_config('index' + Type) or "default"
|
||||
index = get_config('index' + ip_type) or "default"
|
||||
if str(index).isdigit():
|
||||
value = getattr(ip, "local_v" + Type)(index)
|
||||
value = getattr(ip, "local_v" + ip_type)(index)
|
||||
|
||||
else:
|
||||
value = getattr(ip, index + "_v" + Type)()
|
||||
value = getattr(ip, index + "_v" + ip_type)()
|
||||
|
||||
if value is None:
|
||||
return False
|
||||
@@ -57,9 +56,9 @@ def update_ip(Type, cache):
|
||||
else:
|
||||
cache[ipname] = value
|
||||
print 'update %s to: %s' % (ipname, value)
|
||||
record_type = (Type == '4') and 'A' or 'AAAA'
|
||||
record_type = (ip_type == '4') and 'A' or 'AAAA'
|
||||
for domain in domains:
|
||||
print DNS.update_record(domain, value, record_type=record_type)
|
||||
print dns.update_record(domain, value, record_type=record_type)
|
||||
|
||||
|
||||
def main():
|
||||
@@ -68,19 +67,21 @@ def main():
|
||||
"""
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('-c', default="config.json")
|
||||
get_config(file=parser.parse_args().c)
|
||||
get_config(path=parser.parse_args().c)
|
||||
if get_config('dns', 'dnspod').startswith('ali'):
|
||||
DNS = alidns
|
||||
DNS.ID, DNS.TOKEN = get_config('id'), get_config('token')
|
||||
DNS.PROXY = get_config('proxy')
|
||||
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 = Cache(CACHE_FILE)
|
||||
if len(cache) < 1:
|
||||
print "=" * 25 + " " + time.ctime() + " " + "=" * 25
|
||||
|
||||
update_ip('4', cache)
|
||||
update_ip('6', cache)
|
||||
update_ip('4', cache, dns)
|
||||
update_ip('6', cache, dns)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
Reference in New Issue
Block a user