Refactor DNS configuration to use Tencent Cloud API; update environment variables and logging settings. Modify .gitignore to exclude sensitive files. Enhance DNS record management in main.py, including improved error handling and record cleanup logic.

This commit is contained in:
wood chen 2025-01-12 04:32:25 +08:00
parent 4fdd50fcc3
commit 831b4cd4f7
6 changed files with 314 additions and 200 deletions

View File

@ -1,6 +1,9 @@
# DNSPOD API 配置
DNSPOD_ID=your_id_here
DNSPOD_TOKEN=your_token_here
# 腾讯云API配置
TENCENT_SECRET_ID=your_secret_id_here
TENCENT_SECRET_KEY=your_secret_key_here
# 日志级别
LOG_LEVEL=INFO
# 域名配置 - 域名1
DOMAIN_1=example1.com
@ -12,14 +15,14 @@ IPV4_ENABLED_1=true # 是否启用IPv4记录
IPV6_ENABLED_1=true # 是否启用IPv6记录
ENABLED_1=true # 是否启用此域名配置
# 域名配置 - 域名2更频繁的更新
# 域名配置 - 域名2可选
DOMAIN_2=example2.com
SUB_DOMAIN_2=www
REMARK_2=CF优选 # 记录备注
TTL_2=300 # 更短的TTL
UPDATE_INTERVAL_2=5 # 更频繁的更新
IPV4_ENABLED_2=true # 只启用IPv4
IPV6_ENABLED_2=false # 不启用IPv6
REMARK_2=优选IP
TTL_2=600
UPDATE_INTERVAL_2=15
IPV4_ENABLED_2=true
IPV6_ENABLED_2=true
ENABLED_2=true
# 域名配置 - 域名3更长的缓存

4
.gitignore vendored
View File

@ -1 +1,3 @@
/__pycache__
/__pycache__
.env
logs/

103
config.py
View File

@ -4,90 +4,37 @@ from dotenv import load_dotenv
# 加载环境变量
load_dotenv()
# DNSPOD API配置
DNSPOD_ID = os.getenv("DNSPOD_ID")
DNSPOD_TOKEN = os.getenv("DNSPOD_TOKEN")
# 腾讯云API配置
SECRET_ID = os.getenv("TENCENT_SECRET_ID")
SECRET_KEY = os.getenv("TENCENT_SECRET_KEY")
# API接口配置
API_URL = "https://api.vvhan.com/tool/cf_ip"
# 默认配置
DEFAULT_TTL = 600
DEFAULT_UPDATE_INTERVAL = 15
# 日志级别
LOG_LEVEL = os.getenv("LOG_LEVEL", "INFO")
# 支持的线路类型
LINE_TYPES = ["默认", "移动", "联通", "电信"]
# 支持的记录类型
RECORD_TYPES = ["A", "AAAA"]
# 域名配置
# 获取所有域名配置
DOMAINS = []
# 从环境变量加载域名配置
i = 1
index = 1
while True:
domain_key = f"DOMAIN_{i}"
if not os.getenv(domain_key):
domain = os.getenv(f"DOMAIN_{index}")
if not domain:
break
# 获取基本配置
base_config = {
"domain": os.getenv(domain_key),
"sub_domain": os.getenv(f"SUB_DOMAIN_{i}", "@"),
"line": LINE_TYPES,
"ttl": int(os.getenv(f"TTL_{i}", str(DEFAULT_TTL))),
"update_interval": int(
os.getenv(f"UPDATE_INTERVAL_{i}", str(DEFAULT_UPDATE_INTERVAL))
),
"enabled": os.getenv(f"ENABLED_{i}", "true").lower() == "true",
"remark": os.getenv(f"REMARK_{i}", "YXIP"),
}
# 检查是否启用IPv4
ipv4_enabled = os.getenv(f"IPV4_ENABLED_{i}", "true").lower() == "true"
# 检查是否启用IPv6
ipv6_enabled = os.getenv(f"IPV6_ENABLED_{i}", "true").lower() == "true"
# 为每个启用的记录类型创建配置
if ipv4_enabled:
ipv4_config = base_config.copy()
ipv4_config["record_type"] = "A"
DOMAINS.append(ipv4_config)
if ipv6_enabled:
ipv6_config = base_config.copy()
ipv6_config["record_type"] = "AAAA"
DOMAINS.append(ipv6_config)
i += 1
# 如果没有配置任何域名,使用默认配置
if not DOMAINS:
base_config = {
"domain": os.getenv("DOMAIN", "example.com"),
"sub_domain": os.getenv("SUB_DOMAIN", "@"),
"line": LINE_TYPES,
"ttl": int(os.getenv("TTL", str(DEFAULT_TTL))),
"update_interval": int(
os.getenv("UPDATE_INTERVAL", str(DEFAULT_UPDATE_INTERVAL))
),
"enabled": True,
}
# 检查默认的IPv4和IPv6设置
ipv4_enabled = os.getenv("IPV4_ENABLED", "true").lower() == "true"
ipv6_enabled = os.getenv("IPV6_ENABLED", "true").lower() == "true"
if ipv4_enabled:
ipv4_config = base_config.copy()
ipv4_config["record_type"] = "A"
DOMAINS.append(ipv4_config)
if ipv6_enabled:
ipv6_config = base_config.copy()
ipv6_config["record_type"] = "AAAA"
DOMAINS.append(ipv6_config)
# 日志配置
LOG_LEVEL = "INFO"
DOMAINS.append(
{
"domain": domain,
"sub_domain": os.getenv(f"SUB_DOMAIN_{index}", "@"),
"record_type": os.getenv(f"RECORD_TYPE_{index}", "A"),
"remark": os.getenv(f"REMARK_{index}", "优选IP"),
"ttl": int(os.getenv(f"TTL_{index}", "600")),
"update_interval": int(os.getenv(f"UPDATE_INTERVAL_{index}", "15")),
"ipv4_enabled": os.getenv(f"IPV4_ENABLED_{index}", "true").lower()
== "true",
"ipv6_enabled": os.getenv(f"IPV6_ENABLED_{index}", "true").lower()
== "true",
"enabled": os.getenv(f"ENABLED_{index}", "true").lower() == "true",
}
)
index += 1

