如何从socks代理创建虚拟接口?

Cla*_*man 3 ubuntu socks network-interface

我想从本地主机上的 SOCKS 5 代理创建一个网络接口。这最终将与我拥有的无线接口桥接,该接口将以 AdHoc 模式运行。这将允许该接口上的所有客户端使用袜子代理,即使它们不支持 SOCKS。当我在旅途中时,这将支持我的 iDevices、PSP 等。它将从我的笔记本电脑上运行。谁能提供一些有用的资源来帮助我在我的 Ubuntu 机器上设置 SOCKS 代理接口?提前致谢!

Lis*_*yen 5

您需要 tun2socks,它是 BadVPN 项目的一部分(https://github.com/ambrop72/badvpn)。这里已经有一个答案推荐 tun2socks,但缺乏示例。

您可以从源代码构建 tun2socks 或安装预编译的包。例如,在 Ubuntu 中:

sudo add-apt-repository ppa:hda-me/badvpn-tun2socks
sudo apt-get update
sudo apt-get install badvpn-tun2socks

ip tuntap add dev tun1 mode tun user nobody
ifconfig tun1 up 10.0.0.1/24

# --netif-ipaddr 10.0.0.2 is not a typo.
# It specifies the IP address of the virtual router inside the TUN device,
# and must be different than the IP of the TUN interface itself.
# This command assumes you have tor up and running on 127.0.0.1:9050.
# Of course, you can use any other SOCKS proxy instead.
badvpn-tun2socks --tundev tun1 --netif-ipaddr 10.0.0.2 --netif-netmask 255.255.255.0 --socks-server-addr 127.0.0.1:9050
Run Code Online (Sandbox Code Playgroud)

接下来,您需要决定是否要通过代理路由所有流量,还是仅将流量路由到某些特定 IP 或主机。如果您有这样一个列表(或者可以通过合理的努力创建一个列表),那么这是最有效的解决方案。

# Here list of hosts or IPs to route through proxy is just an example.
# You need to specify your own list.
list_of_IPs_to_route_via_proxy="example.com 203.0.113.5"
for IP in $(echo $list_of_IPs_to_route_via_proxy); do
  route add $IP gw 10.0.0.2; done
Run Code Online (Sandbox Code Playgroud)

如果您需要通过代理路由所有流量,则会稍微复杂一些。在这种情况下,我建议仔细阅读https://github.com/ambrop72/badvpn/wiki/Tun2socks。官方文档假设您要使用 localhost:1080 作为代理,并给出了如何创建具有 SSH 动态转发的本地 SOCKS 服务器的说明。文档最重要的部分:

连接到 SSH 服务器,将 -D localhost:1080 传递给 ssh 命令以启用动态转发。这将使 ssh 打开 badvpn-tun2socks 将使用的本地 SOCKS 服务器。如果您使用 Putty,请转至“连接”->“SSH”->“隧道”,在“源端口”中键入 1080,选择“动态”,然后单击“添加”。

剩下的就是通过 TUN 设备而不是现有的默认网关路由连接。这是按如下方式完成的:

通过现有网关添加到 SSH 服务器的路由,其度量值低于原始默认路由。如果您的 DNS 服务器位于 Internet(而不是您的本地网络),还要为它们添加路由(如 SSH 服务器)。这是必需的,因为 tun2socks 默认情况下不转发 UDP(见下文)。通过TUN设备中的虚拟路由器添加默认路由,其度量值比原来的默认路由低,但比SSH和DNS路由高。

这将使所有外部连接都通过 TUN 设备,但 SSH 连接除外(否则 SSH 将通过 TUN 设备,而 TUN 设备将通过... SSH)。

例如(假设不存在度量 <=6 的现有默认路由;否则删除它们或更改其度量),在 Linux 中:

route add <IP_of_SSH_server> gw <IP_of_original_gateway> metric 5 <same for DNS>
route add default gw 10.0.0.2 metric 6
Run Code Online (Sandbox Code Playgroud)

文档还提供了使用 tun2socks 进行 UDP 转发、如何通过 Tor 路由所有流量以及如何获得 IPv6 支持的示例。