github.com/xtls/xray-core@v1.8.12-0.20240518155711-3168d27b0bdb/common/protocol/headers.go (about)

     1  package protocol
     2  
     3  import (
     4  	"runtime"
     5  
     6  	"github.com/xtls/xray-core/common/bitmask"
     7  	"github.com/xtls/xray-core/common/net"
     8  	"github.com/xtls/xray-core/common/uuid"
     9  	"golang.org/x/sys/cpu"
    10  )
    11  
    12  // RequestCommand is a custom command in a proxy request.
    13  type RequestCommand byte
    14  
    15  const (
    16  	RequestCommandTCP = RequestCommand(0x01)
    17  	RequestCommandUDP = RequestCommand(0x02)
    18  	RequestCommandMux = RequestCommand(0x03)
    19  )
    20  
    21  func (c RequestCommand) TransferType() TransferType {
    22  	switch c {
    23  	case RequestCommandTCP, RequestCommandMux:
    24  		return TransferTypeStream
    25  	case RequestCommandUDP:
    26  		return TransferTypePacket
    27  	default:
    28  		return TransferTypeStream
    29  	}
    30  }
    31  
    32  const (
    33  	// [DEPRECATED 2023-06] RequestOptionChunkStream indicates request payload is chunked. Each chunk consists of length, authentication and payload.
    34  	RequestOptionChunkStream bitmask.Byte = 0x01
    35  
    36  	// 0x02 legacy setting
    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  	ValidMin byte
    79  }
    80  
    81  var (
    82  	hasGCMAsmAMD64 = cpu.X86.HasAES && cpu.X86.HasPCLMULQDQ
    83  	hasGCMAsmARM64 = cpu.ARM64.HasAES && cpu.ARM64.HasPMULL
    84  	// Keep in sync with crypto/aes/cipher_s390x.go.
    85  	hasGCMAsmS390X = cpu.S390X.HasAES && cpu.S390X.HasAESCBC && cpu.S390X.HasAESCTR &&
    86  		(cpu.S390X.HasGHASH || cpu.S390X.HasAESGCM)
    87  
    88  	hasAESGCMHardwareSupport = runtime.GOARCH == "amd64" && hasGCMAsmAMD64 ||
    89  		runtime.GOARCH == "arm64" && hasGCMAsmARM64 ||
    90  		runtime.GOARCH == "s390x" && hasGCMAsmS390X
    91  )
    92  
    93  func (sc *SecurityConfig) GetSecurityType() SecurityType {
    94  	if sc == nil || sc.Type == SecurityType_AUTO {
    95  		if hasAESGCMHardwareSupport {
    96  			return SecurityType_AES128_GCM
    97  		}
    98  		return SecurityType_CHACHA20_POLY1305
    99  	}
   100  	return sc.Type
   101  }
   102  
   103  func isDomainTooLong(domain string) bool {
   104  	return len(domain) > 256
   105  }