174
dnspod.log Normal file
View File

@ -0,0 +1,174 @@
2025-01-12 04:14:02.822 | INFO | __main__:check_and_update:273 - 开始更新域名: q58.pro 的 A 记录
2025-01-12 04:14:03.071 | INFO | __main__:update_domain_records:222 - 更新A记录: q58.pro - @ - 默认 - 190.93.247.127 (延迟: 55ms)
2025-01-12 04:14:03.650 | INFO | __main__:clean_existing_records:142 - 删除旧记录: q58.pro - @ - 默认 - 190.93.247.127
2025-01-12 04:14:04.727 | INFO | __main__:clean_existing_records:142 - 删除旧记录: q58.pro - @ - 默认 - 104.19.41.90
2025-01-12 04:14:05.381 | INFO | __main__:clean_existing_records:142 - 删除旧记录: q58.pro - @ - 默认 - 190.93.247.127
2025-01-12 04:14:06.019 | INFO | __main__:clean_existing_records:142 - 删除旧记录: q58.pro - @ - 默认 - 104.21.29.190
2025-01-12 04:14:06.688 | INFO | __main__:clean_existing_records:142 - 删除旧记录: q58.pro - @ - 默认 - 1.0.0.5
2025-01-12 04:14:08.060 | INFO | __main__:update_domain_records:245 - 更新A记录: q58.pro - @ - 移动 - 104.21.225.183 (延迟: 193ms)
2025-01-12 04:14:08.451 | INFO | __main__:clean_existing_records:142 - 删除旧记录: q58.pro - @ - 移动 - 190.93.247.127
2025-01-12 04:14:10.032 | INFO | __main__:update_domain_records:245 - 更新A记录: q58.pro - @ - 联通 - 190.93.247.127 (延迟: 55ms)
2025-01-12 04:14:10.504 | INFO | __main__:clean_existing_records:142 - 删除旧记录: q58.pro - @ - 联通 - 104.21.225.183
2025-01-12 04:14:11.807 | INFO | __main__:update_domain_records:245 - 更新A记录: q58.pro - @ - 电信 - 162.159.240.184 (延迟: 162ms)
2025-01-12 04:14:12.238 | INFO | __main__:clean_existing_records:142 - 删除旧记录: q58.pro - @ - 电信 - 190.93.247.127
2025-01-12 04:14:13.928 | INFO | __main__:check_and_update:273 - 开始更新域名: q58.pro 的 AAAA 记录
2025-01-12 04:14:14.161 | INFO | __main__:update_domain_records:222 - 更新AAAA记录: q58.pro - @ - 默认 - 2606:4700:91b8:9ee0:db85:c8d2:4713:4666 (延迟: 20ms)
2025-01-12 04:14:14.792 | INFO | __main__:clean_existing_records:142 - 删除旧记录: q58.pro - @ - 默认 - 2606:4700:91b8:9ee0:db85:c8d2:4713:4666
2025-01-12 04:14:15.446 | INFO | __main__:clean_existing_records:142 - 删除旧记录: q58.pro - @ - 默认 - 2606:4700:91b8:9ee0:db85:c8d2:4713:4666
2025-01-12 04:14:16.091 | INFO | __main__:clean_existing_records:142 - 删除旧记录: q58.pro - @ - 默认 - 2606:4700:91b8:9ee0:db85:c8d2:4713:4666
2025-01-12 04:14:17.205 | INFO | __main__:clean_existing_records:142 - 删除旧记录: q58.pro - @ - 默认 - 2606:4700:91b8:9ee0:db85:c8d2:4713:4666
2025-01-12 04:14:18.660 | INFO | __main__:update_domain_records:245 - 更新AAAA记录: q58.pro - @ - 移动 - 2606:4700:91b8:9ee0:db85:c8d2:4713:4666 (延迟: 20ms)
2025-01-12 04:14:19.059 | INFO | __main__:clean_existing_records:142 - 删除旧记录: q58.pro - @ - 移动 - 2606:4700:91b8:9ee0:db85:c8d2:4713:4666
2025-01-12 04:14:20.870 | INFO | __main__:update_domain_records:245 - 更新AAAA记录: q58.pro - @ - 联通 - 2606:4700:91b8:9ee0:db85:c8d2:4713:4666 (延迟: 20ms)
2025-01-12 04:14:21.275 | INFO | __main__:clean_existing_records:142 - 删除旧记录: q58.pro - @ - 联通 - 2606:4700:91b8:9ee0:db85:c8d2:4713:4666
2025-01-12 04:14:23.367 | INFO | __main__:update_domain_records:245 - 更新AAAA记录: q58.pro - @ - 电信 - 2606:4700:91b8:9ee0:db85:c8d2:4713:4666 (延迟: 20ms)
2025-01-12 04:14:23.915 | INFO | __main__:clean_existing_records:142 - 删除旧记录: q58.pro - @ - 电信 - 2606:4700:91b8:9ee0:db85:c8d2:4713:4666
2025-01-12 04:14:25.985 | INFO | __main__:check_and_update:273 - 开始更新域名: freedvd.cc 的 A 记录
2025-01-12 04:14:26.225 | INFO | __main__:update_domain_records:222 - 更新A记录: freedvd.cc - @ - 默认 - 190.93.247.127 (延迟: 55ms)
2025-01-12 04:14:26.710 | INFO | __main__:clean_existing_records:142 - 删除旧记录: freedvd.cc - @ - 默认 - 190.93.247.127
2025-01-12 04:14:27.324 | INFO | __main__:clean_existing_records:142 - 删除旧记录: freedvd.cc - @ - 默认 - 104.19.41.90
2025-01-12 04:14:28.255 | INFO | __main__:clean_existing_records:142 - 删除旧记录: freedvd.cc - @ - 默认 - 190.93.247.127
2025-01-12 04:14:29.333 | INFO | __main__:clean_existing_records:142 - 删除旧记录: freedvd.cc - @ - 默认 - 104.21.29.190
2025-01-12 04:14:30.431 | INFO | __main__:clean_existing_records:142 - 删除旧记录: freedvd.cc - @ - 默认 - 1.0.0.5
2025-01-12 04:14:32.469 | INFO | __main__:update_domain_records:245 - 更新A记录: freedvd.cc - @ - 移动 - 104.21.225.183 (延迟: 193ms)
2025-01-12 04:14:32.908 | INFO | __main__:clean_existing_records:142 - 删除旧记录: freedvd.cc - @ - 移动 - 190.93.247.127
2025-01-12 04:14:34.707 | INFO | __main__:update_domain_records:245 - 更新A记录: freedvd.cc - @ - 联通 - 190.93.247.127 (延迟: 55ms)
2025-01-12 04:14:35.337 | INFO | __main__:clean_existing_records:142 - 删除旧记录: freedvd.cc - @ - 联通 - 104.21.225.183
2025-01-12 04:14:36.974 | INFO | __main__:update_domain_records:245 - 更新A记录: freedvd.cc - @ - 电信 - 162.159.240.184 (延迟: 162ms)
2025-01-12 04:14:37.475 | INFO | __main__:clean_existing_records:142 - 删除旧记录: freedvd.cc - @ - 电信 - 190.93.247.127
2025-01-12 04:14:39.531 | INFO | __main__:check_and_update:273 - 开始更新域名: freedvd.cc 的 AAAA 记录
2025-01-12 04:14:39.746 | INFO | __main__:update_domain_records:222 - 更新AAAA记录: freedvd.cc - @ - 默认 - 2606:4700:91b8:9ee0:db85:c8d2:4713:4666 (延迟: 20ms)
2025-01-12 04:14:40.394 | INFO | __main__:clean_existing_records:142 - 删除旧记录: freedvd.cc - @ - 默认 - 2606:4700:91b8:9ee0:db85:c8d2:4713:4666
2025-01-12 04:14:40.930 | INFO | __main__:clean_existing_records:142 - 删除旧记录: freedvd.cc - @ - 默认 - 2606:4700:91b8:9ee0:db85:c8d2:4713:4666
2025-01-12 04:14:42.042 | INFO | __main__:clean_existing_records:142 - 删除旧记录: freedvd.cc - @ - 默认 - 2606:4700:91b8:9ee0:db85:c8d2:4713:4666
2025-01-12 04:14:42.579 | INFO | __main__:clean_existing_records:142 - 删除旧记录: freedvd.cc - @ - 默认 - 2606:4700:91b8:9ee0:db85:c8d2:4713:4666
2025-01-12 04:14:44.088 | INFO | __main__:update_domain_records:245 - 更新AAAA记录: freedvd.cc - @ - 移动 - 2606:4700:91b8:9ee0:db85:c8d2:4713:4666 (延迟: 20ms)
2025-01-12 04:14:44.433 | INFO | __main__:clean_existing_records:142 - 删除旧记录: freedvd.cc - @ - 移动 - 2606:4700:91b8:9ee0:db85:c8d2:4713:4666
2025-01-12 04:14:46.225 | INFO | __main__:update_domain_records:245 - 更新AAAA记录: freedvd.cc - @ - 联通 - 2606:4700:91b8:9ee0:db85:c8d2:4713:4666 (延迟: 20ms)
2025-01-12 04:14:46.661 | INFO | __main__:clean_existing_records:142 - 删除旧记录: freedvd.cc - @ - 联通 - 2606:4700:91b8:9ee0:db85:c8d2:4713:4666
2025-01-12 04:14:47.782 | INFO | __main__:update_domain_records:245 - 更新AAAA记录: freedvd.cc - @ - 电信 - 2606:4700:91b8:9ee0:db85:c8d2:4713:4666 (延迟: 20ms)
2025-01-12 04:14:48.330 | INFO | __main__:clean_existing_records:142 - 删除旧记录: freedvd.cc - @ - 电信 - 2606:4700:91b8:9ee0:db85:c8d2:4713:4666
2025-01-12 04:14:49.833 | INFO | __main__:check_and_update:273 - 开始更新域名: woodchen.ink 的 A 记录
2025-01-12 04:14:50.007 | INFO | __main__:update_domain_records:222 - 更新A记录: woodchen.ink - @ - 默认 - 190.93.247.127 (延迟: 55ms)
2025-01-12 04:14:50.348 | INFO | __main__:clean_existing_records:142 - 删除旧记录: woodchen.ink - @ - 默认 - 190.93.247.127
2025-01-12 04:14:50.889 | INFO | __main__:clean_existing_records:142 - 删除旧记录: woodchen.ink - @ - 默认 - 104.19.41.90
2025-01-12 04:14:51.978 | INFO | __main__:clean_existing_records:142 - 删除旧记录: woodchen.ink - @ - 默认 - 190.93.247.127
2025-01-12 04:14:52.545 | INFO | __main__:clean_existing_records:142 - 删除旧记录: woodchen.ink - @ - 默认 - 104.21.29.190
2025-01-12 04:14:53.630 | INFO | __main__:update_domain_records:245 - 更新A记录: woodchen.ink - @ - 移动 - 104.21.225.183 (延迟: 193ms)
2025-01-12 04:14:54.182 | INFO | __main__:clean_existing_records:142 - 删除旧记录: woodchen.ink - @ - 移动 - 190.93.247.127
2025-01-12 04:14:55.462 | INFO | __main__:update_domain_records:245 - 更新A记录: woodchen.ink - @ - 联通 - 190.93.247.127 (延迟: 55ms)
2025-01-12 04:14:55.952 | INFO | __main__:clean_existing_records:142 - 删除旧记录: woodchen.ink - @ - 联通 - 104.21.225.183
2025-01-12 04:14:57.778 | INFO | __main__:update_domain_records:245 - 更新A记录: woodchen.ink - @ - 电信 - 162.159.240.184 (延迟: 162ms)
2025-01-12 04:14:58.196 | INFO | __main__:clean_existing_records:142 - 删除旧记录: woodchen.ink - @ - 电信 - 190.93.247.127
2025-01-12 04:15:01.004 | INFO | __main__:check_and_update:273 - 开始更新域名: woodchen.ink 的 AAAA 记录
2025-01-12 04:15:01.191 | INFO | __main__:update_domain_records:222 - 更新AAAA记录: woodchen.ink - @ - 默认 - 2606:4700:91b8:9ee0:db85:c8d2:4713:4666 (延迟: 20ms)
2025-01-12 04:15:02.787 | INFO | __main__:clean_existing_records:142 - 删除旧记录: woodchen.ink - @ - 默认 - 2606:4700:91b8:9ee0:db85:c8d2:4713:4666
2025-01-12 04:15:03.867 | INFO | __main__:clean_existing_records:142 - 删除旧记录: woodchen.ink - @ - 默认 - 2606:4700:91b8:9ee0:db85:c8d2:4713:4666
2025-01-12 04:15:04.427 | INFO | __main__:clean_existing_records:142 - 删除旧记录: woodchen.ink - @ - 默认 - 2606:4700:91b8:9ee0:db85:c8d2:4713:4666
2025-01-12 04:15:04.960 | INFO | __main__:clean_existing_records:142 - 删除旧记录: woodchen.ink - @ - 默认 - 2606:4700:91b8:9ee0:db85:c8d2:4713:4666
2025-01-12 04:15:06.629 | INFO | __main__:update_domain_records:245 - 更新AAAA记录: woodchen.ink - @ - 移动 - 2606:4700:91b8:9ee0:db85:c8d2:4713:4666 (延迟: 20ms)
2025-01-12 04:15:07.210 | INFO | __main__:clean_existing_records:142 - 删除旧记录: woodchen.ink - @ - 移动 - 2606:4700:91b8:9ee0:db85:c8d2:4713:4666
2025-01-12 04:15:08.710 | INFO | __main__:update_domain_records:245 - 更新AAAA记录: woodchen.ink - @ - 联通 - 2606:4700:91b8:9ee0:db85:c8d2:4713:4666 (延迟: 20ms)
2025-01-12 04:15:09.317 | INFO | __main__:clean_existing_records:142 - 删除旧记录: woodchen.ink - @ - 联通 - 2606:4700:91b8:9ee0:db85:c8d2:4713:4666
2025-01-12 04:15:10.957 | INFO | __main__:update_domain_records:245 - 更新AAAA记录: woodchen.ink - @ - 电信 - 2606:4700:91b8:9ee0:db85:c8d2:4713:4666 (延迟: 20ms)
2025-01-12 04:15:11.310 | INFO | __main__:clean_existing_records:142 - 删除旧记录: woodchen.ink - @ - 电信 - 2606:4700:91b8:9ee0:db85:c8d2:4713:4666
2025-01-12 04:15:12.665 | INFO | __main__:check_and_update:273 - 开始更新域名: czl.net 的 A 记录
2025-01-12 04:15:12.877 | INFO | __main__:update_domain_records:222 - 更新A记录: czl.net - @ - 默认 - 190.93.247.127 (延迟: 55ms)
2025-01-12 04:15:13.253 | INFO | __main__:clean_existing_records:142 - 删除旧记录: czl.net - @ - 默认 - 190.93.247.127
2025-01-12 04:15:14.044 | INFO | __main__:clean_existing_records:142 - 删除旧记录: czl.net - @ - 默认 - 104.19.16.172
2025-01-12 04:15:14.909 | INFO | __main__:clean_existing_records:142 - 删除旧记录: czl.net - @ - 默认 - 190.93.247.127
2025-01-12 04:15:15.563 | INFO | __main__:clean_existing_records:142 - 删除旧记录: czl.net - @ - 默认 - 104.21.30.243
2025-01-12 04:15:16.187 | INFO | __main__:clean_existing_records:142 - 删除旧记录: czl.net - @ - 默认 - 1.0.0.5
2025-01-12 04:15:18.285 | INFO | __main__:update_domain_records:245 - 更新A记录: czl.net - @ - 移动 - 104.21.225.183 (延迟: 193ms)
2025-01-12 04:15:18.759 | INFO | __main__:clean_existing_records:142 - 删除旧记录: czl.net - @ - 移动 - 190.93.247.127
2025-01-12 04:15:20.844 | INFO | __main__:update_domain_records:245 - 更新A记录: czl.net - @ - 联通 - 190.93.247.127 (延迟: 55ms)
2025-01-12 04:15:21.280 | INFO | __main__:clean_existing_records:142 - 删除旧记录: czl.net - @ - 联通 - 104.21.225.183
2025-01-12 04:15:22.590 | INFO | __main__:update_domain_records:245 - 更新A记录: czl.net - @ - 电信 - 162.159.240.184 (延迟: 162ms)
2025-01-12 04:15:23.155 | INFO | __main__:clean_existing_records:142 - 删除旧记录: czl.net - @ - 电信 - 190.93.247.127
2025-01-12 04:15:25.502 | INFO | __main__:check_and_update:273 - 开始更新域名: czl.net 的 AAAA 记录
2025-01-12 04:15:25.679 | INFO | __main__:update_domain_records:222 - 更新AAAA记录: czl.net - @ - 默认 - 2606:4700:91b8:9ee0:db85:c8d2:4713:4666 (延迟: 20ms)
2025-01-12 04:15:26.324 | INFO | __main__:clean_existing_records:142 - 删除旧记录: czl.net - @ - 默认 - 2606:4700:91b8:9ee0:db85:c8d2:4713:4666
2025-01-12 04:15:27.308 | INFO | __main__:clean_existing_records:142 - 删除旧记录: czl.net - @ - 默认 - 2606:4700:839f:8028:9842:28ff:1744:4451
2025-01-12 04:15:28.205 | INFO | __main__:clean_existing_records:142 - 删除旧记录: czl.net - @ - 默认 - 2606:4700:91b8:9ee0:db85:c8d2:4713:4666
2025-01-12 04:15:28.677 | INFO | __main__:clean_existing_records:142 - 删除旧记录: czl.net - @ - 默认 - 2606:4700:839f:8028:9842:28ff:1744:4451
2025-01-12 04:15:29.402 | INFO | __main__:clean_existing_records:142 - 删除旧记录: czl.net - @ - 默认 - 2606:4700:91b8:9ee0:db85:c8d2:4713:4666
2025-01-12 04:15:30.426 | INFO | __main__:clean_existing_records:142 - 删除旧记录: czl.net - @ - 默认 - 2606:4700:91b8:9ee0:db85:c8d2:4713:4666
2025-01-12 04:15:31.868 | INFO | __main__:update_domain_records:245 - 更新AAAA记录: czl.net - @ - 移动 - 2606:4700:91b8:9ee0:db85:c8d2:4713:4666 (延迟: 20ms)
2025-01-12 04:15:32.266 | INFO | __main__:clean_existing_records:142 - 删除旧记录: czl.net - @ - 移动 - 2606:4700:91b8:9ee0:db85:c8d2:4713:4666
2025-01-12 04:15:33.657 | INFO | __main__:update_domain_records:245 - 更新AAAA记录: czl.net - @ - 联通 - 2606:4700:91b8:9ee0:db85:c8d2:4713:4666 (延迟: 20ms)
2025-01-12 04:15:33.987 | INFO | __main__:clean_existing_records:142 - 删除旧记录: czl.net - @ - 联通 - 2606:4700:91b8:9ee0:db85:c8d2:4713:4666
2025-01-12 04:15:35.168 | INFO | __main__:update_domain_records:245 - 更新AAAA记录: czl.net - @ - 电信 - 2606:4700:91b8:9ee0:db85:c8d2:4713:4666 (延迟: 20ms)
2025-01-12 04:15:35.714 | INFO | __main__:clean_existing_records:142 - 删除旧记录: czl.net - @ - 电信 - 2606:4700:91b8:9ee0:db85:c8d2:4713:4666
2025-01-12 04:16:28.007 | INFO | __main__:check_and_update:267 - 开始更新域名: q58.pro 的 A 记录
2025-01-12 04:16:28.246 | INFO | __main__:update_domain_records:222 - 更新A记录: q58.pro - @ - 默认 - 190.93.247.127 (延迟: 55ms)
2025-01-12 04:16:28.802 | INFO | __main__:clean_existing_records:142 - 删除旧记录: q58.pro - @ - 默认 - 162.159.240.184
2025-01-12 04:16:31.026 | INFO | __main__:update_domain_records:238 - 更新A记录: q58.pro - @ - 移动 - 104.21.225.183 (延迟: 193ms)
2025-01-12 04:16:31.563 | INFO | __main__:clean_existing_records:142 - 删除旧记录: q58.pro - @ - 移动 - 190.93.247.127
2025-01-12 04:16:33.415 | INFO | __main__:update_domain_records:238 - 更新A记录: q58.pro - @ - 联通 - 190.93.247.127 (延迟: 55ms)
2025-01-12 04:16:34.026 | INFO | __main__:clean_existing_records:142 - 删除旧记录: q58.pro - @ - 联通 - 104.21.225.183
2025-01-12 04:16:35.685 | INFO | __main__:update_domain_records:238 - 更新A记录: q58.pro - @ - 电信 - 162.159.240.184 (延迟: 162ms)
2025-01-12 04:16:36.084 | INFO | __main__:clean_existing_records:142 - 删除旧记录: q58.pro - @ - 电信 - 190.93.247.127
2025-01-12 04:16:37.932 | INFO | __main__:check_and_update:267 - 开始更新域名: q58.pro 的 AAAA 记录
2025-01-12 04:16:38.157 | INFO | __main__:update_domain_records:222 - 更新AAAA记录: q58.pro - @ - 默认 - 2606:4700:91b8:9ee0:db85:c8d2:4713:4666 (延迟: 20ms)
2025-01-12 04:16:38.582 | INFO | __main__:clean_existing_records:142 - 删除旧记录: q58.pro - @ - 默认 - 2606:4700:91b8:9ee0:db85:c8d2:4713:4666
2025-01-12 04:16:40.377 | INFO | __main__:update_domain_records:238 - 更新AAAA记录: q58.pro - @ - 移动 - 2606:4700:91b8:9ee0:db85:c8d2:4713:4666 (延迟: 20ms)
2025-01-12 04:16:40.983 | INFO | __main__:clean_existing_records:142 - 删除旧记录: q58.pro - @ - 移动 - 2606:4700:91b8:9ee0:db85:c8d2:4713:4666
2025-01-12 04:16:42.086 | INFO | __main__:update_domain_records:238 - 更新AAAA记录: q58.pro - @ - 联通 - 2606:4700:91b8:9ee0:db85:c8d2:4713:4666 (延迟: 20ms)
2025-01-12 04:16:42.731 | INFO | __main__:clean_existing_records:142 - 删除旧记录: q58.pro - @ - 联通 - 2606:4700:91b8:9ee0:db85:c8d2:4713:4666
2025-01-12 04:16:45.017 | INFO | __main__:update_domain_records:238 - 更新AAAA记录: q58.pro - @ - 电信 - 2606:4700:91b8:9ee0:db85:c8d2:4713:4666 (延迟: 20ms)
2025-01-12 04:16:45.549 | INFO | __main__:clean_existing_records:142 - 删除旧记录: q58.pro - @ - 电信 - 2606:4700:91b8:9ee0:db85:c8d2:4713:4666
2025-01-12 04:16:46.844 | INFO | __main__:check_and_update:267 - 开始更新域名: freedvd.cc 的 A 记录
2025-01-12 04:16:47.084 | INFO | __main__:update_domain_records:222 - 更新A记录: freedvd.cc - @ - 默认 - 190.93.247.127 (延迟: 55ms)
2025-01-12 04:16:47.660 | INFO | __main__:clean_existing_records:142 - 删除旧记录: freedvd.cc - @ - 默认 - 162.159.240.184
2025-01-12 04:16:50.036 | INFO | __main__:update_domain_records:238 - 更新A记录: freedvd.cc - @ - 移动 - 104.21.225.183 (延迟: 193ms)
2025-01-12 04:16:50.536 | INFO | __main__:clean_existing_records:142 - 删除旧记录: freedvd.cc - @ - 移动 - 190.93.247.127
2025-01-12 04:16:52.167 | INFO | __main__:update_domain_records:238 - 更新A记录: freedvd.cc - @ - 联通 - 190.93.247.127 (延迟: 55ms)
2025-01-12 04:16:52.647 | INFO | __main__:clean_existing_records:142 - 删除旧记录: freedvd.cc - @ - 联通 - 104.21.225.183
2025-01-12 04:16:54.627 | INFO | __main__:update_domain_records:238 - 更新A记录: freedvd.cc - @ - 电信 - 162.159.240.184 (延迟: 162ms)
2025-01-12 04:16:55.116 | INFO | __main__:clean_existing_records:142 - 删除旧记录: freedvd.cc - @ - 电信 - 190.93.247.127
2025-01-12 04:16:57.197 | INFO | __main__:check_and_update:267 - 开始更新域名: freedvd.cc 的 AAAA 记录
2025-01-12 04:16:57.452 | INFO | __main__:update_domain_records:222 - 更新AAAA记录: freedvd.cc - @ - 默认 - 2606:4700:91b8:9ee0:db85:c8d2:4713:4666 (延迟: 20ms)
2025-01-12 04:16:57.864 | INFO | __main__:clean_existing_records:142 - 删除旧记录: freedvd.cc - @ - 默认 - 2606:4700:91b8:9ee0:db85:c8d2:4713:4666
2025-01-12 04:18:19.664 | INFO | __main__:check_and_update:271 - 开始更新域名: q58.pro 的 A 记录
2025-01-12 04:18:19.848 | INFO | __main__:update_domain_records:222 - 更新A记录: q58.pro - @ - 默认 - 190.93.247.127 (延迟: 55ms)
2025-01-12 04:18:20.423 | INFO | __main__:clean_existing_records:142 - 删除旧记录: q58.pro - @ - 默认 - 162.159.240.184
2025-01-12 04:18:27.407 | INFO | __main__:update_domain_records:240 - 更新A记录: q58.pro - @ - 移动 - 104.21.225.183 (延迟: 190ms)
2025-01-12 04:18:27.803 | INFO | __main__:clean_existing_records:142 - 删除旧记录: q58.pro - @ - 移动 - 190.93.247.127
2025-01-12 04:18:34.550 | INFO | __main__:update_domain_records:240 - 更新A记录: q58.pro - @ - 联通 - 190.93.247.127 (延迟: 55ms)
2025-01-12 04:18:35.158 | INFO | __main__:clean_existing_records:142 - 删除旧记录: q58.pro - @ - 联通 - 104.21.225.183
2025-01-12 04:18:41.987 | INFO | __main__:update_domain_records:240 - 更新A记录: q58.pro - @ - 电信 - 104.17.80.170 (延迟: 134ms)
2025-01-12 04:18:42.524 | INFO | __main__:clean_existing_records:142 - 删除旧记录: q58.pro - @ - 电信 - 190.93.247.127
2025-01-12 04:18:49.335 | INFO | __main__:check_and_update:271 - 开始更新域名: q58.pro 的 AAAA 记录
2025-01-12 04:18:49.566 | INFO | __main__:update_domain_records:222 - 更新AAAA记录: q58.pro - @ - 默认 - 2606:4700:8ddf:73bf:22f2:db27:a0ad:bd8 (延迟: 20ms)
2025-01-12 04:18:49.910 | INFO | __main__:clean_existing_records:142 - 删除旧记录: q58.pro - @ - 默认 - 2606:4700:91b8:9ee0:db85:c8d2:4713:4666
2025-01-12 04:18:57.186 | INFO | __main__:update_domain_records:240 - 更新AAAA记录: q58.pro - @ - 移动 - 2606:4700:8ddf:73bf:22f2:db27:a0ad:bd8 (延迟: 20ms)
2025-01-12 04:18:57.841 | INFO | __main__:clean_existing_records:142 - 删除旧记录: q58.pro - @ - 移动 - 2606:4700:8ddf:73bf:22f2:db27:a0ad:bd8
2025-01-12 04:19:04.246 | INFO | __main__:update_domain_records:240 - 更新AAAA记录: q58.pro - @ - 联通 - 2606:4700:8ddf:73bf:22f2:db27:a0ad:bd8 (延迟: 20ms)
2025-01-12 04:19:04.664 | INFO | __main__:clean_existing_records:142 - 删除旧记录: q58.pro - @ - 联通 - 2606:4700:8ddf:73bf:22f2:db27:a0ad:bd8
2025-01-12 04:19:11.746 | INFO | __main__:update_domain_records:240 - 更新AAAA记录: q58.pro - @ - 电信 - 2606:4700:8ddf:73bf:22f2:db27:a0ad:bd8 (延迟: 20ms)
2025-01-12 04:19:12.111 | INFO | __main__:clean_existing_records:142 - 删除旧记录: q58.pro - @ - 电信 - 2606:4700:8ddf:73bf:22f2:db27:a0ad:bd8
2025-01-12 04:19:18.423 | INFO | __main__:check_and_update:271 - 开始更新域名: freedvd.cc 的 A 记录
2025-01-12 04:19:18.597 | INFO | __main__:update_domain_records:222 - 更新A记录: freedvd.cc - @ - 默认 - 190.93.247.127 (延迟: 55ms)
2025-01-12 04:19:19.172 | INFO | __main__:clean_existing_records:142 - 删除旧记录: freedvd.cc - @ - 默认 - 162.159.240.184
2025-01-12 04:19:26.218 | INFO | __main__:update_domain_records:240 - 更新A记录: freedvd.cc - @ - 移动 - 104.21.225.183 (延迟: 190ms)
2025-01-12 04:19:26.784 | INFO | __main__:clean_existing_records:142 - 删除旧记录: freedvd.cc - @ - 移动 - 190.93.247.127
2025-01-12 04:19:33.492 | INFO | __main__:update_domain_records:240 - 更新A记录: freedvd.cc - @ - 联通 - 190.93.247.127 (延迟: 55ms)
2025-01-12 04:19:33.963 | INFO | __main__:clean_existing_records:142 - 删除旧记录: freedvd.cc - @ - 联通 - 104.21.225.183
2025-01-12 04:19:41.426 | INFO | __main__:update_domain_records:240 - 更新A记录: freedvd.cc - @ - 电信 - 104.17.80.170 (延迟: 134ms)
2025-01-12 04:19:41.807 | INFO | __main__:clean_existing_records:142 - 删除旧记录: freedvd.cc - @ - 电信 - 190.93.247.127
2025-01-12 04:19:48.170 | INFO | __main__:check_and_update:271 - 开始更新域名: freedvd.cc 的 AAAA 记录
2025-01-12 04:19:48.406 | INFO | __main__:update_domain_records:222 - 更新AAAA记录: freedvd.cc - @ - 默认 - 2606:4700:8ddf:73bf:22f2:db27:a0ad:bd8 (延迟: 20ms)
2025-01-12 04:19:48.872 | INFO | __main__:clean_existing_records:142 - 删除旧记录: freedvd.cc - @ - 默认 - 2606:4700:91b8:9ee0:db85:c8d2:4713:4666
2025-01-12 04:19:55.260 | INFO | __main__:update_domain_records:240 - 更新AAAA记录: freedvd.cc - @ - 移动 - 2606:4700:8ddf:73bf:22f2:db27:a0ad:bd8 (延迟: 20ms)
2025-01-12 04:19:55.867 | INFO | __main__:clean_existing_records:142 - 删除旧记录: freedvd.cc - @ - 移动 - 2606:4700:8ddf:73bf:22f2:db27:a0ad:bd8
2025-01-12 04:20:02.213 | INFO | __main__:update_domain_records:240 - 更新AAAA记录: freedvd.cc - @ - 联通 - 2606:4700:8ddf:73bf:22f2:db27:a0ad:bd8 (延迟: 20ms)
2025-01-12 04:20:02.896 | INFO | __main__:clean_existing_records:142 - 删除旧记录: freedvd.cc - @ - 联通 - 2606:4700:8ddf:73bf:22f2:db27:a0ad:bd8
2025-01-12 04:20:09.978 | INFO | __main__:update_domain_records:240 - 更新AAAA记录: freedvd.cc - @ - 电信 - 2606:4700:8ddf:73bf:22f2:db27:a0ad:bd8 (延迟: 20ms)
2025-01-12 04:20:10.502 | INFO | __main__:clean_existing_records:142 - 删除旧记录: freedvd.cc - @ - 电信 - 2606:4700:8ddf:73bf:22f2:db27:a0ad:bd8
2025-01-12 04:20:17.547 | INFO | __main__:check_and_update:271 - 开始更新域名: woodchen.ink 的 A 记录
2025-01-12 04:20:17.720 | INFO | __main__:update_domain_records:222 - 更新A记录: woodchen.ink - @ - 默认 - 190.93.247.127 (延迟: 55ms)
2025-01-12 04:20:18.322 | INFO | __main__:clean_existing_records:142 - 删除旧记录: woodchen.ink - @ - 默认 - 162.159.240.184
2025-01-12 04:20:25.162 | INFO | __main__:update_domain_records:240 - 更新A记录: woodchen.ink - @ - 移动 - 104.21.225.183 (延迟: 190ms)
2025-01-12 04:20:25.722 | INFO | __main__:clean_existing_records:142 - 删除旧记录: woodchen.ink - @ - 移动 - 190.93.247.127
2025-01-12 04:20:32.612 | INFO | __main__:update_domain_records:240 - 更新A记录: woodchen.ink - @ - 联通 - 190.93.247.127 (延迟: 55ms)
2025-01-12 04:20:33.072 | INFO | __main__:clean_existing_records:142 - 删除旧记录: woodchen.ink - @ - 联通 - 104.21.225.183

