protocol

package
v0.1.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 20, 2021 License: MIT Imports: 16 Imported by: 0

README

socks5

socks5 protocol

Auth

认证流程

client ---> server

客户端发送请求确认版本和请求方式,单位字节

+-------------------------+
| VER |	NMETHODS | METHODS|
+-------------------------+
| 1	  |    1     | 1-255  |
+-------------------------+
  • VER: socks 的版本,这里应该是 0x05
  • NMETHODS: METHODS 部分的长度,取值范围 1~255
  • METHODS: 客户端支持的认证方式列表,每个方法占1字节,支持的认证方式有
    • 0x00: 不需要认证
    • 0x01: GSSAPI
    • 0x02: 用户名,密码认证
    • 0x03-0x7F: 保留,有 IANA 分配
    • 0x80-0xFE: 自定义,私人方法
    • 0xFF: 无可接受的的方法
server ---> client

服务器端返回支持的方法,单位字节

+--------------+
| VER |	METHOD |
+--------------+
| 1	  |    1   |
+--------------+
  • VER: socks 的版本,这里应该是 0x05
  • METHOD: 服务端选中的方法。如果返回 0xFF 表示没有选中任何方法,客户端需要关闭连接。

Connect

认证结束之后,客户端发送请求信息

client ---> server
+---------------------------------------------------+
| VER |	CMD  | RSV  | ATYP  |  DST.ADDR |  DST.PORT |
+---------------------------------------------------+
| 1	  |   1  |  1   |  1    | (dynamic) |    2      |
+---------------------------------------------------+
  • VER: socks 的版本,这里应该是 0x05
  • CMD: sock 的命令码
    • 0x01: 表示 CONNECT 请求
    • 0x02: 表示 BIND 请求
    • 0x03: 表示 UDP 转发
  • RSV: 保留,固定 0x00
  • ATYP: DST.ADDR 的地址类型
    • 0x01: IPv4 地址,DST.ADDR 部分4字节长度
    • 0x03: DST.ADDR 部分第一个字节为域名长度,DST.ADDR 剩余部分内容为域名,没有\0的结束符
    • 0x04: IPv6 地址,DST.ADDR 部分16字节长度
  • DST.ADDR 目的地址
  • DST.PORT 目的端口
server ---> client
+---------------------------------------------------+
| VER |	REP  | RSV  | ATYP  |  DST.ADDR |  DST.PORT |
+---------------------------------------------------+
| 1	  |   1  |  1   |  1    | (dynamic) |    2      |
+---------------------------------------------------+
  • VER: socks 的版本,这里应该是 0x05
  • REP: 应答字段,表示请求状态
    • 0x00: 表示成功
    • 0x01: 表示 SOCKS 服务器连接失败
    • 0x02: 表示现有规则不允许连接
    • 0x03: 表示网络不可达
    • 0x04: 表示主机不可达
    • 0x05: 表示连接被拒
    • 0x06: 表示TTL超时
    • 0x07: 表示不支持的命令
    • 0x08: 表示不支持的地址类型
    • 0x09-0xFF: 未定义
  • RSV: 保留,固定 0x00
  • ATYP: DST.ADDR 的地址类型
    • 0x01: IPv4 地址,DST.ADDR 部分4字节长度
    • 0x03: DST.ADDR 部分第一个字节为域名长度,DST.ADDR 剩余部分内容为域名,没有\0的结束符
    • 0x04: IPv6 地址,DST.ADDR 部分16字节长度
  • DST.ADDR 服务器绑定的地址
  • DST.PORT 服务器绑定的端口

Forward

端口转发

UserAuth

使用用户名密码的方式进行用户认证

client ---> server
+--------------------------------------------------------------------+
| VER |	username length |  username  | password length |  password   |
+--------------------------------------------------------------------+
| 1	  |        1        | (dynamic)  |        1        |  (dynamic)  |
+--------------------------------------------------------------------+
  • VER: 鉴定协议版本,目前为 0x01
  • username length: 用户名长度
  • username: 用户名
  • password length: 密码长度
  • password: 密码
server ---> client
+--------------+
| VER |	STATUS |
+--------------+
| 1	  |    1   |
+--------------+
  • VER: 鉴定协议版本,目前为 0x01
  • STATUS: 鉴定状态
    • 0x00: 表示成功
    • 0x01: 表示失败

TODO

  1. kcp with encrypt
    1. 同时加密的情况下,key 或者 salt 不匹配的话,server 都是拿不到结果的哟,就是说 listener.Accept 无法解密拿到连接
    2. 就算是 server 端不加密,client 使用加密算法,或者直接使用 nc 或其他方式创建 udp 连接发送数据也是接收不到的,因为 kcp 是特有的协议,有自己的协议头解析规则
    3. 所以,首先是需要加密或者不加密,以及加密方式保持一致,然后密钥和盐保持一致,才能正确的互通互联。
  2. kcp with encrypt and socks5
    1. 直接替换接口,模块划分的很好
    2. 如何处理低延迟高丢包的网络情况
  3. http with socket proxy
    1. HTTP proxy implement
  4. study shadowsocks
    1. DNS

socks4

socks4 protocol

Connect

客户端直接发送请求信息,包括目的地址信息

