import yaml import json # Load the YAML configuration file with open('clash.yml', 'r') as f: config = yaml.safe_load(f) # Start with fixed entries that are always in out.json output = [ { "tag": "IPv4_out", "protocol": "freedom", "settings": {} }, { "tag": "IPv6_out", "protocol": "freedom", "settings": { "domainStrategy": "UseIPv6" } }, { "tag": "Hack", "protocol": "freedom" }, { "tag": "WG", "protocol": "wireguard", "settings": { "secretKey": "oMbevD0bHyZ9VhTY4Sfb+PO4K53x3Ke+KwkrJz7tfmU=", "address": [ "172.16.0.2", "2606:4700:110:84e2:c89d:19e5:7948:94ad" ], "peers": [ { "publicKey": "bmXOC+F1FxEMF9dyiK2H5/1SUtzH0JuVo51h2wPfgyo=", "endpoint": "engage.cloudflareclient.com:2408", "persistentKeepalive": 0 } ], "kernelMode": True, "mtu": 1420, "workers": 2 } } ] # For each proxy in the YAML file, create a corresponding JSON entry. # The transformation wraps the proxy details into a settings.servers list # and adds streamSettings with fixed values. for proxy in config.get("proxies", []): proxy_entry = { "tag": proxy["name"], "protocol": proxy["type"], "settings": { "servers": [ { "address": proxy["server"], "port": proxy["port"], "password": proxy["password"] } ] }, "streamSettings": { "network": "tcp", "security": "tls", "tlsSettings": { "allowInsecure": True, "serverName": proxy["server"] } } } output.append(proxy_entry) # Optionally, add a final fixed entry (e.g. block) as in the sample out.json. output.append({ "protocol": "blackhole", "tag": "block" }) # Write the transformed data to out.json with indentation for readability. with open('out.json', 'w') as f: json.dump(output, f, indent=2)