2021-11-17 15:00:32 +08:00
|
|
|
package vless
|
|
|
|
|
|
|
|
import (
|
2023-02-25 13:12:19 +08:00
|
|
|
"bytes"
|
|
|
|
"crypto/subtle"
|
|
|
|
gotls "crypto/tls"
|
2021-11-17 15:00:32 +08:00
|
|
|
"encoding/binary"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net"
|
2023-02-25 13:12:19 +08:00
|
|
|
"reflect"
|
2023-02-10 10:03:37 +08:00
|
|
|
"sync"
|
2023-02-25 13:12:19 +08:00
|
|
|
"unsafe"
|
2021-11-17 15:00:32 +08:00
|
|
|
|
2023-01-16 10:50:31 +08:00
|
|
|
"github.com/Dreamacro/clash/common/buf"
|
|
|
|
N "github.com/Dreamacro/clash/common/net"
|
2023-02-25 13:12:19 +08:00
|
|
|
tlsC "github.com/Dreamacro/clash/component/tls"
|
|
|
|
"github.com/Dreamacro/clash/log"
|
2023-01-16 10:50:31 +08:00
|
|
|
|
2021-11-17 15:00:32 +08:00
|
|
|
"github.com/gofrs/uuid"
|
2023-02-25 13:12:19 +08:00
|
|
|
utls "github.com/sagernet/utls"
|
2021-11-17 15:00:32 +08:00
|
|
|
xtls "github.com/xtls/go"
|
|
|
|
"google.golang.org/protobuf/proto"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Conn struct {
|
2023-02-25 13:12:19 +08:00
|
|
|
N.ExtendedWriter
|
|
|
|
N.ExtendedReader
|
|
|
|
net.Conn
|
2021-11-17 15:00:32 +08:00
|
|
|
dst *DstAddr
|
|
|
|
id *uuid.UUID
|
|
|
|
addons *Addons
|
|
|
|
received bool
|
2023-02-10 10:03:37 +08:00
|
|
|
|
|
|
|
handshake chan struct{}
|
|
|
|
handshakeMutex sync.Mutex
|
|
|
|
err error
|
2023-02-25 13:12:19 +08: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 15:00:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (vc *Conn) Read(b []byte) (int, error) {
|
|
|
|
if vc.received {
|
2023-02-25 13:12:19 +08:00
|
|
|
if vc.readProcess {
|
|
|
|
buffer := buf.With(b)
|
|
|
|
err := vc.ReadBuffer(buffer)
|
|
|
|
return buffer.Len(), err
|
|
|
|
}
|
|
|
|
return vc.ExtendedReader.Read(b)
|
2021-11-17 15:00:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := vc.recvResponse(); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
vc.received = true
|
2023-02-25 13:12:19 +08:00
|
|
|
return vc.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 {
|
2023-02-25 13:12:19 +08: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:
|
2023-02-26 11:11:55 +08:00
|
|
|
needReturn := false
|
2023-02-25 13:12:19 +08:00
|
|
|
if vc.input != nil {
|
|
|
|
_, err := buffer.ReadFrom(vc.input)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if vc.input.Len() == 0 {
|
2023-02-26 11:11:55 +08:00
|
|
|
needReturn = true
|
2023-02-25 13:12:19 +08:00
|
|
|
vc.input = nil
|
2023-02-26 11:11:55 +08:00
|
|
|
} else { // buffer is full
|
2023-02-25 13:12:19 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if vc.rawInput != nil {
|
|
|
|
_, err := buffer.ReadFrom(vc.rawInput)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-02-26 11:11:55 +08:00
|
|
|
needReturn = true
|
2023-02-25 13:12:19 +08:00
|
|
|
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")
|
|
|
|
}
|
2023-02-26 11:11:55 +08:00
|
|
|
if needReturn {
|
|
|
|
return nil
|
|
|
|
}
|
2023-02-25 13:12:19 +08:00
|
|
|
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 09:42:03 +08:00
|
|
|
}
|
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
|
2023-02-25 13:12:19 +08:00
|
|
|
return vc.ReadBuffer(buffer)
|
2023-01-16 09:42:03 +08:00
|
|
|
}
|
2021-11-17 15:00:32 +08:00
|
|
|
|
2023-02-10 10:03:37 +08:00
|
|
|
func (vc *Conn) Write(p []byte) (int, error) {
|
|
|
|
select {
|
|
|
|
case <-vc.handshake:
|
|
|
|
default:
|
2023-02-11 15:13:17 +08: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 10:03:37 +08:00
|
|
|
}
|
|
|
|
}
|
2023-02-25 13:12:19 +08: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 10:03:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (vc *Conn) WriteBuffer(buffer *buf.Buffer) error {
|
|
|
|
select {
|
|
|
|
case <-vc.handshake:
|
|
|
|
default:
|
2023-02-11 15:13:17 +08:00
|
|
|
if vc.sendRequest(buffer.Bytes()) {
|
|
|
|
return vc.err
|
|
|
|
}
|
|
|
|
if vc.err != nil {
|
|
|
|
return vc.err
|
2023-02-10 10:03:37 +08:00
|
|
|
}
|
|
|
|
}
|
2023-02-25 19:11:23 +08:00
|
|
|
if vc.writeFilterApplicationData {
|
2023-02-25 13:12:19 +08:00
|
|
|
buffer2 := ReshapeBuffer(buffer)
|
|
|
|
defer buffer2.Release()
|
|
|
|
vc.FilterTLS(buffer.Bytes())
|
|
|
|
command := commandPaddingContinue
|
2023-02-25 19:11:23 +08:00
|
|
|
if !vc.isTLS {
|
|
|
|
command = commandPaddingEnd
|
|
|
|
|
|
|
|
// disable XTLS
|
|
|
|
vc.readProcess = false
|
|
|
|
vc.writeFilterApplicationData = false
|
|
|
|
vc.packetsToFilter = 0
|
|
|
|
} else if buffer.Len() > 6 && bytes.Equal(buffer.To(3), tlsApplicationDataStart) || vc.packetsToFilter <= 0 {
|
2023-02-25 13:12:19 +08:00
|
|
|
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")
|
2023-02-27 01:06:41 +08:00
|
|
|
//time.Sleep(5 * time.Millisecond)
|
2023-02-25 13:12:19 +08:00
|
|
|
}
|
|
|
|
if buffer2 != nil {
|
2023-02-25 19:11:23 +08:00
|
|
|
if vc.writeDirect || !vc.isTLS {
|
2023-02-25 13:12:19 +08:00
|
|
|
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 10:03:37 +08:00
|
|
|
}
|
|
|
|
|
2023-02-11 15:13:17 +08:00
|
|
|
func (vc *Conn) sendRequest(p []byte) bool {
|
2023-02-10 10:03:37 +08:00
|
|
|
vc.handshakeMutex.Lock()
|
|
|
|
defer vc.handshakeMutex.Unlock()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-vc.handshake:
|
2023-02-11 15:13:17 +08:00
|
|
|
// The handshake has been completed before.
|
|
|
|
// So return false to remind the caller.
|
|
|
|
return false
|
2023-02-10 10:03:37 +08:00
|
|
|
default:
|
|
|
|
}
|
|
|
|
defer close(vc.handshake)
|
|
|
|
|
2023-01-16 09:42:03 +08:00
|
|
|
var addonsBytes []byte
|
2021-11-17 15:00:32 +08:00
|
|
|
if vc.addons != nil {
|
2023-02-11 15:13:17 +08:00
|
|
|
addonsBytes, vc.err = proto.Marshal(vc.addons)
|
|
|
|
if vc.err != nil {
|
|
|
|
return true
|
2021-11-17 15:00:32 +08:00
|
|
|
}
|
|
|
|
}
|
2023-02-25 13:12:19 +08:00
|
|
|
isVision := vc.IsXTLSVisionEnabled()
|
2023-02-10 10:03:37 +08:00
|
|
|
|
2023-02-25 13:12:19 +08: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 09:42:03 +08:00
|
|
|
|
2023-01-16 10:50:31 +08:00
|
|
|
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))),
|
2023-01-16 10:50:31 +08:00
|
|
|
buf.Error(buffer.Write(addonsBytes)),
|
2023-01-16 09:42:03 +08:00
|
|
|
)
|
2021-11-17 15:00:32 +08:00
|
|
|
|
2023-01-11 22:01:15 +08:00
|
|
|
if vc.dst.Mux {
|
2023-01-16 10:50:31 +08:00
|
|
|
buf.Must(buffer.WriteByte(CommandMux))
|
2021-11-17 15:00:32 +08:00
|
|
|
} else {
|
2023-01-11 22:01:15 +08:00
|
|
|
if vc.dst.UDP {
|
2023-01-16 10:50:31 +08:00
|
|
|
buf.Must(buffer.WriteByte(CommandUDP))
|
2023-01-11 22:01:15 +08:00
|
|
|
} else {
|
2023-01-16 10:50:31 +08:00
|
|
|
buf.Must(buffer.WriteByte(CommandTCP))
|
2023-01-11 22:01:15 +08:00
|
|
|
}
|
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)
|
2023-01-16 10:50:31 +08:00
|
|
|
buf.Must(
|
2023-01-16 09:42:03 +08:00
|
|
|
buffer.WriteByte(vc.dst.AddrType),
|
2023-01-16 10:50:31 +08:00
|
|
|
buf.Error(buffer.Write(vc.dst.Addr)),
|
2023-01-16 09:42:03 +08:00
|
|
|
)
|
2023-01-11 22:01:15 +08:00
|
|
|
}
|
2021-11-17 15:00:32 +08:00
|
|
|
|
2023-02-25 13:12:19 +08:00
|
|
|
if isVision && !vc.dst.UDP && !vc.dst.Mux {
|
|
|
|
if len(p) == 0 {
|
2023-02-25 19:11:23 +08:00
|
|
|
WriteWithPadding(buffer, nil, commandPaddingContinue, vc.id)
|
2023-02-25 13:12:19 +08:00
|
|
|
} else {
|
|
|
|
vc.FilterTLS(p)
|
|
|
|
if vc.isTLS {
|
|
|
|
WriteWithPadding(buffer, p, commandPaddingContinue, vc.id)
|
|
|
|
} else {
|
|
|
|
buf.Must(buf.Error(buffer.Write(p)))
|
2023-02-25 15:00:21 +08:00
|
|
|
|
|
|
|
// disable XTLS
|
2023-02-25 13:12:19 +08:00
|
|
|
vc.readProcess = false
|
|
|
|
vc.writeFilterApplicationData = false
|
|
|
|
vc.packetsToFilter = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
buf.Must(buf.Error(buffer.Write(p)))
|
|
|
|
}
|
2023-02-10 10:03:37 +08:00
|
|
|
|
2023-02-25 13:12:19 +08: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 15:13:17 +08:00
|
|
|
return true
|
2021-11-17 15:00:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (vc *Conn) recvResponse() error {
|
2023-02-27 01:06:41 +08:00
|
|
|
var buffer [1]byte
|
|
|
|
_, vc.err = io.ReadFull(vc.ExtendedReader, buffer[:])
|
2023-02-10 10:03:37 +08:00
|
|
|
if vc.err != nil {
|
|
|
|
return vc.err
|
2021-11-17 15:00:32 +08:00
|
|
|
}
|
|
|
|
|
2023-02-27 01:06:41 +08:00
|
|
|
if buffer[0] != Version {
|
2021-11-17 15:00:32 +08:00
|
|
|
return errors.New("unexpected response version")
|
|
|
|
}
|
|
|
|
|
2023-02-27 01:06:41 +08:00
|
|
|
_, vc.err = io.ReadFull(vc.ExtendedReader, buffer[:])
|
2023-02-10 10:03:37 +08:00
|
|
|
if vc.err != nil {
|
|
|
|
return vc.err
|
2021-11-17 15:00:32 +08:00
|
|
|
}
|
|
|
|
|
2023-02-27 01:06:41 +08:00
|
|
|
length := int64(buffer[0])
|
2021-11-17 15:00:32 +08:00
|
|
|
if length != 0 { // addon data length > 0
|
2023-02-25 13:12:19 +08:00
|
|
|
io.CopyN(io.Discard, vc.ExtendedReader, length) // just discard
|
2021-11-17 15:00:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-02-25 13:12:19 +08:00
|
|
|
func (vc *Conn) FrontHeadroom() int {
|
|
|
|
if vc.IsXTLSVisionEnabled() {
|
|
|
|
return paddingHeaderLen
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2023-01-16 09:42:03 +08:00
|
|
|
func (vc *Conn) Upstream() any {
|
2023-02-25 13:12:19 +08:00
|
|
|
if vc.tlsConn == nil {
|
|
|
|
return vc.Conn
|
|
|
|
}
|
|
|
|
return vc.tlsConn
|
|
|
|
}
|
|
|
|
|
2023-02-27 00:26:49 +08:00
|
|
|
func (vc *Conn) NeedHandshake() bool {
|
|
|
|
select {
|
|
|
|
case <-vc.handshake:
|
2023-02-27 09:46:00 +08:00
|
|
|
return false
|
2023-02-27 00:26:49 +08:00
|
|
|
default:
|
|
|
|
}
|
2023-02-27 09:46:00 +08:00
|
|
|
return true
|
2023-02-27 00:26:49 +08:00
|
|
|
}
|
|
|
|
|
2023-02-25 13:12:19 +08:00
|
|
|
func (vc *Conn) IsXTLSVisionEnabled() bool {
|
|
|
|
return vc.addons != nil && vc.addons.Flow == XRV
|
2023-01-16 09:42:03 +08:00
|
|
|
}
|
|
|
|
|
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{
|
2023-02-25 13:12:19 +08:00
|
|
|
ExtendedReader: N.NewExtendedReader(conn),
|
|
|
|
ExtendedWriter: N.NewExtendedWriter(conn),
|
|
|
|
Conn: conn,
|
|
|
|
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)
|
|
|
|
}
|
2023-02-25 13:12:19 +08: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:
|
2023-02-27 01:06:41 +08:00
|
|
|
//log.Debugln("type tls")
|
2023-02-25 13:12:19 +08:00
|
|
|
c.Conn = underlying.NetConn()
|
|
|
|
c.tlsConn = underlying
|
|
|
|
t = reflect.TypeOf(underlying).Elem()
|
|
|
|
p = uintptr(unsafe.Pointer(underlying))
|
|
|
|
case *utls.UConn:
|
2023-02-27 01:06:41 +08:00
|
|
|
//log.Debugln("type *utls.UConn")
|
2023-02-25 13:12:19 +08:00
|
|
|
c.Conn = underlying.NetConn()
|
|
|
|
c.tlsConn = underlying
|
|
|
|
t = reflect.TypeOf(underlying.Conn).Elem()
|
|
|
|
p = uintptr(unsafe.Pointer(underlying.Conn))
|
|
|
|
case *tlsC.UConn:
|
2023-02-27 01:06:41 +08:00
|
|
|
//log.Debugln("type *tlsC.UConn")
|
2023-02-25 13:12:19 +08:00
|
|
|
c.Conn = underlying.NetConn()
|
|
|
|
c.tlsConn = underlying.UConn
|
|
|
|
t = reflect.TypeOf(underlying.Conn).Elem()
|
2023-02-27 01:06:41 +08:00
|
|
|
//log.Debugln("t:%v", t)
|
2023-02-25 13:12:19 +08:00
|
|
|
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))
|
2023-02-27 01:06:41 +08:00
|
|
|
//if _, ok := c.Conn.(*net.TCPConn); !ok {
|
|
|
|
// log.Debugln("XTLS underlying conn is not *net.TCPConn, got %T", c.Conn)
|
|
|
|
//}
|
2021-11-17 15:00:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return c, nil
|
|
|
|
}
|