config.go 992 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package quic
  2. import (
  3. "crypto/aes"
  4. "crypto/cipher"
  5. "crypto/sha256"
  6. "golang.org/x/crypto/chacha20poly1305"
  7. "v2ray.com/core/common"
  8. "v2ray.com/core/common/protocol"
  9. "v2ray.com/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 + "v2ray-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. }