utils

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: 35 Imported by: 0

Documentation

Overview

Package utils provides general utilities.

Index

Constants

View Source
const (
	Log_debug = iota
	Log_info
	Log_warning
	Log_error //error一般用于输出 some 连接/客户端协议 错误, 但不致命
	Log_dpanic
	Log_panic
	Log_fatal

	DefaultLL = Log_info
)
View Source
const (
	ProjectName = "v2ray_simple"
	ProjectPath = ProjectName + "/"
)
View Source
const (
	//即 Maximum transmission unit, 参照的是 Ethernet v2 的MTU;
	MTU int = 1500

	//本作设定的最大包 长度大小,64k
	MaxPacketLen = 64 * 1024
)
View Source
const (
	Readv_buffer_allocLen = 8
	ReadvSingleBufLen     = 4096
)
View Source
const ExampleUUID = "a684455c-b14f-11ea-bf0d-42010aaa0003"
View Source
const SystemAutoUseAes = runtime.GOARCH == "amd64" || runtime.GOARCH == "s390x" || runtime.GOARCH == "arm64"

有些系统对aes支持不好,有些支持好。SystemAutoUseAes若为true,则说明支持很好,使用aes作为加密算法速度最佳。

View Source
const UUID_BytesLen int = 16

Variables

View Source
var (
	ErrUnImplemented       = errors.New("not implemented")
	ErrNilParameter        = errors.New("nil parameter")
	ErrNilOrWrongParameter = errors.New("nil or wrong parameter")
	ErrWrongParameter      = errors.New("wrong parameter")
	ErrInvalidData         = errors.New("invalid data")
	ErrNoMatch             = errors.New("no matched")
	ErrInvalidNumber       = errors.New("invalid number")

	ErrInvalidWrite = errors.New("readfrom, invalid write result")

	ErrShortRead = errors.New("short read") //io包有ErrShortWrite,却没有ShortRead
	ErrHandled   = errors.New("handled")
	ErrFailed    = errors.New("failed") //最无脑的Err, 在能描述清楚错误时不要使用 ErrFailed
)
View Source
var (
	LogLevel        int
	LogLevelForFile int = -1 //这一项如果非负,则控制台的日志等级按 LogLevel 走,日志文件登记按 LogLevelForFile 走, 否则都按 LogLevel 走
	ZapLogger       *zap.Logger

	//日志输出文件名称
	LogOutFileName string
)

LogLevel 值越小越唠叨, 废话越多,值越大打印的越少,见log_开头的常量;

我们的loglevel具体值 与 zap的 loglevel+1 的含义等价

View Source
var (
	ExtraSearchPath = "" //若给出,则会优先在此路径里查找
)
View Source
var GivenFlags map[string]*flag.Flag

Functions

func AllSubSets

func AllSubSets[T comparable](set []T) (subsets [][]T)

func AllSubSets edited from https://github.com/mxschmitt/golang-combinations with MIT License All returns all combinations for a given T array. This is essentially a powerset of the given set except that the empty set is disregarded.

func AllSubSets_improve1

func AllSubSets_improve1[T comparable](set []T) (subsets [][]T)

AllSubSets 测速有点慢, 我改进一下内存分配,可加速一倍多

func AnyToBool added in v1.2.4

func AnyToBool(a any) (v bool, ok bool)

func AnyToFloat64 added in v1.2.4

func AnyToFloat64(a any) (float64, bool)

func AnyToInt64 added in v1.2.4

func AnyToInt64(a any) (int64, bool)

func AnyToUInt16Array added in v1.2.5

func AnyToUInt16Array(a any) ([]uint16, bool)

func ArrayToPtrArray added in v1.2.5

func ArrayToPtrArray[T any](a []T) (r []*T)

func BuffersLen

func BuffersLen(bs [][]byte) (allnum int)

获取所有子[]byte 长度总和

func BuffersToMultiReader added in v1.2.5

func BuffersToMultiReader(bs [][]byte) io.Reader

func BuffersWriteTo

func BuffersWriteTo(bs [][]byte, writer io.Writer) (num int64, err error)

按顺序将bs内容写入writer

func CanLogDebug

func CanLogDebug(msg string) *zapcore.CheckedEntry

func CanLogErr

func CanLogErr(msg string) *zapcore.CheckedEntry

func CanLogFatal

func CanLogFatal(msg string) *zapcore.CheckedEntry

func CanLogInfo

func CanLogInfo(msg string) *zapcore.CheckedEntry

func CanLogLevel

func CanLogLevel(l int, msg string) *zapcore.CheckedEntry

assume ZapLogger is not nil.

func CanLogWarn

