Chore: split component to transport
This commit is contained in:
18
transport/ssr/tools/bufPool.go
Normal file
18
transport/ssr/tools/bufPool.go
Normal file
@ -0,0 +1,18 @@
|
||||
package tools
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"math/rand"
|
||||
"sync"
|
||||
|
||||
"github.com/Dreamacro/clash/common/pool"
|
||||
)
|
||||
|
||||
var BufPool = sync.Pool{New: func() interface{} { return &bytes.Buffer{} }}
|
||||
|
||||
func AppendRandBytes(b *bytes.Buffer, length int) {
|
||||
randBytes := pool.Get(length)
|
||||
defer pool.Put(randBytes)
|
||||
rand.Read(randBytes)
|
||||
b.Write(randBytes)
|
||||
}
|
33
transport/ssr/tools/crypto.go
Normal file
33
transport/ssr/tools/crypto.go
Normal file
@ -0,0 +1,33 @@
|
||||
package tools
|
||||
|
||||
import (
|
||||
"crypto/hmac"
|
||||
"crypto/md5"
|
||||
"crypto/sha1"
|
||||
)
|
||||
|
||||
const HmacSHA1Len = 10
|
||||
|
||||
func HmacMD5(key, data []byte) []byte {
|
||||
hmacMD5 := hmac.New(md5.New, key)
|
||||
hmacMD5.Write(data)
|
||||
return hmacMD5.Sum(nil)
|
||||
}
|
||||
|
||||
func HmacSHA1(key, data []byte) []byte {
|
||||
hmacSHA1 := hmac.New(sha1.New, key)
|
||||
hmacSHA1.Write(data)
|
||||
return hmacSHA1.Sum(nil)
|
||||
}
|
||||
|
||||
func MD5Sum(b []byte) []byte {
|
||||
h := md5.New()
|
||||
h.Write(b)
|
||||
return h.Sum(nil)
|
||||
}
|
||||
|
||||
func SHA1Sum(b []byte) []byte {
|
||||
h := sha1.New()
|
||||
h.Write(b)
|
||||
return h.Sum(nil)
|
||||
}
|
57
transport/ssr/tools/random.go
Normal file
57
transport/ssr/tools/random.go
Normal file
@ -0,0 +1,57 @@
|
||||
package tools
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
|
||||
"github.com/Dreamacro/clash/common/pool"
|
||||
)
|
||||
|
||||
// XorShift128Plus - a pseudorandom number generator
|
||||
type XorShift128Plus struct {
|
||||
s [2]uint64
|
||||
}
|
||||
|
||||
func (r *XorShift128Plus) Next() uint64 {
|
||||
x := r.s[0]
|
||||
y := r.s[1]
|
||||
r.s[0] = y
|
||||
x ^= x << 23
|
||||
x ^= y ^ (x >> 17) ^ (y >> 26)
|
||||
r.s[1] = x
|
||||
return x + y
|
||||
}
|
||||
|
||||
func (r *XorShift128Plus) InitFromBin(bin []byte) {
|
||||
var full []byte
|
||||
if len(bin) < 16 {
|
||||
full := pool.Get(16)[:0]
|
||||
defer pool.Put(full)
|
||||
full = append(full, bin...)
|
||||
for len(full) < 16 {
|
||||
full = append(full, 0)
|
||||
}
|
||||
} else {
|
||||
full = bin
|
||||
}
|
||||
r.s[0] = binary.LittleEndian.Uint64(full[:8])
|
||||
r.s[1] = binary.LittleEndian.Uint64(full[8:16])
|
||||
}
|
||||
|
||||
func (r *XorShift128Plus) InitFromBinAndLength(bin []byte, length int) {
|
||||
var full []byte
|
||||
if len(bin) < 16 {
|
||||
full := pool.Get(16)[:0]
|
||||
defer pool.Put(full)
|
||||
full = append(full, bin...)
|
||||
for len(full) < 16 {
|
||||
full = append(full, 0)
|
||||
}
|
||||
}
|
||||
full = bin
|
||||
binary.LittleEndian.PutUint16(full, uint16(length))
|
||||
r.s[0] = binary.LittleEndian.Uint64(full[:8])
|
||||
r.s[1] = binary.LittleEndian.Uint64(full[8:16])
|
||||
for i := 0; i < 4; i++ {
|
||||
r.Next()
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user