Documentation ¶
Overview ¶
Package utils provides general utilities.
Index ¶
- Constants
- Variables
- func AllSubSets[T comparable](set []T) (subsets [][]T)
- func AllSubSets_improve1[T comparable](set []T) (subsets [][]T)
- func BuffersLen(bs [][]byte) (allnum int)
- func BuffersWriteTo(bs [][]byte, writeConn io.Writer) (num int64, err2 error)
- func CanLogDebug(msg string) *zapcore.CheckedEntry
- func CanLogErr(msg string) *zapcore.CheckedEntry
- func CanLogFatal(msg string) *zapcore.CheckedEntry
- func CanLogInfo(msg string) *zapcore.CheckedEntry
- func CanLogLevel(l int, msg string) *zapcore.CheckedEntry
- func CanLogWarn(msg string) *zapcore.CheckedEntry
- func CloneSlice[T any](a []T) (r []T)
- func Debug(msg string)
- func DirExist(dirname string) bool
- func Error(msg string)
- func Fatal(msg string)
- func FileExist(path string) bool
- func GenerateRandomChar() byte
- func GenerateRandomString() string
- func GenerateUUID() (r [16]byte)
- func GenerateUUIDStr() string
- func GenerateUUID_v4() (r [16]byte)
- func GetBuf() *bytes.Buffer
- func GetBytes(size int) []byte
- func GetFilePath(fileName string) string
- func GetGivenFlags() (m map[string]*flag.Flag)
- func GetMTU() []byte
- func GetMapSortedKeySlice[K constraints.Ordered, V any](theMap map[K]V) []K
- func GetPacket() []byte
- func GetPurgedTomlStr(v any) (string, error)
- func GetRandomWord() (result string)
- func Info(msg string)
- func InitLog(firstMsg string)
- func IsFilePath(s string) error
- func IsFlagGiven(name string) bool
- func IsTimezoneCN() bool
- func LogLevelStr(lvl int) string
- func LogLevelStrList() (sl []string)
- func MergeBuffers(bs [][]byte) (result []byte, duplicate bool)
- func ParseFlags()
- func PrintBuffers(bs [][]byte)
- func PutBuf(buf *bytes.Buffer)
- func PutBytes(bs []byte)
- func PutPacket(bs []byte)
- func RecoverBuffers(bs [][]byte, oldLen, old_sub_len int) [][]byte
- func ShrinkBuffers(bs [][]byte, all_len int, SingleBufLen int) int
- func StrToUUID(s string) (uuid [16]byte, err error)
- func TrimSlice[T any](a []T, deleteIndex int) []T
- func UUIDToStr(u [16]byte) string
- func Warn(msg string)
- func WrapFuncForPromptUI(f func(string) bool) func(string) error
- type ByteReader
- type ByteWriter
- type ChanCloser
- type DummyReadCloser
- type DummyWriteCloser
- type ErrBuffer
- type ErrInErr
- type Heap
- type MultiReader
- type MultiWriter
- type NumErr
- type RW
- type ReadSwitcher
- type ReadWrapper
- type SystemReadver
- type User
- type UserBus
- type UserConn
- type UserContainer
- type UserHaser
- type V2rayUser
- type WriteSwitcher
Constants ¶
const ( Log_debug = iota Log_info Log_warning Log_error //error一般用于输出 some 连接/客户端协议 错误, 但不致命 Log_dpanic Log_panic Log_fatal DefaultLL = Log_info )
const ( ProjectName = "v2ray_simple" ProjectPath = ProjectName + "/" )
const ( //即 Maximum transmission unit, 参照的是 Ethernet v2 的MTU; MTU int = 1500 //本作设定的最大包 长度大小,64k MaxPacketLen = 64 * 1024 )
Variables ¶
var ( ErrNotImplemented = 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") ErrShortRead = errors.New("short read") ErrHandled = errors.New("handled") ErrFailed = errors.New("failed") //最无脑的Err, 在能描述清楚错误时不要使用 ErrFailed )
var ( LogLevel int ZapLogger *zap.Logger //日志输出文件名称 LogOutFileName string )
LogLevel 值越小越唠叨, 废话越多,值越大打印的越少,见log_开头的常量;
我们的loglevel具体值 与 zap的 loglevel+1 的含义等价
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 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
func CanLogWarn ¶
func CanLogWarn(msg string) *zapcore.CheckedEntry
func CloneSlice ¶
func CloneSlice[T any](a []T) (r []T)
func GenerateUUIDStr ¶
func GenerateUUIDStr() string
func GetFilePath ¶
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. Same folder with exec file 2. Same folder of the source file, 一种可能 是 用于 go test等情况 3. Same folder of working folder
func GetGivenFlags ¶ added in v1.2.0
flag包有个奇葩的缺点, 没法一下子获取所有的已经配置的参数, 只能遍历; 如果我们有大量的参数需要判断是否给出过, 那么不如先提取到到map里。
实际上flag包的底层也是用的一个map, 但是它是私有的, 而且我们也不宜用unsafe暴露出来.
func GetMapSortedKeySlice ¶
func GetMapSortedKeySlice[K constraints.Ordered, V any](theMap map[K]V) []K
func GetPacket ¶
func GetPacket() []byte
建议在 Read net.Conn 时, 使用 GetPacket函数 获取到足够大的 []byte(MaxBufLen)
func GetRandomWord ¶
func GetRandomWord() (result string)
func IsFlagGiven ¶
func IsTimezoneCN ¶
func IsTimezoneCN() bool
func LogLevelStr ¶
func LogLevelStrList ¶
func LogLevelStrList() (sl []string)
func MergeBuffers ¶
如果 分配了新内存来 包含数据,则 duplicate ==true, 此时可以用PutPacket函数放回;
如果利用了原有的第一个[]byte, 则 duplicate==false。
如果 duplicate==false, 不要 使用 PutPacket等方法放入Pool;
因为 在更上级的调用会试图去把 整个bs 放入pool;
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 RecoverBuffers ¶
通过reslice 方式将 bs的长度以及 子 []byte 的长度 恢复至指定长度
func ShrinkBuffers ¶
削减buffer内部的子[]byte 到合适的长度;返回削减后 bs应有的长度.
Types ¶
type ByteReader ¶
bufio.Reader and bytes.Buffer implemented ByteReader
type ByteWriter ¶
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 DummyReadCloser ¶ added in v1.2.0
type DummyReadCloser struct {
ReadCount int
}
type DummyWriteCloser ¶ added in v1.2.0
type DummyWriteCloser struct {
WriteCount int
}
type Heap ¶
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 ¶
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]) 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).
type MultiReader ¶
type MultiReader interface { ReadBuffers() ([][]byte, error) //在底层没有实现readbuffers时,显然调用 ReadBuffers没有什么意义。 //所以我们通过 WillReadBuffersBenifit 方法 查询 调用是否有益。 WillReadBuffersBenifit() bool }
专门用于 grpc multiMode 的情况. 因为其他协议并没有能够加速的情形。
type MultiWriter ¶
因为 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 以及 grpc 的 multiConn
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
type ReadWrapper ¶ added in v1.2.0
optionally read from OptionalReader
func (*ReadWrapper) Close ¶ added in v1.2.0
func (rw *ReadWrapper) Close() error
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 User ¶ added in v1.2.0
type User interface { GetIdentityStr() string //每个user唯一,通过比较这个string 即可 判断两个User 是否相等 GetIdentityBytes() []byte }
User是一个唯一身份标识。
type UserConn ¶ added in v1.2.0
type UserConn interface { io.ReadWriter User GetProtocolVersion() int }
type UserContainer ¶ added in v1.2.0
type V2rayUser ¶ added in v1.2.0
type V2rayUser [16]byte
一种专门用于v2ray协议族(vmess/vless)的 用于标识用户的符号 , 实现 User 接口
func NewV2rayUser ¶ added in v1.2.0
func (V2rayUser) GetIdentityBytes ¶ added in v1.2.0
func (V2rayUser) GetIdentityStr ¶ added in v1.2.0
type WriteSwitcher ¶ added in v1.2.0
type WriteSwitcher struct {
Old, New io.Writer //non-nil
SwitchChan chan struct{} //non-nil
io.Closer
}
先向Old写,若SwitchChan被关闭, 改向New写
func (*WriteSwitcher) Close ¶ added in v1.2.0
func (d *WriteSwitcher) Close() error