func CanLogWarn(msg string) *zapcore.CheckedEntry

func ClassicCopy added in v1.2.5

func ClassicCopy(w io.Writer, r io.Reader) (written int64, err error)

摘自 io.CopyBuffer。 因为原始的 CopyBuffer会又调用ReadFrom, 如果splice调用的话会产生无限递归。

这里删掉了ReadFrom, 直接进行经典拷贝

func CloneSlice

func CloneSlice[T any](a []T) (r []T)

func CommonSplit added in v1.2.4

func CommonSplit(s, e1, e2 string) (ok bool, v1, v2 string)

本作的惯例, 经常使用如下字符串作为配置: s = "e1:v1\ne2:v2",

func CommonSplit_regex added in v1.2.4

func CommonSplit_regex(s, e1, e2 string) (ok bool, v1, v2 string)

func CommonSplit_strings added in v1.2.4

func CommonSplit_strings(s, e1, e2 string) (ok bool, v1, v2 string)

func Debug

func Debug(msg string)

assume ZapLogger is not nil.

func DirExist

func DirExist(dirname string) bool

func DownloadAndUnzip added in v1.2.5

func DownloadAndUnzip(fname, downloadLink, dst string) (ok bool)

func Error

func Error(msg string)

assume ZapLogger is not nil.

func ExecCmd added in v1.2.5

func ExecCmd(cmdStr string) (err error)

func ExecCmdList added in v1.2.5

func ExecCmdList(strs []string) (err error)

func ExecCmdMultilineList added in v1.2.5

func ExecCmdMultilineList(cmdStr string) (err error)

func Fatal

func Fatal(msg string)

assume ZapLogger is not nil.

func FileExist

func FileExist(path string) bool

func FmtPrintRunCmd added in v1.2.5

func FmtPrintRunCmd(name string, arg ...string) (out string, err error)

Stdout, Stderr to fmt

func GenerateRandomChar

func GenerateRandomChar() byte

ascii 97-122

func GenerateRandomString

func GenerateRandomString() string

6-11 字节的字符串

func GenerateUUIDStr

func GenerateUUIDStr() string

func GetBuf

func GetBuf() *bytes.Buffer

从Pool中获取一个 *bytes.Buffer

func GetBuffers added in v1.2.5

func GetBuffers() [][]byte

从pool获取buffers,长度为8,每个长4k

func GetBytes

func GetBytes(size int) []byte

从pool中获取 []byte, 根据给出长度不同,来源于的Pool会不同.

func GetFilePath

func GetFilePath(fileName string) string

Function that search the specified file in the following directories:

 -1. If starts with '/', or is an empty string, return directly
 0. If starts with string similar to "C:/", "D:\\", or "e:/", return directly
 1. Search in ExtraSearchPath
	2. Same folder with exec file
 3. Same folder of the source file, 一种可能 是 用于 go test等情况
 4. Same folder of working folder

func GetGivenFlags added in v1.2.0

func GetGivenFlags() (m map[string]*flag.Flag)

flag包有个奇葩的缺点, 没法一下子获取所有的已经配置的参数, 只能遍历; 如果我们有大量的参数需要判断是否给出过, 那么不如先提取到到map里。

实际上flag包的底层也是用的一个map, 但是它是私有的, 而且我们也不宜用unsafe暴露出来.

func GetMTU

func GetMTU() []byte

从Pool中获取一个 MTU 长度的 []byte

func GetMapSortedKeySlice

func GetMapSortedKeySlice[K constraints.Ordered, V any](theMap map[K]V) []K

func GetPacket

func GetPacket() []byte

建议在 Read net.Conn 时, 使用 GetPacket函数 获取到足够大的 []byte (MaxPacketLen)

func GetPurgedTomlBytes added in v1.2.5

func GetPurgedTomlBytes(v any) ([]byte, error)

mimic GetPurgedTomlStr

func GetPurgedTomlStr

func GetPurgedTomlStr(v any) (string, error)

从any生成toml字符串, 移除 = "", = 0 和 = false 的项

func GetRandomWord

func GetRandomWord() (result string)

func GetSystemKillChan added in v1.2.5

func GetSystemKillChan() <-chan os.Signal

func GivenFlagKVs added in v1.2.1

func GivenFlagKVs() (r map[string]string)

return kv pairs for GivenFlags

func Info

func Info(msg string)

assume ZapLogger is not nil.

func InitLog

func InitLog(firstMsg string)

本作大量用到zap打印输出, 所以必须调用InitLog函数来初始化,否则就会闪退

func IsFilePath

func IsFilePath(s string) error

判断一个字符串是否是合法的文件名, 注意本函数不实际检查是否存在该文件

func IsFlagGiven

