Files
ionscale/internal/handlers/key.go
T
Johan Siebens 5ad89ff02f initial working version
Signed-off-by: Johan Siebens <johan.siebens@gmail.com>
2022-05-09 21:54:06 +02:00

40 lines
869 B
Go

package handlers
import (
"github.com/jsiebens/ionscale/internal/config"
"github.com/labstack/echo/v4"
"net/http"
"strconv"
"tailscale.com/tailcfg"
)
const (
NoiseCapabilityVersion = 28
)
func KeyHandler(keys *config.ServerKeys) echo.HandlerFunc {
legacyPublicKey := keys.LegacyControlKey.Public()
publicKey := keys.ControlKey.Public()
return func(c echo.Context) error {
v := c.QueryParam("v")
if v != "" {
clientCapabilityVersion, err := strconv.Atoi(v)
if err != nil {
return c.String(http.StatusBadRequest, "Invalid version")
}
if clientCapabilityVersion >= NoiseCapabilityVersion {
resp := tailcfg.OverTLSPublicKeyResponse{
LegacyPublicKey: legacyPublicKey,
PublicKey: publicKey,
}
return c.JSON(http.StatusOK, resp)
}
}
return c.String(http.StatusOK, legacyPublicKey.UntypedHexString())
}
}