2023-03-08 17:18:46 +08:00
|
|
|
package outbound
|
|
|
|
|
|
|
|
import (
|
2023-12-09 15:43:40 +08:00
|
|
|
"crypto/ecdh"
|
2023-03-08 17:18:46 +08:00
|
|
|
"encoding/base64"
|
|
|
|
"encoding/hex"
|
|
|
|
"errors"
|
2023-12-09 15:43:40 +08:00
|
|
|
"fmt"
|
2023-03-08 17:18:46 +08:00
|
|
|
|
2023-11-03 21:01:45 +08:00
|
|
|
tlsC "github.com/metacubex/mihomo/component/tls"
|
2023-03-08 17:18:46 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
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-12-09 15:43:40 +08:00
|
|
|
const x25519ScalarSize = 32
|
|
|
|
var publicKey [x25519ScalarSize]byte
|
|
|
|
n, err := base64.RawURLEncoding.Decode(publicKey[:], []byte(o.PublicKey))
|
|
|
|
if err != nil || n != x25519ScalarSize {
|
2023-03-08 20:28:12 +08:00
|
|
|
return nil, errors.New("invalid REALITY public key")
|
|
|
|
}
|
2023-12-09 15:43:40 +08:00
|
|
|
config.PublicKey, err = ecdh.X25519().NewPublicKey(publicKey[:])
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("fail to create REALITY public key: %w", err)
|
|
|
|
}
|
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
|
|
|
|
}
|