前端时间家里的路由器坏了,导致一些服务挂掉了,包括自己的博客,闺蜜圈 wiki 等。然后就是每天收到各种离线消息。
终于网络恢复之后,感觉一切正常然而,通过 cdn 反问就是回源错误。于是跟技术沟通了一下下下,发现盾云的域名回源竟然存在延迟,并且这个延迟好几个小时,这就比较蛋疼了。所以,每次换了 ip 首先要去设置 ip 地址才能正常回源。
既然,这个东西存在延迟,那么 dnspod 的 vip 的动态 ddns 解析也就没有任何的用了,延迟已经不是动态域名的问题了,是 cdn 域名刷新的问题。
今天早上忽然想到,既然域名有延迟,那就直接 ip 地址回源吧,不过域名太多了也不能每次都改啊。直接尝试代码解决吧,下面就是全部的代码了:
# 盾云SCDN 接口工具 # by:obaby # https://h4ck.org.cn # https://oba.by from datetime import datetime import requests username = '邮箱地址' password = '密码,先设置用户邮箱地址和密码' def login(): url = "https://scdn.ddunyun.com/prod-api/login" payload = "{\"email\":\""+username+"\",\"password\":\""+password+"\"}" headers = { 'accept': 'application/json, text/plain, */*', 'accept-language': 'zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7', 'content-type': 'application/json;charset=UTF-8', 'origin': 'https://scdn.ddunyun.com', 'priority': 'u=1, i', 'referer': 'https://scdn.ddunyun.com/', 'sec-ch-ua': '"Not/A)Brand";v="8", "Chromium";v="126", "Google Chrome";v="126"', 'sec-ch-ua-mobile': '?0', 'sec-ch-ua-platform': '"macOS"', 'sec-fetch-dest': 'empty', 'sec-fetch-mode': 'cors', 'sec-fetch-site': 'same-origin', 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36' } response = requests.request("POST", url, headers=headers, data=payload) # print(response.json()) js = response.json() if js['code'] == 0: return js['data']['access_token'] else: print('[E] 发生错误:', js['message']) return None # return None def get_site_list(token): url = "https://scdn.ddunyun.com/prod-api/site?domain_name=&sub_domain_name=&cname=&status=¤t_page=1&total=0&page_size=20" payload = {} headers = { 'accept': 'application/json, text/plain, */*', 'accept-language': 'zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7', 'authorization': 'Bearer ' + token, # 'cookie': 'LeCDN-Client=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJjbGllbnQiLCJleHAiOjE3MzMyOTM5NDcsIm5iZiI6MTczMzI3MTM0NywianRpIjoiNDEifQ.lpSIjvtVGE7wcxZgNF2upZiZ8QsPh2CYajevHqgsjzg; sidebarStatus=0', 'priority': 'u=1, i', 'referer': 'https://scdn.ddunyun.com/', 'sec-ch-ua': '"Not/A)Brand";v="8", "Chromium";v="126", "Google Chrome";v="126"', 'sec-ch-ua-mobile': '?0', 'sec-ch-ua-platform': '"macOS"', 'sec-fetch-dest': 'empty', 'sec-fetch-mode': 'cors', 'sec-fetch-site': 'same-origin', 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36' } response = requests.request("GET", url, headers=headers, data=payload) # print(response.text) js = response.json() if js['code'] == 0: return js['data']['data'] else: print('[E] 发生错误:', js['message']) return None def get_site_source(access_token, site_id): url = "https://scdn.ddunyun.com/prod-api/site_source?site_id=" + str(site_id) payload = {} headers = { 'accept': 'application/json, text/plain, */*', 'accept-language': 'zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7', 'authorization': 'Bearer ' + access_token, # 'cookie': 'LeCDN-Client=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJjbGllbnQiLCJleHAiOjE3MzMyOTM5NDcsIm5iZiI6MTczMzI3MTM0NywianRpIjoiNDEifQ.lpSIjvtVGE7wcxZgNF2upZiZ8QsPh2CYajevHqgsjzg; sidebarStatus=0', 'priority': 'u=1, i', 'referer': 'https://scdn.ddunyun.com/', 'sec-ch-ua': '"Not/A)Brand";v="8", "Chromium";v="126", "Google Chrome";v="126"', 'sec-ch-ua-mobile': '?0', 'sec-ch-ua-platform': '"macOS"', 'sec-fetch-dest': 'empty', 'sec-fetch-mode': 'cors', 'sec-fetch-site': 'same-origin', 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36' } response = requests.request("GET", url, headers=headers, data=payload) js = response.json() if js['code'] == 0: return js['data']['data'] else: print('[E] 发生错误:', js['message']) return None def delete_source(access_token, site_id, source_id): # url = "https://scdn.ddunyun.com/prod-api/site_source/593?site_id=382" url = "https://scdn.ddunyun.com/prod-api/site_source/" + str(source_id) + "?site_id=" + str(site_id) payload = {} headers = { 'accept': 'application/json, text/plain, */*', 'accept-language': 'zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7', 'authorization': 'Bearer ' + access_token, # 'cookie': 'LeCDN-Client=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJjbGllbnQiLCJleHAiOjE3MzMyOTM5NDcsIm5iZiI6MTczMzI3MTM0NywianRpIjoiNDEifQ.lpSIjvtVGE7wcxZgNF2upZiZ8QsPh2CYajevHqgsjzg; sidebarStatus=0', 'origin': 'https://scdn.ddunyun.com', 'priority': 'u=1, i', 'referer': 'https://scdn.ddunyun.com/', 'sec-ch-ua': '"Not/A)Brand";v="8", "Chromium";v="126", "Google Chrome";v="126"', 'sec-ch-ua-mobile': '?0', 'sec-ch-ua-platform': '"macOS"', 'sec-fetch-dest': 'empty', 'sec-fetch-mode': 'cors', 'sec-fetch-site': 'same-origin', 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36' } response = requests.request("DELETE", url, headers=headers, data=payload) js = response.json() if js['code'] == 0: return js['data'] else: print('[E] 发生错误:', js['message']) return None def edit_source(access_token, site_id, source_id, ip_address): url = "https://scdn.ddunyun.com/prod-api/site_source/" + str(source_id) payload = "{\"id\":" + str(source_id) + ",\"site_id\":" + str( site_id) + ",\"type\":\"ipaddr\",\"content\":\"" + ip_address + "\",\"priority\":\"20\",\"weight\":15,\"created_at\":\"2024-11-25 13:21:23\",\"updated_at\":\"2024-12-01 13:39:13\",\"isEdit\":true}" headers = { 'accept': 'application/json, text/plain, */*', 'accept-language': 'zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7', 'authorization': 'Bearer ' + access_token, 'content-type': 'application/json;charset=UTF-8', # 'cookie': 'LeCDN-Client=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJjbGllbnQiLCJleHAiOjE3MzMyOTM5NDcsIm5iZiI6MTczMzI3MTM0NywianRpIjoiNDEifQ.lpSIjvtVGE7wcxZgNF2upZiZ8QsPh2CYajevHqgsjzg; sidebarStatus=0', 'origin': 'https://scdn.ddunyun.com', 'priority': 'u=1, i', 'referer': 'https://scdn.ddunyun.com/', 'sec-ch-ua': '"Not/A)Brand";v="8", "Chromium";v="126", "Google Chrome";v="126"', 'sec-ch-ua-mobile': '?0', 'sec-ch-ua-platform': '"macOS"', 'sec-fetch-dest': 'empty', 'sec-fetch-mode': 'cors', 'sec-fetch-site': 'same-origin', 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36' } response = requests.request("PUT", url, headers=headers, data=payload) js = response.json() if js['code'] == 0: return js['data'] else: print('[E] 发生错误:', js['message']) return None def add_source(access_token, site_id, ip_address): import requests url = "https://scdn.ddunyun.com/prod-api/site_source" payload = "{\"type\":\"ipaddr\",\"content\":\"" + ip_address + "\",\"priority\":\"20\",\"weight\":15,\"isEdit\":true,\"site_id\":" + str( site_id) + "}" headers = { 'accept': 'application/json, text/plain, */*', 'accept-language': 'zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7', 'authorization': 'Bearer ' + access_token, 'content-type': 'application/json;charset=UTF-8', # 'cookie': 'LeCDN-Client=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJjbGllbnQiLCJleHAiOjE3MzMyOTM5NDcsIm5iZiI6MTczMzI3MTM0NywianRpIjoiNDEifQ.lpSIjvtVGE7wcxZgNF2upZiZ8QsPh2CYajevHqgsjzg; sidebarStatus=0', 'origin': 'https://scdn.ddunyun.com', 'priority': 'u=1, i', 'referer': 'https://scdn.ddunyun.com/', 'sec-ch-ua': '"Not/A)Brand";v="8", "Chromium";v="126", "Google Chrome";v="126"', 'sec-ch-ua-mobile': '?0', 'sec-ch-ua-platform': '"macOS"', 'sec-fetch-dest': 'empty', 'sec-fetch-mode': 'cors', 'sec-fetch-site': 'same-origin', 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36' } response = requests.request("POST", url, headers=headers, data=payload) js = response.json() if js['code'] == 0: return js['data'] else: print('[E] 发生错误:', js['message']) return None def update_domain_source(domain_list, new_ip_address): token = login() print('[T] 获取到 token:', token) # 访问 https://www.jetbrains.com/help/pycharm/ 获取 PyCharm 帮助 site_list = get_site_list(token) # new_ip_address = '123.234.187.19' # print(site_list) for s in site_list: # print(s['domain_name']) dn = s['domain_name'] site_id = s['id'] if dn in domain_list: source_list = s['sources'] # print(source_list) print('-' * 100) print('[U] 时间:', datetime.now()) print('[U] 更新域名配置:') print('[U] 域名:', dn) ip_count = 0 for sl in source_list: source_id = sl['id'] # site_id = sl['site_id'] ip_address = sl['content'] try: if sl['type'] == 'ipaddr': print('[U] 旧 IP 地址:', sl['content']) # edit_source(token, site_id, source_id, ip_address) if ip_address != new_ip_address: print('[U] 更新:正在更新 ip 地址:123.234.187.19') edit_source(token, site_id, source_id, new_ip_address) else: print('[U] 忽略:IP地址无变化') ip_count += 1 else: print('[D] 删除:删除域名回源:', ip_address) delete_source(token, site_id, source_id) except Exception as e: print('[E] 发生异常:', e) # 如果不存在 IP 地址回源 添加 ip 回源 if ip_count == 0: print('[U] 添加:添加IP地址回源:', new_ip_address) add_source(token, site_id, new_ip_address) print('*' * 100) print('[U] 更新完成') print('#' * 100) def print_usage(): print('*' * 100) print('盾云 SCDN 回源地址配置工具') print('说明:用于更新盾云 ip 地址回源,解决域名回源导致的延迟问题') print('by: obaby') print('https://oba.by') print('https://h4ck.org.cn') print('#' * 100) if __name__ == '__main__': print('*' * 100) print('盾云 SCDN 回源地址配置工具') print('by: obaby') print('https://oba.by') print('https://h4ck.org.cn')
然后在需要更新 ip 地址的地方通过下面的代码调用即可:
from baby_dunyun import * if __name__ == '__main__': domain_list = [ "image.dlj.kim", "nai.dog", "image.h4ck.org.cn", "wiki.guimiquan.cn", "baby.lc", "danteng.me", "loli.gifts", "lang.bi", "da.bi", "zhongxiaojie.cn", "oba.by", "zhongxiaojie.com", "lang.ma", "dlj.kim", "h4ck.org.cn", ] new_ip_address = '123.234.187.19' update_domain_source(domain_list, new_ip_address)
运行效果:
此工具仅支持盾云 scdn,https://scdn.ddunyun.com/#/dashboard