方永、南天紫云

道亦有道

OpenWrt配置透明代理
2023年02月11日

上篇,固件更新了,更为重要的是配置好透明代理。

方案是Phantun + WireGuard + SmartDns

首先是 Phantun的配置:

Phantun官网即可下载预编译的binary,然后在 /etc/init.d/ 目录添加名为phantun的文件:

#!/bin/sh /etc/rc.common
# Copyright (C) 2006-2011 OpenWrt.org

START=50

USE_PROCD=1
PROG=/usr/local/bin/phantun

start_service() {
        procd_open_instance
        procd_set_param command "$PROG" --local 127.0.0.1:1234 --remote x.com:5678
        procd_set_param respawn
        procd_close_instance
}

phantun将以服务的形式启动和管理。

备注:记得在远端用 nft list table inet nat 查看端口号。

phantun 需要创建tun设备,故依赖 kmod-tun,检查此内核模块已安装。

若需要调试,在shell中直接运行 RUST_LOG=info /usr/local/bin/phantun --local 127.0.0.1:1234 --remote x.com:5678

在luci的 Network->Interfaces 中添加Protocol为 UnmanagedinterfaceDevicetun0Firewall Settings选择lan

然后是 WireGuard的配置:

在luci的 Network->Interfaces 中添加Protocol为 WireGuard VPNinterfaceFirewall Settings选择lan。添加完成后/etc/config/network会新增如下配置项:

config interface 'wg0'
        option proto 'wireguard'
        option private_key 'xxx'
        list addresses '10.0.0.2'
        option mtu '1250'
        option nohostroute '1'
        option ip4table '21'

config wireguard_wg0 'wgserver'
        option description 'a'
        option public_key 'xxx'
        option endpoint_host '127.0.0.1'
        option endpoint_port '1234'
        option persistent_keepalive '25'
        list allowed_ips '0.0.0.0'
        option route_allowed_ips '1'

config wireguard_wg0
        option description 'b'
        option public_key 'xxx'
        option endpoint_host 'xxx'
        option persistent_keepalive '20'
        list allowed_ips '10.0.0.2'
        option route_allowed_ips '1'

注意:上述配置添加了两个peer a 和 b, a 作为默认节点(配置了list allowed_ips '0.0.0.0'),所以wg0的下一跳会路由到这个节点;b 显式地配置了路由,即 list allowed_ips '10.0.0.2',那么目的地址是 10.0.0.2 的数据包会路由到这个peer。

Network->Firewall->NAT Rules 中添加一项, ActionMASQUERADE, Outbound Device 选 wg0

wg show 查看,应该已经握手成功。

接下来配置路由,相关命令如下:

nft add set inet fw4 fq { type ipv4_addr \;}
nft add element inet fw4 fq { 10.0.0.1 }
nft add rule inet fw4 mangle_output ip daddr @fq counter mark set 0x15
nft add rule inet fw4 mangle_prerouting ip daddr @fq counter mark set 0x15
ip rule add fwmark 0x15 table 21
ip route add 192.168.1.0/24 dev br-lan table 21

意思就是添加一个名为fq的 nft set,并将所有目的地址在这个set中的数据包添加标记0x15, 然后添加规则将所有标记0x15的数据包使用表21的路由配置,在21的路由表中,添加默认路由到wg0。

当然,这只是调试时的写法,nft命令对应的配置添加到/etc/nftables.d/10-custom-filter-chains.nft:

set fq {
    typeof ip daddr
    flags interval
    auto-merge
    elements = { 10.0.0.0/24, 1.1.1.1 }
}

chain mangle_prerouting {
    type filter hook prerouting priority mangle; policy accept;
    ip daddr @fq counter mark set 0x15;
}

chain mangle_output {
    type route hook output priority mangle; policy accept;
    ip daddr @fq counter mark set 0x15;
}

# as of OpenWrt version 23.05.1, it is necessary to add this to correctly handle TCP MSS. Future versions may no longer require it.
chain mangle_forward {
    type filter hook forward priority mangle; policy accept;
    iifname "wg0" tcp flags syn tcp option maxseg size set rt mtu
    oifname "wg0" tcp flags syn tcp option maxseg size set rt mtu
}

ip命令对应的配置添加到/etc/config/network:

config rule
        option lookup '21'
        option mark '0x15'

config route
        option interface 'lan'
        option target '192.168.1.0/24'
        option table '21'

或通过luci添加。

此时 ping 10.0.0.1,可以ping通,10.0.0.1是WireGuard的一个peer的地址。 如果 ping 超时,用 ip rule 查看是否有 from all fwmark 0x15 lookup 21 条目, 如果没有且配置正确,尝试重启路由器。或者查看路由表21 ip r show table 21,正确的路由项如下:

default dev wg0 scope link
10.0.0.2 dev wg0 scope link
192.168.1.0/24 dev br-lan scope link

可尝试用 traceroute 1.1.1.1 在OpenWrt上跟踪路由是否正确配置。

2023年12月02日更新

存在A、B、C、D 4个 WireGuard的节点, 如图所示:

若要通过C和B连通D和A,其中B节点是本文提到的OpenWrt, 则此OpenWrt还需要添加如下路由:

config rule
    option dest '10.0.0.0/24'
    option lookup '0x15'

当然,每个节点的 allow-ips 仍需正确配置。若 nftables set 中有 10.0.0.0/24, 那么这条规则无需配置。

SmartDns的配置:

官网下载ipk并安装, 注意必须是39以后的版本,代理相关的配置有以下两行:

domain-set -name fq -type list -file fq.list
domain-rules /domain-set:fq/ -nameserver fq -nftset #4:inet#fw4#fq

将需要代理的域名放在/etc/smartdns/fq.list

配置完成。

请求 fq.list 中的域名,查看 nftables 的set 是否增加相对应的IP。

nft list set inet fw4 fq