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