fix PublicKeys panic

This commit is contained in:
xmdhs 2023-10-15 23:51:33 +08:00
parent 9235e92c0a
commit 0442dc3256
No known key found for this signature in database
GPG Key ID: E809D6D43DEFCC95

View File

@ -25,6 +25,8 @@ type Yggdrasil struct {
cache cache.Cache cache cache.Cache
config config.Config config config.Config
prikey *rsa.PrivateKey prikey *rsa.PrivateKey
pubStr func() string
} }
func NewYggdrasil(client *ent.Client, cache cache.Cache, c config.Config, prikey *rsa.PrivateKey) *Yggdrasil { func NewYggdrasil(client *ent.Client, cache cache.Cache, c config.Config, prikey *rsa.PrivateKey) *Yggdrasil {
@ -33,6 +35,10 @@ func NewYggdrasil(client *ent.Client, cache cache.Cache, c config.Config, prikey
cache: cache, cache: cache,
config: c, config: c,
prikey: prikey, prikey: prikey,
pubStr: sync.OnceValue[string](func() string {
derBytes := lo.Must(x509.MarshalPKIXPublicKey(&prikey.PublicKey))
return base64.StdEncoding.EncodeToString(derBytes)
}),
} }
} }
@ -93,8 +99,7 @@ var mojangPubKeyStr = sync.OnceValue(func() string {
func (y *Yggdrasil) PublicKeys(ctx context.Context) yggdrasil.PublicKeys { func (y *Yggdrasil) PublicKeys(ctx context.Context) yggdrasil.PublicKeys {
mojangPub := mojangPubKeyStr() mojangPub := mojangPubKeyStr()
derBytes := lo.Must(x509.MarshalPKIXPublicKey(y.prikey.PublicKey)) myPub := y.pubStr()
myPub := base64.StdEncoding.EncodeToString(derBytes)
pl := []yggdrasil.PublicKeyList{{PublicKey: mojangPub}, {PublicKey: myPub}} pl := []yggdrasil.PublicKeyList{{PublicKey: mojangPub}, {PublicKey: myPub}}