mihomo/adapter/outbound/reality.go

36 lines
798 B
Go
Raw Normal View History

2023-03-08 17:18:46 +08:00
package outbound
import (
"encoding/base64"
"encoding/hex"
"errors"
2023-11-03 21:01:45 +08:00
tlsC "github.com/metacubex/mihomo/component/tls"
2023-03-08 17:18:46 +08:00
"golang.org/x/crypto/curve25519"
)
type RealityOptions struct {
2023-03-08 20:28:12 +08:00
PublicKey string `proxy:"public-key"`
ShortID string `proxy:"short-id"`
2023-03-08 17:18:46 +08:00
}
func (o RealityOptions) Parse() (*tlsC.RealityConfig, error) {
2023-03-08 20:28:12 +08:00
if o.PublicKey != "" {
config := new(tlsC.RealityConfig)
2023-03-08 17:18:46 +08:00
2023-03-08 20:28:12 +08:00
n, err := base64.RawURLEncoding.Decode(config.PublicKey[:], []byte(o.PublicKey))
if err != nil || n != curve25519.ScalarSize {
return nil, errors.New("invalid REALITY public key")
}
2023-03-08 17:18:46 +08:00
2023-03-11 12:23:27 +08:00
n, err = hex.Decode(config.ShortID[:], []byte(o.ShortID))
if err != nil || n > tlsC.RealityMaxShortIDLen {
2023-03-08 20:28:12 +08:00
return nil, errors.New("invalid REALITY short ID")
2023-03-08 17:18:46 +08:00
}
2023-03-08 20:28:12 +08:00
return config, nil
2023-03-08 17:18:46 +08:00
}
return nil, nil
}