Files
v2ray_simple/utils/crypto.go
e1732a364fed 3c754a0a89 feat: 实现 shadowTls v2;修订示例,文档,代码; 添加shadowTls的示例文件
shadowTls v2时客户端自动使用utls,且增强了探测防御

proxy.SetCommonReadTimeout -> netLayer.SetCommonReadTimeout

tlsLayer 配置也使用Extra,目前用于 shadowTls
2022-12-22 17:06:35 +08:00

74 lines
1.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package utils
import (
"crypto/hmac"
"crypto/sha1"
"hash"
"io"
"runtime"
)
// 有些系统对aes支持不好有些支持好。SystemAutoUseAes若为true则说明支持很好使用aes作为加密算法速度最佳。
const SystemAutoUseAes = runtime.GOARCH == "amd64" || runtime.GOARCH == "s390x" || runtime.GOARCH == "arm64"
type HashReader struct {
io.Reader
hmac hash.Hash
}
func NewHashReader(conn io.Reader, key []byte) *HashReader {
return &HashReader{
conn,
hmac.New(sha1.New, key),
}
}
func (c *HashReader) Read(b []byte) (n int, err error) {
n, err = c.Reader.Read(b)
if err != nil {
return
}
_, err = c.hmac.Write(b[:n])
return
}
func (c *HashReader) Sum() []byte {
return c.hmac.Sum(nil)[:8]
}
type HashWriter struct {
io.Writer
hmac hash.Hash
written bool
}
func NewHashWriter(conn io.Writer, key []byte) *HashWriter {
return &HashWriter{
Writer: conn,
hmac: hmac.New(sha1.New, key),
}
}
func (c *HashWriter) Write(p []byte) (n int, err error) {
if c.hmac != nil {
c.hmac.Write(p)
c.written = true
}
return c.Writer.Write(p)
}
func (c *HashWriter) Sum() []byte {
return c.hmac.Sum(nil)[:8]
}
func (c *HashWriter) StopHashing() {
c.hmac = nil
c.written = false
}
// Has the hash been written
func (c *HashWriter) Written() bool {
return c.written
}