ss

package
v1.2.4 Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2020 License: Apache-2.0 Imports: 27 Imported by: 0

Documentation

Overview

客户端实现udp代理使用, 服务端没有使用此文件

Index

Constants

View Source
const (
	// 地址类型
	S5ipv4   byte = 0x01
	S5domain byte = 0x03
	S5ipv6   byte = 0x04

	S5lenPort = 2 // port 字段长度
	// VER NMETHODS CMD位置,
	S5VerIdx     = 0
	S5NmethodIdx = 1
	S5CmdIdx     = 1

	// ATYP 字段前面的字节数
	S5reqBaseLen = 3
	S5repBaseLen = 3
	// 从 ATYP 字段算起的下标
	S5TypeIdx   = 0                   // address type index
	S5IP0Idx    = 1                   // ip address start index
	S5DmLenIdx  = 1                   // domain address length index
	S5Dm0Idx    = 2                   // domain address start index
	S5lenIPv4   = 1 + net.IPv4len + 2 // 1addrType + ipv4 + 2port
	S5lenIPv6   = 1 + net.IPv6len + 2 // 1addrType + ipv6 + 2port
	S5lenDmBase = 1 + 1 + 2           // 1addrType + 1addrLen + 2port, plus addrLen

	// 认证方式
	S5NoAuthentication byte = 0x00
	// 版本
	S5Ver byte = 0x05
	// CMD 字段
	S5CmdConnect byte = 0x01
	S5CmdBind    byte = 0x02
	S5CmdUdp     byte = 0x03
)

客户端 -> 代理服务器, 请求认证:

+----+----------+----------+ |VER | NMETHODS | METHODS | +----+----------+----------+ | 1 | 1 | 1 to 255 | +----+----------+----------+

VER 版本号 固定为 5 NMETHODS 可供选择的认证方法 选了多少种 METHODS 选择的方法

代理服务器 -> 客户端, 确认信息

+----+--------+ |VER | METHOD | +----+--------+ | 1 | 1 | +----+--------+

METHOD 认证方式: o X'00' NO AUTHENTICATION REQUIRED o X'01' GSSAPI o X'02' USERNAME/PASSWORD o X'03' to X'7F' IANA ASSIGNED o X'80' to X'FE' RESERVED FOR PRIVATE METHODS o X'FF' NO ACCEPTABLE METHODS

客户端 -> 代理服务器,代理请求 SOCKS request is formed as follows:

+----+-----+-------+------+----------+----------+ |VER | CMD | RSV | ATYP | DST.ADDR | DST.PORT | +----+-----+-------+------+----------+----------+ | 1 | 1 | X'00' | 1 | Variable | 2 | +----+-----+-------+------+----------+----------+

Replies

+----+-----+-------+------+----------+----------+ |VER | REP | RSV | ATYP | BND.ADDR | BND.PORT | +----+-----+-------+------+----------+----------+ | 1 | 1 | X'00' | 1 | Variable | 2 | +----+-----+-------+------+----------+----------+

View Source
const (
	S5socksVer5       = 5
	S5socksCmdConnect = 1
)
View Source
const (
	AddrMask byte = 0xf
)

Variables

View Source
var ErrNilCipher = errors.New("cipher can not be nil")

Functions

func CheckCipherMethod

func CheckCipherMethod(method string) error

func CreateURI

func CreateURI(method, password, host, port string) (uri string)

ss://base64(method:password@host:port)

func GetPublicIP

func GetPublicIP() string

func IsFileExist

func IsFileExist(path string) (bool, error)

检查常规文件是否存在, 常规文件不是 ModeDir | ModeSymlink | ModeNamedPipe | ModeSocket | ModeDevice | ModeCharDevice | ModeIrregular

func PipeThenClose

func PipeThenClose(src, dst net.Conn, addTraffic func(int))

PipeThenClose 从 src 读取数据送到 dst, 完成后关闭 dst

func Pipeloop

func Pipeloop(write net.PacketConn, writeAddr net.Addr, readClose net.PacketConn, addTraffic func(int))

通过 write 将 readClose 发来的数据 发送给 writeaddr 这个地址

func PrintVersion

func PrintVersion()

func RawAddr

func RawAddr(addr string) (buf []byte, err error)

处理原始地址,返回 sock5 协议中地址信息, 从ATYP字段开始。

func ReadAndHandleUDPReq

func ReadAndHandleUDPReq(c *SecurePacketConn, addTraffic func(int)) (err error)

func SetDebug

func SetDebug(b bool)

是否启用 Debug , 传 bool

func SetReadTimeout

func SetReadTimeout(c net.Conn)

func UpdateConfig

func UpdateConfig(old, new *Config)

Types

type Cipher

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

负责提供加密解密功能

func NewCipher

func NewCipher(method, password string) (c *Cipher, err error)

NewCipher 创建一个 Cipher 使用 cipher.Copy() 可以创建一个使用相同加密算法和密码的 cipher

func (*Cipher) Copy

func (c *Cipher) Copy() *Cipher

Copy 返回一个使用相同加密算法和密码的 cipher enc 和 dec 都为 nil

type Config

