马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
x
手工设置透明代理_openwrt-22.03_xray_tproxy_fw4_nftables
转载注明泉源: 本文链接 来自osnosn的博客,写于 2025-04-25.
系统环境
- openwrt-22.03 , 利用 fw4 , 即 nftables
xray 配置
- 没有配置dns走xray
- routing 中要设置 udp/123直连,用于ntp对时协议不走xray。假如是用本地局域网ntp服务,则跳过这项。
- freedom 的出站设置 domainStrategy 为 UseIP,以避免直连时因为利用本机的 DNS 出现一些奇怪问题;
- 要在所有的 outbound 加一个 mark=2,这个 mark 与下文 防火墙规则 配合,以直连 xray 发出的流量(blackhole 不消配置 mark)。
- {
- "inbounds": [
- { //在inbounds 中加入这一节 "dokodemo-door"
- "tag": "tproxy-in",
- "listen": "127.0.0.1",
- "port": 12345,
- "protocol": "dokodemo-door",
- "settings": {
- "network": "tcp,udp",
- "followRedirect": true
- },
- "sniffing": { //开启snifing,否则路由无法匹配域名
- "enabled": true,
- "destOverride": ["http", "tls", "quic"]
- },
- "streamSettings": {
- "sockopt": { "tproxy": "tproxy" }
- }
- },
- ... 其他配置
- ],
- "outbounds": [
- //每一个outbounds的出口中,除了blackhole,包含 "mark":2 参数
- // "streamSettings": {
- // "sockopt": { "mark": 2 }
- // }
- {
- "tag": "direct",
- "protocol": "freedom",
- "settings": { "domainStrategy": "UseIP" },
- "streamSettings": {
- "sockopt": { "mark": 2 }
- }
- },
- ... 其他配置
- ],
- "routing": {
- ... 其他配置
- "rules": [
- { //ntp对时协议
- "type": "field",
- "inboundTag": ["tproxy-in"],
- "port": 123,
- "network": "udp",
- "outboundTag": "direct"
- },
- ... 其他配置
- ]
- }
- }
复制代码 计谋路由
- 方法1(不推荐),把这两句,写入 /etc/rc.local 中,
- # 设置策略路由, 仅ipv4,
- # 添加路由表 100,指向local的loopback
- ip route add local 0.0.0.0/0 dev lo table 100
- # 所有标记1的Packet走路由表100
- ip rule add fwmark 1 table 100
复制代码 - 方法2,用luci的 web页面配置
- 添加路由表 100,指向local的loopback
在web配置页面,Network->Routing->Static IPv4 Routes 添加一条,- General Settings
- Interface: loopback
- Route type: local
- Target: 0.0.0.0/0
- Gateway: 留空
- Advanced Settings
- Table: 100
- 其他: 留空或不修改
复制代码 - 所有标记1的Packet走路由表100
在web配置页面,Network->Routing->IPv4 Rules 添加一条,- General Settings
- Priority: 30000 #自选,1-32765 都可以,
- Route type: unicast #默认值
- Table: 100
- 其他: 留空或不修改
- Advanced Settings
- Firewall mark: 1
- Invert match: 不勾 #默认值
- 其他: 留空或不修改
复制代码
防火墙规则
需要安装 nft-tproxy 的内核支持,
opkg update && opkg install kmod-nft-tproxy
创建文件 ""/etc/nftables.d/20-xray-rules.nft" 内容如下, 仅ipv4,
文件名只要是 .nft结尾就行,文件名任意。- define RESERVED_IP = {
- 10.0.0.0/8,
- 100.64.0.0/10,
- 127.0.0.0/8,
- 169.254.0.0/16,
- 172.16.0.0/12,
- 192.0.0.0/24,
- 224.0.0.0/4,
- 240.0.0.0/4,
- 255.255.255.255/32
- }
- define LAN_IP = {
- 192.168.10.0/24,
- 192.168.20.0/24
- }
- chain mangle_xray_prerouting {
- type filter hook prerouting priority mangle; policy accept;
- ip saddr != $LAN_IP return
- ip daddr $RESERVED_IP return
- ip daddr 192.168.0.0/16 tcp dport != 53 return
- ip daddr 192.168.0.0/16 udp dport != 53 return
- ip protocol tcp tproxy ip to 127.0.0.1:12345 meta mark set 1
- ip protocol udp tproxy ip to 127.0.0.1:12345 meta mark set 1
- }
- chain mangle_xray_output {
- type route hook output priority mangle; policy accept;
- ip daddr $RESERVED_IP return
- ip daddr 192.168.0.0/16 tcp dport != 53 return
- ip daddr 192.168.0.0/16 udp dport != 53 return
- meta mark 2 return
- ip protocol tcp meta mark set 1
- ip protocol udp meta mark set 1
- }
复制代码
- 在 openwrt的命令行中,实行 fw4 reload 假如没有任何输出,则OK,(没有报错)。
- 防火墙规则,没有配置 ipv6,
- 防火墙规则,支持openwrt本机,和lan口下其他呆板的透明代理。
- 内核包kmod-nft-socket,没有用到就没装。假如添加的规则中用到才需要安装。
手工设置完成
- 重启 openwrt,配置不丢失,计谋路由和防火墙规则自动生效。
----end----
转载注明泉源: 本文链接 https://www.cnblogs.com/osnosn/p/18844435.html
来自 osnosn的博客 https://www.cnblogs.com/osnosn/ .
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |