Tailscale-Public-Nodes/3.generate_config.py

53 lines
2.0 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.

# Description: 生成Tailscale.json文件和更新README.md文件
import json
import yaml
import time
if __name__ == "__main__":
# 读取all_nodes.yaml文件
with open("all_nodes.yaml", "r", encoding="utf-8") as f:
nodes = yaml.load(f, Loader=yaml.FullLoader)
# 将README.md文件中的节点列表替换为最新的节点信息
with open("README.md", "r", encoding="utf-8") as f:
readme = f.read()
node_list = ""
RegionID = 900
for node in nodes:
node['RegionID'] = RegionID
node_list += f"| {node['info']} | {RegionID} | {node['ip']} | {node['port']} |\n"
RegionID += 1
readme = readme[:readme.find("| :-: | :-: | :-: | :-: |") + 26] + node_list
# 保存README.md文件
with open("README.md", "w", encoding="utf-8") as f:
f.write(readme)
print("README.md文件已更新")
# 将旧的Tailscale.json重命名为Tailscale_时间戳.json
with open("./config/Tailscale.json", "r", encoding="utf-8") as f:
old_json = f.read()
with open(f"./config/Tailscale_{int(time.time())}.json", "w", encoding="utf-8") as f:
f.write(old_json)
# 生成Tailscale.json文件格式
with open("./config/Tailscale.json", "w", encoding="utf-8") as f:
f.write(json.dumps({
"derpMap": {
"OmitDefaultRegions": True,
"Regions": {
str(node["RegionID"]): {
"RegionID": node["RegionID"],
"RegionCode": node["info"],
"Nodes": [
{
"Name": node["info"],
"RegionID": node["RegionID"],
"HostName": node["ip"],
"DERPPort": node["port"],
"InsecureForTests": True
}
]
}
for node in nodes
}
}
}, ensure_ascii=False, indent=4))