chore: cleanup vless code

This commit is contained in:
wwqgtxx 2025-09-08 15:58:54 +08:00
parent 0336d64e52
commit 50e1afd963
4 changed files with 47 additions and 67 deletions

View File

@ -5,7 +5,6 @@ import (
"errors" "errors"
"io" "io"
"net" "net"
"sync"
"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"
@ -16,17 +15,12 @@ import (
) )
type Conn struct { type Conn struct {
N.ExtendedWriter N.ExtendedConn
N.ExtendedReader
net.Conn
dst *DstAddr dst *DstAddr
id *uuid.UUID id uuid.UUID
addons *Addons addons *Addons
received bool received bool
sent bool
handshakeMutex sync.Mutex
needHandshake bool
err error
} }
func (vc *Conn) Read(b []byte) (int, error) { func (vc *Conn) Read(b []byte) (int, error) {
@ -36,7 +30,7 @@ func (vc *Conn) Read(b []byte) (int, error) {
} }
vc.received = true vc.received = true
} }
return vc.ExtendedReader.Read(b) return vc.ExtendedConn.Read(b)
} }
func (vc *Conn) ReadBuffer(buffer *buf.Buffer) error { func (vc *Conn) ReadBuffer(buffer *buf.Buffer) error {
@ -46,58 +40,39 @@ func (vc *Conn) ReadBuffer(buffer *buf.Buffer) error {
} }
vc.received = true vc.received = true
} }
return vc.ExtendedReader.ReadBuffer(buffer) return vc.ExtendedConn.ReadBuffer(buffer)
} }
func (vc *Conn) Write(p []byte) (int, error) { func (vc *Conn) Write(p []byte) (int, error) {
if vc.needHandshake { if !vc.sent {
vc.handshakeMutex.Lock() if err := vc.sendRequest(p); err != nil {
if vc.needHandshake { return 0, err
vc.needHandshake = false
if vc.sendRequest(p) {
vc.handshakeMutex.Unlock()
if vc.err != nil {
return 0, vc.err
} }
return len(p), vc.err vc.sent = true
} return len(p), nil
if vc.err != nil {
vc.handshakeMutex.Unlock()
return 0, vc.err
}
}
vc.handshakeMutex.Unlock()
} }
return vc.ExtendedWriter.Write(p) return vc.ExtendedConn.Write(p)
} }
func (vc *Conn) WriteBuffer(buffer *buf.Buffer) error { func (vc *Conn) WriteBuffer(buffer *buf.Buffer) error {
if vc.needHandshake { if !vc.sent {
vc.handshakeMutex.Lock() if err := vc.sendRequest(buffer.Bytes()); err != nil {
if vc.needHandshake { return err
vc.needHandshake = false
if vc.sendRequest(buffer.Bytes()) {
vc.handshakeMutex.Unlock()
return vc.err
} }
if vc.err != nil { vc.sent = true
vc.handshakeMutex.Unlock() return nil
return vc.err
}
}
vc.handshakeMutex.Unlock()
} }
return vc.ExtendedWriter.WriteBuffer(buffer) return vc.ExtendedConn.WriteBuffer(buffer)
} }
func (vc *Conn) sendRequest(p []byte) bool { func (vc *Conn) sendRequest(p []byte) (err error) {
var addonsBytes []byte var addonsBytes []byte
if vc.addons != nil { if vc.addons != nil {
addonsBytes, vc.err = proto.Marshal(vc.addons) addonsBytes, err = proto.Marshal(vc.addons)
if vc.err != nil { if err != nil {
return true return
} }
} }
@ -141,15 +116,15 @@ func (vc *Conn) sendRequest(p []byte) bool {
buf.Must(buf.Error(buffer.Write(p))) buf.Must(buf.Error(buffer.Write(p)))
_, vc.err = vc.ExtendedWriter.Write(buffer.Bytes()) _, err = vc.ExtendedConn.Write(buffer.Bytes())
return true return
} }
func (vc *Conn) recvResponse() error { func (vc *Conn) recvResponse() (err error) {
var buffer [2]byte var buffer [2]byte
_, vc.err = io.ReadFull(vc.ExtendedReader, buffer[:]) _, err = io.ReadFull(vc.ExtendedConn, buffer[:])
if vc.err != nil { if err != nil {
return vc.err return err
} }
if buffer[0] != Version { if buffer[0] != Version {
@ -158,29 +133,35 @@ func (vc *Conn) recvResponse() error {
length := int64(buffer[1]) length := int64(buffer[1])
if length != 0 { // addon data length > 0 if length != 0 { // addon data length > 0
io.CopyN(io.Discard, vc.ExtendedReader, length) // just discard io.CopyN(io.Discard, vc.ExtendedConn, length) // just discard
} }
return nil return
} }
func (vc *Conn) Upstream() any { func (vc *Conn) Upstream() any {
return vc.Conn return vc.ExtendedConn
}
func (vc *Conn) ReaderReplaceable() bool {
return vc.received
}
func (vc *Conn) WriterReplaceable() bool {
return vc.sent
} }
func (vc *Conn) NeedHandshake() bool { func (vc *Conn) NeedHandshake() bool {
return vc.needHandshake return !vc.sent
} }
// 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{
ExtendedReader: N.NewExtendedReader(conn), ExtendedConn: N.NewExtendedConn(conn),
ExtendedWriter: N.NewExtendedWriter(conn),
Conn: conn,
id: client.uuid, id: client.uuid,
addons: client.Addons,
dst: dst, dst: dst,
needHandshake: true,
} }
if client.Addons != nil { if client.Addons != nil {
@ -190,7 +171,6 @@ func newConn(conn net.Conn, client *Client, dst *DstAddr) (net.Conn, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
c.addons = client.Addons
return visionConn, nil return visionConn, nil
} }
} }