func IsFlagGiven(name string) bool

func IsTimezoneCN

func IsTimezoneCN() bool

func LogExecCmdList added in v1.2.5

func LogExecCmdList(strs []string) (err error)

按顺序执行, 遇到一个错误后就会停止运行, 直接返回

func LogLevelStr

func LogLevelStr(lvl int) string

func LogLevelStrList

func LogLevelStrList() (sl []string)

func LogRunCmd added in v1.2.5

func LogRunCmd(name string, arg ...string) (out string, err error)

Stdout, Stderr to zap, output log level debug

func MergeBuffers

func MergeBuffers(bs [][]byte) (result []byte, duplicate bool)

如果 分配了新内存来 包含数据,则 duplicate ==true, 此时可以用PutPacket函数放回;

如果利用了原有的第一个[]byte, 则 duplicate==false。

如果 duplicate==false, 不要 使用 PutPacket等方法放入Pool;

因为 在更上级的调用会试图去把 整个bs 放入pool;

func MergeBuffersWithPrefix added in v1.2.5

func MergeBuffersWithPrefix(prefix []byte, bs [][]byte) (result []byte, duplicate bool)

similar to MergeBuffers. prefix must has content

func MoveItem added in v1.2.4

func MoveItem[T any](arr *[]T, fromIndex, toIndex int)

func OpenFile added in v1.2.5

func OpenFile(name string) error

func Openbrowser added in v1.2.5

func Openbrowser(url string) error

https://gist.github.com/hyg/9c4afcd91fe24316cbf0

func ParseFlags added in v1.2.0

func ParseFlags()

call flag.Parse() and assign given flags to GivenFlags.

func PrintBuffers

func PrintBuffers(bs [][]byte)

func PrintStr added in v1.2.1

func PrintStr(str string)

本来可以直接用 fmt.Print, 但是那个Print多了一次到any的装箱, 而且准备步骤太多, 所以如果只 打印一个字符串的话,不妨直接调用 os.Stdout.WriteString(str)。

func PutBuf

func PutBuf(buf *bytes.Buffer)

将 buf 放回 Pool

func PutBuffers added in v1.2.5

func PutBuffers(bs [][]byte)

将用 GetBuffers 得到的 buffers 放回pool

func PutBytes

func PutBytes(bs []byte)

根据bs长度 选择放入各种pool中, 只有 cap(bs)>=MTU 才会被处理

func PutPacket

func PutPacket(bs []byte)

放回用 GetPacket 获取的 []byte

func Put_readvMem added in v1.2.5

func Put_readvMem(rm *ReadvMem)

将创建好的rm放回 readvPool

func QueryNegative added in v1.2.4

func QueryNegative(query url.Values, key string) bool

func QueryPositive added in v1.2.4

func QueryPositive(query url.Values, key string) bool

func ReadvFrom added in v1.2.5

func ReadvFrom(rawReadConn syscall.RawConn, rm *ReadvMem) ([][]byte, error)
ReadvFrom 用于读端 为rawRead的情况,如 从socks5或direct读取 数据, 等裸协议的情况。

rm可为nil,但不建议,因为提供非nil的readvMem 可以节省内存分配开销。

返回错误时,会返回 原buffer 或 在函数内部新分配的buffer.

本函数不负责 释放分配的内存. 因为有时需要重复利用缓存。

TryCopy函数使用到了本函数 来进行readv相关操作。

func RecoverBuffers

func RecoverBuffers(bs [][]byte, oldLen, old_sub_len int) [][]byte

通过reslice 方式将 bs的长度以及 子 []byte 的长度 恢复至指定长度

func ReplaceBytesSynonyms added in v1.2.5

func ReplaceBytesSynonyms(bs []byte, synonyms [][2][]byte) (result []byte)

the first part of synonyms is the one to be replaced, the last part of synonyms is the persistent one.

func ReplaceStringsSynonyms added in v1.2.5

func ReplaceStringsSynonyms(bs string, synonyms [][2]string) (result string)

same as ReplaceBytesSynonyms

func ShrinkBuffers

func ShrinkBuffers(bs [][]byte, all_len int, SingleBufLen int) int

削减buffer内部的子[]byte 到合适的长度;返回削减后 bs应有的长度.

func SimpleDownloadFile added in v1.2.5

func SimpleDownloadFile(fname, downloadLink string) (ok bool)

func SortByOrder added in v1.2.4

func SortByOrder[T any](arr []T, order []int) (result []T, neworder []int, erri int)

根据传入的order来对arr重新排序;order必须长度与arr一致,而且包含所有索引 若erri>0 则证明传入的order内容有误。1表示过长,2表示过短,3表示内容出错. 在 erri>0 时,本函数会试图修复order,生成一个neworder并用该 neworder 对arr排序。