209
main.py
View File

@ -5,38 +5,30 @@ import schedule
from loguru import logger
from typing import Dict, List, Optional, Tuple
import config
from tencentcloud.common import credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.dnspod.v20210323 import dnspod_client, models
# 配置日志
logger.add("dnspod.log", rotation="10 MB", level=config.LOG_LEVEL)
logger.add("logs/dnspod.log", rotation="10 MB", level=config.LOG_LEVEL)
class DNSPodManager:
def __init__(self):
self.api_url = "https://dnsapi.cn"
self.common_params = {
"login_token": f"{config.DNSPOD_ID},{config.DNSPOD_TOKEN}",
"format": "json",
"lang": "cn",
"error_on_empty": "no",
}
# 实例化一个认证对象
cred = credential.Credential(config.SECRET_ID, config.SECRET_KEY)
# 实例化一个http选项可选的没有特殊需求可以跳过
httpProfile = HttpProfile()
httpProfile.endpoint = "dnspod.tencentcloudapi.com"
# 实例化一个client选项可选的没有特殊需求可以跳过
clientProfile = ClientProfile()
clientProfile.httpProfile = httpProfile
# 实例化要请求产品的client对象
self.client = dnspod_client.DnspodClient(cred, "", clientProfile)
# 记录每个域名每种记录类型的最后更新时间
self.last_update = {} # 格式: {domain: {'A': timestamp, 'AAAA': timestamp}}
def _api_request(self, path: str, data: Dict) -> Dict:
"""发送API请求"""
try:
url = f"{self.api_url}/{path}"
response = requests.post(url, data={**self.common_params, **data})
result = response.json()
if int(result.get("status", {}).get("code", -1)) != 1:
raise Exception(result.get("status", {}).get("message", "未知错误"))
return result
except Exception as e:
logger.error(f"API请求失败: {str(e)}")
raise
def get_optimal_ips(self) -> Dict:
"""获取优选IP"""
try:
@ -79,46 +71,61 @@ class DNSPodManager:
best_ip = min(ips, key=lambda x: x["latency"])
return (best_ip["ip"], best_ip["latency"])
def get_record_list(self, domain: str) -> List:
def get_record_list(
self, domain: str, sub_domain: str = None, record_type: str = None
) -> List:
"""获取域名记录列表"""
try:
result = self._api_request("Record.List", {"domain": domain})
return result.get("records", [])
# 实例化一个请求对象
req = models.DescribeRecordListRequest()
req.Domain = domain
if sub_domain:
req.Subdomain = sub_domain
if record_type:
req.RecordType = record_type
# 通过client对象调用DescribeRecordList接口
resp = self.client.DescribeRecordList(req)
return resp.RecordList
except Exception as e:
logger.error(f"获取记录列表失败: {str(e)}")
return []
def delete_record(self, domain: str, record_id: str) -> bool:
def delete_record(self, domain: str, record_id: int) -> bool:
"""删除DNS记录"""
try:
self._api_request(
"Record.Remove", {"domain": domain, "record_id": record_id}
)
req = models.DeleteRecordRequest()
req.Domain = domain
req.RecordId = record_id
self.client.DeleteRecord(req)
return True
except Exception as e:
logger.error(f"删除记录失败: {str(e)}")
return False
def handle_record_conflicts(self, domain: str, sub_domain: str, record_type: str):
"""处理记录冲突"""
records = self.get_record_list(domain)
for record in records:
# 如果是要添加A记录需要删除CNAME记录
if (
record_type == "A"
and record["type"] == "CNAME"
and record["name"] == sub_domain
):
logger.info(f"删除冲突的CNAME记录: {domain} - {sub_domain}")
self.delete_record(domain, record["id"])
# 如果是要添加CNAME记录需要删除A记录
elif (
record_type == "CNAME"
and record["type"] == "A"
and record["name"] == sub_domain
):
logger.info(f"删除冲突的A记录: {domain} - {sub_domain}")
self.delete_record(domain, record["id"])
def clean_existing_records(
self, domain: str, sub_domain: str, record_type: str, line: str
) -> None:
"""清理指定类型和线路的现有记录"""
try:
# 定义我们要管理的线路
managed_lines = ["默认", "移动", "联通", "电信"]
# 如果不是我们管理的线路,直接返回
if line not in managed_lines:
return
records = self.get_record_list(domain, sub_domain, record_type)
for record in records:
# 只删除我们管理的线路中的记录
if record.Line == line and record.Line in managed_lines:
logger.info(
f"删除旧记录: {domain} - {sub_domain} - {line} - {record.Value}"
)
self.delete_record(domain, record.RecordId)
time.sleep(1) # 添加短暂延时
except Exception as e:
logger.error(f"清理记录时出错: {str(e)}")
def update_record(
self,
@ -128,43 +135,26 @@ class DNSPodManager:
line: str,
value: str,
ttl: int,
remark: str = "YXIP",
remark: str = None,
) -> bool:
"""更新DNS记录"""
"""更新或创建DNS记录"""
try:
# 处理记录冲突
self.handle_record_conflicts(domain, sub_domain, record_type)
# 先清理现有记录
self.clean_existing_records(domain, sub_domain, record_type, line)
time.sleep(1) # 添加短暂延时
# 获取域名记录列表
records = self.get_record_list(domain)
# 创建新记录
req = models.CreateRecordRequest()
req.Domain = domain
req.SubDomain = sub_domain
req.RecordType = record_type
req.RecordLine = line
req.Value = value
req.TTL = ttl
if remark:
req.Remark = remark
# 查找匹配的记录
record_id = None
for record in records:
if (
record["name"] == sub_domain
and record["line"] == line
and record["type"] == record_type
):
record_id = record["id"]
break
# 更新或创建记录
data = {
"domain": domain,
"sub_domain": sub_domain,
"record_type": record_type,
"record_line": line,
"value": value,
"ttl": ttl,
"remark": remark,
}
if record_id:
data["record_id"] = record_id
self._api_request("Record.Modify", data)
else:
self._api_request("Record.Create", data)
self.client.CreateRecord(req)
return True
except Exception as e:
logger.error(f"更新DNS记录失败: {str(e)}")
@ -211,32 +201,30 @@ class DNSPodManager:
logger.info(
f"更新{record_type}记录: {domain} - {sub_domain} - 默认 - {ip} (延迟: {latency}ms)"
)
self.update_record(domain, sub_domain, record_type, "默认", ip, ttl, remark)
success = self.update_record(
domain, sub_domain, record_type, "默认", ip, ttl, remark
)
if not success:
logger.error(f"更新默认线路记录失败: {domain} - {sub_domain}")
time.sleep(1) # 添加延时
# 更新其他线路的记录
for line in domain_config["line"]:
if line == "默认":
continue
line_mapping = {"移动": "CM", "联通": "CU", "电信": "CT"}
if line == "移动":
line_key = "CM"
elif line == "联通":
line_key = "CU"
elif line == "电信":
line_key = "CT"
else:
continue
if line_key in ip_data[ip_version]:
for line, line_key in line_mapping.items():
if line_key in ip_data[ip_version] and ip_data[ip_version][line_key]:
best_ip = self.find_line_best_ip(ip_data, ip_version, line_key)
if best_ip:
ip, latency = best_ip
logger.info(
f"更新{record_type}记录: {domain} - {sub_domain} - {line} - {ip} (延迟: {latency}ms)"
)
self.update_record(
success = self.update_record(
domain, sub_domain, record_type, line, ip, ttl, remark
)
if not success:
logger.error(f"更新{line}线路记录失败: {domain} - {sub_domain}")
time.sleep(1) # 添加延时
def check_and_update(self):
"""检查并更新所有域名"""
@ -247,21 +235,20 @@ class DNSPodManager:
continue
domain = domain_config["domain"]
record_type = domain_config["record_type"]
update_interval = domain_config["update_interval"] * 60 # 转换为秒
# 初始化域名的更新时间记录
if domain not in self.last_update:
self.last_update[domain] = {}
# 处理IPv4记录
if domain_config["ipv4_enabled"]:
ipv4_config = domain_config.copy()
ipv4_config["record_type"] = "A"
self.update_domain_records(ipv4_config)
time.sleep(1) # 添加延时
# 获取该记录类型的最后更新时间
last_update = self.last_update[domain].get(record_type, 0)
# 检查是否需要更新
if current_time - last_update >= update_interval:
logger.info(f"开始更新域名: {domain}{record_type} 记录")
self.update_domain_records(domain_config)
self.last_update[domain][record_type] = current_time
# 处理IPv6记录
if domain_config["ipv6_enabled"]:
ipv6_config = domain_config.copy()
ipv6_config["record_type"] = "AAAA"
self.update_domain_records(ipv6_config)
time.sleep(1) # 添加延时
def main():

View File

@ -1,4 +1,5 @@
requests>=2.31.0
python-dotenv>=1.0.0
schedule>=1.2.1
loguru>=0.7.2
loguru>=0.7.2
tencentcloud-sdk-python>=3.0.1000