Files
website/src/lib/server/minecraft.ts

79 lines
3.4 KiB
TypeScript

import { createHash } from 'node:crypto';
export class UserNotFoundError extends Error {
constructor(username: string) {
super(`Ein Spieler mit dem Namen '${username}' konnte nicht gefunden werden`);
}
}
export async function getJavaUuid(username: string): Promise<string> {
const response = await fetch(`https://api.mojang.com/users/profiles/minecraft/${username}`);
if (!response.ok) {
throw response.status < 500
? new UserNotFoundError(username)
: new Error(`mojang server error (${response.status}): ${await response.text()}`);
}
const json = await response.json();
const id: string = json['id'];
// prettier-ignore
return `${id.substring(0, 8)}-${id.substring(8, 12)}-${id.substring(12, 16)}-${id.substring(16, 20)}-${id.substring(20)}`;
}
// https://github.com/carlop3333/XUIDGrabber/blob/main/grabber.js
export async function getBedrockUuid(username: string): Promise<string> {
const initialPageResponse = await fetch('https://cxkes.me/xbox/xuid');
const initialPageContent = await initialPageResponse.text();
const token = /name="_token"\svalue="(?<token>\w+)"/.exec(initialPageContent)?.groups?.token;
if (token === undefined) throw new Error("couldn't grab token from xuid converter website");
const cookies = initialPageResponse.headers.get('set-cookie')?.split(' ');
if (cookies === undefined)
throw new Error("couldn't get response cookies from xuid converter website");
else if (cookies.length < 11) throw new Error('xuid converter website sent unexpected cookies');
const requestBody = new URLSearchParams();
requestBody.set('_token', token);
requestBody.set('gamertag', username);
const resultPageResponse = await fetch('https://cxkes.me/xbox/xuid', {
method: 'post',
body: requestBody,
// prettier-ignore
headers: {
'Host': 'www.cxkes.me',
'Accept-Encoding': 'gzip, deflate,br',
'Content-Length': Buffer.byteLength(requestBody.toString()).toString(),
'Origin': 'https://www.cxkes.me',
'DNT': '1',
'Connection': 'keep-alive',
'Referer': 'https://www.cxkes.me/xbox/xuid',
'Cookie': `${cookies[0]} ${cookies[10].slice(0, cookies[10].length - 1)}`,
'Upgrade-Insecure-Requests': '1',
'Sec-Fectch-Dest': 'document',
'Sec-Fetch-Mode': 'navigate',
'Sec-Fetch-Site': 'same-origin',
'Sec-Fetch-User': '?1',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/115.0',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8',
'Accept-Language': 'es-ES,es;q=0.8,en-US;q=0.5,en;q=0.3',
'Content-Type': 'application/x-www-form-urlencoded'
}
});
const resultPageContent = await resultPageResponse.text();
let xuid: string | undefined;
if ((xuid = /id="xuidHex">(?<xuid>\w+)</.exec(resultPageContent)?.groups?.xuid) === undefined) {
throw new UserNotFoundError(username);
}
return `00000000-0000-0000-${xuid.substring(0, 4)}-${xuid.substring(4)}`;
}
// https://gist.github.com/yushijinhun/69f68397c5bb5bee76e80d192295f6e0
export function getCrackedUuid(username: string): string {
const data = createHash('md5').update(`OfflinePlayer:${username}`).digest('binary').split('');
data[6] = String.fromCharCode((data[6].charCodeAt(0) & 0x0f) | 0x30);
data[8] = String.fromCharCode((data[8].charCodeAt(0) & 0x3f) | 0x80);
const uid = Buffer.from(data.join(''), 'ascii').toString('hex');
// prettier-ignore
return `${uid.substring(0, 8)}-${uid.substring(8, 12)}-${uid.substring(12, 16)}-${uid.substring(16, 20)}-${uid.substring(20)}`;
}