func Splice added in v1.2.4

func Splice[T any](source *[]T, start int, deleteCount int, items ...T) (removed []T)

splices 包在 Nov 10, 2022 添加了Replace函数, 就不用我们自己的实现了 v0.0.0-20221110155412-d0897a79cd37, 不过我们为了代码兼容依然保存该代码,直到2.x.x版本.

items to insert at start, delete deleteCount items at start.

See https://github.com/zzwx/splice/blob/main/splice.go

func StrNegative added in v1.2.4

func StrNegative(value string) bool

func StrPositive added in v1.2.4

func StrPositive(value string) bool

func StrToUUID_slice added in v1.2.2

func StrToUUID_slice(s string) []byte

func TrimSlice

func TrimSlice[T any](a []T, deleteIndex int) []T

TrimSlice 从一个slice中移除一个元素, 会直接改动原slice数据

func TryDownloadWithProxyUrl added in v1.2.3

func TryDownloadWithProxyUrl(proxyUrl, downloadLink string) (thehttpClient *http.Client, resp *http.Response, err error)

TryDownloadWithProxyUrl try to download from a link with the given proxy url. thehttpClient is the client created, could be http.DefaultClient or a newly created one.

If proxyUrl is empty, the function will call http.DefaultClient.Get, else it will create with a client with a transport with proxy set to proxyUrl. If err==nil, then thehttpClient!=nil .

func UUIDToStr

func UUIDToStr(u []byte) string

func Warn

func Warn(msg string)

assume ZapLogger is not nil.

func WrapFuncForPromptUI

func WrapFuncForPromptUI(f func(string) bool) func(string) error

Types

type BuffersReader added in v1.2.5

type BuffersReader interface {
	ReadBuffers() ([][]byte, error)

	//将 ReadBuffers 放回缓存,以便重复利用内存. 因为这里不确定这个buffers是如何获取到的, 所以由实现者自行确定
	PutBuffers([][]byte)
}

type ByteReader

type ByteReader interface {
	ReadByte() (byte, error)
	Read(p []byte) (n int, err error)
}

bufio.Reader and bytes.Buffer implemented ByteReader

type ByteWriter

type ByteWriter interface {
	WriteByte(byte) error
	Write(p []byte) (n int, err error)
}

bytes.Buffer implemented ByteWriter

type ChanCloser added in v1.2.0

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

simple structure that send a signal by chan when Close called.

func NewChanCloser added in v1.2.0

func NewChanCloser() (*ChanCloser, chan struct{})

func (*ChanCloser) Close added in v1.2.0

func (cc *ChanCloser) Close() error

type DownloadPrintCounter added in v1.2.5

type DownloadPrintCounter struct {
	Total uint64
}

https://golangcode.com/download-a-file-with-progress/

func (DownloadPrintCounter) PrintProgress added in v1.2.5

func (wc DownloadPrintCounter) PrintProgress()

func (*DownloadPrintCounter) Write added in v1.2.5

func (wc *DownloadPrintCounter) Write(p []byte) (int, error)

type DummyReadCloser added in v1.2.0

type DummyReadCloser struct {
	ReadCount int
}

func (DummyReadCloser) Close added in v1.2.0

func (DummyReadCloser) Close() error

return nil

func (*DummyReadCloser) Read added in v1.2.0

func (d *DummyReadCloser) Read(p []byte) (int, error)

ReadCount -= 1 at each call. if ReadCount<0, return 0, io.EOF

type DummyWriteCloser added in v1.2.0

type DummyWriteCloser struct {
	WriteCount int
}

func (DummyWriteCloser) Close added in v1.2.0

func (DummyWriteCloser) Close() error

return nil

func (*DummyWriteCloser) Write added in v1.2.0

func (d *DummyWriteCloser) Write(p []byte) (int, error)

WriteCount -= 1 at each call. if WriteCount<0, return 0, io.EOF

type ErrBuffer added in v1.2.0

type ErrBuffer struct {
	Err error
	Buf *bytes.Buffer
}

an err with a buffer, nothing special

func (ErrBuffer) Error added in v1.2.0

func (ef ErrBuffer) Error() string

func (ErrBuffer) Is added in v1.2.3

func (e ErrBuffer) Is(target error) bool

func (ErrBuffer) Unwarp added in v1.2.0

func (ef ErrBuffer) Unwarp() error

type ErrInErr

type ErrInErr struct {
	ErrDesc   string
	ErrDetail error
	Data      any

	ExtraIs []error
}

ErrInErr 很适合一个err包含另一个err,并且提供附带数据的情况. 类似 fmt.Errorf.

