
我建立了一個 Ubuntu 16.04 lxd 容器並在其中設定了 Stunnel、Tinyproxy 和 OpenVPN 用戶端。
目標是透過 Stunnel 連接到 Tinyproxy 並強制 Tinyproxy 使用 OpenVPN 的介面進行傳出連線。
Stunnel -> Tinyproxy 工作正常 - 瀏覽器中的頁面正在按預期加載,但是,一旦我啟動 OpenVPN 服務,客戶端上的 Stunnel 就會因超時而失敗,並且瀏覽器會一直等待響應。
由於 Tinyproxy 1.8.3(ubuntu 16.04 的最新版本)不支援將傳出連線綁定到特定介面的選項,我必須讓 OpenVPN 透過其tun0
介面新增預設路由。
OpenVPN 用戶端按預期工作 - 來自容器的所有資料包都通過 VPN。容器所在的主機是具有公用 IP 的遠端主機。 DNAT 已設定到容器。
我不太熟悉路由內部結構,我只能設定 SNAT/DNAT 並使用 iptables 進行過濾。因此,我無法理解問題的根源。
以下是最重要的環境參數:
如果配置
$ ifconfig -a
eth0 Link encap:Ethernet HWaddr 00:16:3e:5f:46:ba
inet addr:10.227.60.197 Bcast:10.227.60.255 Mask:255.255.255.0
inet6 addr: fe80::216:3eff:fe5f:46ba/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:16291 errors:0 dropped:0 overruns:0 frame:0
TX packets:15632 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:5044056 (5.0 MB) TX bytes:4171187 (4.1 MB)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:2446 errors:0 dropped:0 overruns:0 frame:0
TX packets:2446 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1
RX bytes:2483699 (2.4 MB) TX bytes:2483699 (2.4 MB)
tun0 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
inet addr:10.8.0.3 P-t-P:10.8.0.3 Mask:255.255.255.0
UP POINTOPOINT RUNNING NOARP MULTICAST MTU:1500 Metric:1
RX packets:3 errors:0 dropped:0 overruns:0 frame:0
TX packets:3 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:500
RX bytes:252 (252.0 B) TX bytes:252 (252.0 B)
路線
$ route -v -e
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
default 10.8.0.1 128.0.0.0 UG 0 0 0 tun0
default 10.227.60.1 0.0.0.0 UG 0 0 0 eth0
10.8.0.0 * 255.255.255.0 U 0 0 0 tun0
10.227.60.0 * 255.255.255.0 U 0 0 0 eth0
128.0.0.0 10.8.0.1 128.0.0.0 UG 0 0 0 tun0
<vpn server IP> 10.227.60.1 255.255.255.255 UGH 0 0 0 eth0
stunnel.con
...
accept = 10.227.60.197:8081
connect = 127.0.0.1:8080
...
tinyproxy.conf
...
Port 8080
Listen 127.0.0.1
...
VPN客戶端設定檔
dev tun
proto udp
remote <vpn server ip> 1195
resolv-retry infinite
nobind
persist-key
persist-tun
remote-cert-tls server
auth SHA512
cipher AES-256-CBC
key-direction 1
verb 3
#route-nopull
...
iptables是空的。
答案1
問題出在路由表配置。
我注意到在刪除 OpenVPN 新增的路由時:
Destination Gateway Genmask Flags MSS Window irtt Iface
default 10.8.0.1 128.0.0.0 UG 0 0 0 tun0
並嘗試執行ping 8.8.8.8 -I tun0
並同時監視資料包tcpdump -nn icmp
,回复 icmp 請求實際上已命中,eth0
但不再繼續。經過一番調查後,我發現tun0
由於伺服器有 2 個接口,因此也應該有一個單獨的路由表和規則。
最終,我將tinyproxy更新到最新版本,以便能夠指定出站接口,並禁用OpenVPN推送預設路由,就像我上面刪除的那樣。
然後,我將表添加到/etc/iproute2/rt_tables
:
...
12 vpn
新增路由到此表和規則:
ip route add 10.8.0.0/24 dev tun0 src 10.8.0.3 table vpn
ip route add default via 10.8.0.1 dev tun0 table vpn
ip rule add from 10.8.0.3/32 table vpn
ip rule add to 10.8.0.3/32 table vpn
之後一切都如預期進行。