← 返回首页

v4 日本入-v4台湾出-记

发布时间:2026-01-05 16:25分类:技术

环境准备 (两端通用)

双端通用

# 下载、解压并安装
wget https://github.com/SagerNet/sing-box/releases/download/v1.12.14/sing-box-1.12.14-linux-amd64.tar.gz && \
tar -xzvf sing-box-1.12.14-linux-amd64.tar.gz && \
cp sing-box-1.12.14-linux-amd64/sing-box /usr/local/bin/ && \
chmod +x /usr/local/bin/sing-box

# 验证安装
sing-box version

过程

出口端

配置文件
nano /etc/sing-box/config.json

{
  "log": {
    "level": "info",
    "timestamp": true
  },
  "inbounds": [
    {
      "type": "socks",
      "tag": "socks-in",
      "listen": "::",
      "listen_port": 8080,
      "users": [
        {
          "username": "你的用户名",
          "password": "你的密码"
        }
      ]
    }
  ],
  "outbounds": [
    {
      "type": "direct",
      "tag": "direct"
    }
  ]
}

入口端

域

nano /etc/sing-box/config.json

{
  "log": {
    "level": "info",
    "timestamp": true
  },
  "dns": {
    "servers": [
      { "tag": "dns_direct", "address": "223.5.5.5", "detour": "direct", "strategy": "ipv4_only" },
      { "tag": "dns_proxy", "address": "[https://8.8.8.8/dns-query](https://8.8.8.8/dns-query)", "detour": "proxy" }
    ],
    "rules": [
      { "domain": ["你的出口域名"], "server": "dns_direct" },
      { "query_type": ["A", "AAAA"], "server": "dns_proxy" }
    ],
    "final": "dns_direct"
  },
  "inbounds": [
    {
      "type": "tun",
      "tag": "tun-in",
      "interface_name": "tun0",
      "address": ["10.255.0.1/30"],
      "mtu": 1400,
      "stack": "system",
      "auto_route": false,
      "strict_route": false,
      "sniff": true,
      "endpoint_independent_nat": true
    }
  ],
  "outbounds": [
    {
      "type": "socks",
      "tag": "proxy",
      "server": "你的出口域名",
      "server_port": 10240,
      "version": "5",
      "username": "你的用户名",
      "password": "你的密码",
      "udp_over_tcp": true,
      "routing_mark": 666
    },
    { 
      "type": "direct", 
      "tag": "direct", 
      "bind_interface": "ens18" 
    }
  ],
  "route": {
    "auto_detect_interface": true,
    "rules": [
      { "protocol": "dns", "action": "hijack-dns" },
      { "outbound": "proxy" }
    ]
  }
}

脚本保活

#!/bin/bash
IFACE_WAN="$WAN_IFACE"
IFACE_TUN="tun0"
MARK_OUTBOUND_HEX="0x29a"
MARK_INBOUND_HEX="0x114"
TABLE_TUN="100"
sysctl -w net.ipv4.ip_forward=1 > /dev/null
sysctl -w net.ipv4.conf.all.rp_filter=2 > /dev/null
sysctl -w net.ipv4.conf.\$IFACE_WAN.rp_filter=2 > /dev/null
while true; do
    RULE_EXIST=\$(ip rule show | grep "lookup \$TABLE_TUN")
    ROUTE_EXIST=\$(ip route show table \$TABLE_TUN | grep "default")
    if [[ -z "\$RULE_EXIST" ]] || [[ -z "\$ROUTE_EXIST" ]]; then
        ip link set dev \$IFACE_TUN up 2>/dev/null
        ip route flush table \$TABLE_TUN
        ip route add default dev \$IFACE_TUN table \$TABLE_TUN 2>/dev/null
        ip rule add fwmark \$MARK_INBOUND_HEX lookup main priority 50 2>/dev/null
        ip rule add fwmark \$MARK_OUTBOUND_HEX lookup main priority 60 2>/dev/null
        ip rule add to 10.0.0.0/8 lookup main priority 70 2>/dev/null
        ip rule add to 172.16.0.0/12 lookup main priority 70 2>/dev/null
        ip rule add to 192.168.0.0/16 lookup main priority 70 2>/dev/null
        ip rule add from all lookup \$TABLE_TUN priority 100 2>/dev/null
    fi
    if ! iptables -t mangle -C PREROUTING -i \$IFACE_WAN -m conntrack --ctstate NEW -j CONNMARK --set-mark \$MARK_INBOUND_HEX 2>/dev/null; then
         iptables -t mangle -A PREROUTING -i \$IFACE_WAN -m conntrack --ctstate NEW -j CONNMARK --set-mark \$MARK_INBOUND_HEX
         iptables -t mangle -A PREROUTING -m connmark --mark \$MARK_INBOUND_HEX -j CONNMARK --restore-mark
         iptables -t mangle -A OUTPUT -m connmark --mark \$MARK_INBOUND_HEX -j CONNMARK --restore-mark
    fi
    sleep 2
done

记得改网卡名

chmod +x /root/keep_alive.sh

手动运行

双端皆为

sing-box run -c /etc/sing-box/config.json

持久化运行

写入系统服务

  1. Sing-box 服务
nano /etc/systemd/system/sing-box.service

[Unit]
Description=sing-box Service
Documentation=[https://sing-box.sagernet.org/](https://sing-box.sagernet.org/)
After=network.target nss-lookup.target network-online.target

[Service]
Type=simple
ExecStart=/usr/local/bin/sing-box run -c /etc/sing-box/config.json
Restart=always
RestartSec=3
CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_BIND_SERVICE CAP_NET_RAW
AmbientCapabilities=CAP_NET_ADMIN CAP_NET_BIND_SERVICE CAP_NET_RAW
NoNewPrivileges=true

[Install]
WantedBy=multi-user.target
  1. Keep Alive 服务
nano /etc/systemd/system/keep-alive.service

[Unit]
Description=Sing-box Routing Keep Alive Daemon
After=network.target sing-box.service

[Service]
Type=simple
ExecStart=/bin/bash /root/keep_alive.sh
Restart=always
RestartSec=3

[Install]
WantedBy=multi-user.target

启动服务:

systemctl daemon-reload #重载文件 \
systemctl enable sing-box keep-alive #配置开机自启 \
systemctl restart sing-box keep-alive #启动服务

查看 IP

curl ip.sb -4

查看日志:

journalctl -u sing-box -f

一键脚本

bash <(curl -Ls http://blog.imly.cc/api/gk/arbc.sh)
本文作者:bilicapr
版权声明:本博客所有文章除特别声明外,均采用CC BY-NC-SA 4.0许可协议。转载请注明出处!