def run(version): assert (version == 4 or version == 6) dnsType = 'AAAA' if version == 6 else 'A' domains = CONFIG[f'ipv{version}'] if not domains: APP.debug(f'未配置IPv{version},不需要更新。') return try: APP.debug('*' * 40 + f'IPv{version}' + '*' * 40) newIP = getIp(version) APP.debug(f'解析IPv{version}结果为:{newIP}') changedDomains = [] for subDomain in domains: oldIp = getOldIP(version, subDomain) if newIP != oldIp or CONFIG['force']: APP.info(f'域名{subDomain}的IPv{version}已发生改变,上次地址为{oldIp}') result = refreshRecord(subDomain, newIP, version) status = result['status'] if status['code'] != '1': raise RuntimeError('{}-{}'.format(status['code'], status['message'])) APP.info(f'域名{subDomain}的{dnsType}纪录已经更新为{newIP}') changedDomains.append(subDomain) else: APP.debug(f'域名{subDomain}的{dnsType}纪录未发生改变') if not CONFIG['dry']: saveIP(version, newIP, domains) if changedDomains: notify({ 'version': version, 'dnsType': dnsType, 'ip': newIP, 'domains': ','.join(changedDomains) }) except Exception as ex: APP.error(f'!!!运行失败,原因:{ex}') notify({ 'version': version, 'dnsType': dnsType, 'domains': ','.join(domains), 'error': ex }) if CONFIG['dry']: print('\n\n' + '!' * 20 + f'-这是在dry模式下运行,实际IPv{version}域名未作更新-' + '!' * 20)
def set_ip(): domains = request_get('domain') token = request_get('token') #print(domains, token) if token != TOKEN: message = {'error': 'Unauthorized'} return jsonify(message), 404 if not domains or type(domains) not in (str, list): message = { 'error': 'Invalid request data', 'example1': { 'domain': 'sub.domain.com' }, 'example2': { 'domain': ['sub1.domain.com', 'sub2.domain.com'] } } return jsonify(message), 422 ipv6 = get_real_ip() ipv4 = ipv6to4(ipv6) newIP = ipv4 if ipv4 else ipv6 version = 4 if ipv4 else 6 dnsType = 'AAAA' if version == 6 else 'A' if type(domains) == str: domains = [domains] results = [] changedDomains = [] for domain in domains: oldIp = getOldIP(version, domain) if newIP != oldIp or CONFIG['force']: APP.info(f'域名{domain}的IPv{version}已发生改变,上次地址为{oldIp}') result = refreshRecord(domain, newIP, version) results.append(result) changedDomains.append(domain) else: APP.debug(f'域名{domain}的{dnsType}纪录未发生改变') results.append({ "status": { "code": "1", "message": "Action completed successful", "created_at": now() }, 'record': { "name": domain, "value": newIP, "status": "unchanged" } }) if not CONFIG['dry']: saveIP(version, newIP, domains) if changedDomains: notify({ 'version': version, 'dnsType': 'AAAA' if version == 6 else 'A', 'ip': newIP, 'domains': ','.join(changedDomains) }) return jsonify(results)