scripts/share/oversea_websites.py

66 lines
2.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from cloudflare_manager import *
from namesilo_manager import *
cf_manager = CloudflareManager()
ns_manager = NameSiloMgr()
# 需要重新设置nameserver的域名
# 已成功激活的域名
# 需要添加并激活的域名
# cloudflare 中已存在的域名
list_df_res = json.loads(cf_manager.zone_list().json())
list_websites = safe_get_list(list_df_res, "result")
cf_domain_stat_map = dict()
for web in list_websites:
domain = safe_get_str(web, "name")
cf_domain_stat_map[domain] = web
ns_domains = ns_manager.client.list_domains()
idxx = 0
for ns_domain in ns_domains:
idxx += 1
domain = safe_get_str(ns_domain, "#text")
cf_stat = safe_get_dict(cf_domain_stat_map, domain)
status = safe_get_str(cf_stat, "status")
print(idxx, domain, status)
if status == "active":
continue
if len(status) > 0:
continue
# 如果不在cloudflare里面在cloudflare创建zone
if domain not in cf_domain_stat_map.keys():
cf_stat = json.loads(cf_manager.create_zone(domain).json())
# 从namesilo删除域名解析
all_ns_dns_records = list()
try:
ns_dns_records = ns_manager.client.list_dns_records(domain)
if isinstance(ns_dns_records, list):
for record in ns_dns_records:
record_new = record
record_new["domain"] = domain
all_ns_dns_records.append(record_new)
elif isinstance(ns_dns_records, dict):
record_new = ns_dns_records
record_new["domain"] = domain
all_ns_dns_records.append(record_new)
else:
print(idxx, domain, "invalid", ns_dns_records)
except Exception as e:
print(idxx, domain, "invalid", str(e))
for r in all_ns_dns_records:
domain = safe_get_str(r, "domain")
record_id = safe_get_str(r, "record_id")
ret = ns_manager.delete_dns_record(domain, record_id)
print(idxx, domain, record_id, ret)
# 把namesilo nameserver替换成 cf的nameservers
cf_nameservers = safe_get_list(cf_stat, "name_servers")
print(idxx, domain, cf_nameservers)
chane_nss_ret = ns_manager.change_nameservers(domain, cf_nameservers)
print(idxx, domain, cf_nameservers, chane_nss_ret)