feat: support additional /publickeys endpoint
This commit is contained in:
		@@ -1,5 +1,5 @@
 | 
			
		||||
/*
 | 
			
		||||
 * Copyright (C) 2022. Gardel <sunxinao@hotmail.com> and contributors
 | 
			
		||||
 * Copyright (C) 2022-2023. Gardel <sunxinao@hotmail.com> and contributors
 | 
			
		||||
 *
 | 
			
		||||
 * This program is free software: you can redistribute it and/or modify
 | 
			
		||||
 * it under the terms of the GNU Affero General Public License as published by
 | 
			
		||||
@@ -18,8 +18,11 @@
 | 
			
		||||
package router
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"encoding/base64"
 | 
			
		||||
	"encoding/pem"
 | 
			
		||||
	"github.com/gin-gonic/gin"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"yggdrasil-go/util"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type MetaInfo struct {
 | 
			
		||||
@@ -42,17 +45,31 @@ type ServerMeta struct {
 | 
			
		||||
	SignaturePublickey string   `json:"signaturePublickey"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type KeyPair struct {
 | 
			
		||||
	PrivateKey string `json:"privateKey,omitempty"`
 | 
			
		||||
	PublicKey  string `json:"publicKey,omitempty"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type PublicKeys struct {
 | 
			
		||||
	ProfilePropertyKeys   []KeyPair `json:"profilePropertyKeys,omitempty"`
 | 
			
		||||
	PlayerCertificateKeys []KeyPair `json:"playerCertificateKeys,omitempty"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type HomeRouter interface {
 | 
			
		||||
	Home(c *gin.Context)
 | 
			
		||||
	PublicKeys(c *gin.Context)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type homeRouterImpl struct {
 | 
			
		||||
	serverMeta ServerMeta
 | 
			
		||||
	myPubKey   KeyPair
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func NewHomeRouter(meta *ServerMeta) HomeRouter {
 | 
			
		||||
	signaturePubKey, _ := pem.Decode([]byte(meta.SignaturePublickey))
 | 
			
		||||
	homeRouter := homeRouterImpl{
 | 
			
		||||
		serverMeta: *meta,
 | 
			
		||||
		myPubKey:   KeyPair{PublicKey: base64.StdEncoding.EncodeToString(signaturePubKey.Bytes)},
 | 
			
		||||
	}
 | 
			
		||||
	return &homeRouter
 | 
			
		||||
}
 | 
			
		||||
@@ -61,3 +78,15 @@ func NewHomeRouter(meta *ServerMeta) HomeRouter {
 | 
			
		||||
func (h *homeRouterImpl) Home(c *gin.Context) {
 | 
			
		||||
	c.JSON(http.StatusOK, h.serverMeta)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (h *homeRouterImpl) PublicKeys(c *gin.Context) {
 | 
			
		||||
	publicKeys := PublicKeys{}
 | 
			
		||||
	err := util.GetObject("https://api.minecraftservices.com/publickeys", &publicKeys)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		util.HandleError(c, err)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	publicKeys.ProfilePropertyKeys = append(publicKeys.ProfilePropertyKeys, h.myPubKey)
 | 
			
		||||
	publicKeys.PlayerCertificateKeys = append(publicKeys.PlayerCertificateKeys, h.myPubKey)
 | 
			
		||||
	c.JSON(http.StatusOK, publicKeys)
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user