github.com/imannamdari/v2ray-core/v5@v5.0.5/transport/internet/headers/tls/dtls.go (about)

     1  package tls
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/imannamdari/v2ray-core/v5/common"
     7  	"github.com/imannamdari/v2ray-core/v5/common/dice"
     8  )
     9  
    10  // DTLS writes header as DTLS. See https://tools.ietf.org/html/rfc6347
    11  type DTLS struct {
    12  	epoch    uint16
    13  	length   uint16
    14  	sequence uint32
    15  }
    16  
    17  // Size implements PacketHeader.
    18  func (*DTLS) Size() int32 {
    19  	return 1 + 2 + 2 + 6 + 2
    20  }
    21  
    22  // Serialize implements PacketHeader.
    23  func (d *DTLS) Serialize(b []byte) {
    24  	b[0] = 23 // application data
    25  	b[1] = 254
    26  	b[2] = 253
    27  	b[3] = byte(d.epoch >> 8)
    28  	b[4] = byte(d.epoch)
    29  	b[5] = 0
    30  	b[6] = 0
    31  	b[7] = byte(d.sequence >> 24)
    32  	b[8] = byte(d.sequence >> 16)
    33  	b[9] = byte(d.sequence >> 8)
    34  	b[10] = byte(d.sequence)
    35  	d.sequence++
    36  	b[11] = byte(d.length >> 8)
    37  	b[12] = byte(d.length)
    38  	d.length += 17
    39  	if d.length > 100 {
    40  		d.length -= 50
    41  	}
    42  }
    43  
    44  // New creates a new UTP header for the given config.
    45  func New(ctx context.Context, config interface{}) (interface{}, error) {
    46  	return &DTLS{
    47  		epoch:    dice.RollUint16(),
    48  		sequence: 0,
    49  		length:   17,
    50  	}, nil
    51  }
    52  
    53  func init() {
    54  	common.Must(common.RegisterConfig((*PacketConfig)(nil), New))
    55  }