mihomo/transport/vless/conn.go

211 lines
4.1 KiB
Go
Raw Normal View History

2021-11-17 15:00:32 +08:00
package vless
import (
"encoding/binary"
"errors"
"fmt"
"io"
"net"
"sync"
"time"
2021-11-17 15:00:32 +08:00
"github.com/Dreamacro/clash/common/buf"
N "github.com/Dreamacro/clash/common/net"
2021-11-17 15:00:32 +08:00
"github.com/gofrs/uuid"
xtls "github.com/xtls/go"
"google.golang.org/protobuf/proto"
)
type Conn struct {
N.ExtendedConn
2021-11-17 15:00:32 +08:00
dst *DstAddr
id *uuid.UUID
addons *Addons
received bool
handshake chan struct{}
handshakeMutex sync.Mutex
err error
2021-11-17 15:00:32 +08:00
}
func (vc *Conn) Read(b []byte) (int, error) {
if vc.received {
2023-01-16 09:42:03 +08:00
return vc.ExtendedConn.Read(b)
2021-11-17 15:00:32 +08:00
}
if err := vc.recvResponse(); err != nil {
return 0, err
}
vc.received = true
2023-01-16 09:42:03 +08:00
return vc.ExtendedConn.Read(b)
2021-11-17 15:00:32 +08:00
}
2023-01-16 09:42:03 +08:00
func (vc *Conn) ReadBuffer(buffer *buf.Buffer) error {
if vc.received {
return vc.ExtendedConn.ReadBuffer(buffer)
}
2021-11-17 15:00:32 +08:00
2023-01-16 09:42:03 +08:00
if err := vc.recvResponse(); err != nil {
return err
}
vc.received = true
return vc.ExtendedConn.ReadBuffer(buffer)
}
2021-11-17 15:00:32 +08:00
func (vc *Conn) Write(p []byte) (int, error) {
select {
case <-vc.handshake:
default:
err := vc.sendRequest(p)
if err != nil {
return 0, err
}
}
return vc.ExtendedConn.Write(p)
}
func (vc *Conn) WriteBuffer(buffer *buf.Buffer) error {
select {
case <-vc.handshake:
default:
err := vc.sendRequest(buffer.Bytes())
if err != nil {
return err
}
}
return vc.ExtendedConn.WriteBuffer(buffer)
}
func (vc *Conn) sendRequest(p []byte) (err error) {
vc.handshakeMutex.Lock()
defer vc.handshakeMutex.Unlock()
select {
case <-vc.handshake:
return vc.err
default:
}
defer close(vc.handshake)
2023-01-16 09:42:03 +08:00
requestLen := 1 // protocol version
requestLen += 16 // UUID
requestLen += 1 // addons length
var addonsBytes []byte
2021-11-17 15:00:32 +08:00
if vc.addons != nil {
2023-01-16 09:42:03 +08:00
addonsBytes, err = proto.Marshal(vc.addons)
2021-11-17 15:00:32 +08:00
if err != nil {
return err
}
}
2023-01-16 09:42:03 +08:00
requestLen += len(addonsBytes)
requestLen += 1 // command
if !vc.dst.Mux {
requestLen += 2 // port
requestLen += 1 // addr type
requestLen += len(vc.dst.Addr)
}
requestLen += len(p)
2023-01-16 09:42:03 +08:00
_buffer := buf.StackNewSize(requestLen)
defer buf.KeepAlive(_buffer)
buffer := buf.Dup(_buffer)
2023-01-16 09:42:03 +08:00
defer buffer.Release()
buf.Must(
buffer.WriteByte(Version), // protocol version
buf.Error(buffer.Write(vc.id.Bytes())), // 16 bytes of uuid
2023-01-16 09:42:03 +08:00
buffer.WriteByte(byte(len(addonsBytes))),
buf.Error(buffer.Write(addonsBytes)),
2023-01-16 09:42:03 +08:00
)
2021-11-17 15:00:32 +08:00
if vc.dst.Mux {
buf.Must(buffer.WriteByte(CommandMux))
2021-11-17 15:00:32 +08:00
} else {
if vc.dst.UDP {
buf.Must(buffer.WriteByte(CommandUDP))
} else {
buf.Must(buffer.WriteByte(CommandTCP))
}
2021-11-17 15:00:32 +08:00
2023-01-16 09:42:03 +08:00
binary.BigEndian.PutUint16(buffer.Extend(2), vc.dst.Port)
buf.Must(
2023-01-16 09:42:03 +08:00
buffer.WriteByte(vc.dst.AddrType),
buf.Error(buffer.Write(vc.dst.Addr)),
2023-01-16 09:42:03 +08:00
)
}
2021-11-17 15:00:32 +08:00
buf.Must(buf.Error(buffer.Write(p)))
2023-01-16 09:42:03 +08:00
_, err = vc.ExtendedConn.Write(buffer.Bytes())
return
2021-11-17 15:00:32 +08:00
}
func (vc *Conn) recvResponse() error {
2023-01-16 09:42:03 +08:00
var buf [1]byte
_, vc.err = io.ReadFull(vc.ExtendedConn, buf[:])
if vc.err != nil {
return vc.err
2021-11-17 15:00:32 +08:00
}
if buf[0] != Version {
return errors.New("unexpected response version")
}
_, vc.err = io.ReadFull(vc.ExtendedConn, buf[:])
if vc.err != nil {
return vc.err
2021-11-17 15:00:32 +08:00
}
length := int64(buf[0])
if length != 0 { // addon data length > 0
2023-01-16 09:42:03 +08:00
io.CopyN(io.Discard, vc.ExtendedConn, length) // just discard
2021-11-17 15:00:32 +08:00
}
return nil
}
2023-01-16 09:42:03 +08:00
func (vc *Conn) Upstream() any {
return vc.ExtendedConn
}
2021-11-17 15:00:32 +08:00
// newConn return a Conn instance
func newConn(conn net.Conn, client *Client, dst *DstAddr) (*Conn, error) {
c := &Conn{
ExtendedConn: N.NewExtendedConn(conn),
2023-01-16 09:42:03 +08:00
id: client.uuid,
dst: dst,
handshake: make(chan struct{}),
2021-11-17 15:00:32 +08:00
}
if !dst.UDP && client.Addons != nil {
switch client.Addons.Flow {
case XRO, XRD, XRS:
if xtlsConn, ok := conn.(*xtls.Conn); ok {
xtlsConn.RPRX = true
xtlsConn.SHOW = client.XTLSShow
xtlsConn.MARK = "XTLS"
if client.Addons.Flow == XRS {
client.Addons.Flow = XRD
}
if client.Addons.Flow == XRD {
xtlsConn.DirectMode = true
}
c.addons = client.Addons
} else {
return nil, fmt.Errorf("failed to use %s, maybe \"security\" is not \"xtls\"", client.Addons.Flow)
}
}
}
go func() {
select {
case <-c.handshake:
case <-time.After(200 * time.Millisecond):
_ = c.sendRequest(nil)
}
}()
2021-11-17 15:00:32 +08:00
return c, nil
}