认识
Socket Secure(SOCKS)是一个 Internet 协议,通过代理服务器在客户端和服务器之间交换网络数据包。SOCKS5 额外提供身份验证,因此只有授权用户才能访问服务器。实际上,SOCKS 服务器代理可以连接到任意 IP 地址的 TCP 连接,并提供 UDP 数据包转发的方式。
SOCKS 在 OSI 模型的第 5 层(会话层,表示层和传输层之间的中间层)执行。SOCKS 服务器接受 TCP 端口 1080 上的传入客户端连接。
组成
原理简述
要讲 SOCKS 的原理就不得不提一提传统的网络交互模式和 GFW。
以前我们是这么访问网络的。。。
这个时候我可能还没有出生,我们访问各种网站都是简单而直接的,用户的请求通过路由(Router)发送到服务提供方,服务返回数据。
后来出现一个东西,它叫 GFW。。。
GFW 可以理解为 Router 的升级。它有很多的功能,比如数据包内容检查。每当用户需要获取信息,都经过了 GFW 的审查,GFW 将它不喜欢的内容统统过滤掉。于是客户的数据包内容触发 GFW 的过滤规则的时候,就会收到 Connection Reset 这样的响应内容,而无法接收到正常的内容。
再后来出现了一个东西。。。
后来出现了一个协议,它叫做 SOCKS 协议。由于发送给 GFW 的数据加密了,所以 GFW 也发现不了数据包的内容,也就没有办法触发 GFW 的过滤规则。
后来 GFW 发现 SSH 的发送的数据包的内容虽然是加密的,但是 SSH 发送的数据包有某些特征,然后 GFW 就对其进行了干扰,导致 SSH 很不稳定。
再后来,Shadowsocks 出现了,SOCKS 升级了
于是 clowwindy 同学分享并开源了他的解决方案。简单理解的话,shadowsocks 是将原来 ssh 创建的 Socks5 协议拆开成 server 端和 client 端,所以 Shadowsocks 基本上与 ssh tunnel 的原理大致类似。ss-local 和 ss-server 两端通过多种可选的加密方法进行通讯,经过 GFW 的数据包是加密的,因此 GFW 也无法对通讯数据进行解密,而且最重要的是数据包没有明显的特征码,对于 GFW 来说是常规的 TCP 包,没有办法审查内容。
后来,作者喝茶了,因为技术上很难封杀 Shadowsocks。
协议交互
The TCP address that a client wants to proxy
google.com:443
Let’s assign roles. Here, a client may be a Chrome browser, a curl command, or a game client. The Socks 5 Server is the server.
The interaction between the client and the server is as follows. For the convenience of understanding, the original protocol is abstracted into human-readable content.
Step 1: Client -> Server (TCP), ask the protocol version and authentication method
Ask for Version and Auth method
Step 2: Server -> Client (TCP), response protocol version and authentication method
Response Version and Auth method
Step 3: Client -> Server (TCP), if the server needs authentication, send the authentication information
Auth Info
Step 4: Server -> Client (TCP), if the server needs authentication, respond to the authentication result
Auth Success
Step 5: Client -> Server (TCP), send the address to be proxied
TCP google.com:443
Step 6: Server -> Client (TCP), response result
Response OK
Then the tunnel is established, and the client and server can transmit content to each other through this tunnel. We noticed that the whole process between the client and the server is the TCP protocol.
改进
如何检测 Socks 协议:
Socks5 代理协议详解-云社区-华为云
Testing the Status of a SOCKS5 Proxy in Python
参考
Wikipedia/SOCKS
SOCKS Protocol Version 5
道高一尺,牆高一丈:互聯網封鎖是如何升級的
Shadowsocks 原理简介及安装指南
ShadowSocks 的翻墙原理(两篇)
ssh 建隧道简单介绍:科学上网、本地服务穿透到外网
shadowsocks 实现原理
Shadowsocks 原理和搭建(学习)
Shadowsocks 原理详解
Understand the weird Socks 5 protocol and HTTP proxy