sing-box 实现v4 in v6 out

一个实验需求,要搭建简单的v4转v6代理,考虑到客户端分流的需求,用了 sing-box。背景信息是,我的服务器有 v4/v6 双栈,但是很多云服务商v4的IP什么都干不了,但是v6目前还干净。

本身sing-box能做的很少,配置仅增加了 routing_mark,这样outbound 流量能搭配上PBR进行流量调度,我的配置如下:

  "outbounds": [
    {
      "tag": "direct",
      "type": "direct",
      "routing_mark": 13001
    }
  ]

下一步需要完成PBR并做两件事情,1 阻断v4,2 配置v6路由:


#!/bin/bash
# singbox-pbr.sh
# sing-box outbound routing_mark=13001 IPv6 出口,阻断 IPv4 fallback
TABLE_ID=13001
TABLE_NAME=singbox_v6
DEV=venet0  # VPS 虚拟接口

# --- 1. 确保 rt_tables 有记录 ---
grep -q "$TABLE_NAME" /etc/iproute2/rt_tables || \
    echo "$TABLE_ID $TABLE_NAME" >> /etc/iproute2/rt_tables

# --- 2. 清理旧规则和表 ---
ip rule del fwmark $TABLE_ID lookup $TABLE_NAME 2>/dev/null
ip rule del fwmark $TABLE_ID unreachable 2>/dev/null
ip -6 route flush table $TABLE_NAME 2>/dev/null

# --- 3. 添加 IPv6 默认路由 ---
# VPS venet0 接口通常是点对点,不需要指定网关
ip -6 route add default dev $DEV table $TABLE_NAME

# --- 4. 添加 fwmark 规则 ---
ip rule add fwmark $TABLE_ID lookup $TABLE_NAME priority 100
ip rule add fwmark $TABLE_ID unreachable priority 101

我是用 rc.local 启动的,各种启动方式都可以。

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *