尽管你可能配置了代理,但是,使用命令行的时候,其命令并没有走代理路线。
但是,有的时候,我们需要使用命令行来走代理路线,这个时候就要用到 proxychains。
ps: 这个最后貌似失败了,虽然明面上的代理可以用,但是,完全不会返回什么数据!
参考资料
- 命令行代理神器 proxychains
- 使用proxychains进行命令行proxy设置
- 试图不关闭 SIP 在 macOS Sierra 上使用 proxychains-ng
- macOS 下解决新安装 Dropbox 后无法设置代理的问题
环境说明
- macbook 10.14
- 代理 shadowsocks
安装
brew install proxychains-ng配置文件
proxychains 的配置文件顺序是当前目录下的 ./proxychains.conf 然后是 $HOME/.proxychains/proxychains.conf 最后是系统目录下的 /etc/proxychains.conf
但是,我们使用 brew 安装的时候,并不会在 $HOME 下创建配置文件,所以,我们可以自己创建。
1 | mkdir ~/.proxychains |
内容如下:
1 | # proxychains.conf VER 4.x |
有的教程最下面类似于是
1 | socks5 127.0.0.1 6153 |
但是,我使用上述的描写并不能进行代理。尤其是,我的并没有 http。
安装完之后的命令名为:proxychains4,而不是proxychains。
执行 whcih curl
如果出现
/bin/curl那么当你使用
proxychains4 curl www.google.com会出现
socket error or timeout!这是因为苹果的 SIP 机制
SIP 机制
中
- Mac OS X 10.11 (El Capitan) ships with a new security feature called SIP
that prevents hooking of system apps.
workarounds are to partially disable SIP by issuing
csrutil enable –without debug in recovery mode,
or to copy the system binary into the home directory and run it from there.
see github issue #78 for details.
SIP 是苹果的保护机制,根据 根据苹果的 官方说明,以下路径受到保护:
/System
/usr (不包含 /usr/local)
/bin
/sbin
Apps that are pre-installed with OS X苹果自身带有各种命令,比如 curl、wget 等,这些命令放在 bin 下,所以,这里面的路径受 SIP 的限制。
解决方案
很多教程都是关掉 SIP ,但是,这样做有两个缺点
- 需要重启,我觉得麻烦
- 不安全
其实,解决思路非常简单,只要讲命令不放在保护路径之下就可以了。所以,有两个解决方案。
通过 brew 重新安装
我们可以通过 brew 重新安装上面的命令,比如
brew install curl由于,brew 安装的路径可以在
复制到其他路径
我使用的是这个。把那些二进制复制到不受保护的路径。
如果你使用
which curl会出现
/bin/curl首先在我的 ~/.bash_profile 中添加
export PATH=/Users/$(whoami)/usr/bin:$PATH比如我的用户名是 licong ,所以,我可以在 /User/licong 下创建 /usr/bin 目录。
把 curl 复制到我的 bin 路径:
cp $(which curl) ~/usr/bin/curl这个时候,你再使用 which curl 就会发现
/Users/licong/usr/bin所以,你再使用
proxychains4 curl www.google.com就可以了,我本地出现
[proxychains] config file found: /Users/licong/.proxychains/proxychains.conf
[proxychains] preloading /usr/local/Cellar/proxychains-ng/4.14/lib/libproxychains4.dylib
[proxychains] DLL init: proxychains-ng 4.14
[proxychains] Dynamic chain ... 0.0.0.0:1086 ... 31.13.95.33:80 ... OK
curl: (52) Empty reply from server上面访问 google 什么的有问题,但是,访问 baidu 是好的,可能是 google 加了什么限制,以后要是想起来再解决吧。