client ---> server
+----+----+----+----+----+----+----+----+----+----+....+----+
| VN | CD | DSTPORT |      DSTIP        | USERID       |NULL|
+----+----+----+----+----+----+----+----+----+----+....+----+
| 1  | 1  |    2    |         4         | variable     | 1  |
+----+----+---------+-------------------+---------+....+----+
  • VN: socks 的版本(SOCKS protocol version number),这里应该是 0x04
  • CD: socks 操作命令(SOCKS command code)
    • 0x01: 表示 CONNECT 请求
    • 0x02: 表示 BIND 请求
  • DSTPORT: 目的端口
  • DSTIP: 目的地址
  • USERID: 用户id
  • NULL: 终止符,这里应该是 0x00
server --> client
+----+----+----+----+----+----+----+----+
| VN | CD | DSTPORT |      DSTIP        |
+----+----+----+----+----+----+----+----+
| 1  | 1  |    2    |        4          |
+----+----+---------+-------------------+
  • VER: socks 的版本,这里应该是 0x04
  • REP: 应答字段,表示请求状态
    • 0x5a: (90)表示请求得到允许;
    • 0x5b: (91)表示请求被拒绝或失败;
    • 0x5c: (92)表示由于SOCKS服务器无法连接到客户端的identd(一个验证身份的进程),请求被拒绝;
    • 0x5d: (93)表示客户端程序与identd报告的用户身份不同,连接被拒绝。
  • DSTPORT: 服务器绑定的地址,但被忽略。
  • DSTIP: 服务器绑定的端口,但被忽略。

SOCKS4a

其实是和 socks4 基本一样的,只是多了请求域名的简单扩展。

+----+----+----+----+----+----+----+----+----+----+....+----+----------+----+
| VN | CD | DSTPORT |      DSTIP        | USERID       |NULL| HOSTNAME |NULL|
+----+----+----+----+----+----+----+----+----+----+....+----+----------+----+
| 1  | 1  |    2    |         4         | variable     | 1  | variable | 1  |
+----+----+---------+-------------------+---------+....+----+----------+----+

参考链接

实战:150行Go实现高性能socks5代理 实战:150行Go实现高性能加密隧道 实战:+65行Go实现低延迟隧道

Documentation

Index

Constants

This section is empty.

Variables

View Source
var GlobalConfig = KCPConfig{}
View Source
var SecretKey string
View Source
var SecretKeyByte [32]byte
View Source
var UserAuthInfo = Socks5UserAuthInfo{
	Username: []byte("YOUR_PROXY_LOGIN"),
	Password: []byte("YOUR_PROXY_PASSWORD"),
}

Functions

func HandleEcho

func HandleEcho(client net.Conn)

func InitSecretKey

func InitSecretKey()

func KCPClient

func KCPClient()

func KCPDemo

func KCPDemo()

func KCPEncryptedLocalServe

func KCPEncryptedLocalServe()

func KCPEncryptedRemoteServe

func KCPEncryptedRemoteServe()

func KCPEncryptedServe

func KCPEncryptedServe()

func KCPLocalCompatSocks5Serve

func KCPLocalCompatSocks5Serve()

func KCPLocalServe

func KCPLocalServe()

func KCPRemoteServe

func KCPRemoteServe()

func KCPServe

func KCPServe()

func KCPServer

func KCPServer()

func PureRelayKCPToTCP added in v0.1.4

func PureRelayKCPToTCP(client net.Conn)

func PureRelayTCPToKCP added in v0.1.4

func PureRelayTCPToKCP(client net.Conn)

func RelayKCPToTCP

func RelayKCPToTCP(client net.Conn)

func RelayProcess

func RelayProcess(client net.Conn, remoteAddr string, role string)

func RelayServe

func RelayServe()

func RelayTCPToEncryptedKCP

func RelayTCPToEncryptedKCP(client net.Conn)

func RelayTCPToKCP

func RelayTCPToKCP(client net.Conn)

func ReverseEncryptProxyProcess

func ReverseEncryptProxyProcess(client net.Conn)

func ReverseProxyProcess

func ReverseProxyProcess(client net.Conn)

func Serve

func Serve()

func Socks4Connect

func Socks4Connect(client net.Conn) (dstConn net.Conn, err error)

func Socks4Process

func Socks4Process(client net.Conn)

func Socks5Auth

func Socks5Auth(client net.Conn) (err error)

func Socks5Connect

func Socks5Connect(client net.Conn) (dstConn net.Conn, err error)

func Socks5DecryptProcess

func Socks5DecryptProcess(client net.Conn)

func Socks5Forward

func Socks5Forward(client, target net.Conn)

func Socks5Process

func Socks5Process(client net.Conn)

func Socks5UserAuth

func Socks5UserAuth(client net.Conn) (err error)

func StreamForward

func StreamForward(client, target CipherStream)

Types

type Chacha20Stream

type Chacha20Stream struct {
	// contains filtered or unexported fields
}

func NewChacha20Stream

func NewChacha20Stream(key []byte, conn net.Conn) (*Chacha20Stream, error)

func (*Chacha20Stream) Close

func (s *Chacha20Stream) Close() error

func (*Chacha20Stream) Read

func (s *Chacha20Stream) Read(p []byte) (int, error)

func (*Chacha20Stream) Write

func (s *Chacha20Stream) Write(p []byte) (int, error)

type CipherStream

type CipherStream interface {
	Close() error
	Read(p []byte) (n int, err error)
	Write(p []byte) (n int, err error)
}

type KCPConfig

type KCPConfig struct {
	ListenAddr string
	RemoteAddr string
	SecretKey  []byte
	SecretSalt []byte
}

type Socks5UserAuthInfo

type Socks5UserAuthInfo struct {
	Username []byte
	Password []byte
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL