github.com/Uhtred009/v2ray-core-1@v4.31.2+incompatible/transport/internet/quic/config.go (about)

     1  // +build !confonly
     2  
     3  package quic
     4  
     5  import (
     6  	"crypto/aes"
     7  	"crypto/cipher"
     8  	"crypto/sha256"
     9  
    10  	"golang.org/x/crypto/chacha20poly1305"
    11  	"v2ray.com/core/common"
    12  	"v2ray.com/core/common/protocol"
    13  	"v2ray.com/core/transport/internet"
    14  )
    15  
    16  func getAuth(config *Config) (cipher.AEAD, error) {
    17  	security := config.Security.GetSecurityType()
    18  	if security == protocol.SecurityType_NONE {
    19  		return nil, nil
    20  	}
    21  
    22  	salted := []byte(config.Key + "v2ray-quic-salt")
    23  	key := sha256.Sum256(salted)
    24  
    25  	if security == protocol.SecurityType_AES128_GCM {
    26  		block, err := aes.NewCipher(key[:16])
    27  		common.Must(err)
    28  		return cipher.NewGCM(block)
    29  	}
    30  
    31  	if security == protocol.SecurityType_CHACHA20_POLY1305 {
    32  		return chacha20poly1305.New(key[:])
    33  	}
    34  
    35  	return nil, newError("unsupported security type")
    36  }
    37  
    38  func getHeader(config *Config) (internet.PacketHeader, error) {
    39  	if config.Header == nil {
    40  		return nil, nil
    41  	}
    42  
    43  	msg, err := config.Header.GetInstance()
    44  	if err != nil {
    45  		return nil, err
    46  	}
    47  
    48  	return internet.CreatePacketHeader(msg)
    49  }