如何确保我的所有 Mac 的 TCP 流量都通过 SOCKS5 代理?

如何确保我的所有 Mac 的 TCP 流量都通过 SOCKS5 代理?

我有一个带端口的 SOCKS 代理。

我怎样才能让我的 MAC 使用它?我在网络设置下要更改哪些设置?

答案1

为什么系统偏好设置->网络->(选择窗口左侧的网络,然后在右下角选择高级)->代理(顶部的选项卡)不起作用?

在此处输入图片描述

答案2

如果你可以自己设置 SSH 服务器,那么免费的穿梭巴士可以通过连接传输所有 TCP 流量,为您完成所有防火墙工作。

要将所有 TCP 流量和 DNS 请求转发到远程 SSH 服务器,命令很简单:

sshuttle --dns -vr ssh_server 0/0

除了 TCP 和 DNS,sshuttle 不会转发其他请求,例如 UDP、ICMP、ping 等。

有关更多信息和示例,请参阅文章在日常工作中使用 Sshuttle

答案3

虽然设置系统范围的代理设置是一个好的开始,但您可能还想研究使用 iptables 来确保所有流量都通过代理。有些应用程序不使用系统范围的配置设置(Firefox 就是其中之一),因此您必须定制规则以不允许直接连接,而只通过代理路由流量。

编辑:虽然我个人使用iptables规则来管理 VPN 的潜在“泄漏”,但我最初误以为 iptables 可以直接与 socks 代理配合使用。你需要类似的东西tun2socks以便制作虚拟隧道接口(例如vpn的使用)。

接下来,您可以设置类似于以下内容的 iptables 脚本:

#!/bin/bash
if [[ $EUID -ne 0 ]]; then
    echo "This script must be run as root" 1>&2
    exit 1
fi

# name of primary network interface (before tunnel)
PRIMARY=eth0

# gateway ip address (before tunnel - adsl router ip address)
# automatically determine the ip from the default route
GATEWAY=`route -n | grep $PRIMARY | egrep "^0\.0\.0\.0" | tr -s " " | cut -d" " -f2`

# provided by tun2socks: interface name
TUNNEL=tun0

# If you'd like, putting the tun2socks command here is a good idea.  It may or may not be necessary to do so, but either way is more convenient than running the two commands separately.

# iptables rules - important!

LOCAL_NET=192.168.0.0/16
#LOCAL_NET=$GATEWAY

# Flush all previous filter rules, you might not want to include this line if you already have other rules setup
iptables -t filter --flush

iptables -t filter -X MYVPN
iptables -t filter -N MYVPN

# Add local routes to routing table
route add -net 192.168.1.0 netmask 255.255.255.0 dev eth0
route add -host 23.21.163.237 dev eth0 gw 192.168.1.1

# Add ssh routes to routing table
ip route add table 128 to 192.168.1.0/24 dev eth0
ip route add table 128 default via 192.168.1.1

# Exceptions for local traffic & vpn server
iptables -t filter -A MYVPN -o lo -j RETURN
iptables -t filter -A MYVPN -o ${TUNNEL} -j RETURN
iptables -t filter -A MYVPN --dst 127.0.0.1 -j RETURN
iptables -t filter -A MYVPN --dst $LOCAL_NET -j RETURN
iptables -t filter -A MYVPN --dst ${SERVER} -j RETURN
iptables -t filter -A MYVPN --dst ${VPN_SERVER} -j RETURN

# Add extra local nets here as necessary

iptables -t filter -A MYVPN -j DROP

# MYVPN traffic leaving this host:
iptables -t filter -A OUTPUT -p tcp --syn -j MYVPN
iptables -t filter -A OUTPUT -p icmp -j MYVPN
iptables -t filter -A OUTPUT -p udp -j MYVPN

当然,你会希望让这个脚本反映你的特定网络(即,如果你使用 192.168.0.0/24 子网,请进行相应调整)。此外,它非常紧密地基于我与 VPN 一起使用的脚本,因此,所有提及 MYVPN 或 VPN 的地方——虽然你没有使用 VPN,但tun2socks实际上表现得就像你正在使用 VPN 一样,所以一切都应该以相同的方式工作。

特别感谢这个答案在 Unix.SE 上为我指明了正确的方向来回答这个问题。

再次编辑:所以,看起来 OS X 实际上会使用ipfw而不是 iptables 来执行此操作(抱歉,我主要是 Linux 用户,并认为 OS X 有可用的 iptables)。有一些等效性,因此可以调整脚本,其中一些已被指出这里. man ipfw应该能让你了解语法。我会保留原始iptables脚本作为模板,这样你就可以从概念上了解发生了什么。 水屋面看起来它可能有助于使使用ipfw更加用户友好;其他前端也可能可用。

答案4

我发现的唯一免费解决方案是 tun2socks,它要求您设置自己的路线,我遵循了官方文档https://github.com/xjasonyu/tun2socks/wiki/Examples

但是必须从中排除诸如 DNS 之类的条目 IP,我通过在默认网关上添加想要排除的 IP 来实现这一点。

它在 MacOS 上运行得很好。

相关内容