github.com/xraypb/xray-core@v1.6.6/common/protocol/headers.go (about)

     1  package protocol
     2  
     3  import (
     4  	"runtime"
     5  
     6  	"github.com/xraypb/xray-core/common/bitmask"
     7  	"github.com/xraypb/xray-core/common/net"
     8  	"github.com/xraypb/xray-core/common/uuid"
     9  )
    10  
    11  // RequestCommand is a custom command in a proxy request.
    12  type RequestCommand byte
    13  
    14  const (
    15  	RequestCommandTCP = RequestCommand(0x01)
    16  	RequestCommandUDP = RequestCommand(0x02)
    17  	RequestCommandMux = RequestCommand(0x03)
    18  )
    19  
    20  func (c RequestCommand) TransferType() TransferType {
    21  	switch c {
    22  	case RequestCommandTCP, RequestCommandMux:
    23  		return TransferTypeStream
    24  	case RequestCommandUDP:
    25  		return TransferTypePacket
    26  	default:
    27  		return TransferTypeStream
    28  	}
    29  }
    30  
    31  const (
    32  	// RequestOptionChunkStream indicates request payload is chunked. Each chunk consists of length, authentication and payload.
    33  	RequestOptionChunkStream bitmask.Byte = 0x01
    34  
    35  	// RequestOptionConnectionReuse indicates client side expects to reuse the connection.
    36  	RequestOptionConnectionReuse bitmask.Byte = 0x02
    37  
    38  	RequestOptionChunkMasking bitmask.Byte = 0x04
    39  
    40  	RequestOptionGlobalPadding bitmask.Byte = 0x08
    41  
    42  	RequestOptionAuthenticatedLength bitmask.Byte = 0x10
    43  )
    44  
    45  type RequestHeader struct {
    46  	Version  byte
    47  	Command  RequestCommand
    48  	Option   bitmask.Byte
    49  	Security SecurityType
    50  	Port     net.Port
    51  	Address  net.Address
    52  	User     *MemoryUser
    53  }
    54  
    55  func (h *RequestHeader) Destination() net.Destination {
    56  	if h.Command == RequestCommandUDP {
    57  		return net.UDPDestination(h.Address, h.Port)
    58  	}
    59  	return net.TCPDestination(h.Address, h.Port)
    60  }
    61  
    62  const (
    63  	ResponseOptionConnectionReuse bitmask.Byte = 0x01
    64  )
    65  
    66  type ResponseCommand interface{}
    67  
    68  type ResponseHeader struct {
    69  	Option  bitmask.Byte
    70  	Command ResponseCommand
    71  }
    72  
    73  type CommandSwitchAccount struct {
    74  	Host     net.Address
    75  	Port     net.Port
    76  	ID       uuid.UUID
    77  	Level    uint32
    78  	AlterIds uint16
    79  	ValidMin byte
    80  }
    81  
    82  func (sc *SecurityConfig) GetSecurityType() SecurityType {
    83  	if sc == nil || sc.Type == SecurityType_AUTO {
    84  		if runtime.GOARCH == "amd64" || runtime.GOARCH == "s390x" || runtime.GOARCH == "arm64" {
    85  			return SecurityType_AES128_GCM
    86  		}
    87  		return SecurityType_CHACHA20_POLY1305
    88  	}
    89  	return sc.Type
    90  }
    91  
    92  func isDomainTooLong(domain string) bool {
    93  	return len(domain) > 256
    94  }