reality.go 1003 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package outbound
  2. import (
  3. "crypto/ecdh"
  4. "encoding/base64"
  5. "encoding/hex"
  6. "errors"
  7. "fmt"
  8. tlsC "github.com/metacubex/mihomo/component/tls"
  9. )
  10. type RealityOptions struct {
  11. PublicKey string `proxy:"public-key"`
  12. ShortID string `proxy:"short-id"`
  13. }
  14. func (o RealityOptions) Parse() (*tlsC.RealityConfig, error) {
  15. if o.PublicKey != "" {
  16. config := new(tlsC.RealityConfig)
  17. const x25519ScalarSize = 32
  18. var publicKey [x25519ScalarSize]byte
  19. n, err := base64.RawURLEncoding.Decode(publicKey[:], []byte(o.PublicKey))
  20. if err != nil || n != x25519ScalarSize {
  21. return nil, errors.New("invalid REALITY public key")
  22. }
  23. config.PublicKey, err = ecdh.X25519().NewPublicKey(publicKey[:])
  24. if err != nil {
  25. return nil, fmt.Errorf("fail to create REALITY public key: %w", err)
  26. }
  27. n, err = hex.Decode(config.ShortID[:], []byte(o.ShortID))
  28. if err != nil || n > tlsC.RealityMaxShortIDLen {
  29. return nil, errors.New("invalid REALITY short ID")
  30. }
  31. return config, nil
  32. }
  33. return nil, nil
  34. }