如何在命令行里设代理

在mac中,如何在terminal中可以做到访问google

前提是已经使用proxy可以在浏览器中访问google

首先查看当前的位置信息

1
2
3
4
5
6
7
8
9
10
➜  ~ curl cip.cc
IP : 211.97.3.171
地址 : 中国 广东 广州
运营商 : 联通

数据二 : 广东省广州市 | 联通

数据三 : 中国广东省广州市 | 联通

URL : http://www.cip.cc/211.97.3.171

在配置文件中添加两行proxy的配置,

vim ~/.zshrc

vim ~/.bash_profile

1
2
export http_proxy=socks5://127.0.0.1:1080 # 配置http 代理访问
export https_proxy=socks5://127.0.0.1:1080 # 配置https 代理访问

或者

1
2
export http_proxy=http://127.0.0.1:1087
export https_proxy=http://127.0.0.1:1087

检验是否配置成功

1
2
3
➜  ~ env | grep -i proxy
http_proxy=socks5://127.0.0.1:1080
https_proxy=socks5://127.0.0.1:1080

临时取消配置

1
2
3
➜  ~ unset http_proxy
➜ ~ unset https_proxy
➜ ~ env | grep -i proxy

查看修改后的位置信息

1
2
3
4
5
6
7
8
9
10
11
12
➜  ~ curl cip.cc
IP : 45.58.149.2
地址 : 荷兰 北荷兰省 阿姆斯特丹
运营商 : sharktech.net

数据二 : 荷兰 | 北荷兰省阿姆斯特丹Sharktech数据中心

数据三 : 荷兰阿姆斯特丹阿姆斯特丹

URL : http://www.cip.cc/45.58.149.2
➜ ~ curl www.google.com
// 成功,但信息太多就不放进来了

终端可以访问Google,但终端无法ping的通Google

1
2
3
4
5
6
7
8
9
➜  ~ ping www.google.com
PING www.google.com (172.217.160.68): 56 data bytes
Request timeout for icmp_seq 0
Request timeout for icmp_seq 1
Request timeout for icmp_seq 2
Request timeout for icmp_seq 3
^C
--- www.google.com ping statistics ---
5 packets transmitted, 0 packets received, 100.0% packet loss

原因是上层模型依赖下层模型

  1. ping命令使用的ICMP协议,作用在OSI模型第3层——网络层(IP层)

    使用wireshark抓包查看ping命令发送给目标地址的数据包

  2. 而SOCKS作用在OSI模型的第5层——会话层

  3. HTTP、FTP、SMTP作用在OSI模型第7层——网络层

    本机与代理服务器之间发送的tcp数据包

ss支持的代理协议是socks(5 会话层)和http(7 应用层),最终都依赖于TCP(4 传输层)和IP协议(3 网络层),发送tcp数据包,但并不能代理第3层——网络层

OSI七层网络协议

不过在这里可以换一个工具httping

安装

1
brew install httping

使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
➜  ~  httping -x 127.0.0.1:1087 -g google.com
PING google.com:80 (/):
connected to google.com:80 (390 bytes), seq=0 time=2176.16 ms
connected to google.com:80 (390 bytes), seq=1 time=1033.12 ms
connected to google.com:80 (390 bytes), seq=2 time=1011.46 ms
connected to google.com:80 (390 bytes), seq=3 time=1029.67 ms
connected to google.com:80 (390 bytes), seq=4 time=1014.58 ms
connected to google.com:80 (390 bytes), seq=5 time=874.47 ms
connected to google.com:80 (390 bytes), seq=6 time=1093.33 ms
connected to google.com:80 (390 bytes), seq=7 time=1294.69 ms
connected to google.com:80 (390 bytes), seq=8 time=1260.06 ms
connected to google.com:80 (390 bytes), seq=9 time=1585.57 ms
connected to google.com:80 (390 bytes), seq=10 time=913.17 ms
connected to google.com:80 (390 bytes), seq=11 time=1050.31 ms
connected to google.com:80 (390 bytes), seq=12 time=1098.28 ms
connected to google.com:80 (390 bytes), seq=13 time=1057.10 ms
connected to google.com:80 (390 bytes), seq=14 time=994.80 ms
connected to google.com:80 (390 bytes), seq=15 time=1484.78 ms
connected to google.com:80 (390 bytes), seq=16 time=966.29 ms
connected to google.com:80 (390 bytes), seq=17 time=1032.56 ms
connected to google.com:80 (390 bytes), seq=18 time=1031.75 ms
connected to google.com:80 (390 bytes), seq=19 time=1023.04 ms
^CGot signal 2

--- http://google.com/ ping statistics ---
21 connects, 20 ok, 4.76% failed, time 44082ms
round-trip min/avg/max = 874.5/1151.3/2176.2 ms
➜ ~

同理,Telnet协议是TCP/IP协议族中的一员,所以telnet也无法访问google

使用wireshark抓包查看telnet命令发送给目标地址的数据包

捧个钱场?