From 0240a412d4c942c1472149f62f9ca3addbdceea3 Mon Sep 17 00:00:00 2001 From: xmdhs Date: Wed, 11 Oct 2023 15:42:01 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=89=8D=E7=AB=AF=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E7=9A=84=E6=8E=A5=E5=8F=A3=E5=9C=B0=E5=9D=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + frontend/.env | 1 + frontend/src/apis/apis.ts | 23 ++++++++-------- frontend/src/utils/root.ts | 6 +++++ frontend/src/views/profile/Profile.tsx | 3 ++- server/route/middleware.go | 7 +++++ server/route/route.go | 3 +++ server/static/static.go | 36 ++++++++++++++++++++++++++ 8 files changed, 68 insertions(+), 12 deletions(-) create mode 100644 frontend/.env create mode 100644 frontend/src/utils/root.ts create mode 100644 server/static/static.go diff --git a/.gitignore b/.gitignore index 8aa5c29..0bd59fa 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ cmd/testserver cmd/authlibskin/config.yaml cmd/authlibskin/skin cmd/authlibskin/authlibskin.exe +server/static/files diff --git a/frontend/.env b/frontend/.env new file mode 100644 index 0000000..44c88d6 --- /dev/null +++ b/frontend/.env @@ -0,0 +1 @@ +VITE_APIADDR = '' \ No newline at end of file diff --git a/frontend/src/apis/apis.ts b/frontend/src/apis/apis.ts index 117ea23..e34773b 100644 --- a/frontend/src/apis/apis.ts +++ b/frontend/src/apis/apis.ts @@ -1,8 +1,9 @@ import type { tokenData, ApiUser, ApiServerInfo, YggProfile, ApiConfig, List, UserInfo, EditUser } from '@/apis/model' import { apiGet } from '@/apis/utils' +import root from '@/utils/root' export async function login(email: string, password: string, captchaToken: string) { - const v = await fetch(import.meta.env.VITE_APIADDR + "/api/v1/user/login", { + const v = await fetch(root() + "/api/v1/user/login", { method: "POST", body: JSON.stringify({ "email": email, @@ -14,7 +15,7 @@ export async function login(email: string, password: string, captchaToken: strin } export async function register(email: string, username: string, password: string, captchaToken: string) { - const v = await fetch(import.meta.env.VITE_APIADDR + "/api/v1/user/reg", { + const v = await fetch(root() + "/api/v1/user/reg", { method: "POST", body: JSON.stringify({ "Email": email, @@ -28,7 +29,7 @@ export async function register(email: string, username: string, password: string export async function userInfo(token: string) { if (token == "") return - const v = await fetch(import.meta.env.VITE_APIADDR + "/api/v1/user", { + const v = await fetch(root() + "/api/v1/user", { headers: { "Authorization": "Bearer " + token } @@ -38,13 +39,13 @@ export async function userInfo(token: string) { export async function serverInfo() { - const v = await fetch(import.meta.env.VITE_APIADDR + "/api/yggdrasil") + const v = await fetch(root() + "/api/yggdrasil") return await v.json() as ApiServerInfo } export async function yggProfile(uuid: string) { if (uuid == "") return - const v = await fetch(import.meta.env.VITE_APIADDR + "/api/yggdrasil/sessionserver/session/minecraft/profile/" + uuid) + const v = await fetch(root() + "/api/yggdrasil/sessionserver/session/minecraft/profile/" + uuid) const data = await v.json() if (!v.ok) { throw new Error(data?.errorMessage) @@ -57,7 +58,7 @@ export async function upTextures(uuid: string, token: string, textureType: 'skin f.set("file", file) f.set("model", model) - const r = await fetch(import.meta.env.VITE_APIADDR + "/api/yggdrasil/api/user/profile/" + uuid + "/" + textureType, { + const r = await fetch(root() + "/api/yggdrasil/api/user/profile/" + uuid + "/" + textureType, { method: "PUT", body: f, headers: { @@ -70,7 +71,7 @@ export async function upTextures(uuid: string, token: string, textureType: 'skin } export async function changePasswd(old: string, newpa: string, token: string) { - const r = await fetch(import.meta.env.VITE_APIADDR + "/api/v1/user/password", { + const r = await fetch(root() + "/api/v1/user/password", { method: "POST", body: JSON.stringify({ "old": old, @@ -84,12 +85,12 @@ export async function changePasswd(old: string, newpa: string, token: string) { } export async function getConfig() { - const r = await fetch(import.meta.env.VITE_APIADDR + "/api/v1/config") + const r = await fetch(root() + "/api/v1/config") return await apiGet(r) } export async function changeName(name: string, token: string) { - const r = await fetch(import.meta.env.VITE_APIADDR + "/api/v1/user/name", { + const r = await fetch(root() + "/api/v1/user/name", { method: "POST", body: JSON.stringify({ "name": name, @@ -102,7 +103,7 @@ export async function changeName(name: string, token: string) { } export async function ListUser(page: number, token: string, email: string, name: string) { - const u = new URL(import.meta.env.VITE_APIADDR + "/api/v1/admin/users") + const u = new URL(root() + "/api/v1/admin/users") u.searchParams.set("page", String(page)) u.searchParams.set("email", email) u.searchParams.set("name", name) @@ -116,7 +117,7 @@ export async function ListUser(page: number, token: string, email: string, name: } export async function editUser(u: EditUser, token: string, uid: string) { - const r = await fetch(import.meta.env.VITE_APIADDR + "/api/v1/admin/user/" + uid, { + const r = await fetch(root() + "/api/v1/admin/user/" + uid, { method: "PATCH", headers: { "Authorization": "Bearer " + token diff --git a/frontend/src/utils/root.ts b/frontend/src/utils/root.ts new file mode 100644 index 0000000..d5d67a1 --- /dev/null +++ b/frontend/src/utils/root.ts @@ -0,0 +1,6 @@ +export default function root() { + if (import.meta.env.VITE_APIADDR != "") { + return import.meta.env.VITE_APIADDR + } + return location.origin +} \ No newline at end of file diff --git a/frontend/src/views/profile/Profile.tsx b/frontend/src/views/profile/Profile.tsx index 0f66eef..4293215 100644 --- a/frontend/src/views/profile/Profile.tsx +++ b/frontend/src/views/profile/Profile.tsx @@ -10,6 +10,7 @@ import { useNavigate } from 'react-router-dom'; import Box from '@mui/material/Box'; import useTitle from '@/hooks/useTitle'; import SkinViewUUID from '@/components/SkinViewUUID'; +import root from '@/utils/root'; const Profile = function Profile() { const navigate = useNavigate(); @@ -61,7 +62,7 @@ const Profile = function Profile() { } function getYggRoot() { - const u = new URL((import.meta.env.VITE_APIADDR ?? location.origin) + "/api/yggdrasil") + const u = new URL(root() + "/api/yggdrasil") return u.toString() } diff --git a/server/route/middleware.go b/server/route/middleware.go index 46d0ef1..9a10c33 100644 --- a/server/route/middleware.go +++ b/server/route/middleware.go @@ -73,3 +73,10 @@ func (l *StructuredLoggerEntry) Panic(v interface{}, stack []byte) { slog.String("panic", fmt.Sprintf("%+v", v)), ) } + +func APILocationIndication(handle http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("X-Authlib-Injector-API-Location", "/api/yggdrasil/") + handle.ServeHTTP(w, r) + }) +} diff --git a/server/route/route.go b/server/route/route.go index e19a79e..cd0c164 100644 --- a/server/route/route.go +++ b/server/route/route.go @@ -11,6 +11,7 @@ import ( "github.com/xmdhs/authlib-skin/config" "github.com/xmdhs/authlib-skin/handle" "github.com/xmdhs/authlib-skin/handle/yggdrasil" + "github.com/xmdhs/authlib-skin/server/static" ) func NewRoute(handelY *yggdrasil.Yggdrasil, handel *handle.Handel, c config.Config, sl slog.Handler) http.Handler { @@ -24,7 +25,9 @@ func NewRoute(handelY *yggdrasil.Yggdrasil, handel *handle.Handel, c config.Conf if c.RaelIP { r.Use(middleware.RealIP) } + r.Use(APILocationIndication) + r.Mount("/", static.StaticServer()) r.Mount("/api/v1", newSkinApi(handel)) r.Mount("/api/yggdrasil", newYggdrasil(handelY)) diff --git a/server/static/static.go b/server/static/static.go new file mode 100644 index 0000000..5805130 --- /dev/null +++ b/server/static/static.go @@ -0,0 +1,36 @@ +package static + +import ( + "embed" + "io/fs" + "net/http" + + "github.com/go-chi/chi/v5" +) + +//go:embed files +var staticFs embed.FS + +func StaticServer() http.Handler { + serverRoot, err := fs.Sub(staticFs, "files") + if err != nil { + panic(err) + } + + r := chi.NewRouter() + + r.Get("/", index) + r.Get("/*", index) + + r.Mount("/assets", http.FileServer(http.FS(serverRoot))) + + return r +} + +func index(w http.ResponseWriter, r *http.Request) { + b, err := staticFs.ReadFile("files/index.html") + if err != nil { + panic(err) + } + w.Write(b) +}