func (ErrInErr) Error

func (e ErrInErr) Error() string

func (ErrInErr) Is

func (e ErrInErr) Is(target error) bool

func (ErrInErr) String

func (e ErrInErr) String() string

func (ErrInErr) Unwarp

func (e ErrInErr) Unwarp() error

type Errs added in v1.2.4

type Errs struct {
	List []ErrsItem
}

func (*Errs) Add added in v1.2.4

func (ee *Errs) Add(e ErrsItem)

func (Errs) Error added in v1.2.4

func (e Errs) Error() string

func (Errs) OK added in v1.2.4

func (e Errs) OK() bool

func (Errs) String added in v1.2.4

func (e Errs) String() string

type ErrsItem added in v1.2.4

type ErrsItem struct {
	Index int
	E     error
}

type HashReader added in v1.2.5

type HashReader struct {
	io.Reader
	// contains filtered or unexported fields
}

func NewHashReader added in v1.2.5

func NewHashReader(conn io.Reader, key []byte) *HashReader

func (*HashReader) Read added in v1.2.5

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

func (*HashReader) Sum added in v1.2.5

func (c *HashReader) Sum() []byte

type HashWriter added in v1.2.5

type HashWriter struct {
	io.Writer
	// contains filtered or unexported fields
}

func NewHashWriter added in v1.2.5

func NewHashWriter(conn io.Writer, key []byte) *HashWriter

func (*HashWriter) StopHashing added in v1.2.5

func (c *HashWriter) StopHashing()

func (*HashWriter) Sum added in v1.2.5

func (c *HashWriter) Sum() []byte

func (*HashWriter) Write added in v1.2.5

func (c *HashWriter) Write(p []byte) (n int, err error)

func (*HashWriter) Written added in v1.2.5

func (c *HashWriter) Written() bool

Has the hash been written

type Heap

type Heap[T any] struct {
	LessFunc func(i, j int, a []T) bool

	Array []T
}

The Heap type describes the requirements for a type using the routines in this package. Any type that implements it may be used as a min-heap with the following invariants (established after Init has been called or if the data is empty or sorted):

!h.Less(j, i) for 0 <= i < h.Len() and 2*i+1 <= j <= 2*i+2 and j < h.Len()

Note that Push and Pop in this interface are for package heap's implementation to call. To add and remove things from the heap, use heap.Push and heap.Pop.

实际上我们这个包装已经类似 优先队列了, 至于如何优先取决于 LessFunc

func (*Heap[T]) Fix

func (h *Heap[T]) Fix(i int)

Fix re-establishes the heap ordering after the element at index i has changed its value. Changing the value of the element at index i and then calling Fix is equivalent to, but less expensive than, calling Remove(h, i) followed by a Push of the new value. The complexity is O(log n) where n = h.Len().

func (*Heap[T]) Init

func (h *Heap[T]) Init()

Init establishes the heap invariants required by the other routines in this package. Init is idempotent with respect to the heap invariants and may be called whenever the heap invariants may have been invalidated. The complexity is O(n) where n = h.Len().

func (*Heap[T]) Len

func (h *Heap[T]) Len() int

func (*Heap[T]) Pop

func (h *Heap[T]) Pop() T

Pop removes and returns the minimum element (according to Less) from the heap. The complexity is O(log n) where n = h.Len(). Pop is equivalent to Remove(h, 0).

func (*Heap[T]) Push

func (h *Heap[T]) Push(x T)

Push pushes the element x onto the heap. The complexity is O(log n) where n = h.Len().

func (*Heap[T]) Remove

func (h *Heap[T]) Remove(i int) T

Remove removes and returns the element at index i from the heap. The complexity is O(log n) where n = h.Len().

type InvalidDataErr added in v1.2.2

type InvalidDataErr string

func (InvalidDataErr) Error added in v1.2.2

func (e InvalidDataErr) Error() string

func (InvalidDataErr) Is added in v1.2.2

func (e InvalidDataErr) Is(err error) bool

return err == e || err == ErrInvalidData

type MultiCloser added in v1.2.4

type MultiCloser struct {
	Closers []io.Closer
	sync.Once
}

func (*MultiCloser) Close added in v1.2.4

func (cc *MultiCloser) Close() (result error)

type MultiReader

type MultiReader interface {

	//在底层没有实现readbuffers时, 或者协议设计中并没有涉及多buf, 则显然调用 ReadBuffers没有什么意义。
	//所以我们通过 WillReadBuffersBenifit 方法 查询 调用是否有益。
	//如果int为1,说明它是一个 BuffersReader; 如果int为2,说明它是一个 Readver
	WillReadBuffersBenifit() int

	CanMultiRead() bool //一个协议的握手阶段可能需要一些操作后,才能真正执行MultiRead
}