type Config struct {
	Server       interface{} `json:"server"` // ss local 用
	ServerPort   int         `json:"server_port"`
	LocalPort    int         `json:"local_port"`    // ss local 用
	LocalAddress string      `json:"local_address"` // ss local 用
	Password     string      `json:"password"`
	Method       string      `json:"method"`

	// 以下选项只用于 ss server
	PortPassword map[string]string `json:"port_password"` // port:password  如果指定,会忽略上面的 server_port password
	Timeout      int               `json:"timeout"`

	// 以下选项只用于 ss client
	// 客户端配置中, 服务器顺序很重要, 因此使用数组而不是map
	ServerPassword [][]string `json:"server_password"` // 多个服务器 [](server, passwd [,method])
}

func ParseConfig

func ParseConfig(path string) (config *Config, err error)

func (*Config) GetServerArray

func (config *Config) GetServerArray() []string

type Conn

type Conn struct {
	net.Conn // 内嵌接口,可以使用所有实现了 net.Conn 接口的实例来初始化
	*Cipher  // 组合,Conn 可以直接使用 Cipher 成员  [ˈsaɪfər] 密码; 暗号;
	// contains filtered or unexported fields
}

func Dial

func Dial(addr, server string, cipher *Cipher) (c *Conn, err error)

Dial 地址 addr 应该是 host:port 的形式

func DialWithRawAddr

func DialWithRawAddr(rawaddr []byte, server string, cipher *Cipher) (c *Conn, err error)

DialWithRawAddr 为实现本地SOCKS代理的用户使用 rawaddr应该包含socks请求中的部分数据,从ATYP字段开始。

func NewConn

func NewConn(c net.Conn, cipher *Cipher) *Conn

func (*Conn) Close

func (c *Conn) Close() error

func (*Conn) Read

func (c *Conn) Read(b []byte) (n int, err error)

func (*Conn) Write

func (c *Conn) Write(b []byte) (n int, err error)

type DecOrEnc

type DecOrEnc int
const (
	Decrypt DecOrEnc = iota
	Encrypt
)

常量代表 加密 解密

type Dialer

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

func NewDialer

func NewDialer(server string, cipher *Cipher) (dialer *Dialer, err error)

func (*Dialer) Dial

func (d *Dialer) Dial(network, addr string) (c net.Conn, err error)

type LeakyBuf

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

buffer pool 重复使用避免申请内存的开销

func NewLeakyBuf

func NewLeakyBuf(n, bufSize int) *LeakyBuf

NewLeakyBuf 创建一个 leaky buffer, 可以包含 n 个 buffer, 每个大小为 bufSize

func (*LeakyBuf) Get

func (lb *LeakyBuf) Get() (b []byte)

Get 从 leaky buffer 中返回一个 buffer, 或者创建一个新的 buffer

func (*LeakyBuf) Put

func (lb *LeakyBuf) Put(b []byte)

Put 在 free buffer pool 中加入一个 buffer, 如果 buffer 的大小和 leaky buffer中的不一致 将引发一个 panic, 以此来暴漏错误的用法

type NoneCipherStream added in v1.2.4

type NoneCipherStream struct {
}

无加密模式

func (*NoneCipherStream) XORKeyStream added in v1.2.4

func (none *NoneCipherStream) XORKeyStream(dst, src []byte)

type ProxyAddr

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

func (*ProxyAddr) Network

func (a *ProxyAddr) Network() string

func (*ProxyAddr) String

func (a *ProxyAddr) String() string

type ProxyConn

type ProxyConn struct {
	*Conn
	// contains filtered or unexported fields
}

func (*ProxyConn) LocalAddr

func (c *ProxyConn) LocalAddr() net.Addr

func (*ProxyConn) RemoteAddr

func (c *ProxyConn) RemoteAddr() net.Addr

func (*ProxyConn) SetDeadline

func (c *ProxyConn) SetDeadline(t time.Time) error

func (*ProxyConn) SetReadDeadline

func (c *ProxyConn) SetReadDeadline(t time.Time) error

func (*ProxyConn) SetWriteDeadline

func (c *ProxyConn) SetWriteDeadline(t time.Time) error

type SecurePacketConn

type SecurePacketConn struct {
	net.PacketConn
	*Cipher
}

加密的 分组 传输

func NewSecurePacketConn

func NewSecurePacketConn(c net.PacketConn, cipher *Cipher) *SecurePacketConn

func (*SecurePacketConn) LocalAddr

func (c *SecurePacketConn) LocalAddr() net.Addr

func (*SecurePacketConn) ReadFrom

func (c *SecurePacketConn) ReadFrom(b []byte) (n int, src net.Addr, err error)

func (*SecurePacketConn) SetDeadline

func (c *SecurePacketConn) SetDeadline(t time.Time) error

func (*SecurePacketConn) SetReadDeadline

func (c *SecurePacketConn) SetReadDeadline(t time.Time) error

func (*SecurePacketConn) SetWriteDeadline

func (c *SecurePacketConn) SetWriteDeadline(t time.Time) error

func (*SecurePacketConn) WriteTo

func (c *SecurePacketConn) WriteTo(b []byte, dst net.Addr) (n int, err error)

Jump to

Keyboard shortcuts

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