socks5

package
v1.2.5 Latest Latest
Warning

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

Go to latest
Published: Dec 28, 2022 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package socks5 provies socks5 proxy for proxy.Client and proxy.Server.

Supports USER/PASSWORD authentication.

Reference

English: https://www.ietf.org/rfc/rfc1928.txt

中文: https://aber.sh/articles/Socks5/

参考 https://studygolang.com/articles/31404

USER/PASSWORD authentication rfc:

https://datatracker.ietf.org/doc/html/rfc1929

注意,socks5可能同时使用tcp和udp,但是一定会使用到tcp,socks5的network只能设置为tcp或者dual

Off Topic

纵观各种代理协议,vless/vmess/trojan/shadowsocks协议 都借鉴了socks5,有不少类似的地方。 所以 制作代理, 有必要先学习socks5标准。

关于socks4, 它太简单了, 既不支持udp, 也不支持ipv6, 也没有验证功能, 所以本作不予支持

Index

Constants

View Source
const (
	AuthNone     = 0x00
	AuthPassword = 0x02

	AuthNoACCEPTABLE = 0xff
)

SOCKS auth type

View Source
const (
	CmdConnect      = 0x01
	CmdBind         = 0x02
	CmdUDPAssociate = 0x03
)

SOCKS request commands as defined in RFC 1928 section 4

View Source
const (
	ATypIP4    = 0x1
	ATypDomain = 0x3
	ATypIP6    = 0x4
)

SOCKS address types as defined in RFC 1928 section 4

Note: vmess/vless用的是123,而这里用的是134,所以是不一样的。
View Source
const Name = "socks5"
View Source
const Version5 = 0x05

socks5 version number.

Variables

This section is empty.

Functions

func Client_EstablishUDPAssociate

func Client_EstablishUDPAssociate(conn net.Conn) (port int, err error)

传入 conn必须非nil,否则panic

func Client_ReadUDPResponse

func Client_ReadUDPResponse(udpConn *net.UDPConn, supposedServerAddr *net.UDPAddr) (target netLayer.Addr, data []byte, e error)

从 一个 socks5服务器的udp端口 读取一次 udp回应。 传入 conn必须非nil,否则panic

func Client_RequestUDP

func Client_RequestUDP(udpConn *net.UDPConn, target *netLayer.Addr, data []byte) error

RequestUDP 向一个 socks5服务器监听的 udp端口发送一次udp请求 在udp associate结束后,就已经知道了服务器给我们专用的port了,向这个端口发送一个udp请求.

另外的备忘是, 服务器返回的数据使用了相同的结构。

传入 conn必须非nil,否则panic

Types

type Client

type Client struct {
	proxy.Base
	utils.UserPass
}

func (*Client) EstablishUDPChannel

func (c *Client) EstablishUDPChannel(underlay net.Conn, firstPayload []byte, target netLayer.Addr) (netLayer.MsgConn, error)

func (*Client) GetCreator added in v1.2.4

func (*Client) GetCreator() proxy.ClientCreator

func (*Client) Handshake

func (c *Client) Handshake(underlay net.Conn, firstPayload []byte, target netLayer.Addr) (result io.ReadWriteCloser, err error)

func (*Client) Name

func (*Client) Name() string

type ClientCreator added in v1.2.1

type ClientCreator struct{ proxy.CreatorCommonStruct }

func (ClientCreator) MultiTransportLayer added in v1.2.4

func (ClientCreator) MultiTransportLayer() bool

true

func (ClientCreator) NewClient added in v1.2.1

func (ClientCreator) NewClient(dc *proxy.DialConf) (proxy.Client, error)

func (ClientCreator) URLToDialConf added in v1.2.4

func (ClientCreator) URLToDialConf(u *url.URL, dc *proxy.DialConf, format int) (*proxy.DialConf, error)

type ClientUDPConn