type MultiUserMap added in v1.2.2

type MultiUserMap struct {
	IDMap   map[string]User
	AuthMap map[string]User

	Mutex sync.RWMutex

	TheIDBytesLen, TheAuthBytesLen int

	StoreKeyByStr bool //如果这一项给出, 则内部会用 identityStr/AuthStr 作为key;否则会用 string(identityBytes) 或 string(AuthBytes) 作为key

	IDStrToBytesFunc func(string) []byte

	IDBytesToStrFunc func([]byte) string //必须与 Key_StrToBytesFunc 同时给出

	AuthStrToBytesFunc func(string) []byte

	AuthBytesToStrFunc func([]byte) string //必须与 Key_AuthStrToBytesFunc 同时给出

}

implements UserBus, UserSet, UserGetter; 只能存储同一类型的User. 通过 bytes存储用户id,而不是 str。

func NewMultiUserMap added in v1.2.2

func NewMultiUserMap() *MultiUserMap

func (*MultiUserMap) AddUser added in v1.2.2

func (mu *MultiUserMap) AddUser(u User) error

same as AddUser_nolock but with lock; concurrent safe

func (*MultiUserMap) AddUser_nolock added in v1.2.3

func (mu *MultiUserMap) AddUser_nolock(u User)

not concurrent safe, use with caution.

func (*MultiUserMap) AuthBytesLen added in v1.2.3

func (mu *MultiUserMap) AuthBytesLen() int

func (*MultiUserMap) AuthUserByBytes added in v1.2.3

func (mu *MultiUserMap) AuthUserByBytes(bs []byte) User

通过Auth查找

func (*MultiUserMap) AuthUserByStr added in v1.2.3

func (mu *MultiUserMap) AuthUserByStr(str string) User

通过Auth查找

func (*MultiUserMap) DelUser added in v1.2.2

func (mu *MultiUserMap) DelUser(u User) error

func (*MultiUserMap) HasUserByBytes added in v1.2.2

func (mu *MultiUserMap) HasUserByBytes(bs []byte) User

通过ID查找

func (*MultiUserMap) HasUserByStr added in v1.2.2

func (mu *MultiUserMap) HasUserByStr(str string) bool

通过ID查找

func (*MultiUserMap) IDBytesLen added in v1.2.3

func (mu *MultiUserMap) IDBytesLen() int

func (*MultiUserMap) LoadUsers added in v1.2.2

func (mu *MultiUserMap) LoadUsers(us []User)

func (*MultiUserMap) SetUseUUIDStr_asKey added in v1.2.2

func (mu *MultiUserMap) SetUseUUIDStr_asKey()

type MultiWriter

type MultiWriter interface {
	WriteBuffers([][]byte) (int64, error)
}

因为 net.Buffers 的 WriteTo方法只会查看其是否实现了net包私有的 writeBuffers 接口 我们无法用WriteTo来给其它 代码提升性能;因此我们重新定义一个新的借口, 实现了 MultiWriter 接口的结构 我们就认为它会提升性能,比直接用 net.Buffers.WriteTo 要更强.

本接口 在代理中的用途,基本上只适合 加密层 能够做到分组加密 的情况; 因为如果不加密的话就是裸协议,直接splice/writev,也不需要这么麻烦;

如果是tls的话,可能涉及自己魔改tls把私有函数暴露出来然后分组加密;

如果是vmess的话,倒是有可能的,不过我还没研究vmess的 加密细节;

而如果是ss 那种简单混淆 异或加密的话,则是完全可以的

分组加密然后 一起用 writev 发送出去,可以降低网络延迟, 不过writev性能的提升可能是非常细微的, 也不必纠结这里.

如果考虑另一种情况,即需要加包头和包尾,则区别就很大了;

WriteTo会调用N次Write,如果包装的话,会包装N 个包头和 包尾;而如果我们实现WriteBuffers方法,
只需先写入包头,而在 最后一个 []byte 后加 包尾,那么就可以获得性能提升,
我们只需增添两个新的 []byte 放在其前后即可, 然后再用 writev 一起发送出去

那么实际上 websocket 的gobwas/ws 包在不开启缓存时,就是 每次Write都写一次包头的情况;

所以websocket很有必要实现 WriteBuffers 方法.

目前实现 的有 vless/trojan 的 UserTCPConn ,  ws.Conn

WriteV本身几乎没什么加速,但是因为ReadV有加速,所以通过WriteV写入ReadV读到的数组才能配合ReadV加速

type NilCloserWrapper added in v1.2.5

