2020-01-02 02:12:45 +05:30
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
2024-02-07 06:06:18 +05:30
|
|
|
"context"
|
2021-02-26 07:15:45 +05:30
|
|
|
"encoding/base64"
|
2020-01-02 02:12:45 +05:30
|
|
|
"encoding/json"
|
2024-02-07 18:54:41 +05:30
|
|
|
"fmt"
|
2024-03-05 17:37:54 +05:30
|
|
|
"io"
|
2020-01-02 02:12:45 +05:30
|
|
|
"net/http"
|
|
|
|
"strings"
|
2021-02-26 07:15:45 +05:30
|
|
|
"time"
|
2020-01-02 02:12:45 +05:30
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
|
2024-02-01 12:42:34 +05:30
|
|
|
"ely.by/chrly/internal/db"
|
|
|
|
"ely.by/chrly/internal/mojang"
|
|
|
|
"ely.by/chrly/internal/utils"
|
2020-01-02 02:12:45 +05:30
|
|
|
)
|
|
|
|
|
2021-02-26 07:15:45 +05:30
|
|
|
var timeNow = time.Now
|
|
|
|
|
2024-01-30 13:35:04 +05:30
|
|
|
type ProfilesProvider interface {
|
2024-02-07 06:06:18 +05:30
|
|
|
FindProfileByUsername(ctx context.Context, username string, allowProxy bool) (*db.Profile, error)
|
2020-01-02 02:12:45 +05:30
|
|
|
}
|
|
|
|
|
2024-03-05 17:37:54 +05:30
|
|
|
// SignerService uses context because in the future we may separate this logic as an external microservice
|
|
|
|
type SignerService interface {
|
|
|
|
Sign(ctx context.Context, data string) (string, error)
|
|
|
|
GetPublicKey(ctx context.Context, format string) (string, error)
|
2021-02-26 07:15:45 +05:30
|
|
|
}
|
|
|
|
|
2020-01-02 02:12:45 +05:30
|
|
|
type Skinsystem struct {
|
2024-01-30 13:35:04 +05:30
|
|
|
ProfilesProvider
|
2024-03-05 17:37:54 +05:30
|
|
|
SignerService
|
2020-04-19 05:01:09 +05:30
|
|
|
TexturesExtraParamName string
|
|
|
|
TexturesExtraParamValue string
|
2020-01-02 02:12:45 +05:30
|
|
|
}
|
|
|
|
|
2024-03-05 17:37:54 +05:30
|
|
|
func (s *Skinsystem) Handler() *mux.Router {
|
2020-01-02 02:12:45 +05:30
|
|
|
router := mux.NewRouter().StrictSlash(true)
|
|
|
|
|
2024-03-05 17:37:54 +05:30
|
|
|
router.HandleFunc("/skins/{username}", s.skinHandler).Methods(http.MethodGet)
|
|
|
|
router.HandleFunc("/cloaks/{username}", s.capeHandler).Methods(http.MethodGet)
|
2024-01-30 13:35:04 +05:30
|
|
|
// TODO: alias /capes/{username}?
|
2024-03-05 17:37:54 +05:30
|
|
|
router.HandleFunc("/textures/{username}", s.texturesHandler).Methods(http.MethodGet)
|
|
|
|
router.HandleFunc("/textures/signed/{username}", s.signedTexturesHandler).Methods(http.MethodGet)
|
|
|
|
router.HandleFunc("/profile/{username}", s.profileHandler).Methods(http.MethodGet)
|
2020-01-02 02:12:45 +05:30
|
|
|
// Legacy
|
2024-03-05 17:37:54 +05:30
|
|
|
router.HandleFunc("/skins", s.skinGetHandler).Methods(http.MethodGet)
|
|
|
|
router.HandleFunc("/cloaks", s.capeGetHandler).Methods(http.MethodGet)
|
2021-02-26 07:15:45 +05:30
|
|
|
// Utils
|
2024-03-05 17:37:54 +05:30
|
|
|
router.HandleFunc("/signature-verification-key.{format:(?:pem|der)}", s.signatureVerificationKeyHandler).Methods(http.MethodGet)
|
2020-01-02 02:12:45 +05:30
|
|
|
|
|
|
|
return router
|
|
|
|
}
|
|
|
|
|
2024-03-05 17:37:54 +05:30
|
|
|
func (s *Skinsystem) skinHandler(response http.ResponseWriter, request *http.Request) {
|
|
|
|
profile, err := s.ProfilesProvider.FindProfileByUsername(request.Context(), parseUsername(mux.Vars(request)["username"]), true)
|
2021-02-26 07:15:45 +05:30
|
|
|
if err != nil {
|
2024-02-07 18:54:41 +05:30
|
|
|
apiServerError(response, fmt.Errorf("unable to retrieve a profile: %w", err))
|
2024-01-30 13:35:04 +05:30
|
|
|
return
|
2020-04-30 00:24:40 +05:30
|
|
|
}
|
|
|
|
|
2024-01-30 13:35:04 +05:30
|
|
|
if profile == nil || profile.SkinUrl == "" {
|
2020-01-02 02:12:45 +05:30
|
|
|
response.WriteHeader(http.StatusNotFound)
|
|
|
|
}
|
|
|
|
|
2024-01-30 13:35:04 +05:30
|
|
|
http.Redirect(response, request, profile.SkinUrl, http.StatusMovedPermanently)
|
2020-01-02 02:12:45 +05:30
|
|
|
}
|
|
|
|
|
2024-03-05 17:37:54 +05:30
|
|
|
func (s *Skinsystem) skinGetHandler(response http.ResponseWriter, request *http.Request) {
|
2020-01-02 02:12:45 +05:30
|
|
|
username := request.URL.Query().Get("name")
|
|
|
|
if username == "" {
|
|
|
|
response.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
mux.Vars(request)["username"] = username
|
|
|
|
|
2024-03-05 17:37:54 +05:30
|
|
|
s.skinHandler(response, request)
|
2020-01-02 02:12:45 +05:30
|
|
|
}
|
|
|
|
|
2024-03-05 17:37:54 +05:30
|
|
|
func (s *Skinsystem) capeHandler(response http.ResponseWriter, request *http.Request) {
|
|
|
|
profile, err := s.ProfilesProvider.FindProfileByUsername(request.Context(), parseUsername(mux.Vars(request)["username"]), true)
|
2021-02-26 07:15:45 +05:30
|
|
|
if err != nil {
|
2024-02-07 18:54:41 +05:30
|
|
|
apiServerError(response, fmt.Errorf("unable to retrieve a profile: %w", err))
|
2024-01-30 13:35:04 +05:30
|
|
|
return
|
2020-01-02 02:12:45 +05:30
|
|
|
}
|
|
|
|
|
2024-01-30 13:35:04 +05:30
|
|
|
if profile == nil || profile.CapeUrl == "" {
|
2020-04-30 00:24:40 +05:30
|
|
|
response.WriteHeader(http.StatusNotFound)
|
|
|
|
}
|
|
|
|
|
2024-01-30 13:35:04 +05:30
|
|
|
http.Redirect(response, request, profile.CapeUrl, http.StatusMovedPermanently)
|
2020-01-02 02:12:45 +05:30
|
|
|
}
|
|
|
|
|
2024-03-05 17:37:54 +05:30
|
|
|
func (s *Skinsystem) capeGetHandler(response http.ResponseWriter, request *http.Request) {
|
2020-01-02 02:12:45 +05:30
|
|
|
username := request.URL.Query().Get("name")
|
|
|
|
if username == "" {
|
|
|
|
response.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
mux.Vars(request)["username"] = username
|
|
|
|
|
2024-03-05 17:37:54 +05:30
|
|
|
s.capeHandler(response, request)
|
2020-01-02 02:12:45 +05:30
|
|
|
}
|
|
|
|
|
2024-03-05 17:37:54 +05:30
|
|
|
func (s *Skinsystem) texturesHandler(response http.ResponseWriter, request *http.Request) {
|
|
|
|
profile, err := s.ProfilesProvider.FindProfileByUsername(request.Context(), mux.Vars(request)["username"], true)
|
2021-02-26 07:15:45 +05:30
|
|
|
if err != nil {
|
2024-02-07 18:54:41 +05:30
|
|
|
apiServerError(response, fmt.Errorf("unable to retrieve a profile: %w", err))
|
2024-01-30 13:35:04 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if profile == nil {
|
|
|
|
response.WriteHeader(http.StatusNotFound)
|
|
|
|
return
|
2021-02-26 07:15:45 +05:30
|
|
|
}
|
|
|
|
|
2024-01-30 13:35:04 +05:30
|
|
|
if profile.SkinUrl == "" && profile.CapeUrl == "" {
|
2021-02-26 07:15:45 +05:30
|
|
|
response.WriteHeader(http.StatusNoContent)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-30 13:35:04 +05:30
|
|
|
textures := texturesFromProfile(profile)
|
|
|
|
|
|
|
|
responseData, _ := json.Marshal(textures)
|
2021-02-26 07:15:45 +05:30
|
|
|
response.Header().Set("Content-Type", "application/json")
|
|
|
|
_, _ = response.Write(responseData)
|
|
|
|
}
|
|
|
|
|
2024-03-05 17:37:54 +05:30
|
|
|
func (s *Skinsystem) signedTexturesHandler(response http.ResponseWriter, request *http.Request) {
|
|
|
|
profile, err := s.ProfilesProvider.FindProfileByUsername(
|
2024-02-07 06:06:18 +05:30
|
|
|
request.Context(),
|
2024-01-30 13:35:04 +05:30
|
|
|
mux.Vars(request)["username"],
|
|
|
|
getToBool(request.URL.Query().Get("proxy")),
|
|
|
|
)
|
2021-02-26 07:15:45 +05:30
|
|
|
if err != nil {
|
2024-02-07 18:54:41 +05:30
|
|
|
apiServerError(response, fmt.Errorf("unable to retrieve a profile: %w", err))
|
2024-01-30 13:35:04 +05:30
|
|
|
return
|
2021-02-26 07:15:45 +05:30
|
|
|
}
|
|
|
|
|
2024-01-30 13:35:04 +05:30
|
|
|
if profile == nil {
|
|
|
|
response.WriteHeader(http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if profile.MojangTextures == "" {
|
2021-02-26 07:15:45 +05:30
|
|
|
response.WriteHeader(http.StatusNoContent)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-30 13:35:04 +05:30
|
|
|
profileResponse := &mojang.ProfileResponse{
|
|
|
|
Id: profile.Uuid,
|
2021-02-26 07:15:45 +05:30
|
|
|
Name: profile.Username,
|
|
|
|
Props: []*mojang.Property{
|
|
|
|
{
|
|
|
|
Name: "textures",
|
|
|
|
Signature: profile.MojangSignature,
|
|
|
|
Value: profile.MojangTextures,
|
|
|
|
},
|
|
|
|
{
|
2024-03-05 17:37:54 +05:30
|
|
|
Name: s.TexturesExtraParamName,
|
|
|
|
Value: s.TexturesExtraParamValue,
|
2021-02-26 07:15:45 +05:30
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
responseJson, _ := json.Marshal(profileResponse)
|
|
|
|
response.Header().Set("Content-Type", "application/json")
|
|
|
|
_, _ = response.Write(responseJson)
|
|
|
|
}
|
|
|
|
|
2024-03-05 17:37:54 +05:30
|
|
|
func (s *Skinsystem) profileHandler(response http.ResponseWriter, request *http.Request) {
|
|
|
|
profile, err := s.ProfilesProvider.FindProfileByUsername(request.Context(), mux.Vars(request)["username"], true)
|
2021-02-26 07:15:45 +05:30
|
|
|
if err != nil {
|
2024-02-07 18:54:41 +05:30
|
|
|
apiServerError(response, fmt.Errorf("unable to retrieve a profile: %w", err))
|
2024-01-30 13:35:04 +05:30
|
|
|
return
|
2021-02-26 07:15:45 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if profile == nil {
|
2024-01-30 13:35:04 +05:30
|
|
|
response.WriteHeader(http.StatusNotFound)
|
2021-02-26 07:15:45 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
texturesPropContent := &mojang.TexturesProp{
|
|
|
|
Timestamp: utils.UnixMillisecond(timeNow()),
|
2024-01-30 13:35:04 +05:30
|
|
|
ProfileID: profile.Uuid,
|
2021-02-26 07:15:45 +05:30
|
|
|
ProfileName: profile.Username,
|
2024-01-30 13:35:04 +05:30
|
|
|
Textures: texturesFromProfile(profile),
|
2021-02-26 07:15:45 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
texturesPropValueJson, _ := json.Marshal(texturesPropContent)
|
|
|
|
texturesPropEncodedValue := base64.StdEncoding.EncodeToString(texturesPropValueJson)
|
|
|
|
|
|
|
|
texturesProp := &mojang.Property{
|
|
|
|
Name: "textures",
|
|
|
|
Value: texturesPropEncodedValue,
|
|
|
|
}
|
|
|
|
|
2024-01-30 13:35:04 +05:30
|
|
|
if request.URL.Query().Has("unsigned") && !getToBool(request.URL.Query().Get("unsigned")) {
|
2024-03-05 17:37:54 +05:30
|
|
|
signature, err := s.SignerService.Sign(request.Context(), texturesProp.Value)
|
2021-02-26 07:15:45 +05:30
|
|
|
if err != nil {
|
2024-02-07 18:54:41 +05:30
|
|
|
apiServerError(response, fmt.Errorf("unable to sign textures: %w", err))
|
2024-01-30 13:35:04 +05:30
|
|
|
return
|
2021-02-26 07:15:45 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
texturesProp.Signature = signature
|
|
|
|
}
|
|
|
|
|
2024-01-30 13:35:04 +05:30
|
|
|
profileResponse := &mojang.ProfileResponse{
|
|
|
|
Id: profile.Uuid,
|
2021-02-26 07:15:45 +05:30
|
|
|
Name: profile.Username,
|
|
|
|
Props: []*mojang.Property{
|
|
|
|
texturesProp,
|
|
|
|
{
|
2024-03-05 17:37:54 +05:30
|
|
|
Name: s.TexturesExtraParamName,
|
|
|
|
Value: s.TexturesExtraParamValue,
|
2021-02-26 07:15:45 +05:30
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
responseJson, _ := json.Marshal(profileResponse)
|
|
|
|
response.Header().Set("Content-Type", "application/json")
|
|
|
|
_, _ = response.Write(responseJson)
|
|
|
|
}
|
|
|
|
|
2024-03-05 17:37:54 +05:30
|
|
|
func (s *Skinsystem) signatureVerificationKeyHandler(response http.ResponseWriter, request *http.Request) {
|
|
|
|
format := mux.Vars(request)["format"]
|
|
|
|
publicKey, err := s.SignerService.GetPublicKey(request.Context(), format)
|
2021-02-26 07:15:45 +05:30
|
|
|
if err != nil {
|
2024-03-05 17:37:54 +05:30
|
|
|
apiServerError(response, fmt.Errorf("unable to retrieve public key: %w", err))
|
|
|
|
return
|
2021-02-26 07:15:45 +05:30
|
|
|
}
|
|
|
|
|
2024-03-05 17:37:54 +05:30
|
|
|
if format == "pem" {
|
|
|
|
response.Header().Set("Content-Type", "application/x-pem-file")
|
|
|
|
response.Header().Set("Content-Disposition", `attachment; filename="yggdrasil_session_pubkey.pem"`)
|
2021-03-03 18:03:56 +05:30
|
|
|
} else {
|
|
|
|
response.Header().Set("Content-Type", "application/octet-stream")
|
2024-03-05 17:37:54 +05:30
|
|
|
response.Header().Set("Content-Disposition", `attachment; filename="yggdrasil_session_pubkey.der"`)
|
2021-03-03 18:03:56 +05:30
|
|
|
}
|
2024-03-05 17:37:54 +05:30
|
|
|
|
|
|
|
_, _ = io.WriteString(response, publicKey)
|
2021-02-26 07:15:45 +05:30
|
|
|
}
|
|
|
|
|
2024-01-30 13:35:04 +05:30
|
|
|
func parseUsername(username string) string {
|
|
|
|
return strings.TrimSuffix(username, ".png")
|
|
|
|
}
|
2020-01-02 02:12:45 +05:30
|
|
|
|
2024-01-30 13:35:04 +05:30
|
|
|
func getToBool(v string) bool {
|
|
|
|
return v == "true" || v == "1" || v == "yes"
|
|
|
|
}
|
2020-01-02 02:12:45 +05:30
|
|
|
|
2024-01-30 13:35:04 +05:30
|
|
|
func texturesFromProfile(profile *db.Profile) *mojang.TexturesResponse {
|
|
|
|
var skin *mojang.SkinTexturesResponse
|
|
|
|
if profile.SkinUrl != "" {
|
|
|
|
skin = &mojang.SkinTexturesResponse{
|
|
|
|
Url: profile.SkinUrl,
|
2021-02-26 07:15:45 +05:30
|
|
|
}
|
2024-01-30 13:35:04 +05:30
|
|
|
if profile.SkinModel != "" {
|
|
|
|
skin.Metadata = &mojang.SkinTexturesMetadata{
|
|
|
|
Model: profile.SkinModel,
|
2021-02-26 07:15:45 +05:30
|
|
|
}
|
2020-01-02 02:12:45 +05:30
|
|
|
}
|
2024-01-30 13:35:04 +05:30
|
|
|
}
|
2021-02-26 07:15:45 +05:30
|
|
|
|
2024-01-30 13:35:04 +05:30
|
|
|
var cape *mojang.CapeTexturesResponse
|
|
|
|
if profile.CapeUrl != "" {
|
|
|
|
cape = &mojang.CapeTexturesResponse{
|
|
|
|
Url: profile.CapeUrl,
|
2021-02-26 07:15:45 +05:30
|
|
|
}
|
2020-01-02 02:12:45 +05:30
|
|
|
}
|
|
|
|
|
2024-01-30 13:35:04 +05:30
|
|
|
return &mojang.TexturesResponse{
|
|
|
|
Skin: skin,
|
|
|
|
Cape: cape,
|
|
|
|
}
|
2020-01-02 02:12:45 +05:30
|
|
|
}
|