mirror of
https://github.com/woodchen-ink/dnspod-yxip.git
synced 2025-07-18 13:52:10 +08:00
Enhance DNS record management in main.py to support detailed IPv4 and IPv6 updates. Implement logic for updating A and AAAA records based on line-specific best IPs, including handling cases where all lines share the same IPv6 address. Improve logging for record updates and ensure proper caching of current records.
This commit is contained in:
parent
6458525f6c
commit
064e53fb0a
112
main.py
112
main.py
@ -216,8 +216,9 @@ class DNSPodManager:
|
|||||||
self.current_ips[domain] = self.get_current_records(domain, sub_domain)
|
self.current_ips[domain] = self.get_current_records(domain, sub_domain)
|
||||||
current_records = self.current_ips[domain]
|
current_records = self.current_ips[domain]
|
||||||
|
|
||||||
# 处理默认线路
|
# 处理IPv4记录
|
||||||
if domain_config["ipv4_enabled"] and "v4" in ip_data:
|
if domain_config["ipv4_enabled"] and "v4" in ip_data:
|
||||||
|
# 处理默认线路
|
||||||
best_ip = self.find_best_ip(ip_data, "v4")
|
best_ip = self.find_best_ip(ip_data, "v4")
|
||||||
if best_ip:
|
if best_ip:
|
||||||
ip, latency = best_ip
|
ip, latency = best_ip
|
||||||
@ -235,62 +236,91 @@ class DNSPodManager:
|
|||||||
current_records["默认"]["A"] = ip
|
current_records["默认"]["A"] = ip
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
|
|
||||||
|
# 更新其他线路的IPv4记录
|
||||||
|
line_mapping = {"移动": "CM", "联通": "CU", "电信": "CT"}
|
||||||
|
for line, line_key in line_mapping.items():
|
||||||
|
if line_key in ip_data["v4"]:
|
||||||
|
best_ip = self.find_line_best_ip(ip_data, "v4", line_key)
|
||||||
|
if best_ip:
|
||||||
|
ip, latency = best_ip
|
||||||
|
current_ip = current_records.get(line, {}).get("A")
|
||||||
|
if current_ip != ip:
|
||||||
|
logger.info(
|
||||||
|
f"更新A记录: {domain} - {sub_domain} - {line} - {ip} (延迟: {latency}ms)"
|
||||||
|
)
|
||||||
|
if self.update_record(
|
||||||
|
domain, sub_domain, "A", line, ip, ttl, remark
|
||||||
|
):
|
||||||
|
# 更新成功后更新缓存
|
||||||
|
if line not in current_records:
|
||||||
|
current_records[line] = {}
|
||||||
|
current_records[line]["A"] = ip
|
||||||
|
time.sleep(1)
|
||||||
|
|
||||||
|
# 处理IPv6记录
|
||||||
if domain_config["ipv6_enabled"] and "v6" in ip_data:
|
if domain_config["ipv6_enabled"] and "v6" in ip_data:
|
||||||
best_ip = self.find_best_ip(ip_data, "v6")
|
# 获取所有线路的最佳IPv6地址
|
||||||
if best_ip:
|
line_mapping = {"移动": "CM", "联通": "CU", "电信": "CT"}
|
||||||
ip, latency = best_ip
|
best_ips = {}
|
||||||
current_ip = current_records.get("默认", {}).get("AAAA")
|
for line, line_key in line_mapping.items():
|
||||||
if current_ip != ip:
|
if line_key in ip_data["v6"]:
|
||||||
logger.info(
|
best_ip = self.find_line_best_ip(ip_data, "v6", line_key)
|
||||||
f"更新AAAA记录: {domain} - {sub_domain} - 默认 - {ip} (延迟: {latency}ms)"
|
if best_ip:
|
||||||
)
|
ip, latency = best_ip
|
||||||
if self.update_record(
|
best_ips[line] = (ip, latency)
|
||||||
domain, sub_domain, "AAAA", "默认", ip, ttl, remark
|
|
||||||
):
|
|
||||||
# 更新成功后更新缓存
|
|
||||||
if "默认" not in current_records:
|
|
||||||
current_records["默认"] = {}
|
|
||||||
current_records["默认"]["AAAA"] = ip
|
|
||||||
time.sleep(1)
|
|
||||||
|
|
||||||
# 更新其他线路的记录
|
# 检查是否所有线路的IPv6地址都相同
|
||||||
line_mapping = {"移动": "CM", "联通": "CU", "电信": "CT"}
|
if best_ips:
|
||||||
|
unique_ips = {ip for ip, _ in best_ips.values()}
|
||||||
for line, line_key in line_mapping.items():
|
if len(unique_ips) == 1:
|
||||||
if domain_config["ipv4_enabled"] and "v4" in ip_data:
|
# 所有线路的IPv6地址相同,只添加默认线路
|
||||||
best_ip = self.find_line_best_ip(ip_data, "v4", line_key)
|
ip = list(unique_ips)[0]
|
||||||
if best_ip:
|
min_latency = min(latency for _, latency in best_ips.values())
|
||||||
ip, latency = best_ip
|
current_ip = current_records.get("默认", {}).get("AAAA")
|
||||||
current_ip = current_records.get(line, {}).get("A")
|
|
||||||
if current_ip != ip:
|
if current_ip != ip:
|
||||||
logger.info(
|
logger.info(
|
||||||
f"更新A记录: {domain} - {sub_domain} - {line} - {ip} (延迟: {latency}ms)"
|
f"更新AAAA记录: {domain} - {sub_domain} - 默认 - {ip} (延迟: {min_latency}ms) [所有线路IP相同]"
|
||||||
)
|
)
|
||||||
if self.update_record(
|
if self.update_record(
|
||||||
domain, sub_domain, "A", line, ip, ttl, remark
|
domain, sub_domain, "AAAA", "默认", ip, ttl, remark
|
||||||
):
|
):
|
||||||
# 更新成功后更新缓存
|
# 更新成功后更新缓存
|
||||||
if line not in current_records:
|
if "默认" not in current_records:
|
||||||
current_records[line] = {}
|
current_records["默认"] = {}
|
||||||
current_records[line]["A"] = ip
|
current_records["默认"]["AAAA"] = ip
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
|
else:
|
||||||
|
# IPv6地址不同,需要为每个线路添加记录
|
||||||
|
for line, (ip, latency) in best_ips.items():
|
||||||
|
current_ip = current_records.get(line, {}).get("AAAA")
|
||||||
|
if current_ip != ip:
|
||||||
|
logger.info(
|
||||||
|
f"更新AAAA记录: {domain} - {sub_domain} - {line} - {ip} (延迟: {latency}ms)"
|
||||||
|
)
|
||||||
|
if self.update_record(
|
||||||
|
domain, sub_domain, "AAAA", line, ip, ttl, remark
|
||||||
|
):
|
||||||
|
# 更新成功后更新缓存
|
||||||
|
if line not in current_records:
|
||||||
|
current_records[line] = {}
|
||||||
|
current_records[line]["AAAA"] = ip
|
||||||
|
time.sleep(1)
|
||||||
|
|
||||||
if domain_config["ipv6_enabled"] and "v6" in ip_data:
|
# 添加默认线路(使用延迟最低的IP)
|
||||||
best_ip = self.find_line_best_ip(ip_data, "v6", line_key)
|
best_ip = min(best_ips.items(), key=lambda x: x[1][1])
|
||||||
if best_ip:
|
ip, latency = best_ip[1]
|
||||||
ip, latency = best_ip
|
current_ip = current_records.get("默认", {}).get("AAAA")
|
||||||
current_ip = current_records.get(line, {}).get("AAAA")
|
|
||||||
if current_ip != ip:
|
if current_ip != ip:
|
||||||
logger.info(
|
logger.info(
|
||||||
f"更新AAAA记录: {domain} - {sub_domain} - {line} - {ip} (延迟: {latency}ms)"
|
f"更新AAAA记录: {domain} - {sub_domain} - 默认 - {ip} (延迟: {latency}ms)"
|
||||||
)
|
)
|
||||||
if self.update_record(
|
if self.update_record(
|
||||||
domain, sub_domain, "AAAA", line, ip, ttl, remark
|
domain, sub_domain, "AAAA", "默认", ip, ttl, remark
|
||||||
):
|
):
|
||||||
# 更新成功后更新缓存
|
# 更新成功后更新缓存
|
||||||
if line not in current_records:
|
if "默认" not in current_records:
|
||||||
current_records[line] = {}
|
current_records["默认"] = {}
|
||||||
current_records[line]["AAAA"] = ip
|
current_records["默认"]["AAAA"] = ip
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
|
|
||||||
def check_and_update(self):
|
def check_and_update(self):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user