好东西分享 · 2024年 10月 12日 0

sing-box TUN模式仅代理某个文件夹内的进程

我用的版本为1.9.7DNS为回国模式,代理C:\Program Files (x86)\Netease下的所有进程,配置文件如下:

{
    "log": {
        "level": "error",
        "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"
            },


 {
        "outbound": "proxy",
        "process_path": [
//可在此修改或添加要代理的文件夹
"C:\\Program Files (x86)\\Netease"
        ]
      },
      {
        "outbound": "direct",
        "process_name": [
          "*"
        ]
      },

            {
                "ip_is_private": true,
                "outbound": "direct"
            }
        ],
        "final": "direct",
        "auto_detect_interface": true
    },
    "inbounds": [
        {
            "type": "tun",
            "tag": "tun-in",
            "inet4_address": "172.16.0.1/30",
            "inet6_address": "fd00::1/126",
            "mtu": 1492,
            "auto_route": true,
            "strict_route": true,
            "stack": "system",
            "sniff": true,
            "sniff_override_destination": false
        }
    ],
    "outbounds": [
        {
            // 修改为你的客户端配置,需要保留 "tag": "proxy"
            "tag": "proxy",
      "type": "socks",
      "server": "1.1.1.1",
      "server_port": 13512,
      "username": "aaa",
      "password": "bbb"
        },
        {
            "type": "direct",
            "tag": "direct"
        },
        {
            "type": "block",
            "tag": "block"
        },
        {
            "type": "dns",
            "tag": "dns-out"
        }
    ],
    "experimental": {
        "cache_file": {
            "enabled": true,
            "path": "cache.db"
        }
    }
}

保存为config.json在sing-box程序的同目录,用cmd运行 sing-box.exe run

或者可以新建一个bat文件

@echo off
start cmd /c "cd /d C:\Users\文档\sing-box &&sing-box.exe run"
pause