type NilCloserWrapper struct {
	SimpleCloser
}

func (NilCloserWrapper) Close added in v1.2.5

func (nc NilCloserWrapper) Close() error

type NumErr

type NumErr struct {
	N int
	E error
}

nothing special. Normally, N==0 means no error

func (NumErr) Error

func (ne NumErr) Error() string

func (NumErr) Is added in v1.2.3

func (e NumErr) Is(target error) bool

func (NumErr) Unwarp added in v1.2.3

func (ef NumErr) Unwarp() error

type NumStrErr added in v1.2.3

type NumStrErr struct {
	N      int
	Prefix string
}

nothing special

func (NumStrErr) Error added in v1.2.3

func (ne NumStrErr) Error() string

type PrefixWriter added in v1.2.5

type PrefixWriter struct {
	io.Writer
	Prefix []byte
}

func (*PrefixWriter) Write added in v1.2.5

func (lw *PrefixWriter) Write(p []byte) (n int, err error)

type RW

type RW struct {
	io.Reader
	io.Writer
}

一种简单的读写组合, 在ws包中被用到.

type ReadSwitcher added in v1.2.0

type ReadSwitcher struct {
	Old, New   io.Reader     //non-nil
	SwitchChan chan struct{} //non-nil

	io.Closer
	// contains filtered or unexported fields
}

先从Old读,若SwitchChan被关闭, 立刻改为从New读

func (*ReadSwitcher) Close added in v1.2.0

func (d *ReadSwitcher) Close() error

func (*ReadSwitcher) Read added in v1.2.0

func (d *ReadSwitcher) Read(p []byte) (int, error)

type ReadWrapper added in v1.2.0

type ReadWrapper struct {
	io.Reader
	OptionalReader    io.Reader
	RemainFirstBufLen int
}

optionally read from OptionalReader

func (*ReadWrapper) Close added in v1.2.0

func (rw *ReadWrapper) Close() error

func (*ReadWrapper) Read added in v1.2.0

func (rw *ReadWrapper) Read(p []byte) (n int, err error)

type ReadvMem added in v1.2.5

type ReadvMem struct {
	Buffers [][]byte
	Mr      SystemReadver
}

缓存 ReadvMem 以及对应分配的系统相关的 SystemReadver. 使用 ReadvMem的最大好处就是 buffers 和 mr 都是不需要 释放的.

因为不需释放mr, 所以也就节省了多次 mr.Init 的开销.

该 ReadvMem 以及 readvPool 专门服务于 TryCopy 函数.

func Get_readvMem added in v1.2.5

func Get_readvMem() *ReadvMem

type Readver added in v1.2.5

type Readver interface {
	GetRawForReadv() syscall.RawConn
}

type SimpleCloser added in v1.2.5

type SimpleCloser interface {
	Close()
}

type SystemReadver

type SystemReadver interface {
	Init(bs [][]byte, singleBufLen int) //将 给出的buffer 放入内部实际数据中
	Read(fd uintptr) (uint32, error)    //读取一次文件,并放入 buffer中
	Clear()                             //清理内部buffer
	Recover(bsLen int, bs [][]byte)     //恢复内部buffer
}

SystemReadver 是平台相关的 用于 调用readv的 工具. 该 SystemReadver 的用例请参照 netLayer.readvFrom , 在 netLayer/readv.go中; SystemReadver的具体平台相关的实现见 readv_*.go; 用 GetReadVReader() 函数来获取本平台的对应实现。

func GetReadVReader

func GetReadVReader() SystemReadver

type UUID added in v1.2.4

type UUID [UUID_BytesLen]byte

func GenerateUUID

func GenerateUUID() (r UUID)

生成完全随机的uuid,不包含任何uuid版本信息 (即不符合rfc,但是更安全)

func GenerateUUID_v4

func GenerateUUID_v4() (r UUID)

GenerateUUID_v4 生成符合v4标准的uuid.

v4: https://datatracker.ietf.org/doc/html/rfc4122#section-4.4

variant: https://datatracker.ietf.org/doc/html/rfc4122#section-4.1.1

version: https://datatracker.ietf.org/doc/html/rfc4122#section-4.1.3

func StrToUUID

func StrToUUID(s string) (uuid UUID, err error)

type User added in v1.2.0

type User interface {
	IdentityStr() string //每个user唯一,通过比较这个string 即可 判断两个User 是否相等。相当于 user name

	IdentityBytes() []byte //与str类似; 对于程序来说,bytes更方便处理; 可以与str相同,也可以不同.

	AuthStr() string   //AuthStr 可以识别出该用户 并验证该User的真实性。相当于 user name + password
	AuthBytes() []byte //与 AuthStr 类似
}