View File

@ -24,7 +24,7 @@ type Conn struct {
net.Conn // should be *vless.Conn net.Conn // should be *vless.Conn
N.ExtendedReader N.ExtendedReader
N.ExtendedWriter N.ExtendedWriter
userUUID *uuid.UUID userUUID uuid.UUID
// [*tls.Conn] or other tls-like [net.Conn]'s internal variables // [*tls.Conn] or other tls-like [net.Conn]'s internal variables
netConn net.Conn // tlsConn.NetConn() netConn net.Conn // tlsConn.NetConn()

View File

@ -21,7 +21,7 @@ import (
var ErrNotHandshakeComplete = errors.New("tls connection not handshake complete") var ErrNotHandshakeComplete = errors.New("tls connection not handshake complete")
var ErrNotTLS13 = errors.New("XTLS Vision based on TLS 1.3 outer connection") var ErrNotTLS13 = errors.New("XTLS Vision based on TLS 1.3 outer connection")
func NewConn(conn net.Conn, tlsConn net.Conn, userUUID *uuid.UUID) (*Conn, error) { func NewConn(conn net.Conn, tlsConn net.Conn, userUUID uuid.UUID) (*Conn, error) {
c := &Conn{ c := &Conn{
ExtendedReader: N.NewExtendedReader(conn), ExtendedReader: N.NewExtendedReader(conn),
ExtendedWriter: N.NewExtendedWriter(conn), ExtendedWriter: N.NewExtendedWriter(conn),

View File

@ -42,7 +42,7 @@ type DstAddr struct {
// Client is vless connection generator // Client is vless connection generator
type Client struct { type Client struct {
uuid *uuid.UUID uuid uuid.UUID
Addons *Addons Addons *Addons
} }
@ -63,7 +63,7 @@ func NewClient(uuidStr string, addons *Addons) (*Client, error) {
} }
return &Client{ return &Client{
uuid: &uid, uuid: uid,
Addons: addons, Addons: addons,
}, nil }, nil
} }