github.com/xraypb/Xray-core@v1.8.1/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  	"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  	// RequestOptionChunkStream indicates request payload is chunked. Each chunk consists of length, authentication and payload.
    34  	RequestOptionChunkStream bitmask.Byte = 0x01
    35  
    36  	// RequestOptionConnectionReuse indicates client side expects to reuse the connection.
    37  	RequestOptionConnectionReuse bitmask.Byte = 0x02
    38  
    39  	RequestOptionChunkMasking bitmask.Byte = 0x04
    40  
    41  	RequestOptionGlobalPadding bitmask.Byte = 0x08
    42  
    43  	RequestOptionAuthenticatedLength bitmask.Byte = 0x10
    44  )
    45  
    46  type RequestHeader struct {
    47  	Version  byte
    48  	Command  RequestCommand
    49  	Option   bitmask.Byte
    50  	Security SecurityType
    51  	Port     net.Port
    52  	Address  net.Address
    53  	User     *MemoryUser
    54  }
    55  
    56  func (h *RequestHeader) Destination() net.Destination {
    57  	if h.Command == RequestCommandUDP {
    58  		return net.UDPDestination(h.Address, h.Port)
    59  	}
    60  	return net.TCPDestination(h.Address, h.Port)
    61  }
    62  
    63  const (
    64  	ResponseOptionConnectionReuse bitmask.Byte = 0x01
    65  )
    66  
    67  type ResponseCommand interface{}
    68  
    69  type ResponseHeader struct {
    70  	Option  bitmask.Byte
    71  	Command ResponseCommand
    72  }
    73  
    74  type CommandSwitchAccount struct {
    75  	Host     net.Address
    76  	Port     net.Port
    77  	ID       uuid.UUID
    78  	Level    uint32
    79  	AlterIds uint16
    80  	ValidMin byte
    81  }
    82  
    83  var (
    84  	hasGCMAsmAMD64 = cpu.X86.HasAES && cpu.X86.HasPCLMULQDQ
    85  	hasGCMAsmARM64 = cpu.ARM64.HasAES && cpu.ARM64.HasPMULL
    86  	// Keep in sync with crypto/aes/cipher_s390x.go.
    87  	hasGCMAsmS390X = cpu.S390X.HasAES && cpu.S390X.HasAESCBC && cpu.S390X.HasAESCTR &&
    88  		(cpu.S390X.HasGHASH || cpu.S390X.HasAESGCM)
    89  
    90  	hasAESGCMHardwareSupport = runtime.GOARCH == "amd64" && hasGCMAsmAMD64 ||
    91  		runtime.GOARCH == "arm64" && hasGCMAsmARM64 ||
    92  		runtime.GOARCH == "s390x" && hasGCMAsmS390X
    93  )
    94  
    95  func (sc *SecurityConfig) GetSecurityType() SecurityType {
    96  	if sc == nil || sc.Type == SecurityType_AUTO {
    97  		if hasAESGCMHardwareSupport {
    98  			return SecurityType_AES128_GCM
    99  		}
   100  		return SecurityType_CHACHA20_POLY1305
   101  	}
   102  	return sc.Type
   103  }
   104  
   105  func isDomainTooLong(domain string) bool {
   106  	return len(domain) > 256
   107  }