config.go 1.0 KB

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