User是一个 可确定唯一身份,且可验证该身份的 标识。

func InitV2rayUsers added in v1.2.2

func InitV2rayUsers(uc []UserConf) (us []User)

type UserAssigner added in v1.2.5

type UserAssigner interface {
	SetUser(User)
}

type UserAuthenticator added in v1.2.4

type UserAuthenticator interface {
	AuthUserByStr(authStr string) User
	AuthUserByBytes(authBytes []byte) User
	AuthBytesLen() int
}

通过验证信息 试图取出 一个User

type UserBus added in v1.2.0

type UserBus interface {
	AddUser(User) error
	DelUser(User)
}

可以控制 User 登入和登出 的接口

type UserConf added in v1.2.2

type UserConf struct {
	User string `toml:"user"`
	Pass string `toml:"pass"`
}

type UserContainer added in v1.2.0

type UserContainer interface {
	UserSet

	UserAuthenticator
}

用户容器,可判断是否存在,也可以验证

type UserPass added in v1.2.2

type UserPass struct {
	UserID, Password []byte
}

used in proxy/socks5 and proxy.http. implements User

func NewUserPass added in v1.2.2

func NewUserPass(uc UserConf) *UserPass

func NewUserPassByData added in v1.2.3

func NewUserPassByData(user, pass []byte) *UserPass

func (*UserPass) AuthBytes added in v1.2.3

func (ph *UserPass) AuthBytes() []byte

func (*UserPass) AuthStr added in v1.2.3

func (ph *UserPass) AuthStr() string

func (*UserPass) AuthUserByBytes added in v1.2.3

func (ph *UserPass) AuthUserByBytes(bs []byte) User

func (*UserPass) AuthUserByStr added in v1.2.3

func (ph *UserPass) AuthUserByStr(idStr string) User

func (*UserPass) GetUserByPass added in v1.2.2

func (ph *UserPass) GetUserByPass(user, pass []byte) User

func (*UserPass) IdentityBytes added in v1.2.3

func (ph *UserPass) IdentityBytes() []byte

func (*UserPass) IdentityStr added in v1.2.3

func (ph *UserPass) IdentityStr() string

func (*UserPass) InitWithStr added in v1.2.2

func (ph *UserPass) InitWithStr(str string) (ok bool)

uuid: "user:xxxx\npass:xxxx"

func (*UserPass) InitWithUrl added in v1.2.2

func (ph *UserPass) InitWithUrl(u *url.URL) bool

require "user" and "pass" field. return true if both not empty.

func (*UserPass) Valid added in v1.2.2

func (ph *UserPass) Valid() bool

return len(ph.User) > 0 && len(ph.Password) > 0

type UserSet added in v1.2.4

type UserSet interface {
	HasUserByBytes(bs []byte) User
	IDBytesLen() int //用户名bytes的最小长度
}

用户集合,判断用户是否存在并取出

type UserWithPass added in v1.2.2

type UserWithPass interface {
	User
	GetPassword() []byte
}

type V2rayUser added in v1.2.0

type V2rayUser [UUID_BytesLen]byte

一种专门用于v2ray协议族(vmess/vless)的 用于标识用户的符号 , 实现 User 接口. (其实就是uuid)

func InitRealV2rayUsers added in v1.2.3

func InitRealV2rayUsers(uc []UserConf) (us []V2rayUser)

func NewV2rayUser added in v1.2.0

func NewV2rayUser(uuidStr string) (V2rayUser, error)

func (V2rayUser) AuthBytes added in v1.2.3

func (u V2rayUser) AuthBytes() []byte

func (V2rayUser) AuthStr added in v1.2.3

func (u V2rayUser) AuthStr() string

func (V2rayUser) IdentityBytes added in v1.2.3

func (u V2rayUser) IdentityBytes() []byte

func (V2rayUser) IdentityStr added in v1.2.3

func (u V2rayUser) IdentityStr() string

type WriteSwitcher added in v1.2.0

type WriteSwitcher struct {
	Old, New   io.Writer     //non-nil
	SwitchChan chan struct{} //non-nil
	io.Closer
	// contains filtered or unexported fields
}

先向Old写,若SwitchChan被关闭, 改向New写

func (*WriteSwitcher) Close added in v1.2.0

func (d *WriteSwitcher) Close() error

func (*WriteSwitcher) Write added in v1.2.0

func (d *WriteSwitcher) Write(p []byte) (int, error)

type WriteWrapper added in v1.2.3

type WriteWrapper interface {
	io.Writer

	GetRawWriter() io.Writer
	SetRawWriter(io.Writer)
}

Jump to

Keyboard shortcuts

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