出于某些原因,我想编写一个代理服务器,通过其 wifi 接口而不是以太网(这是操作系统级别的默认路由)路由其请求。我知道我可以通过摆弄操作系统路由来做到这一点,但我更喜欢直接在 curl 中做到这一点。
我读到过,您实际上可以使用 --interface 选项选择套接字将使用的网络接口,但它对我来说似乎不起作用:(
我尝试了以下操作:
curl --interface wlp2s0 google.com
但我只得到 ETIMEDOUT 错误。另一方面,如果我这样做
curl --interface enp0s20 google.com
它按预期工作。
有人知道发生了什么事吗?
答案1
正如正确指出的那样马特·克拉克,超时表示无论出于何种原因,您都无法到达指定站点。
但我还要指出的是,你误解了- 界面选项。它的意思是:如果我有两个或多个接口,则使用我指定的接口的 IP 地址,而不是默认的 IP 地址”。但对于路由,IE,决定请求通过哪个接口路由,它是内核它根据你拥有的路由表来决定,而不是你的命令:如果路由表指定了默认网关eth0例如,卷曲请求将通过它路由,即使您已指定--接口 wlan0。
让我们检查一下:我设置了两个tcpdump会话,一个在我的以太网设备上(usb0),一个在 wifi 设备上(wlan0);两者都已连接,但路由表已将网关置于usb0。现在,在第三个 shell 中,我发送两个卷曲请求如下:
$ date && curl --interface usb0 146.255.36.1/plain
mar 22 nov 2016, 18.44.21, CET
<!-- pageok -->
<!-- managed by puppet -->
<html>
<pre>pageok</pre>
</html>
$ date && curl --interface wlan0 146.255.36.1/plain
mar 22 nov 2016, 18.44.44, CET
<!-- pageok -->
<!-- managed by puppet -->
<html>
<pre>pageok</pre>
</html>
这是我的两个tcpdump会话记录:wifi 上的会话
# tcpdump -i wlan0 -n host 146.255.36.1
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on wlan0, link-type EN10MB (Ethernet), capture size 262144 bytes
以及以太网上的
# tcpdump -i usb0 -n host 146.255.36.1
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on usb0, link-type EN10MB (Ethernet), capture size 262144 bytes
18:44:21.118505 IP 192.168.73.74.52601 > 146.255.36.1.80: Flags [S], seq 2178801514, win 29200, options [mss 1460,sackOK,TS val 10527374 ecr 0,nop,wscale 7], length 0
18:44:21.147291 IP 146.255.36.1.80 > 192.168.73.74.52601: Flags [S.], seq 95036242, ack 2178801515, win 14480, options [mss 1460,nop,nop,TS val 500605800 ecr 10527374,nop,wscale 8], length 0
18:44:21.147322 IP 192.168.73.74.52601 > 146.255.36.1.80: Flags [.], ack 1, win 229, options [nop,nop,TS val 10527381 ecr 500605800], length 0
18:44:21.147375 IP 192.168.73.74.52601 > 146.255.36.1.80: Flags [P.], seq 1:82, ack 1, win 229, options [nop,nop,TS val 10527381 ecr 500605800], length 81: HTTP: GET /plain HTTP/1.1
18:44:21.175829 IP 146.255.36.1.80 > 192.168.73.74.52601: Flags [.], ack 82, win 57, options [nop,nop,TS val 500605829 ecr 10527381], length 0
18:44:21.178222 IP 146.255.36.1.80 > 192.168.73.74.52601: Flags [P.], seq 1:285, ack 82, win 57, options [nop,nop,TS val 500605832 ecr 10527381], length 284: HTTP: HTTP/1.1 200 OK
18:44:21.178236 IP 192.168.73.74.52601 > 146.255.36.1.80: Flags [.], ack 285, win 237, options [nop,nop,TS val 10527389 ecr 500605832], length 0
18:44:21.178321 IP 192.168.73.74.52601 > 146.255.36.1.80: Flags [F.], seq 82, ack 285, win 237, options [nop,nop,TS val 10527389 ecr 500605832], length 0
18:44:21.206618 IP 146.255.36.1.80 > 192.168.73.74.52601: Flags [F.], seq 285, ack 83, win 57, options [nop,nop,TS val 500605860 ecr 10527389], length 0
18:44:21.206642 IP 192.168.73.74.52601 > 146.255.36.1.80: Flags [.], ack 286, win 237, options [nop,nop,TS val 10527396 ecr 500605860], length 0
18:44:44.284057 IP 192.168.73.45.41804 > 146.255.36.1.80: Flags [S], seq 2817430695, win 29200, options [mss 1460,sackOK,TS val 10533165 ecr 0,nop,wscale 7], length 0
18:44:44.313090 IP 146.255.36.1.80 > 192.168.73.45.41804: Flags [S.], seq 3652186052, ack 2817430696, win 14480, options [mss 1460,nop,nop,TS val 500628966 ecr 10533165,nop,wscale 8], length 0
18:44:44.313117 IP 192.168.73.45.41804 > 146.255.36.1.80: Flags [.], ack 1, win 229, options [nop,nop,TS val 10533172 ecr 500628966], length 0
18:44:44.313152 IP 192.168.73.45.41804 > 146.255.36.1.80: Flags [P.], seq 1:82, ack 1, win 229, options [nop,nop,TS val 10533172 ecr 500628966], length 81: HTTP: GET /plain HTTP/1.1
18:44:44.342243 IP 146.255.36.1.80 > 192.168.73.45.41804: Flags [.], ack 82, win 57, options [nop,nop,TS val 500628995 ecr 10533172], length 0
18:44:44.343076 IP 146.255.36.1.80 > 192.168.73.45.41804: Flags [P.], seq 1:285, ack 82, win 57, options [nop,nop,TS val 500628996 ecr 10533172], length 284: HTTP: HTTP/1.1 200 OK
18:44:44.343090 IP 192.168.73.45.41804 > 146.255.36.1.80: Flags [.], ack 285, win 237, options [nop,nop,TS val 10533180 ecr 500628996], length 0
18:44:44.343168 IP 192.168.73.45.41804 > 146.255.36.1.80: Flags [F.], seq 82, ack 285, win 237, options [nop,nop,TS val 10533180 ecr 500628996], length 0
18:44:44.372262 IP 146.255.36.1.80 > 192.168.73.45.41804: Flags [F.], seq 285, ack 83, win 57, options [nop,nop,TS val 500629025 ecr 10533180], length 0
18:44:44.372302 IP 192.168.73.45.41804 > 146.255.36.1.80: Flags [.], ack 286, win 237, options [nop,nop,TS val 10533187 ecr 500629025], length 0
^C
20 packets captured
20 packets received by filter
0 packets dropped by kernel
注意:
每个请求对应的时间戳,
不同的 LAN Ip 地址,以太网为 192.168.73.74,wifi 为 192.168.73.45。
那全部数据包被检测到tcpdump以太网接口上的会话,无论我选择什么- 界面。
你可能会问:那么什么时候- 界面选项有用吗?当您有两个(或更多)路由表时,每个接口一个,每个路由表都有自己的网关。 然后- 界面允许您指定使用哪个接口来访问卷曲的目的地。如果你想了解更多关于如何设置策略路由,你可以读到这里。