github.com/v2fly/v2ray-core/v4@v4.45.2/transport/internet/quic/config.go (about)

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