github.com/metacubex/mihomo@v1.18.5/transport/hysteria/core/protocol.go (about)

     1  package core
     2  
     3  import (
     4  	"time"
     5  )
     6  
     7  const (
     8  	protocolVersion   = uint8(3)
     9  	protocolVersionV2 = uint8(2)
    10  	protocolTimeout   = 10 * time.Second
    11  
    12  	closeErrorCodeGeneric  = 0
    13  	closeErrorCodeProtocol = 1
    14  	closeErrorCodeAuth     = 2
    15  )
    16  
    17  type transmissionRate struct {
    18  	SendBPS uint64
    19  	RecvBPS uint64
    20  }
    21  
    22  type clientHello struct {
    23  	Rate    transmissionRate
    24  	AuthLen uint16 `struc:"sizeof=Auth"`
    25  	Auth    []byte
    26  }
    27  
    28  type serverHello struct {
    29  	OK         bool
    30  	Rate       transmissionRate
    31  	MessageLen uint16 `struc:"sizeof=Message"`
    32  	Message    string
    33  }
    34  
    35  type clientRequest struct {
    36  	UDP     bool
    37  	HostLen uint16 `struc:"sizeof=Host"`
    38  	Host    string
    39  	Port    uint16
    40  }
    41  
    42  type serverResponse struct {
    43  	OK           bool
    44  	UDPSessionID uint32
    45  	MessageLen   uint16 `struc:"sizeof=Message"`
    46  	Message      string
    47  }
    48  
    49  type udpMessage struct {
    50  	SessionID uint32
    51  	HostLen   uint16 `struc:"sizeof=Host"`
    52  	Host      string
    53  	Port      uint16
    54  	MsgID     uint16 // doesn't matter when not fragmented, but must not be 0 when fragmented
    55  	FragID    uint8  // doesn't matter when not fragmented, starts at 0 when fragmented
    56  	FragCount uint8  // must be 1 when not fragmented
    57  	DataLen   uint16 `struc:"sizeof=Data"`
    58  	Data      []byte
    59  }
    60  
    61  func (m udpMessage) HeaderSize() int {
    62  	return 4 + 2 + len(m.Host) + 2 + 2 + 1 + 1 + 2
    63  }
    64  
    65  func (m udpMessage) Size() int {
    66  	return m.HeaderSize() + len(m.Data)
    67  }
    68  
    69  type udpMessageV2 struct {
    70  	SessionID uint32
    71  	HostLen   uint16 `struc:"sizeof=Host"`
    72  	Host      string
    73  	Port      uint16
    74  	DataLen   uint16 `struc:"sizeof=Data"`
    75  	Data      []byte
    76  }