2021-11-17 07:00:32 +00:00
|
|
|
package vless
|
|
|
|
|
|
|
|
import (
|
2023-02-25 05:12:19 +00:00
|
|
|
"bytes"
|
|
|
|
"crypto/subtle"
|
|
|
|
gotls "crypto/tls"
|
2021-11-17 07:00:32 +00:00
|
|
|
"encoding/binary"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net"
|
2023-02-25 05:12:19 +00:00
|
|
|
"reflect"
|
2023-02-10 02:03:37 +00:00
|
|
|
"sync"
|
2023-02-25 05:12:19 +00:00
|
|
|
"unsafe"
|
2021-11-17 07:00:32 +00:00
|
|
|
|
2023-01-16 02:50:31 +00:00
|
|
|
"github.com/Dreamacro/clash/common/buf"
|
|
|
|
N "github.com/Dreamacro/clash/common/net"
|
2023-02-25 05:12:19 +00:00
|
|
|
tlsC "github.com/Dreamacro/clash/component/tls"
|
|
|
|
"github.com/Dreamacro/clash/log"
|
2023-01-16 02:50:31 +00:00
|
|
|
|
2021-11-17 07:00:32 +00:00
|
|
|
"github.com/gofrs/uuid"
|
2023-02-25 05:12:19 +00:00
|
|
|
utls "github.com/sagernet/utls"
|
2021-11-17 07:00:32 +00:00
|
|
|
xtls "github.com/xtls/go"
|
|
|
|
"google.golang.org/protobuf/proto"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Conn struct {
|
2023-02-25 05:12:19 +00:00
|
|
|
N.ExtendedWriter
|
|
|
|
N.ExtendedReader
|
|
|
|
net.Conn
|
2021-11-17 07:00:32 +00:00
|
|
|
dst *DstAddr
|
|
|
|
id *uuid.UUID
|
|
|
|
addons *Addons
|
|
|
|
received bool
|
2023-02-10 02:03:37 +00:00
|
|
|
|
|
|
|
handshake chan struct{}
|
|
|
|
handshakeMutex sync.Mutex
|
|
|
|
err error
|
2023-02-25 05:12:19 +00:00
|
|
|
|
|
|
|
tlsConn net.Conn
|
|
|
|
input *bytes.Reader
|
|
|
|
rawInput *bytes.Buffer
|
|
|
|
|
|
|
|
packetsToFilter int
|
|
|
|
isTLS bool
|
|
|
|
isTLS12orAbove bool
|
|
|
|
enableXTLS bool
|
|
|
|
cipher uint16
|
|
|
|
remainingServerHello uint16
|
|
|
|
readRemainingContent int
|
|
|
|
readRemainingPadding int
|
|
|
|
readProcess bool
|
|
|
|
readFilterUUID bool
|
|
|
|
readLastCommand byte
|
|
|
|
writeFilterApplicationData bool
|
|
|
|
writeDirect bool
|
2021-11-17 07:00:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (vc *Conn) Read(b []byte) (int, error) {
|
|
|
|
if vc.received {
|
2023-02-25 05:12:19 +00:00
|
|
|
if vc.readProcess {
|
|
|
|
buffer := buf.With(b)
|
|
|
|
err := vc.ReadBuffer(buffer)
|
|
|
|
return buffer.Len(), err
|
|
|
|
}
|
|
|
|
return vc.ExtendedReader.Read(b)
|
2021-11-17 07:00:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := vc.recvResponse(); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
vc.received = true
|
2023-02-25 05:12:19 +00:00
|
|
|
return vc.Read(b)
|
2021-11-17 07:00:32 +00:00
|
|
|
}
|
|
|
|
|
2023-01-16 01:42:03 +00:00
|
|
|
func (vc *Conn) ReadBuffer(buffer *buf.Buffer) error {
|
|
|
|
if vc.received {
|
2023-02-25 05:12:19 +00:00
|
|
|
toRead := buffer.FreeBytes()
|
|
|
|
if vc.readRemainingContent > 0 {
|
|
|
|
if vc.readRemainingContent < buffer.FreeLen() {
|
|
|
|
toRead = toRead[:vc.readRemainingContent]
|
|
|
|
}
|
|
|
|
n, err := vc.ExtendedReader.Read(toRead)
|
|
|
|
buffer.Truncate(n)
|
|
|
|
vc.readRemainingContent -= n
|
|
|
|
vc.FilterTLS(toRead)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if vc.readRemainingPadding > 0 {
|
|
|
|
_, err := io.CopyN(io.Discard, vc.ExtendedReader, int64(vc.readRemainingPadding))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
vc.readRemainingPadding = 0
|
|
|
|
}
|
|
|
|
if vc.readProcess {
|
|
|
|
switch vc.readLastCommand {
|
|
|
|
case commandPaddingContinue:
|
|
|
|
//if vc.isTLS || vc.packetsToFilter > 0 {
|
|
|
|
headerUUIDLen := 0
|
|
|
|
if vc.readFilterUUID {
|
|
|
|
headerUUIDLen = uuid.Size
|
|
|
|
}
|
|
|
|
var header []byte
|
|
|
|
if need := headerUUIDLen + paddingHeaderLen; buffer.FreeLen() < need {
|
|
|
|
header = make([]byte, need)
|
|
|
|
} else {
|
|
|
|
header = buffer.FreeBytes()[:need]
|
|
|
|
}
|
|
|
|
_, err := io.ReadFull(vc.ExtendedReader, header)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
pos := 0
|
|
|
|
if vc.readFilterUUID {
|
|
|
|
vc.readFilterUUID = false
|
|
|
|
pos = uuid.Size
|
|
|
|
if subtle.ConstantTimeCompare(vc.id.Bytes(), header[:uuid.Size]) != 1 {
|
|
|
|
err = fmt.Errorf("XTLS Vision server responded unknown UUID: %s",
|
|
|
|
uuid.FromBytesOrNil(header[:uuid.Size]).String())
|
|
|
|
log.Errorln(err.Error())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
vc.readLastCommand = header[pos]
|
|
|
|
vc.readRemainingContent = int(binary.BigEndian.Uint16(header[pos+1:]))
|
|
|
|
vc.readRemainingPadding = int(binary.BigEndian.Uint16(header[pos+3:]))
|
|
|
|
log.Debugln("XTLS Vision read padding: command=%d, payloadLen=%d, paddingLen=%d",
|
|
|
|
vc.readLastCommand, vc.readRemainingContent, vc.readRemainingPadding)
|
|
|
|
return vc.ReadBuffer(buffer)
|
|
|
|
//}
|
|
|
|
case commandPaddingEnd:
|
|
|
|
vc.readProcess = false
|
|
|
|
return vc.ReadBuffer(buffer)
|
|
|
|
case commandPaddingDirect:
|
|
|
|
if vc.input != nil {
|
|
|
|
_, err := buffer.ReadFrom(vc.input)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if vc.input.Len() == 0 {
|
|
|
|
vc.input = nil
|
|
|
|
}
|
|
|
|
if buffer.IsFull() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if vc.rawInput != nil {
|
|
|
|
_, err := buffer.ReadFrom(vc.rawInput)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if vc.rawInput.Len() == 0 {
|
|
|
|
vc.rawInput = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if vc.input == nil && vc.rawInput == nil {
|
|
|
|
vc.readProcess = false
|
|
|
|
vc.ExtendedReader = N.NewExtendedReader(vc.Conn)
|
|
|
|
log.Debugln("XTLS Vision direct read start")
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
err := fmt.Errorf("XTLS Vision read unknown command: %d", vc.readLastCommand)
|
|
|
|
log.Debugln(err.Error())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return vc.ExtendedReader.ReadBuffer(buffer)
|
2023-01-16 01:42:03 +00:00
|
|
|
}
|
2021-11-17 07:00:32 +00:00
|
|
|
|
2023-01-16 01:42:03 +00:00
|
|
|
if err := vc.recvResponse(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
vc.received = true
|
2023-02-25 05:12:19 +00:00
|
|
|
return vc.ReadBuffer(buffer)
|
2023-01-16 01:42:03 +00:00
|
|
|
}
|
2021-11-17 07:00:32 +00:00
|
|
|
|
2023-02-10 02:03:37 +00:00
|
|
|
func (vc *Conn) Write(p []byte) (int, error) {
|
|
|
|
select {
|
|
|
|
case <-vc.handshake:
|
|
|
|
default:
|
2023-02-11 07:13:17 +00:00
|
|
|
if vc.sendRequest(p) {
|
|
|
|
if vc.err != nil {
|
|
|
|
return 0, vc.err
|
|
|
|
}
|
|
|
|
return len(p), vc.err
|
|
|
|
}
|
|
|
|
if vc.err != nil {
|
|
|
|
return 0, vc.err
|
2023-02-10 02:03:37 +00:00
|
|
|
}
|
|
|
|
}
|
2023-02-25 05:12:19 +00:00
|
|
|
if vc.writeFilterApplicationData {
|
|
|
|
_buffer := buf.StackNew()
|
|
|
|
defer buf.KeepAlive(_buffer)
|
|
|
|
buffer := buf.Dup(_buffer)
|
|
|
|
defer buffer.Release()
|
|
|
|
buffer.Write(p)
|
|
|
|
err := vc.WriteBuffer(buffer)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return len(p), nil
|
|
|
|
}
|
|
|
|
return vc.ExtendedWriter.Write(p)
|
2023-02-10 02:03:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (vc *Conn) WriteBuffer(buffer *buf.Buffer) error {
|
|
|
|
select {
|
|
|
|
case <-vc.handshake:
|
|
|
|
default:
|
2023-02-11 07:13:17 +00:00
|
|
|
if vc.sendRequest(buffer.Bytes()) {
|
|
|
|
return vc.err
|
|
|
|
}
|
|
|
|
if vc.err != nil {
|
|
|
|
return vc.err
|
2023-02-10 02:03:37 +00:00
|
|
|
}
|
|
|
|
}
|
2023-02-25 05:12:19 +00:00
|
|
|
if vc.writeFilterApplicationData && vc.isTLS {
|
|
|
|
buffer2 := ReshapeBuffer(buffer)
|
|
|
|
defer buffer2.Release()
|
|
|
|
vc.FilterTLS(buffer.Bytes())
|
|
|
|
command := commandPaddingContinue
|
|
|
|
if buffer.Len() > 6 && bytes.Equal(buffer.To(3), tlsApplicationDataStart) || vc.packetsToFilter <= 0 {
|
|
|
|
command = commandPaddingEnd
|
|
|
|
if vc.enableXTLS {
|
|
|
|
command = commandPaddingDirect
|
|
|
|
vc.writeDirect = true
|
|
|
|
}
|
|
|
|
vc.writeFilterApplicationData = false
|
|
|
|
}
|
|
|
|
ApplyPadding(buffer, command, nil)
|
|
|
|
err := vc.ExtendedWriter.WriteBuffer(buffer)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if vc.writeDirect {
|
|
|
|
vc.ExtendedWriter = N.NewExtendedWriter(vc.Conn)
|
|
|
|
log.Debugln("XTLS Vision direct write start")
|
|
|
|
//time.Sleep(10 * time.Millisecond)
|
|
|
|
}
|
|
|
|
if buffer2 != nil {
|
|
|
|
if vc.writeDirect {
|
|
|
|
return vc.ExtendedWriter.WriteBuffer(buffer2)
|
|
|
|
}
|
|
|
|
vc.FilterTLS(buffer2.Bytes())
|
|
|
|
command = commandPaddingContinue
|
|
|
|
if buffer2.Len() > 6 && bytes.Equal(buffer2.To(3), tlsApplicationDataStart) || vc.packetsToFilter <= 0 {
|
|
|
|
command = commandPaddingEnd
|
|
|
|
if vc.enableXTLS {
|
|
|
|
command = commandPaddingDirect
|
|
|
|
vc.writeDirect = true
|
|
|
|
}
|
|
|
|
vc.writeFilterApplicationData = false
|
|
|
|
}
|
|
|
|
ApplyPadding(buffer2, command, nil)
|
|
|
|
err = vc.ExtendedWriter.WriteBuffer(buffer2)
|
|
|
|
if vc.writeDirect {
|
|
|
|
vc.ExtendedWriter = N.NewExtendedWriter(vc.Conn)
|
|
|
|
log.Debugln("XTLS Vision direct write start")
|
|
|
|
//time.Sleep(10 * time.Millisecond)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
/*if vc.writeDirect {
|
|
|
|
log.Debugln("XTLS Vision Direct write, payloadLen=%d", buffer.Len())
|
|
|
|
}*/
|
|
|
|
return vc.ExtendedWriter.WriteBuffer(buffer)
|
2023-02-10 02:03:37 +00:00
|
|
|
}
|
|
|
|
|
2023-02-11 07:13:17 +00:00
|
|
|
func (vc *Conn) sendRequest(p []byte) bool {
|
2023-02-10 02:03:37 +00:00
|
|
|
vc.handshakeMutex.Lock()
|
|
|
|
defer vc.handshakeMutex.Unlock()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-vc.handshake:
|
2023-02-11 07:13:17 +00:00
|
|
|
// The handshake has been completed before.
|
|
|
|
// So return false to remind the caller.
|
|
|
|
return false
|
2023-02-10 02:03:37 +00:00
|
|
|
default:
|
|
|
|
}
|
|
|
|
defer close(vc.handshake)
|
|
|
|
|
2023-01-16 01:42:03 +00:00
|
|
|
var addonsBytes []byte
|
2021-11-17 07:00:32 +00:00
|
|
|
if vc.addons != nil {
|
2023-02-11 07:13:17 +00:00
|
|
|
addonsBytes, vc.err = proto.Marshal(vc.addons)
|
|
|
|
if vc.err != nil {
|
|
|
|
return true
|
2021-11-17 07:00:32 +00:00
|
|
|
}
|
|
|
|
}
|
2023-02-25 05:12:19 +00:00
|
|
|
isVision := vc.IsXTLSVisionEnabled()
|
2023-02-10 02:03:37 +00:00
|
|
|
|
2023-02-25 05:12:19 +00:00
|
|
|
var buffer *buf.Buffer
|
|
|
|
if isVision {
|
|
|
|
_buffer := buf.StackNew()
|
|
|
|
defer buf.KeepAlive(_buffer)
|
|
|
|
buffer = buf.Dup(_buffer)
|
|
|
|
defer buffer.Release()
|
|
|
|
} else {
|
|
|
|
requestLen := 1 // protocol version
|
|
|
|
requestLen += 16 // UUID
|
|
|
|
requestLen += 1 // addons length
|
|
|
|
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)
|
|
|
|
|
|
|
|
_buffer := buf.StackNewSize(requestLen)
|
|
|
|
defer buf.KeepAlive(_buffer)
|
|
|
|
buffer = buf.Dup(_buffer)
|
|
|
|
defer buffer.Release()
|
|
|
|
}
|
2023-01-16 01:42:03 +00:00
|
|
|
|
2023-01-16 02:50:31 +00:00
|
|
|
buf.Must(
|
|
|
|
buffer.WriteByte(Version), // protocol version
|
|
|
|
buf.Error(buffer.Write(vc.id.Bytes())), // 16 bytes of uuid
|
2023-01-16 01:42:03 +00:00
|
|
|
buffer.WriteByte(byte(len(addonsBytes))),
|
2023-01-16 02:50:31 +00:00
|
|
|
buf.Error(buffer.Write(addonsBytes)),
|
2023-01-16 01:42:03 +00:00
|
|
|
)
|
2021-11-17 07:00:32 +00:00
|
|
|
|
2023-01-11 14:01:15 +00:00
|
|
|
if vc.dst.Mux {
|
2023-01-16 02:50:31 +00:00
|
|
|
buf.Must(buffer.WriteByte(CommandMux))
|
2021-11-17 07:00:32 +00:00
|
|
|
} else {
|
2023-01-11 14:01:15 +00:00
|
|
|
if vc.dst.UDP {
|
2023-01-16 02:50:31 +00:00
|
|
|
buf.Must(buffer.WriteByte(CommandUDP))
|
2023-01-11 14:01:15 +00:00
|
|
|
} else {
|
2023-01-16 02:50:31 +00:00
|
|
|
buf.Must(buffer.WriteByte(CommandTCP))
|
2023-01-11 14:01:15 +00:00
|
|
|
}
|
2021-11-17 07:00:32 +00:00
|
|
|
|
2023-01-16 01:42:03 +00:00
|
|
|
binary.BigEndian.PutUint16(buffer.Extend(2), vc.dst.Port)
|
2023-01-16 02:50:31 +00:00
|
|
|
buf.Must(
|
2023-01-16 01:42:03 +00:00
|
|
|
buffer.WriteByte(vc.dst.AddrType),
|
2023-01-16 02:50:31 +00:00
|
|
|
buf.Error(buffer.Write(vc.dst.Addr)),
|
2023-01-16 01:42:03 +00:00
|
|
|
)
|
2023-01-11 14:01:15 +00:00
|
|
|
}
|
2021-11-17 07:00:32 +00:00
|
|
|
|
2023-02-25 05:12:19 +00:00
|
|
|
if isVision && !vc.dst.UDP && !vc.dst.Mux {
|
|
|
|
if len(p) == 0 {
|
|
|
|
vc.packetsToFilter = 0
|
|
|
|
vc.writeFilterApplicationData = false
|
|
|
|
WriteWithPadding(buffer, nil, commandPaddingEnd, vc.id)
|
|
|
|
} else {
|
|
|
|
vc.FilterTLS(p)
|
|
|
|
if vc.isTLS {
|
|
|
|
WriteWithPadding(buffer, p, commandPaddingContinue, vc.id)
|
|
|
|
} else {
|
|
|
|
buf.Must(buf.Error(buffer.Write(p)))
|
|
|
|
vc.readProcess = false
|
|
|
|
vc.writeFilterApplicationData = false
|
|
|
|
vc.packetsToFilter = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
buf.Must(buf.Error(buffer.Write(p)))
|
|
|
|
}
|
2023-02-10 02:03:37 +00:00
|
|
|
|
2023-02-25 05:12:19 +00:00
|
|
|
_, vc.err = vc.ExtendedWriter.Write(buffer.Bytes())
|
|
|
|
if vc.err != nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if isVision {
|
|
|
|
switch underlying := vc.tlsConn.(type) {
|
|
|
|
case *gotls.Conn:
|
|
|
|
if underlying.ConnectionState().Version != gotls.VersionTLS13 {
|
|
|
|
vc.err = ErrNotTLS13
|
|
|
|
}
|
|
|
|
case *utls.UConn:
|
|
|
|
if underlying.ConnectionState().Version != utls.VersionTLS13 {
|
|
|
|
vc.err = ErrNotTLS13
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
vc.err = fmt.Errorf(`failed to use %s, maybe "security" is not "tls" or "utls"`, vc.addons.Flow)
|
|
|
|
}
|
|
|
|
vc.tlsConn = nil
|
|
|
|
}
|
2023-02-11 07:13:17 +00:00
|
|
|
return true
|
2021-11-17 07:00:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (vc *Conn) recvResponse() error {
|
2023-01-16 01:42:03 +00:00
|
|
|
var buf [1]byte
|
2023-02-25 05:12:19 +00:00
|
|
|
_, vc.err = io.ReadFull(vc.ExtendedReader, buf[:])
|
2023-02-10 02:03:37 +00:00
|
|
|
if vc.err != nil {
|
|
|
|
return vc.err
|
2021-11-17 07:00:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if buf[0] != Version {
|
|
|
|
return errors.New("unexpected response version")
|
|
|
|
}
|
|
|
|
|
2023-02-25 05:12:19 +00:00
|
|
|
_, vc.err = io.ReadFull(vc.ExtendedReader, buf[:])
|
2023-02-10 02:03:37 +00:00
|
|
|
if vc.err != nil {
|
|
|
|
return vc.err
|
2021-11-17 07:00:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
length := int64(buf[0])
|
|
|
|
if length != 0 { // addon data length > 0
|
2023-02-25 05:12:19 +00:00
|
|
|
io.CopyN(io.Discard, vc.ExtendedReader, length) // just discard
|
2021-11-17 07:00:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-02-25 05:12:19 +00:00
|
|
|
func (vc *Conn) FrontHeadroom() int {
|
|
|
|
if vc.IsXTLSVisionEnabled() {
|
|
|
|
return paddingHeaderLen
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2023-01-16 01:42:03 +00:00
|
|
|
func (vc *Conn) Upstream() any {
|
2023-02-25 05:12:19 +00:00
|
|
|
if vc.tlsConn == nil {
|
|
|
|
return vc.Conn
|
|
|
|
}
|
|
|
|
return vc.tlsConn
|
|
|
|
}
|
|
|
|
|
|
|
|
func (vc *Conn) IsXTLSVisionEnabled() bool {
|
|
|
|
return vc.addons != nil && vc.addons.Flow == XRV
|
2023-01-16 01:42:03 +00:00
|
|
|
}
|
|
|
|
|
2021-11-17 07:00:32 +00:00
|
|
|
// newConn return a Conn instance
|
|
|
|
func newConn(conn net.Conn, client *Client, dst *DstAddr) (*Conn, error) {
|
|
|
|
c := &Conn{
|
2023-02-25 05:12:19 +00:00
|
|
|
ExtendedReader: N.NewExtendedReader(conn),
|
|
|
|
ExtendedWriter: N.NewExtendedWriter(conn),
|
|
|
|
Conn: conn,
|
|
|
|
id: client.uuid,
|
|
|
|
dst: dst,
|
|
|
|
handshake: make(chan struct{}),
|
2021-11-17 07:00:32 +00: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)
|
|
|
|
}
|
2023-02-25 05:12:19 +00:00
|
|
|
case XRV:
|
|
|
|
c.packetsToFilter = 6
|
|
|
|
c.readProcess = true
|
|
|
|
c.readFilterUUID = true
|
|
|
|
c.writeFilterApplicationData = true
|
|
|
|
c.addons = client.Addons
|
|
|
|
var t reflect.Type
|
|
|
|
var p uintptr
|
|
|
|
switch underlying := conn.(type) {
|
|
|
|
case *gotls.Conn:
|
|
|
|
c.Conn = underlying.NetConn()
|
|
|
|
c.tlsConn = underlying
|
|
|
|
t = reflect.TypeOf(underlying).Elem()
|
|
|
|
p = uintptr(unsafe.Pointer(underlying))
|
|
|
|
case *utls.UConn:
|
|
|
|
c.Conn = underlying.NetConn()
|
|
|
|
c.tlsConn = underlying
|
|
|
|
t = reflect.TypeOf(underlying.Conn).Elem()
|
|
|
|
p = uintptr(unsafe.Pointer(underlying.Conn))
|
|
|
|
case *tlsC.UConn:
|
|
|
|
c.Conn = underlying.NetConn()
|
|
|
|
c.tlsConn = underlying.UConn
|
|
|
|
t = reflect.TypeOf(underlying.Conn).Elem()
|
|
|
|
p = uintptr(unsafe.Pointer(underlying.Conn))
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf(`failed to use %s, maybe "security" is not "tls" or "utls"`, client.Addons.Flow)
|
|
|
|
}
|
|
|
|
i, _ := t.FieldByName("input")
|
|
|
|
r, _ := t.FieldByName("rawInput")
|
|
|
|
c.input = (*bytes.Reader)(unsafe.Pointer(p + i.Offset))
|
|
|
|
c.rawInput = (*bytes.Buffer)(unsafe.Pointer(p + r.Offset))
|
|
|
|
if _, ok := c.Conn.(*net.TCPConn); !ok {
|
|
|
|
log.Debugln("XTLS underlying conn is not *net.TCPConn, got %s", reflect.TypeOf(conn).Name())
|
|
|
|
}
|
2021-11-17 07:00:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return c, nil
|
|
|
|
}
|