github.com/v2fly/v2ray-core/v5@v5.16.2-0.20240507031116-8191faa6e095/common/protocol/headers.go (about)

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