Skip to content

Instantly share code, notes, and snippets.

@wynemo
Last active December 16, 2024 00:07
Show Gist options
  • Select an option

  • Save wynemo/21d0024e9071ddc938a1137394d38749 to your computer and use it in GitHub Desktop.

Select an option

Save wynemo/21d0024e9071ddc938a1137394d38749 to your computer and use it in GitHub Desktop.
sing-box 例子
import json
import sys
# 用法
# nodes.json 可以用Sub-Store生成 sing-box 格式的
# python3 merge.py sing-box-basic.json nodes.json
# 文件已成功更新并保存为 sing-box-basic-updated.json
# 节点
with open(sys.argv[2], "r", encoding="utf-8") as nodes_file:
nodes_data = json.load(nodes_file)
# 基本配置
with open(sys.argv[1], "r", encoding="utf-8") as sing_box_file:
sing_box_data = json.load(sing_box_file)
# 获取 nodes.json 中的 outbounds
nodes_outbounds = nodes_data.get("outbounds", [])
# 合并 outbounds
sing_box_outbounds = sing_box_data.get("outbounds", [])
sing_box_outbounds.extend(nodes_outbounds)
# 获取 "自动选择" 的 outbound 配置
auto_select = next((out for out in sing_box_outbounds if out.get("tag") == "自动选择"), None)
# 获取 "proxy" 的 outbound 配置
proxy_select = next((out for out in sing_box_outbounds if out.get("tag") == "proxy"), None)
if auto_select and isinstance(auto_select.get("outbounds"), list):
# 添加所有 nodes.json 中的 tag 到 "自动选择" 的 outbounds
auto_select["outbounds"].extend([out["tag"] for out in nodes_outbounds if "tag" in out])
else:
print("未找到 '自动选择' 的 outbound 配置,或其结构不符合预期")
if proxy_select and isinstance(proxy_select.get("outbounds"), list):
proxy_select["outbounds"].extend([out["tag"] for out in nodes_outbounds if "tag" in out])
# 保存修改后的文件
with open("sing-box-basic-updated.json", "w", encoding="utf-8") as updated_file:
json.dump(sing_box_data, updated_file, ensure_ascii=False, indent=2)
print("文件已成功更新并保存为 sing-box-basic-updated.json")
{
"log": {
"level": "info",
"timestamp": true
},
"dns": {
"servers": [
{
"tag": "dns_proxy",
"address": "https://1.1.1.1/dns-query",
"address_resolver": "dns_resolver",
"strategy": "ipv4_only",
"detour": "proxy"
},
{
"tag": "dns_direct",
"address": "https://dns.alidns.com/dns-query",
"address_resolver": "dns_resolver",
"strategy": "ipv4_only",
"detour": "direct"
},
{
"tag": "dns_resolver",
"address": "223.5.5.5",
"detour": "direct"
}
],
"rules": [
{
"outbound": "any",
"server": "dns_resolver"
},
{
"rule_set": "geosite-geolocation-!cn",
"server": "dns_proxy"
}
],
"final": "dns_direct"
},
"route": {
"rule_set": [
{
"tag": "geosite-geolocation-!cn",
"type": "remote",
"format": "binary",
"url": "https://raw.githubusercontent.com/SagerNet/sing-geosite/rule-set/geosite-geolocation-!cn.srs",
"download_detour": "proxy"
},
{
"tag": "geoip-cn",
"type": "remote",
"format": "binary",
"url": "https://raw.githubusercontent.com/SagerNet/sing-geoip/rule-set/geoip-cn.srs",
"download_detour": "proxy"
}
],
"rules": [
{
"protocol": "dns",
"outbound": "dns-out"
},
{
"port": 853,
"network": "tcp",
"outbound": "block"
},
{
"port": [
443,
853
],
"network": "udp",
"outbound": "block"
},
{
"type": "logical",
"mode": "and",
"rules": [
{
"rule_set": "geoip-cn",
"invert": true
},
{
"rule_set": "geosite-geolocation-!cn"
}
],
"outbound": "proxy"
},
{
"rule_set": "geoip-cn",
"outbound": "direct"
},
{
"ip_is_private": true,
"outbound": "direct"
}
],
"final": "proxy",
"auto_detect_interface": true
},
"inbounds": [
{
"type": "tun",
"tag": "tun-in",
"address": [
"172.18.0.1/30",
"fdfe:dcba:9876::1/126"
],
"mtu": 1492,
"auto_route": true,
"strict_route": true,
"stack": "system",
"sniff": true,
"sniff_override_destination": false
}
],
"outbounds": [
{
"type": "direct",
"tag": "direct",
"routing_mark": 100
},
{
"type": "block",
"tag": "block"
},
{
"type": "dns",
"tag": "dns-out"
},
{
"type": "urltest",
"tag": "自动选择",
"outbounds": [
"local-hy2"
]
},
{
"type": "selector",
"tag": "proxy",
"outbounds": [
"direct",
"block",
"自动选择",
"local-hy2"
],
"default": "自动选择"
},
{
"type": "selector",
"tag": "GLOBAL",
"outbounds": [
"direct",
"proxy"
],
"default": "proxy"
},
{
"type": "socks",
"tag": "local-hy2",
"server": "127.0.0.1",
"server_port": 7890,
"version": "5"
}
],
"experimental": {
"cache_file": {
"enabled": true,
"path": "cache.db"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment