chore: code cleanup

This commit is contained in:
wwqgtxx 2025-08-14 23:48:59 +08:00
parent 089766b285
commit 946b4025df
3 changed files with 35 additions and 36 deletions

View File

@ -105,26 +105,20 @@ func (vc *Conn) sendRequest(p []byte) bool {
} }
} }
var buffer *buf.Buffer requestLen := 1 // protocol version
if vc.IsXTLSVisionEnabled() { requestLen += 16 // UUID
buffer = buf.New() requestLen += 1 // addons length
defer buffer.Release() requestLen += len(addonsBytes)
} else { requestLen += 1 // command
requestLen := 1 // protocol version if !vc.dst.Mux {
requestLen += 16 // UUID requestLen += 2 // port
requestLen += 1 // addons length requestLen += 1 // addr type
requestLen += len(addonsBytes) requestLen += len(vc.dst.Addr)
requestLen += 1 // command
if !vc.dst.Mux {
requestLen += 2 // port
requestLen += 1 // addr type
requestLen += len(vc.dst.Addr)
}
requestLen += len(p)
buffer = buf.NewSize(requestLen)
defer buffer.Release()
} }
requestLen += len(p)
buffer := buf.NewSize(requestLen)
defer buffer.Release()
buf.Must( buf.Must(
buffer.WriteByte(Version), // protocol version buffer.WriteByte(Version), // protocol version
@ -182,10 +176,6 @@ func (vc *Conn) NeedHandshake() bool {
return vc.needHandshake return vc.needHandshake
} }
func (vc *Conn) IsXTLSVisionEnabled() bool {
return vc.addons != nil && vc.addons.Flow == XRV
}
// newConn return a Conn instance // newConn return a Conn instance
func newConn(conn net.Conn, client *Client, dst *DstAddr) (net.Conn, error) { func newConn(conn net.Conn, client *Client, dst *DstAddr) (net.Conn, error) {
c := &Conn{ c := &Conn{

View File

@ -3,7 +3,6 @@ package vision
import ( import (
"bytes" "bytes"
"crypto/subtle" "crypto/subtle"
gotls "crypto/tls"
"encoding/binary" "encoding/binary"
"errors" "errors"
"fmt" "fmt"
@ -12,7 +11,6 @@ import (
"github.com/metacubex/mihomo/common/buf" "github.com/metacubex/mihomo/common/buf"
N "github.com/metacubex/mihomo/common/net" N "github.com/metacubex/mihomo/common/net"
tlsC "github.com/metacubex/mihomo/component/tls"
"github.com/metacubex/mihomo/log" "github.com/metacubex/mihomo/log"
"github.com/gofrs/uuid/v5" "github.com/gofrs/uuid/v5"
@ -181,17 +179,10 @@ func (vc *Conn) WriteBuffer(buffer *buf.Buffer) (err error) {
buffer.Release() buffer.Release()
return err return err
} }
switch underlying := vc.tlsConn.(type) { err = vc.checkTLSVersion()
case *gotls.Conn: if err != nil {
if underlying.ConnectionState().Version != gotls.VersionTLS13 { buffer.Release()
buffer.Release() return err
return ErrNotTLS13
}
case *tlsC.UConn:
if underlying.ConnectionState().Version != tlsC.VersionTLS13 {
buffer.Release()
return ErrNotTLS13
}
} }
vc.tlsConn = nil vc.tlsConn = nil
return nil return nil

View File

@ -67,3 +67,21 @@ func NewConn(conn connWithUpstream, userUUID *uuid.UUID) (*Conn, error) {
c.rawInput = (*bytes.Buffer)(unsafe.Add(p, r.Offset)) c.rawInput = (*bytes.Buffer)(unsafe.Add(p, r.Offset))
return c, nil return c, nil
} }
func (vc *Conn) checkTLSVersion() error {
switch underlying := vc.tlsConn.(type) {
case *gotls.Conn:
if underlying.ConnectionState().Version != gotls.VersionTLS13 {
return ErrNotTLS13
}
case *tlsC.Conn:
if underlying.ConnectionState().Version != tlsC.VersionTLS13 {
return ErrNotTLS13
}
case *tlsC.UConn:
if underlying.ConnectionState().Version != tlsC.VersionTLS13 {
return ErrNotTLS13
}
}
return nil
}