以fakeip分流为基石的一套科学方案

本文介绍sing-box(或mihomo)搭配mosdns的fakeip科学方案,sing-box(或mihomo)实现全家设备透明代理,mosdns实现国内外精确分流

一、基础知识

  • 此方案的分流策略是DNS分流,或者说域名分流,第一部分先介绍下DNS解析的基础知识,本文只从实际操作方面做简略的非专业介绍,详细的进阶知识请翻阅skk大佬博客

https://blog.skk.moe/post/what-happend-to-dns-in-proxy/

  • 首先,当你在浏览器中输入 www.google.com 打开后,浏览器实际需要通过IP与google服务器连接,进而首先需要知道 www.google.com 的ip地址,这就引出了DNS解析这一概念
  • 浏览器通过操作系统向本地设置的DNS服务器(例如mosdns)发起解析请求,mosdns返回了www.google.com 的IP(如172.217.31.132),下一步浏览器向172.217.31.132发起请求
  • 传统的CNIP分流法,通过在主路由中添加中国IP列表,判断IP是否属于CN列表进行分流,如刚才的172.217.31.132不属于CNIP列表,则将流量导向代理软件,属于CNIP则直接通过主路由wan口直连

二、fakeip分流的流量走向

  • fakeip分流法是在DNS解析阶段,将google等国外域名解析为一个不存在于公网的、内部的、虚假的IP,例如常用的fakeip网段198.18.0.0/18或28.0.0.0/8
  • 流量路径为浏览器等客户端访问google,获取fakeip后(如28.0.0.5),向该IP(28.0.0.5)发起连接。连接来到主路由后判断28.0.0.5在设置的fakeip段内,通过静态路由或下一跳网关等手段进入代理程序(sing-box或mihomo)
  • 主路由中仅需配置1条规则,将fakeip段导入代理程序即可完成分流(例如RouterOS通过mangle或route的方式,详细的实现手段可参考博主的另一篇文章
    https://blog.847977.xyz/2025/08/14/03-RouterOS%E4%BD%BF%E7%94%A8FakeIP/
  • fakeip到达sing-box或mihomo后,程序根据fakeip域名对照表(后面再介绍表的由来)将fakeip还原为域名,然后将域名发送给代理节点,代理节点再次进行DNS解析完成访问后将内容一路返回到浏览器,你就打开了google看到了内容

三、介绍本方案的拓扑结构

  • 本方案总体来说属于旁路网关
  • 博主的拓扑为:光猫桥接(建议必须桥接)。PVE或ESXI作为底层,在其中部署网络相关的所有虚拟机,包括主路由RouterOS(10.0.0.1)负责DCHP和拨号;部署Debian13作为旁路网关(10.0.0.2),在其中安装sing-box负责透明代理;部署Debian13(10.0.0.3)在其中安装mosdns负责分流。技术过硬的同学sing-box和mosdns也可以部署于一个虚拟机中,博主自己求稳是分开部署。
  • fakeip方案主路由选择很多,目前RouterOS、OPNsense、vyos、ikuai、openwrt均可实现,本质上来说只要支持静态路由的系统均能使用,包括某些硬路由。
  • sing-box和mosdns的安装比较灵活,pve的lxc,debian、ubuntu、alpine的vm均可实现。ALL in one也可以,例如PH大佬作为主路由的openwrt其中也运行了sing-box和mosdns(均是跑裸核),RouterOS的容器也能运行sing-box和mosdns(但不推荐、性能太差)。初次接触的同学建议一个程序一个vm最为稳妥(参考博主的拓扑),用熟悉了再自由搭配。
  • 局域网内设备的IP、网关、DNS关系如下。所有设备网关均为主路由,需科学的设备DNS为mosdns,不科学的设备DNS设置为主路由或其他公网DNS均可(只要不获取fakeip就是直连)。
RouterOS sing-box mosdns 需科学的设备 不科学的设备
IP 10.0.0.1/24 10.0.0.2/24 10.0.0.3/24 任意 任意
网关 拨号 10.0.0.1 10.0.0.1 10.0.0.1 10.0.0.1
DNS 运营商DNS 10.0.0.3 10.0.0.3 10.0.0.3 10.0.0.1或223.5.5.5等其他DNS均可
  • fakeip方案的一大优势在于,所有设备网关均为主路由,国内流量直接从主路由出去,不经过旁路,旁路挂掉的情况下不影响访问国内。同时fakeip方案中只有需要代理的极少IP(如fakeip、TG的IP、奈菲的ip)才会去到旁路走代理,所有PT BT下载等p2p流量天然就是直连,无需担心下载耗费机场或vps流量(但需注意如果qb tr等程序的DNS设置为mosdns,则需要将tracker服务器的域名加入mosdns的白名单中,避免通过代理访问tracker从而被标盒)

四、安装并运行sing-box和mosdns

(一)自动挡用户

1.脚本部署sing-box

  • 新建虚拟机,使用debian或ubuntu,运行脚本(由于脚本在github,需要虚拟机具备科学的能力)
1
wget https://raw.githubusercontent.com/herozmy/StoreHouse/refs/heads/latest/install.sh && bash install.sh

输入 y
kx1.png
输入 3 安装sing-box
kx2.png
vps用户选择“1.官方核心”,机场用户选择“3.reF1nd核心”。以安装reF1nd核心为例,输入 3
kx3.png
输入 y,再粘贴订阅链接
kx5.png
回车
kx6.png
如图则安装成功
kx7.png
可输入 proxytool ,管理sing-box服务
kx9.png
打开提示的地址,主机输入sing-box所属vm的ip地址,点击提交即可打开ui面板
kx8.png

  • 脚本也可以安装mihomo裸核,自行研究吧 ~

2.脚本部署mosdns(PH大佬魔改的带UI的版本)

  • 新建虚拟机,使用debian或ubuntu,运行脚本
1
wget https://raw.githubusercontent.com/herozmy/StoreHouse/refs/heads/latest/install.sh && bash install.sh

输入 y
kx1.png
输入 1
kx10.png
① 输入sing-box的ip:6666(如果使用上述脚本创建的sing-box端口就填6666,如果是自己的配置则填写sing-box的dns入站监听端口)
②输入自己运营商下发的DNS(填别的例如114或你喜欢的DNS上游都可以,不是必须,填入运营商话理论上最快)
③ 输入 999 使用 PH大佬更新维护的越用越快的mosdns,还自带UI界面(UI贴图在后面)
kx11.png
输入 n (不注释socks5代理) ,再输入 y (更改socks5代理),再输入sing-box的socks5入站(如果使用上述脚本创建的sing-box端口就填7891)
kx12.png

图片1.png
图片2.png
图片3.png
图片4.png
图片5.png
图片6.png
图片7.png
图片8.png
图片9.png
图片10.png

更多mosdns和ui相关的设置问题,加入tg群咨询吧(ph、herozmy等各位大佬都在群里哦)
https://t.me/+bzSRf6dtG3lhYWVl

脚本来自于herozmy大佬,项目地址https://github.com/herozmy/StoreHouse ,请各位同学点点star

(二)手动挡用户(进阶学习)

1.手动命令行部署官核sing-box

  • 使用sing-box裸核需要4个步骤:准备sing-box二进制程序;准备sing-box配置文件;准备systemd服务文件;安装并配置nftables
  • 建议使用debian或ubuntu虚拟机,本文以debian13为例,1核,内存1G,硬盘8G,
  • 本文的sing-box配置文件、systemd服务文件、nftables配置文件来自于@Ron大佬,感谢~
  • 下面开始,先以root用户ssh登录debian13虚拟机,虚拟机建议拥有科学能力,或自行去github下载模板文件上传到虚拟机。
(1)安装sing-box程序

更新软件

1
2
3
4
apt update
apt install curl wget unzip
apt upgrade -y
reboot

修改时区为Asian/Shanghai

1
dpkg-reconfigure tzdata

安装sing-box(提供3种方式,新手建议1或2)

① 下载编译好的sing-box:(虚拟机需科学,cpu架构为x86,默认下载1.12.12版本,如需下载指定版本自行修改命令)

1
2
3
4
5
wget https://github.com/SagerNet/sing-box/releases/download/v1.12.12/sing-box-1.12.12-linux-amd64.tar.gz && \
tar -xzf sing-box-1.12.12-linux-amd64.tar.gz && \
mv sing-box-1.12.12-linux-amd64/sing-box /usr/local/bin/ && \
chmod +x /usr/local/bin/sing-box && \
rm -r sing-box-1.12.12-linux-amd64 sing-box-1.12.12-linux-amd64.tar.gz

② 手动下载 :到sing-box的releases界面https://github.com/SagerNet/sing-box/releases,下载对应架构的sing-box,将解压后的sing-box文件上传到 /usr/local/bin 目录下,再使用命令赋予sing-box执行权限

1
chmod +x /usr/local/bin/sing-box

③ 编译安装最新稳定版sing-box

1
2
3
4
5
6
apt -y install curl git build-essential libssl-dev libevent-dev zlib1g-dev gcc-mingw-w64
curl -L https://go.dev/dl/go1.25.1.linux-amd64.tar.gz -o go1.25.1.linux-amd64.tar.gz
tar -C /usr/local -xzf go1.25.1.linux-amd64.tar.gz
echo 'export PATH=$PATH:/usr/local/go/bin' > /etc/profile.d/golang.sh
source /etc/profile.d/golang.sh
go install -v -tags with_quic,with_grpc,with_dhcp,with_wireguard,with_ech,with_utls,with_reality_server,with_clash_api,with_gvisor,with_v2ray_api,with_lwip,with_acme github.com/sagernet/sing-box/cmd/sing-box@latest

如果需要特定版本号,将末尾latest 改为V1.8.5 (就是1.8.5 版本)。以下为1.12.0版本以上的命令,需要旧版本的自行解决

1
2
go install -v -tags with_quic,with_grpc,with_dhcp,with_wireguard,with_utls,with_clash_api,with_gvisor,with_v2ray_api,with_lwip,with_acme github.com/sagernet/sing-box/cmd/sing-box@latest
cp $(go env GOPATH)/bin/sing-box /usr/local/bin/
(2)准备配置文件

创建配置文件夹

1
mkdir -p /usr/local/etc/sing-box/conf

到github仓库https://github.com/jasonxtt/file/tree/main/sing-box/conf下载配置模板,6个文件均放到/usr/local/etc/sing-box/conf目录

或使用命令下载

1
2
cd /usr/local/etc/sing-box/conf
wget https://raw.githubusercontent.com/jasonxtt/file/refs/heads/main/sing-box/conf/00_log.json https://raw.githubusercontent.com/jasonxtt/file/refs/heads/main/sing-box/conf/01_experimental.json https://raw.githubusercontent.com/jasonxtt/file/refs/heads/main/sing-box/conf/02_dns.json https://raw.githubusercontent.com/jasonxtt/file/refs/heads/main/sing-box/conf/03_inbounds.json https://raw.githubusercontent.com/jasonxtt/file/refs/heads/main/sing-box/conf/04_outbound.json https://raw.githubusercontent.com/jasonxtt/file/refs/heads/main/sing-box/conf/05_route.json

使用脚本下载ruleset文件

1
2
3
4
cd /usr/local/etc/sing-box
wget https://raw.githubusercontent.com/jasonxtt/file/refs/heads/main/sing-box/script/singbox_rule_updata.sh
chmod +x singbox_rule_updata.sh
./singbox_rule_updata.sh

修改配置文件。仅需修改”/usr/local/etc/sing-box/conf/04_outbound.json”,将节点加进去。
需要其他策略组的自行添加。修改”GLOBAL”策略组中的顺序,即可修改ui中各策略组的显示顺序。
注意:Proxy分组的名字别动。且需保证节点正常,否则无法下载ui。
使用启动命令查看是否报错,没报错则 ctrl c 取消进行下一步

1
/usr/local/bin/sing-box run -C /usr/local/etc/sing-box/conf/
(3)创建sing-box启动服务
1
2
3
4
cd /etc/systemd/system/
wget https://raw.githubusercontent.com/jasonxtt/file/refs/heads/main/sing-box/service/sing-box.service
systemctl daemon-reload
systemctl enable sing-box --now

查看sing-box运行状态。显示绿色的active (running)则正常启动了sing-box

1
systemctl status sing-box 
(4)安装并配置nftables

安装nftables

1
apt install nftables -y

下载nftables配置文件

1
2
3
cd /etc
mv nftables.conf nftables.conf.bak
wget https://raw.githubusercontent.com/jasonxtt/file/refs/heads/main/sing-box/nftables/nftables.conf

将”/etc/nftables.conf”中10.0.0.0/8替换为自己的内网IP段,保存。如果fakeip段不是28.0.0.0/8和f2b0::/18也自行搜索替换。

启用nftables。默认TCP使用redirect,UDP使用tproxy。

1
2
3
4
5
nft flush ruleset
nft -f /etc/nftables.conf
nft list ruleset
systemctl enable --now nftables
systemctl restart nftables

提供@Ron大佬写的nftable规则切换脚本,选项1是tcp-redirect、udp-tproxy,选项2是tcp、udp都用tproxy。使用前自己修改替换脚本中的内网IP段和fakeIP段
https://raw.githubusercontent.com/jasonxtt/file/refs/heads/main/sing-box/script/switch_nftable.sh

至此,sing-box安装完毕,有问题欢迎进TG群<折腾日记>交流

2.手动部署ph魔改版mosdns