From 51b3b446eb2fba1fb9915064bbdf8f5633e29ee0 Mon Sep 17 00:00:00 2001 From: bytedream Date: Fri, 30 Oct 2026 00:15:43 +0100 Subject: [PATCH] update bedrock uuid fetch endpoint once more --- astro.config.mjs | 4 +++- src/util/minecraft.ts | 25 ++++++++++++++++++------- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/astro.config.mjs b/astro.config.mjs index 64ea0ae..91e7bf3 100644 --- a/astro.config.mjs +++ b/astro.config.mjs @@ -53,7 +53,9 @@ export default defineConfig({ DATABASE_URI: envField.string({ context: 'server', access: 'secret' }), BASE_HOST: envField.string({ context: 'server', access: 'secret', default: 'http://localhost:4321' }), - BASE_PATH: envField.string({ context: 'server', access: 'secret', default: '/' }) + BASE_PATH: envField.string({ context: 'server', access: 'secret', default: '/' }), + + OPENXBL_API_KEY: envField.string({ context: 'server', access: 'secret', optional: true }) } }, diff --git a/src/util/minecraft.ts b/src/util/minecraft.ts index 4619ba5..6427ca0 100644 --- a/src/util/minecraft.ts +++ b/src/util/minecraft.ts @@ -1,3 +1,5 @@ +import { OPENXBL_API_KEY } from 'astro:env/server'; + export async function getJavaUuid(username: string) { const response = await fetch(`https://api.mojang.com/users/profiles/minecraft/${username}`); if (!response.ok) { @@ -14,15 +16,24 @@ export async function getJavaUuid(username: string) { } export async function getBedrockUuid(username: string): Promise { - const response = await fetch(`https://api.geysermc.org/v2/xbox/xuid/${username}`); + if (!OPENXBL_API_KEY) throw new Error(); + const response = await fetch(`https://api.xbl.io/v2/search/${username}`, { + headers: { + 'X-Authorization': OPENXBL_API_KEY + } + }); if (!response.ok) { - // invalid gamertag, either empty or longer than 16 chars - if (response.status == 400) throw new Error(); - // account doesn't exist or the underlying xbox live api has issues - if (response.status == 503) throw new Error(); + // invalid api key + if (response.status == 401) throw new Error(); + // rate limit + if (response.status == 429) throw new Error(); return null; } - const json = await response.json(); - const xuid: string = (json['xuid'] as number).toString(16); + const json: { content: { people: Array<{ gamertag: string; xuid: string }> } } = await response.json(); + + const xuidPerson = json.content.people.find((v) => v.gamertag.toLowerCase() == username.toLowerCase()); + if (!xuidPerson) throw new Error(); + const xuid = parseInt(xuidPerson.xuid).toString(16); + return `00000000-0000-0000-${xuid.substring(0, 4)}-${xuid.substring(4)}`; }