mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2025-12-24 03:40:07 +08:00
chore: change vless encryption code to our style
This commit is contained in:
parent
cdd02a90c3
commit
5c73025b53
@ -10,19 +10,12 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"math/big"
|
"math/big"
|
||||||
"net"
|
"net"
|
||||||
"strings"
|
|
||||||
"sync"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/metacubex/blake3"
|
"github.com/metacubex/blake3"
|
||||||
|
"github.com/metacubex/mihomo/common/pool"
|
||||||
)
|
)
|
||||||
|
|
||||||
var OutBytesPool = sync.Pool{
|
|
||||||
New: func() any {
|
|
||||||
return make([]byte, 5+8192+16)
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
type CommonConn struct {
|
type CommonConn struct {
|
||||||
net.Conn
|
net.Conn
|
||||||
Client *ClientInstance
|
Client *ClientInstance
|
||||||
@ -46,8 +39,8 @@ func (c *CommonConn) Write(b []byte) (int, error) {
|
|||||||
if len(b) == 0 {
|
if len(b) == 0 {
|
||||||
return 0, nil
|
return 0, nil
|
||||||
}
|
}
|
||||||
outBytes := OutBytesPool.Get().([]byte)
|
outBytes := pool.Get(5 + 8192 + 16)
|
||||||
defer OutBytesPool.Put(outBytes)
|
defer pool.Put(outBytes)
|
||||||
for n := 0; n < len(b); {
|
for n := 0; n < len(b); {
|
||||||
b := b[n:]
|
b := b[n:]
|
||||||
if len(b) > 8192 {
|
if len(b) > 8192 {
|
||||||
@ -107,7 +100,7 @@ func (c *CommonConn) Read(b []byte) (int, error) {
|
|||||||
}
|
}
|
||||||
l, err := DecodeHeader(c.PeerInBytes[:5]) // l: 17~17000
|
l, err := DecodeHeader(c.PeerInBytes[:5]) // l: 17~17000
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if c.Client != nil && strings.Contains(err.Error(), "invalid header: ") { // client's 0-RTT
|
if c.Client != nil && errors.Is(err, ErrInvalidHeader) { // client's 0-RTT
|
||||||
c.Client.RWLock.Lock()
|
c.Client.RWLock.Lock()
|
||||||
if bytes.HasPrefix(c.UnitedKey, c.Client.PfsKey) {
|
if bytes.HasPrefix(c.UnitedKey, c.Client.PfsKey) {
|
||||||
c.Client.Expire = time.Now() // expired
|
c.Client.Expire = time.Now() // expired
|
||||||
@ -200,13 +193,15 @@ func EncodeHeader(h []byte, l int) {
|
|||||||
h[4] = byte(l)
|
h[4] = byte(l)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var ErrInvalidHeader = errors.New("invalid header")
|
||||||
|
|
||||||
func DecodeHeader(h []byte) (l int, err error) {
|
func DecodeHeader(h []byte) (l int, err error) {
|
||||||
l = int(h[3])<<8 | int(h[4])
|
l = int(h[3])<<8 | int(h[4])
|
||||||
if h[0] != 23 || h[1] != 3 || h[2] != 3 {
|
if h[0] != 23 || h[1] != 3 || h[2] != 3 {
|
||||||
l = 0
|
l = 0
|
||||||
}
|
}
|
||||||
if l < 17 || l > 17000 { // TODO: TLSv1.3 max length
|
if l < 17 || l > 17000 { // TODO: TLSv1.3 max length
|
||||||
err = fmt.Errorf("invalid header: %v", h[:5]) // DO NOT CHANGE: relied by client's Read()
|
err = fmt.Errorf("%w: %v", ErrInvalidHeader, h[:5]) // DO NOT CHANGE: relied by client's Read()
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user