2020-11-25 14:01:53 +03:00
|
|
|
package encoding
|
|
|
|
|
2020-12-04 04:36:16 +03:00
|
|
|
//go:generate go run github.com/xtls/xray-core/common/errors/errorgen
|
2020-11-25 14:01:53 +03:00
|
|
|
|
|
|
|
import (
|
2022-10-29 07:51:59 +03:00
|
|
|
"bytes"
|
2020-12-04 04:36:16 +03:00
|
|
|
"context"
|
2020-11-25 14:01:53 +03:00
|
|
|
"io"
|
2020-12-04 04:36:16 +03:00
|
|
|
|
|
|
|
"github.com/xtls/xray-core/common/buf"
|
|
|
|
"github.com/xtls/xray-core/common/errors"
|
|
|
|
"github.com/xtls/xray-core/common/net"
|
|
|
|
"github.com/xtls/xray-core/common/protocol"
|
|
|
|
"github.com/xtls/xray-core/common/session"
|
|
|
|
"github.com/xtls/xray-core/common/signal"
|
|
|
|
"github.com/xtls/xray-core/features/stats"
|
2023-05-04 05:21:45 +03:00
|
|
|
"github.com/xtls/xray-core/proxy"
|
2020-12-04 04:36:16 +03:00
|
|
|
"github.com/xtls/xray-core/proxy/vless"
|
2020-11-25 14:01:53 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
Version = byte(0)
|
|
|
|
)
|
|
|
|
|
|
|
|
var addrParser = protocol.NewAddressParser(
|
|
|
|
protocol.AddressFamilyByte(byte(protocol.AddressTypeIPv4), net.AddressFamilyIPv4),
|
|
|
|
protocol.AddressFamilyByte(byte(protocol.AddressTypeDomain), net.AddressFamilyDomain),
|
|
|
|
protocol.AddressFamilyByte(byte(protocol.AddressTypeIPv6), net.AddressFamilyIPv6),
|
|
|
|
protocol.PortThenAddress(),
|
|
|
|
)
|
|
|
|
|
|
|
|
// EncodeRequestHeader writes encoded request header into the given writer.
|
|
|
|
func EncodeRequestHeader(writer io.Writer, request *protocol.RequestHeader, requestAddons *Addons) error {
|
|
|
|
buffer := buf.StackNew()
|
|
|
|
defer buffer.Release()
|
|
|
|
|
|
|
|
if err := buffer.WriteByte(request.Version); err != nil {
|
2024-06-29 21:32:57 +03:00
|
|
|
return errors.New("failed to write request version").Base(err)
|
2020-11-25 14:01:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := buffer.Write(request.User.Account.(*vless.MemoryAccount).ID.Bytes()); err != nil {
|
2024-06-29 21:32:57 +03:00
|
|
|
return errors.New("failed to write request user id").Base(err)
|
2020-11-25 14:01:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := EncodeHeaderAddons(&buffer, requestAddons); err != nil {
|
2024-06-29 21:32:57 +03:00
|
|
|
return errors.New("failed to encode request header addons").Base(err)
|
2020-11-25 14:01:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := buffer.WriteByte(byte(request.Command)); err != nil {
|
2024-06-29 21:32:57 +03:00
|
|
|
return errors.New("failed to write request command").Base(err)
|
2020-11-25 14:01:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if request.Command != protocol.RequestCommandMux {
|
|
|
|
if err := addrParser.WriteAddressPort(&buffer, request.Address, request.Port); err != nil {
|
2024-06-29 21:32:57 +03:00
|
|
|
return errors.New("failed to write request address and port").Base(err)
|
2020-11-25 14:01:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := writer.Write(buffer.Bytes()); err != nil {
|
2024-06-29 21:32:57 +03:00
|
|
|
return errors.New("failed to write request header").Base(err)
|
2020-11-25 14:01:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecodeRequestHeader decodes and returns (if successful) a RequestHeader from an input stream.
|
|
|
|
func DecodeRequestHeader(isfb bool, first *buf.Buffer, reader io.Reader, validator *vless.Validator) (*protocol.RequestHeader, *Addons, bool, error) {
|
|
|
|
buffer := buf.StackNew()
|
|
|
|
defer buffer.Release()
|
|
|
|
|
|
|
|
request := new(protocol.RequestHeader)
|
|
|
|
|
|
|
|
if isfb {
|
|
|
|
request.Version = first.Byte(0)
|
|
|
|
} else {
|
|
|
|
if _, err := buffer.ReadFullFrom(reader, 1); err != nil {
|
2024-06-29 21:32:57 +03:00
|
|
|
return nil, nil, false, errors.New("failed to read request version").Base(err)
|
2020-11-25 14:01:53 +03:00
|
|
|
}
|
|
|
|
request.Version = buffer.Byte(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch request.Version {
|
|
|
|
case 0:
|
|
|
|
|
|
|
|
var id [16]byte
|
|
|
|
|
|
|
|
if isfb {
|
|
|
|
copy(id[:], first.BytesRange(1, 17))
|
|
|
|
} else {
|
|
|
|
buffer.Clear()
|
|
|
|
if _, err := buffer.ReadFullFrom(reader, 16); err != nil {
|
2024-06-29 21:32:57 +03:00
|
|
|
return nil, nil, false, errors.New("failed to read request user id").Base(err)
|
2020-11-25 14:01:53 +03:00
|
|
|
}
|
|
|
|
copy(id[:], buffer.Bytes())
|
|
|
|
}
|
|
|
|
|
|
|
|
if request.User = validator.Get(id); request.User == nil {
|
2024-06-29 21:32:57 +03:00
|
|
|
return nil, nil, isfb, errors.New("invalid request user id")
|
2020-11-25 14:01:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if isfb {
|
|
|
|
first.Advance(17)
|
|
|
|
}
|
|
|
|
|
|
|
|
requestAddons, err := DecodeHeaderAddons(&buffer, reader)
|
|
|
|
if err != nil {
|
2024-06-29 21:32:57 +03:00
|
|
|
return nil, nil, false, errors.New("failed to decode request header addons").Base(err)
|
2020-11-25 14:01:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
buffer.Clear()
|
|
|
|
if _, err := buffer.ReadFullFrom(reader, 1); err != nil {
|
2024-06-29 21:32:57 +03:00
|
|
|
return nil, nil, false, errors.New("failed to read request command").Base(err)
|
2020-11-25 14:01:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
request.Command = protocol.RequestCommand(buffer.Byte(0))
|
|
|
|
switch request.Command {
|
|
|
|
case protocol.RequestCommandMux:
|
|
|
|
request.Address = net.DomainAddress("v1.mux.cool")
|
|
|
|
request.Port = 0
|
|
|
|
case protocol.RequestCommandTCP, protocol.RequestCommandUDP:
|
|
|
|
if addr, port, err := addrParser.ReadAddressPort(&buffer, reader); err == nil {
|
|
|
|
request.Address = addr
|
|
|
|
request.Port = port
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if request.Address == nil {
|
2024-06-29 21:32:57 +03:00
|
|
|
return nil, nil, false, errors.New("invalid request address")
|
2020-11-25 14:01:53 +03:00
|
|
|
}
|
|
|
|
return request, requestAddons, false, nil
|
|
|
|
default:
|
2024-06-29 21:32:57 +03:00
|
|
|
return nil, nil, isfb, errors.New("invalid request version")
|
2020-11-25 14:01:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// EncodeResponseHeader writes encoded response header into the given writer.
|
|
|
|
func EncodeResponseHeader(writer io.Writer, request *protocol.RequestHeader, responseAddons *Addons) error {
|
|
|
|
buffer := buf.StackNew()
|
|
|
|
defer buffer.Release()
|
|
|
|
|
|
|
|
if err := buffer.WriteByte(request.Version); err != nil {
|
2024-06-29 21:32:57 +03:00
|
|
|
return errors.New("failed to write response version").Base(err)
|
2020-11-25 14:01:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := EncodeHeaderAddons(&buffer, responseAddons); err != nil {
|
2024-06-29 21:32:57 +03:00
|
|
|
return errors.New("failed to encode response header addons").Base(err)
|
2020-11-25 14:01:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := writer.Write(buffer.Bytes()); err != nil {
|
2024-06-29 21:32:57 +03:00
|
|
|
return errors.New("failed to write response header").Base(err)
|
2020-11-25 14:01:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecodeResponseHeader decodes and returns (if successful) a ResponseHeader from an input stream.
|
|
|
|
func DecodeResponseHeader(reader io.Reader, request *protocol.RequestHeader) (*Addons, error) {
|
|
|
|
buffer := buf.StackNew()
|
|
|
|
defer buffer.Release()
|
|
|
|
|
|
|
|
if _, err := buffer.ReadFullFrom(reader, 1); err != nil {
|
2024-06-29 21:32:57 +03:00
|
|
|
return nil, errors.New("failed to read response version").Base(err)
|
2020-11-25 14:01:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if buffer.Byte(0) != request.Version {
|
2024-06-29 21:32:57 +03:00
|
|
|
return nil, errors.New("unexpected response version. Expecting ", int(request.Version), " but actually ", int(buffer.Byte(0)))
|
2020-11-25 14:01:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
responseAddons, err := DecodeHeaderAddons(&buffer, reader)
|
|
|
|
if err != nil {
|
2024-06-29 21:32:57 +03:00
|
|
|
return nil, errors.New("failed to decode response header addons").Base(err)
|
2020-11-25 14:01:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return responseAddons, nil
|
|
|
|
}
|
|
|
|
|
2022-10-29 07:51:59 +03:00
|
|
|
// XtlsRead filter and read xtls protocol
|
2024-05-18 18:56:05 +03:00
|
|
|
func XtlsRead(reader buf.Reader, writer buf.Writer, timer *signal.ActivityTimer, conn net.Conn, input *bytes.Reader, rawInput *bytes.Buffer, trafficState *proxy.TrafficState, ob *session.Outbound, ctx context.Context) error {
|
2022-10-29 07:51:59 +03:00
|
|
|
err := func() error {
|
|
|
|
for {
|
2023-09-02 18:37:50 +03:00
|
|
|
if trafficState.ReaderSwitchToDirectCopy {
|
2023-05-04 05:21:45 +03:00
|
|
|
var writerConn net.Conn
|
2024-05-20 06:54:36 +03:00
|
|
|
var inTimer *signal.ActivityTimer
|
2024-05-14 06:14:29 +03:00
|
|
|
if inbound := session.InboundFromContext(ctx); inbound != nil && inbound.Conn != nil {
|
2023-05-04 05:21:45 +03:00
|
|
|
writerConn = inbound.Conn
|
2024-05-20 06:54:36 +03:00
|
|
|
inTimer = inbound.Timer
|
2023-05-04 05:21:45 +03:00
|
|
|
if inbound.CanSpliceCopy == 2 {
|
2024-05-14 04:52:24 +03:00
|
|
|
inbound.CanSpliceCopy = 1
|
|
|
|
}
|
2024-05-14 06:14:29 +03:00
|
|
|
if ob != nil && ob.CanSpliceCopy == 2 { // ob need to be passed in due to context can change
|
2024-05-14 04:52:24 +03:00
|
|
|
ob.CanSpliceCopy = 1
|
2020-12-04 04:36:16 +03:00
|
|
|
}
|
|
|
|
}
|
2024-05-20 06:54:36 +03:00
|
|
|
return proxy.CopyRawConnIfExist(ctx, conn, writerConn, writer, timer, inTimer)
|
2020-11-25 14:01:53 +03:00
|
|
|
}
|
2024-02-02 23:32:46 +03:00
|
|
|
buffer, err := reader.ReadMultiBuffer()
|
2020-11-25 14:01:53 +03:00
|
|
|
if !buffer.IsEmpty() {
|
2023-09-02 18:37:50 +03:00
|
|
|
timer.Update()
|
|
|
|
if trafficState.ReaderSwitchToDirectCopy {
|
|
|
|
// XTLS Vision processes struct TLS Conn's input and rawInput
|
|
|
|
if inputBuffer, err := buf.ReadFrom(input); err == nil {
|
|
|
|
if !inputBuffer.IsEmpty() {
|
|
|
|
buffer, _ = buf.MergeMulti(buffer, inputBuffer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if rawInputBuffer, err := buf.ReadFrom(rawInput); err == nil {
|
|
|
|
if !rawInputBuffer.IsEmpty() {
|
|
|
|
buffer, _ = buf.MergeMulti(buffer, rawInputBuffer)
|
2022-10-29 07:51:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-11-25 14:01:53 +03:00
|
|
|
if werr := writer.WriteMultiBuffer(buffer); werr != nil {
|
|
|
|
return werr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
if err != nil && errors.Cause(err) != io.EOF {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2022-10-29 07:51:59 +03:00
|
|
|
|
|
|
|
// XtlsWrite filter and write xtls protocol
|
2024-05-14 04:52:24 +03:00
|
|
|
func XtlsWrite(reader buf.Reader, writer buf.Writer, timer signal.ActivityUpdater, conn net.Conn, trafficState *proxy.TrafficState, ob *session.Outbound, ctx context.Context) error {
|
2022-10-29 07:51:59 +03:00
|
|
|
err := func() error {
|
|
|
|
var ct stats.Counter
|
|
|
|
for {
|
|
|
|
buffer, err := reader.ReadMultiBuffer()
|
2024-02-17 19:16:37 +03:00
|
|
|
if trafficState.WriterSwitchToDirectCopy {
|
2024-05-14 06:14:29 +03:00
|
|
|
if inbound := session.InboundFromContext(ctx); inbound != nil {
|
2024-05-14 04:52:24 +03:00
|
|
|
if inbound.CanSpliceCopy == 2 {
|
|
|
|
inbound.CanSpliceCopy = 1
|
|
|
|
}
|
2024-05-14 06:14:29 +03:00
|
|
|
if ob != nil && ob.CanSpliceCopy == 2 {
|
2024-05-14 04:52:24 +03:00
|
|
|
ob.CanSpliceCopy = 1
|
|
|
|
}
|
2024-02-17 19:16:37 +03:00
|
|
|
}
|
|
|
|
rawConn, _, writerCounter := proxy.UnwrapRawConn(conn)
|
|
|
|
writer = buf.NewWriter(rawConn)
|
|
|
|
ct = writerCounter
|
|
|
|
trafficState.WriterSwitchToDirectCopy = false
|
|
|
|
}
|
2023-09-02 18:37:50 +03:00
|
|
|
if !buffer.IsEmpty() {
|
|
|
|
if ct != nil {
|
|
|
|
ct.Add(int64(buffer.Len()))
|
2022-10-29 07:51:59 +03:00
|
|
|
}
|
2023-09-02 18:37:50 +03:00
|
|
|
timer.Update()
|
|
|
|
if werr := writer.WriteMultiBuffer(buffer); werr != nil {
|
|
|
|
return werr
|
2022-10-29 07:51:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
if err != nil && errors.Cause(err) != io.EOF {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|