type ClientUDPConn struct {
	*net.UDPConn

	ServerAddr          *net.TCPAddr //用于建立 udp associate的 服务器地址
	ServerUDPPort_forMe int          //socks5服务会为每一个socks5客户端留一个专用的udp端口

	WriteUDP_Target *net.UDPAddr
	// contains filtered or unexported fields
}

实现 net.PacketConn , net.Conn 和 netLayer.MsgConn

func (*ClientUDPConn) Associate

func (cpc *ClientUDPConn) Associate() (err error)

func (*ClientUDPConn) CloseConnWithRaddr

func (cpc *ClientUDPConn) CloseConnWithRaddr(raddr netLayer.Addr) error

func (*ClientUDPConn) Fullcone

func (cpc *ClientUDPConn) Fullcone() bool

func (*ClientUDPConn) Read

func (cpc *ClientUDPConn) Read(p []byte) (n int, err error)

func (*ClientUDPConn) ReadFrom

func (cpc *ClientUDPConn) ReadFrom(p []byte) (n int, addr net.Addr, err error)

func (*ClientUDPConn) ReadMsg added in v1.2.5

func (cpc *ClientUDPConn) ReadMsg() (data []byte, target netLayer.Addr, err error)

func (*ClientUDPConn) Write

func (cpc *ClientUDPConn) Write(p []byte) (n int, err error)

func (*ClientUDPConn) WriteMsg added in v1.2.5

func (cpc *ClientUDPConn) WriteMsg(p []byte, addr netLayer.Addr) (err error)

func (*ClientUDPConn) WriteTo

func (cpc *ClientUDPConn) WriteTo(p []byte, addr net.Addr) (n int, err error)

type Server

type Server struct {
	proxy.Base
	*utils.MultiUserMap

	TrustClient bool //如果为true,则每次握手读取客户端响应前, 不设置deadline. 这能减少一些开销, 但要保证客户端确实可信,不是坏蛋。如果客户端无法被信任,比如在公网或者 不止你一个人使用,则一定要为false,否则会被攻击,导致Server卡住, 造成大量悬垂连接。
}

func NewServer added in v1.2.3

func NewServer() *Server

func (*Server) Handshake

func (s *Server) Handshake(underlay net.Conn) (result net.Conn, udpChannel netLayer.MsgConn, targetAddr netLayer.Addr, returnErr error)

处理tcp收到的请求. 注意, udp associate后的 udp请求并不 直接 通过此函数处理, 而是由 UDPConn 处理

func (*Server) Name

func (*Server) Name() string

type ServerCreator

type ServerCreator struct{ proxy.CreatorCommonStruct }

func (ServerCreator) MultiTransportLayer added in v1.2.4

func (ServerCreator) MultiTransportLayer() bool

true

func (ServerCreator) NewServer

func (ServerCreator) NewServer(lc *proxy.ListenConf) (proxy.Server, error)

func (ServerCreator) URLToListenConf added in v1.2.4

func (ServerCreator) URLToListenConf(u *url.URL, lc *proxy.ListenConf, format int) (*proxy.ListenConf, error)

type ServerUDPConn

type ServerUDPConn struct {
	*net.UDPConn
	// contains filtered or unexported fields
}

用于socks5服务端的 udp连接, 实现 netLayer.MsgConn

func (*ServerUDPConn) CloseConnWithRaddr

func (u *ServerUDPConn) CloseConnWithRaddr(raddr netLayer.Addr) error

func (*ServerUDPConn) Fullcone

func (u *ServerUDPConn) Fullcone() bool

func (*ServerUDPConn) ReadMsg added in v1.2.5

func (u *ServerUDPConn) ReadMsg() ([]byte, netLayer.Addr, error)

从 客户端读取 udp请求

func (*ServerUDPConn) WriteMsg added in v1.2.5

func (u *ServerUDPConn) WriteMsg(bs []byte, raddr netLayer.Addr) error

将远程地址发来的响应 传给客户端

Jump to

Keyboard shortcuts

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