mirror of
https://github.com/WorldEditAxe/eaglerproxy.git
synced 2024-12-22 07:14:13 -08:00
Compare commits
4 Commits
375a275442
...
26275fa0d7
Author | SHA1 | Date | |
---|---|---|---|
|
26275fa0d7 | ||
|
4b465802d1 | ||
|
36adb113c0 | ||
|
d6fa0c082d |
1315
package-lock.json
generated
1315
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
|
@ -16,6 +16,7 @@
|
|||
"@types/ws": "8.5.4",
|
||||
"chalk": "5.2.0",
|
||||
"dotenv": "16.0.3",
|
||||
"jimp": "^0.22.12",
|
||||
"minecraft-protocol": "^1.26.5",
|
||||
"node-fetch": "3.3.1",
|
||||
"parse-domain": "7.0.1",
|
||||
|
@ -24,7 +25,6 @@
|
|||
"prismarine-chunk": "1.33.0",
|
||||
"prismarine-registry": "1.6.0",
|
||||
"semver": "^7.6.0",
|
||||
"sharp": "^0.33.2",
|
||||
"ws": "8.12.0"
|
||||
},
|
||||
"type": "module"
|
||||
|
|
|
@ -4,31 +4,47 @@
|
|||
import { Config } from "./launcher_types.js";
|
||||
|
||||
export const config: Config = {
|
||||
bridge: {
|
||||
enabled: false,
|
||||
motd: null,
|
||||
},
|
||||
adapter: {
|
||||
name: "EaglerProxy",
|
||||
bindHost: "0.0.0.0",
|
||||
bindPort: 8080,
|
||||
maxConcurrentClients: 20,
|
||||
skinUrlWhitelist: undefined,
|
||||
useNatives: false,
|
||||
skinServer: {
|
||||
skinUrlWhitelist: undefined,
|
||||
cache: {
|
||||
useCache: true,
|
||||
folderName: "skinCache",
|
||||
skinCacheLifetime: 60 * 60 * 1000,
|
||||
skinCachePruneInterval: 10 * 60 * 1000,
|
||||
},
|
||||
},
|
||||
motd: true
|
||||
? "FORWARD"
|
||||
: {
|
||||
iconURL: "logo.png",
|
||||
iconURL: "motd.png",
|
||||
l1: "yes",
|
||||
l2: "no",
|
||||
},
|
||||
ratelimits: {
|
||||
lockout: 10,
|
||||
limits: {
|
||||
http: 100,
|
||||
ws: 100,
|
||||
motd: 100,
|
||||
skins: 100, // adjust as necessary
|
||||
skinsIp: 10000,
|
||||
connect: 100,
|
||||
},
|
||||
},
|
||||
origins: {
|
||||
allowOfflineDownloads: true,
|
||||
originWhitelist: null,
|
||||
originBlacklist: null,
|
||||
},
|
||||
server: {
|
||||
host: "no",
|
||||
port: 46625,
|
||||
host: "127.0.0.1",
|
||||
port: 1111,
|
||||
},
|
||||
tls: undefined,
|
||||
},
|
||||
|
|
|
@ -8,16 +8,19 @@ import { PROXY_BRANDING } from "./meta.js";
|
|||
import { PluginManager } from "./proxy/pluginLoader/PluginManager.js";
|
||||
import { dirname, join } from "path";
|
||||
import { fileURLToPath } from "url";
|
||||
import { ImageEditor } from "./proxy/skins/ImageEditor.js";
|
||||
|
||||
const logger = new Logger("Launcher");
|
||||
let proxy: Proxy;
|
||||
|
||||
global.CONFIG = config;
|
||||
config.adapter.useNatives = config.adapter.useNatives ?? true;
|
||||
|
||||
logger.info("Loading libraries...");
|
||||
await ImageEditor.loadLibraries(config.adapter.useNatives);
|
||||
|
||||
logger.info("Loading plugins...");
|
||||
const pluginManager = new PluginManager(
|
||||
join(dirname(fileURLToPath(import.meta.url)), "plugins")
|
||||
);
|
||||
const pluginManager = new PluginManager(join(dirname(fileURLToPath(import.meta.url)), "plugins"));
|
||||
global.PLUGIN_MANAGER = pluginManager;
|
||||
await pluginManager.loadPlugins();
|
||||
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
export type Config = {
|
||||
bridge: BridgeOptions;
|
||||
adapter: AdapterOptions;
|
||||
};
|
||||
|
||||
|
@ -19,7 +18,16 @@ export type AdapterOptions = {
|
|||
bindHost: string;
|
||||
bindPort: number;
|
||||
maxConcurrentClients: 20;
|
||||
skinUrlWhitelist?: string[];
|
||||
useNatives?: boolean;
|
||||
skinServer: {
|
||||
skinUrlWhitelist?: string[];
|
||||
cache: {
|
||||
useCache: boolean;
|
||||
folderName?: string;
|
||||
skinCacheLifetime?: number;
|
||||
skinCachePruneInterval?: number;
|
||||
};
|
||||
};
|
||||
origins: {
|
||||
allowOfflineDownloads: boolean;
|
||||
originWhitelist: string[];
|
||||
|
@ -32,6 +40,17 @@ export type AdapterOptions = {
|
|||
l1: string;
|
||||
l2?: string;
|
||||
};
|
||||
ratelimits: {
|
||||
lockout: number;
|
||||
limits: {
|
||||
http: number;
|
||||
ws: number;
|
||||
motd: number;
|
||||
connect: number;
|
||||
skins: number;
|
||||
skinsIp: number;
|
||||
};
|
||||
};
|
||||
server: {
|
||||
host: string;
|
||||
port: number;
|
||||
|
|
|
@ -144,7 +144,7 @@ export class CustomAuthflow {
|
|||
);
|
||||
}
|
||||
|
||||
async getMinecraftJavaToken(options: any = {}) {
|
||||
async getMinecraftJavaToken(options: any = {}, quit: { quit: boolean }) {
|
||||
const response: any = { token: "", entitlements: {} as any, profile: {} as any };
|
||||
if (await this.mca.verifyTokens()) {
|
||||
const { token } = await this.mca.getCachedAccessToken();
|
||||
|
@ -154,6 +154,7 @@ export class CustomAuthflow {
|
|||
async () => {
|
||||
const xsts = await this.getXboxToken(Endpoints.PCXSTSRelyingParty);
|
||||
response.token = await this.mca.getAccessToken(xsts);
|
||||
if (quit.quit) return;
|
||||
},
|
||||
() => {
|
||||
this.xbl.forceRefresh = true;
|
||||
|
@ -161,6 +162,7 @@ export class CustomAuthflow {
|
|||
2
|
||||
);
|
||||
}
|
||||
if (quit.quit) return;
|
||||
|
||||
if (options.fetchEntitlements) {
|
||||
response.entitlements = await this.mca.fetchEntitlements(response.token).catch((e) => {});
|
||||
|
|
|
@ -31,7 +31,7 @@ class InMemoryCache {
|
|||
}
|
||||
}
|
||||
|
||||
export function auth(): EventEmitter {
|
||||
export function auth(quit: { quit: boolean }): EventEmitter {
|
||||
const emitter = new EventEmitter();
|
||||
const userIdentifier = randomUUID();
|
||||
const flow = new CustomAuthflow(
|
||||
|
@ -48,17 +48,12 @@ export function auth(): EventEmitter {
|
|||
}
|
||||
);
|
||||
flow
|
||||
.getMinecraftJavaToken({ fetchProfile: true })
|
||||
.getMinecraftJavaToken({ fetchProfile: true }, quit)
|
||||
.then(async (data) => {
|
||||
if (!data || quit.quit) return;
|
||||
|
||||
const _data = (await (flow as any).mca.cache.getCached()).mca;
|
||||
if (data.profile == null || (data.profile as any).error)
|
||||
return emitter.emit(
|
||||
"error",
|
||||
new Error(
|
||||
Enums.ChatColor.RED +
|
||||
"Couldn't fetch profile data, does the account own Minecraft: Java Edition?"
|
||||
)
|
||||
);
|
||||
if (data.profile == null || (data.profile as any).error) return emitter.emit("error", new Error(Enums.ChatColor.RED + "Couldn't fetch profile data, does the account own Minecraft: Java Edition?"));
|
||||
emitter.emit("done", {
|
||||
accessToken: data.token,
|
||||
expiresOn: _data.obtainedOn + _data.expires_in * 1000,
|
||||
|
@ -67,19 +62,8 @@ export function auth(): EventEmitter {
|
|||
});
|
||||
})
|
||||
.catch((err) => {
|
||||
if (err.toString().includes("Not Found"))
|
||||
emitter.emit(
|
||||
"error",
|
||||
new Error(
|
||||
Enums.ChatColor.RED +
|
||||
"The provided account doesn't own Minecraft: Java Edition!"
|
||||
)
|
||||
);
|
||||
else
|
||||
emitter.emit(
|
||||
"error",
|
||||
new Error(Enums.ChatColor.YELLOW + err.toString())
|
||||
);
|
||||
if (err.toString().includes("Not Found")) emitter.emit("error", new Error(Enums.ChatColor.RED + "The provided account doesn't own Minecraft: Java Edition!"));
|
||||
else emitter.emit("error", new Error(Enums.ChatColor.YELLOW + err.toString()));
|
||||
});
|
||||
return emitter;
|
||||
}
|
||||
|
|
|
@ -9,25 +9,12 @@ export async function getTokenProfileEasyMc(token: string): Promise<object> {
|
|||
token,
|
||||
}),
|
||||
};
|
||||
const res = await fetch(
|
||||
"https://api.easymc.io/v1/token/redeem",
|
||||
fetchOptions
|
||||
);
|
||||
const res = await fetch("https://api.easymc.io/v1/token/redeem", fetchOptions);
|
||||
const resJson = await res.json();
|
||||
|
||||
if (resJson.error) throw new Error(Enums.ChatColor.RED + `${resJson.error}`);
|
||||
if (!resJson)
|
||||
throw new Error(
|
||||
Enums.ChatColor.RED + "EasyMC replied with an empty response!?"
|
||||
);
|
||||
if (
|
||||
resJson.session?.length !== 43 ||
|
||||
resJson.mcName?.length < 3 ||
|
||||
resJson.uuid?.length !== 36
|
||||
)
|
||||
throw new Error(
|
||||
Enums.ChatColor.RED + "Invalid response from EasyMC received!"
|
||||
);
|
||||
if (!resJson) throw new Error(Enums.ChatColor.RED + "EasyMC replied with an empty response!?");
|
||||
if (resJson.session?.length !== 43 || resJson.mcName?.length < 3 || resJson.uuid?.length !== 36) throw new Error(Enums.ChatColor.RED + "Invalid response from EasyMC received!");
|
||||
return {
|
||||
auth: "mojang",
|
||||
sessionServer: "https://sessionserver.easymc.io",
|
||||
|
|
|
@ -2,9 +2,11 @@ export const config = {
|
|||
bindInternalServerPort: 25569,
|
||||
bindInternalServerIp: "127.0.0.1",
|
||||
allowCustomPorts: true,
|
||||
allowDirectConnectEndpoints: false,
|
||||
disallowHypixel: false,
|
||||
showDisclaimers: false,
|
||||
authentication: {
|
||||
enabled: true,
|
||||
enabled: false,
|
||||
password: "nope",
|
||||
},
|
||||
};
|
||||
|
|
|
@ -222,6 +222,8 @@ CONFIG.adapter.motd = {
|
|||
l1: Enums.ChatColor.GOLD + "EaglerProxy as a Service",
|
||||
};
|
||||
|
||||
PLUGIN_MANAGER.addListener("proxyFinishLoading", () => {
|
||||
registerEndpoints();
|
||||
});
|
||||
if (config.allowDirectConnectEndpoints) {
|
||||
PLUGIN_MANAGER.addListener("proxyFinishLoading", () => {
|
||||
registerEndpoints();
|
||||
});
|
||||
}
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
import { ServerDeviceCodeResponse, auth } from "../auth.js";
|
||||
import { config } from "../config.js";
|
||||
|
||||
export async function registerEndpoints() {
|
||||
|
@ -30,4 +31,77 @@ export async function registerEndpoints() {
|
|||
);
|
||||
}
|
||||
});
|
||||
|
||||
proxy.on("wsConnection", (ws, req, ctx) => {
|
||||
try {
|
||||
if (req.url.startsWith("/eagpaas/token")) {
|
||||
ctx.handled = true;
|
||||
if (config.authentication.enabled) {
|
||||
if (req.headers.authorization !== `Basic ${config.authentication.password}`) {
|
||||
ws.send(
|
||||
JSON.stringify({
|
||||
type: "ERROR",
|
||||
error: "Access Denied",
|
||||
})
|
||||
);
|
||||
ws.close();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
const quit = { quit: false },
|
||||
authHandler = auth(quit),
|
||||
codeCallback = (code: ServerDeviceCodeResponse) => {
|
||||
ws.send(
|
||||
JSON.stringify({
|
||||
type: "CODE",
|
||||
data: code,
|
||||
})
|
||||
);
|
||||
};
|
||||
ws.once("close", () => {
|
||||
quit.quit = true;
|
||||
});
|
||||
authHandler
|
||||
.on("code", codeCallback)
|
||||
.on("error", (err) => {
|
||||
ws.send(
|
||||
JSON.stringify({
|
||||
type: "ERROR",
|
||||
reason: err,
|
||||
})
|
||||
);
|
||||
ws.close();
|
||||
})
|
||||
.on("done", (result) => {
|
||||
ws.send(
|
||||
JSON.stringify({
|
||||
type: "COMPLETE",
|
||||
data: result,
|
||||
})
|
||||
);
|
||||
ws.close();
|
||||
});
|
||||
} else if (req.url.startsWith("/eagpaas/ping")) {
|
||||
ctx.handled = true;
|
||||
if (config.authentication.enabled) {
|
||||
if (req.headers.authorization !== `Basic ${config.authentication.password}`) {
|
||||
ws.send(
|
||||
JSON.stringify({
|
||||
type: "ERROR",
|
||||
error: "Access Denied",
|
||||
})
|
||||
);
|
||||
ws.close();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
ws.once("message", (_) => {
|
||||
ws.send(_);
|
||||
ws.close();
|
||||
});
|
||||
}
|
||||
} catch (err) {}
|
||||
});
|
||||
}
|
||||
|
|
|
@ -241,17 +241,27 @@ export async function onConnect(client: ClientState) {
|
|||
client.state = ConnectionState.AUTH;
|
||||
client.lastStatusUpdate = Date.now();
|
||||
|
||||
sendMessageWarning(client.gameClient, `WARNING: This proxy allows you to connect to any 1.8.9 server. Gameplay has shown no major issues, but please note that EaglercraftX may flag some anticheats while playing.`);
|
||||
await new Promise((res) => setTimeout(res, 2000));
|
||||
client.gameClient.on("packet", (packet, meta) => {
|
||||
if (meta.name == "client_command" && packet.payload == 1) {
|
||||
client.gameClient.write("statistics", {
|
||||
entries: [],
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
sendMessageWarning(
|
||||
client.gameClient,
|
||||
`ADVISORY FOR HYPIXEL PLAYERS: THIS PROXY FALLS UNDER HYPIXEL'S "DISALLOWED MODIFICATIONS" MOD CATEGORY. JOINING THE SERVER WILL RESULT IN AN IRREPEALABLE PUNISHMENT BEING APPLIED TO YOUR ACCOUNT. YOU HAVE BEEN WARNED - PLAY AT YOUR OWN RISK!`
|
||||
);
|
||||
await new Promise((res) => setTimeout(res, 2000));
|
||||
if (config.showDisclaimers) {
|
||||
sendMessageWarning(client.gameClient, `WARNING: This proxy allows you to connect to any 1.8.9 server. Gameplay has shown no major issues, but please note that EaglercraftX may flag some anticheats while playing.`);
|
||||
await new Promise((res) => setTimeout(res, 2000));
|
||||
|
||||
sendMessageWarning(client.gameClient, `WARNING: It is highly suggested that you turn down settings, as gameplay tends to be very laggy and unplayable on low powered devices.`);
|
||||
await new Promise((res) => setTimeout(res, 2000));
|
||||
sendMessageWarning(
|
||||
client.gameClient,
|
||||
`ADVISORY FOR HYPIXEL PLAYERS: THIS PROXY FALLS UNDER HYPIXEL'S "DISALLOWED MODIFICATIONS" MOD CATEGORY. JOINING THE SERVER WILL RESULT IN AN IRREPEALABLE PUNISHMENT BEING APPLIED TO YOUR ACCOUNT. YOU HAVE BEEN WARNED - PLAY AT YOUR OWN RISK!`
|
||||
);
|
||||
await new Promise((res) => setTimeout(res, 2000));
|
||||
|
||||
sendMessageWarning(client.gameClient, `WARNING: It is highly suggested that you turn down settings, as gameplay tends to be very laggy and unplayable on low powered devices.`);
|
||||
await new Promise((res) => setTimeout(res, 2000));
|
||||
}
|
||||
|
||||
if (config.authentication.enabled) {
|
||||
sendCustomMessage(client.gameClient, "This instance is password-protected. Sign in with /password <password>", "gold");
|
||||
|
@ -346,20 +356,27 @@ export async function onConnect(client: ClientState) {
|
|||
}
|
||||
|
||||
if (chosenOption == ConnectType.ONLINE) {
|
||||
sendMessageWarning(
|
||||
client.gameClient,
|
||||
`WARNING: You will be prompted to log in via Microsoft to obtain a session token necessary to join games. Any data related to your account will not be saved and for transparency reasons this proxy's source code is available on Github.`
|
||||
);
|
||||
if (config.showDisclaimers) {
|
||||
sendMessageWarning(
|
||||
client.gameClient,
|
||||
`WARNING: You will be prompted to log in via Microsoft to obtain a session token necessary to join games. Any data related to your account will not be saved and for transparency reasons this proxy's source code is available on Github.`
|
||||
);
|
||||
}
|
||||
await new Promise((res) => setTimeout(res, 2000));
|
||||
|
||||
client.lastStatusUpdate = Date.now();
|
||||
let errored = false,
|
||||
savedAuth;
|
||||
const authHandler = auth(),
|
||||
const quit = { quit: false },
|
||||
authHandler = auth(quit),
|
||||
codeCallback = (code: ServerDeviceCodeResponse) => {
|
||||
updateState(client.gameClient, "AUTH", code.verification_uri, code.user_code);
|
||||
sendMessageLogin(client.gameClient, code.verification_uri, code.user_code);
|
||||
};
|
||||
client.gameClient.once("end", (res) => {
|
||||
quit.quit = true;
|
||||
});
|
||||
|
||||
authHandler.once("error", (err) => {
|
||||
if (!client.gameClient.ended) client.gameClient.end(err.message);
|
||||
errored = true;
|
||||
|
|
|
@ -2,16 +2,16 @@ import * as meta from "../meta.js";
|
|||
|
||||
export namespace Constants {
|
||||
export const EAGLERCRAFT_SKIN_CHANNEL_NAME: string = "EAG|Skins-1.8";
|
||||
export const MAGIC_ENDING_SERVER_SKIN_DOWNLOAD_BUILTIN: number[] = [
|
||||
0x00, 0x00, 0x00,
|
||||
];
|
||||
export const MAGIC_ENDING_CLIENT_UPLOAD_SKIN_BUILTIN: number[] = [
|
||||
0x00, 0x05, 0x01, 0x00, 0x00, 0x00,
|
||||
];
|
||||
export const MAGIC_ENDING_SERVER_SKIN_DOWNLOAD_BUILTIN: number[] = [0x00, 0x00, 0x00];
|
||||
export const MAGIC_ENDING_CLIENT_UPLOAD_SKIN_BUILTIN: number[] = [0x00, 0x05, 0x01, 0x00, 0x00, 0x00];
|
||||
export const EAGLERCRAFT_SKIN_CUSTOM_LENGTH = 64 ** 2 * 4;
|
||||
|
||||
export const JOIN_SERVER_PACKET = 0x01;
|
||||
export const PLAYER_LOOK_PACKET = 0x08;
|
||||
|
||||
export const ICON_SQRT = 64;
|
||||
export const END_BUFFER_LENGTH = ICON_SQRT ** 8;
|
||||
export const IMAGE_DATA_PREPEND = "data:image/png;base64,";
|
||||
}
|
||||
|
||||
export const UPGRADE_REQUIRED_RESPONSE = `<!DOCTYPE html><!-- Served by ${meta.PROXY_BRANDING} (version: ${meta.PROXY_VERSION}) --><html> <head> <title>EaglerProxy landing page</title> <style> :root { font-family: "Arial" } code { padding: 3px 10px 3px 10px; border-radius: 5px; font-family: monospace; background-color: #1a1a1a; color: white; } </style> <script type="text/javascript"> window.addEventListener('load', () => { document.getElementById("connect-url").innerHTML = window.location.href.replace(window.location.protocol, window.location.protocol == "https:" ? "wss:" : "ws:"); }); </script> </head> <body> <h1>426 - Upgrade Required</h1> <p>Hello there! It appears as if you've reached the landing page for this EaglerProxy instance. Unfortunately, you cannot connect to the proxy server from here. To connect, use this server IP/URL: <code id="connect-url">loading...</code> (connect from any recent EaglercraftX client via Multiplayer > Direct Connect)</p> </body></html>`;
|
||||
|
|
|
@ -1,44 +1,36 @@
|
|||
import { randomUUID } from "crypto";
|
||||
import pkg, { NewPingResult } from "minecraft-protocol";
|
||||
import sharp from "sharp";
|
||||
import { PROXY_BRANDING, PROXY_VERSION } from "../meta.js";
|
||||
import { Config } from "../launcher_types.js";
|
||||
import { Chat } from "./Chat.js";
|
||||
import { Constants } from "./Constants.js";
|
||||
import { ImageEditor } from "./skins/ImageEditor.js";
|
||||
const { ping } = pkg;
|
||||
|
||||
export namespace Motd {
|
||||
const ICON_SQRT = 64;
|
||||
const IMAGE_DATA_PREPEND = "data:image/png;base64,";
|
||||
|
||||
export class MOTD {
|
||||
public jsonMotd: JSONMotd;
|
||||
public image?: Buffer;
|
||||
public usingNatives: boolean;
|
||||
|
||||
constructor(motd: JSONMotd, image?: Buffer) {
|
||||
constructor(motd: JSONMotd, native: boolean, image?: Buffer) {
|
||||
this.jsonMotd = motd;
|
||||
this.image = image;
|
||||
this.usingNatives = native;
|
||||
}
|
||||
|
||||
public static async generateMOTDFromPing(
|
||||
host: string,
|
||||
port: number
|
||||
): Promise<MOTD> {
|
||||
public static async generateMOTDFromPing(host: string, port: number, useNatives: boolean): Promise<MOTD> {
|
||||
const pingRes = await ping({ host: host, port: port });
|
||||
if (typeof pingRes.version == "string")
|
||||
throw new Error("Non-1.8 server detected!");
|
||||
if (typeof pingRes.version == "string") throw new Error("Non-1.8 server detected!");
|
||||
else {
|
||||
const newPingRes = pingRes as NewPingResult;
|
||||
let image: Buffer;
|
||||
|
||||
if (newPingRes.favicon != null) {
|
||||
if (!newPingRes.favicon.startsWith(IMAGE_DATA_PREPEND))
|
||||
throw new Error("Invalid MOTD image!");
|
||||
image = await this.generateEaglerMOTDImage(
|
||||
Buffer.from(
|
||||
newPingRes.favicon.substring(IMAGE_DATA_PREPEND.length),
|
||||
"base64"
|
||||
)
|
||||
);
|
||||
if (!newPingRes.favicon.startsWith(Constants.IMAGE_DATA_PREPEND)) throw new Error("Invalid MOTD image!");
|
||||
image = useNatives
|
||||
? await ImageEditor.generateEaglerMOTDImage(Buffer.from(newPingRes.favicon.substring(Constants.IMAGE_DATA_PREPEND.length), "base64"))
|
||||
: await ImageEditor.generateEaglerMOTDImageJS(Buffer.from(newPingRes.favicon.substring(Constants.IMAGE_DATA_PREPEND.length), "base64"));
|
||||
}
|
||||
|
||||
return new MOTD(
|
||||
|
@ -49,17 +41,9 @@ export namespace Motd {
|
|||
cache: true,
|
||||
icon: newPingRes.favicon != null ? true : false,
|
||||
max: newPingRes.players.max,
|
||||
motd: [
|
||||
typeof newPingRes.description == "string"
|
||||
? newPingRes.description
|
||||
: Chat.chatToPlainString(newPingRes.description),
|
||||
"",
|
||||
],
|
||||
motd: [typeof newPingRes.description == "string" ? newPingRes.description : Chat.chatToPlainString(newPingRes.description), ""],
|
||||
online: newPingRes.players.online,
|
||||
players:
|
||||
newPingRes.players.sample != null
|
||||
? newPingRes.players.sample.map((v) => v.name)
|
||||
: [],
|
||||
players: newPingRes.players.sample != null ? newPingRes.players.sample.map((v) => v.name) : [],
|
||||
},
|
||||
name: "placeholder name",
|
||||
secure: false,
|
||||
|
@ -68,65 +52,42 @@ export namespace Motd {
|
|||
uuid: randomUUID(), // replace placeholder with global. cached UUID
|
||||
vers: `${PROXY_BRANDING}/${PROXY_VERSION}`,
|
||||
},
|
||||
useNatives,
|
||||
image
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public static async generateMOTDFromConfig(
|
||||
config: Config["adapter"]
|
||||
): Promise<MOTD> {
|
||||
public static async generateMOTDFromConfig(config: Config["adapter"], useNatives: boolean): Promise<MOTD> {
|
||||
if (typeof config.motd != "string") {
|
||||
const motd = new MOTD({
|
||||
brand: PROXY_BRANDING,
|
||||
cracked: true,
|
||||
data: {
|
||||
cache: true,
|
||||
icon: config.motd.iconURL != null ? true : false,
|
||||
max: config.maxConcurrentClients,
|
||||
motd: [config.motd.l1, config.motd.l2 ?? ""],
|
||||
online: 0,
|
||||
players: [],
|
||||
const motd = new MOTD(
|
||||
{
|
||||
brand: PROXY_BRANDING,
|
||||
cracked: true,
|
||||
data: {
|
||||
cache: true,
|
||||
icon: config.motd.iconURL != null ? true : false,
|
||||
max: config.maxConcurrentClients,
|
||||
motd: [config.motd.l1, config.motd.l2 ?? ""],
|
||||
online: 0,
|
||||
players: [],
|
||||
},
|
||||
name: config.name,
|
||||
secure: false,
|
||||
time: Date.now(),
|
||||
type: "motd",
|
||||
uuid: randomUUID(),
|
||||
vers: `${PROXY_BRANDING}/${PROXY_VERSION}`,
|
||||
},
|
||||
name: config.name,
|
||||
secure: false,
|
||||
time: Date.now(),
|
||||
type: "motd",
|
||||
uuid: randomUUID(),
|
||||
vers: `${PROXY_BRANDING}/${PROXY_VERSION}`,
|
||||
});
|
||||
useNatives
|
||||
);
|
||||
if (config.motd.iconURL != null) {
|
||||
motd.image = await this.generateEaglerMOTDImage(config.motd.iconURL);
|
||||
motd.image = useNatives ? await ImageEditor.generateEaglerMOTDImage(config.motd.iconURL) : await ImageEditor.generateEaglerMOTDImageJS(config.motd.iconURL); // TODO: swap between native and pure JS
|
||||
}
|
||||
return motd;
|
||||
} else throw new Error("MOTD is set to be forwarded in the config!");
|
||||
}
|
||||
|
||||
// TODO: fix not working
|
||||
public static generateEaglerMOTDImage(
|
||||
file: string | Buffer
|
||||
): Promise<Buffer> {
|
||||
return new Promise<Buffer>((res, rej) => {
|
||||
sharp(file)
|
||||
.resize(ICON_SQRT, ICON_SQRT, {
|
||||
kernel: "nearest",
|
||||
})
|
||||
.raw({
|
||||
depth: "uchar",
|
||||
})
|
||||
.toBuffer()
|
||||
.then((buff) => {
|
||||
for (const pixel of buff) {
|
||||
if ((pixel & 0xffffff) == 0) {
|
||||
buff[buff.indexOf(pixel)] = 0;
|
||||
}
|
||||
}
|
||||
res(buff);
|
||||
})
|
||||
.catch(rej);
|
||||
});
|
||||
}
|
||||
|
||||
public toBuffer(): [string, Buffer] {
|
||||
return [JSON.stringify(this.jsonMotd), this.image];
|
||||
}
|
||||
|
|
|
@ -11,11 +11,12 @@ import { EaglerSkins } from "./skins/EaglerSkins.js";
|
|||
import { Util } from "./Util.js";
|
||||
import { BungeeUtil } from "./BungeeUtil.js";
|
||||
import { IncomingMessage } from "http";
|
||||
import { Socket } from "net";
|
||||
|
||||
const { createSerializer, createDeserializer } = pkg;
|
||||
|
||||
export class Player extends EventEmitter {
|
||||
public ws: WebSocket & { httpRequest: IncomingMessage };
|
||||
public ws: WebSocket & { httpRequest: IncomingMessage; _socket: Socket };
|
||||
public username?: string;
|
||||
public skin?: EaglerSkins.EaglerSkin;
|
||||
public uuid?: string;
|
||||
|
@ -36,7 +37,7 @@ export class Player extends EventEmitter {
|
|||
constructor(ws: WebSocket & { httpRequest: IncomingMessage }, playerName?: string, serverConnection?: Client) {
|
||||
super();
|
||||
this._logger = new Logger(`PlayerHandler-${playerName}`);
|
||||
this.ws = ws;
|
||||
this.ws = ws as any;
|
||||
this.username = playerName;
|
||||
this.serverConnection = serverConnection;
|
||||
if (this.username != null) this.uuid = Util.generateUUIDFromPlayer(this.username);
|
||||
|
|
|
@ -1,7 +1,4 @@
|
|||
import {
|
||||
encodeULEB128 as _encodeVarInt,
|
||||
decodeULEB128 as _decodeVarInt,
|
||||
} from "@thi.ng/leb128";
|
||||
import { encodeULEB128 as _encodeVarInt, decodeULEB128 as _decodeVarInt } from "@thi.ng/leb128";
|
||||
import { Enums } from "./Enums.js";
|
||||
import { Util } from "./Util.js";
|
||||
|
||||
|
@ -24,10 +21,7 @@ export namespace MineProtocol {
|
|||
return Buffer.from(_encodeVarInt(int));
|
||||
}
|
||||
|
||||
export function readVarInt(
|
||||
buff: Buffer,
|
||||
offset?: number
|
||||
): ReadResult<number> {
|
||||
export function readVarInt(buff: Buffer, offset?: number): ReadResult<number> {
|
||||
buff = offset ? buff.subarray(offset) : buff;
|
||||
const read = _decodeVarInt(buff),
|
||||
len = read[1];
|
||||
|
@ -38,16 +32,35 @@ export namespace MineProtocol {
|
|||
};
|
||||
}
|
||||
|
||||
export function writeVarLong(long: number): Buffer {
|
||||
return writeVarInt(long);
|
||||
}
|
||||
|
||||
export function readVarLong(buff: Buffer, offset?: number): ReadResult<number> {
|
||||
return readVarInt(buff, offset);
|
||||
}
|
||||
|
||||
export function writeBinary(data: Buffer): Buffer {
|
||||
return Buffer.concat([writeVarInt(data.length), data]);
|
||||
}
|
||||
|
||||
export function readBinary(buff: Buffer, offset?: number): ReadResult<Buffer> {
|
||||
buff = offset ? buff.subarray(offset) : buff;
|
||||
const len = readVarInt(buff),
|
||||
data = len.newBuffer.subarray(0, len.value);
|
||||
return {
|
||||
value: data,
|
||||
newBuffer: len.newBuffer.subarray(len.value),
|
||||
};
|
||||
}
|
||||
|
||||
export function writeString(str: string): Buffer {
|
||||
const bufferized = Buffer.from(str, "utf8"),
|
||||
len = writeVarInt(bufferized.length);
|
||||
return Buffer.concat([len, bufferized]);
|
||||
}
|
||||
|
||||
export function readString(
|
||||
buff: Buffer,
|
||||
offset?: number
|
||||
): ReadResult<string> {
|
||||
export function readString(buff: Buffer, offset?: number): ReadResult<string> {
|
||||
buff = offset ? buff.subarray(offset) : buff;
|
||||
const len = readVarInt(buff),
|
||||
str = len.newBuffer.subarray(0, len.value).toString("utf8");
|
||||
|
|
|
@ -25,6 +25,9 @@ import { CSSetSkinPacket } from "./packets/CSSetSkinPacket.js";
|
|||
import { CSChannelMessagePacket } from "./packets/channel/CSChannelMessage.js";
|
||||
import { Constants, UPGRADE_REQUIRED_RESPONSE } from "./Constants.js";
|
||||
import { PluginManager } from "./pluginLoader/PluginManager.js";
|
||||
import ProxyRatelimitManager from "./ratelimit/ProxyRatelimitManager.js";
|
||||
import { ChatColor } from "../plugins/EagProxyAAS/types.js";
|
||||
import { SkinServer } from "./skins/SkinServer.js";
|
||||
|
||||
let instanceCount = 0;
|
||||
const chalk = new Chalk({ level: 2 });
|
||||
|
@ -41,8 +44,9 @@ export class Proxy extends EventEmitter {
|
|||
public config: Config["adapter"];
|
||||
public wsServer: WebSocketServer;
|
||||
public httpServer: http.Server;
|
||||
public skinServer: EaglerSkins.SkinServer;
|
||||
public skinServer: SkinServer;
|
||||
public broadcastMotd?: Motd.MOTD;
|
||||
public ratelimit: ProxyRatelimitManager;
|
||||
|
||||
private _logger: Logger;
|
||||
private initalHandlerLogger: Logger;
|
||||
|
@ -93,13 +97,20 @@ export class Proxy extends EventEmitter {
|
|||
if (this.loaded) throw new Error("Can't initiate if proxy instance is already initialized or is being initialized!");
|
||||
this.loaded = true;
|
||||
this.packetRegistry = await loadPackets();
|
||||
this.skinServer = new EaglerSkins.SkinServer(this, this.config.skinUrlWhitelist);
|
||||
this.skinServer = new SkinServer(
|
||||
this,
|
||||
this.config.useNatives,
|
||||
this.config.skinServer.cache.skinCachePruneInterval,
|
||||
this.config.skinServer.cache.skinCacheLifetime,
|
||||
this.config.skinServer.cache.folderName,
|
||||
this.config.skinServer.cache.useCache,
|
||||
this.config.skinServer.skinUrlWhitelist
|
||||
);
|
||||
global.PACKET_REGISTRY = this.packetRegistry;
|
||||
if (this.config.motd == "FORWARD") {
|
||||
this._pollServer(this.config.server.host, this.config.server.port);
|
||||
} else {
|
||||
// TODO: motd
|
||||
const broadcastMOTD = await Motd.MOTD.generateMOTDFromConfig(this.config);
|
||||
const broadcastMOTD = await Motd.MOTD.generateMOTDFromConfig(this.config, this.config.useNatives);
|
||||
(broadcastMOTD as any)._static = true;
|
||||
this.broadcastMotd = broadcastMOTD;
|
||||
// playercount will be dynamically updated
|
||||
|
@ -136,19 +147,35 @@ export class Proxy extends EventEmitter {
|
|||
this._logger.error(`Error was caught whilst trying to handle WebSocket upgrade! Error: ${err.stack ?? err}`);
|
||||
}
|
||||
});
|
||||
process.on("beforeExit", () => {
|
||||
this._logger.info("Cleaning up before exiting...");
|
||||
this.players.forEach((plr) => plr.disconnect(ChatColor.YELLOW + "Proxy is shutting down."));
|
||||
});
|
||||
this.ratelimit = new ProxyRatelimitManager(this.config.ratelimits);
|
||||
this.pluginManager.emit("proxyFinishLoading", this, this.pluginManager);
|
||||
this._logger.info(`Started WebSocket server and binded to ${this.config.bindHost} on port ${this.config.bindPort}.`);
|
||||
}
|
||||
|
||||
private _handleNonWSRequest(req: http.IncomingMessage, res: http.ServerResponse, config: Config["adapter"]) {
|
||||
const ctx: Util.Handlable = { handled: false };
|
||||
this.emit("httpConnection", req, res, ctx);
|
||||
if (!ctx.handled) res.setHeader("Content-Type", "text/html").writeHead(426).end(UPGRADE_REQUIRED_RESPONSE);
|
||||
if (this.ratelimit.http.consume(req.socket.remoteAddress).success) {
|
||||
const ctx: Util.Handlable = { handled: false };
|
||||
this.emit("httpConnection", req, res, ctx);
|
||||
if (!ctx.handled) res.setHeader("Content-Type", "text/html").writeHead(426).end(UPGRADE_REQUIRED_RESPONSE);
|
||||
}
|
||||
}
|
||||
|
||||
readonly LOGIN_TIMEOUT = 30000;
|
||||
|
||||
private async _handleWSConnection(ws: WebSocket, req: http.IncomingMessage) {
|
||||
const rl = this.ratelimit.ws.consume(req.socket.remoteAddress);
|
||||
if (!rl.success) {
|
||||
return ws.close();
|
||||
}
|
||||
|
||||
const ctx: Util.Handlable = { handled: false };
|
||||
await this.emit("wsConnection", ws, req, ctx);
|
||||
if (ctx.handled) return;
|
||||
|
||||
const firstPacket = await Util.awaitPacket(ws);
|
||||
let player: Player, handled: boolean;
|
||||
setTimeout(() => {
|
||||
|
@ -163,11 +190,10 @@ export class Proxy extends EventEmitter {
|
|||
}
|
||||
}, this.LOGIN_TIMEOUT);
|
||||
try {
|
||||
const ctx: Util.Handlable = { handled: false };
|
||||
await this.emit("wsConnection", ws, req, ctx);
|
||||
if (ctx.handled) return;
|
||||
|
||||
if (firstPacket.toString() === "Accept: MOTD") {
|
||||
if (!this.ratelimit.motd.consume(req.socket.remoteAddress).success) {
|
||||
return ws.close();
|
||||
}
|
||||
if (this.broadcastMotd) {
|
||||
if ((this.broadcastMotd as any)._static) {
|
||||
this.broadcastMotd.jsonMotd.data.online = this.players.size;
|
||||
|
@ -191,6 +217,13 @@ export class Proxy extends EventEmitter {
|
|||
} else {
|
||||
(ws as any).httpRequest = req;
|
||||
player = new Player(ws as any);
|
||||
const rl = this.ratelimit.connect.consume(req.socket.remoteAddress);
|
||||
if (!rl.success) {
|
||||
handled = true;
|
||||
player.disconnect(`${Enums.ChatColor.RED}You have been ratelimited!\nTry again in ${Enums.ChatColor.WHITE}${rl.retryIn / 1000}${Enums.ChatColor.RED} seconds`);
|
||||
return;
|
||||
}
|
||||
|
||||
const loginPacket = new CSLoginPacket().deserialize(firstPacket);
|
||||
player.state = Enums.ClientState.PRE_HANDSHAKE;
|
||||
if (loginPacket.gameVersion != VANILLA_PROTOCOL_VERSION) {
|
||||
|
@ -249,6 +282,7 @@ export class Proxy extends EventEmitter {
|
|||
player.state = Enums.ClientState.POST_HANDSHAKE;
|
||||
this._logger.info(`Handshake Success! Connecting player ${player.username} to server...`);
|
||||
handled = true;
|
||||
|
||||
await player.connect({
|
||||
host: this.config.server.host,
|
||||
port: this.config.server.port,
|
||||
|
@ -278,7 +312,7 @@ export class Proxy extends EventEmitter {
|
|||
try {
|
||||
const msg: CSChannelMessagePacket = packet as any;
|
||||
if (msg.channel == Constants.EAGLERCRAFT_SKIN_CHANNEL_NAME) {
|
||||
await this.skinServer.handleRequest(msg, player);
|
||||
await this.skinServer.handleRequest(msg, player, this);
|
||||
}
|
||||
} catch (err) {
|
||||
this._logger.error(`Failed to process channel message packet! Error: ${err.stack || err}`);
|
||||
|
@ -298,7 +332,7 @@ export class Proxy extends EventEmitter {
|
|||
private _pollServer(host: string, port: number, interval?: number) {
|
||||
(async () => {
|
||||
while (true) {
|
||||
const motd = await Motd.MOTD.generateMOTDFromPing(host, port).catch((err) => {
|
||||
const motd = await Motd.MOTD.generateMOTDFromPing(host, port, this.config.useNatives).catch((err) => {
|
||||
this._logger.warn(`Error polling ${host}:${port} for MOTD: ${err.stack ?? err}`);
|
||||
});
|
||||
if (motd) this.broadcastMotd = motd;
|
||||
|
|
53
src/proxy/databases/DiskDB.ts
Normal file
53
src/proxy/databases/DiskDB.ts
Normal file
|
@ -0,0 +1,53 @@
|
|||
import path from "path";
|
||||
import fs from "fs/promises";
|
||||
import fss from "fs";
|
||||
|
||||
export default class DiskDB<T extends any> {
|
||||
public folder: string;
|
||||
static VALIDATION_REGEX = /^[0-9a-zA-Z_]+$/;
|
||||
|
||||
nameGenerator: (k: string) => string;
|
||||
encoder: (key: T) => Buffer;
|
||||
decoder: (enc: Buffer) => T;
|
||||
|
||||
constructor(folder: string, encoder: (key: T) => Buffer, decoder: (enc: Buffer) => T, nameGenerator: (k: string) => string) {
|
||||
this.folder = path.isAbsolute(folder) ? folder : path.resolve(folder);
|
||||
this.encoder = encoder;
|
||||
this.decoder = decoder;
|
||||
this.nameGenerator = nameGenerator;
|
||||
if (!fss.existsSync(this.folder)) fss.mkdirSync(this.folder);
|
||||
}
|
||||
|
||||
public async filter(f: (v: T) => boolean) {
|
||||
for (const file of await fs.readdir(this.folder)) {
|
||||
const fp = path.join(this.folder, file);
|
||||
if (!f(this.decoder(await fs.readFile(fp)))) await fs.rm(fp);
|
||||
}
|
||||
}
|
||||
|
||||
public async get(k: string): Promise<T | null> {
|
||||
k = this.nameGenerator(k);
|
||||
if (!DiskDB.VALIDATION_REGEX.test(k)) throw new InvalidKeyError("Invalid key, key can only consist of alphanumeric characters and _");
|
||||
const pth = path.join(this.folder, `${k}.data`);
|
||||
try {
|
||||
return this.decoder(await fs.readFile(pth));
|
||||
} catch (err) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public async set(k: string, v: T) {
|
||||
k = this.nameGenerator(k);
|
||||
if (!DiskDB.VALIDATION_REGEX.test(k)) throw new InvalidKeyError("Invalid key, key can only consist of alphanumeric characters and _");
|
||||
const pth = path.join(this.folder, `${k}.data`);
|
||||
await fs.writeFile(pth, this.encoder(v));
|
||||
}
|
||||
}
|
||||
|
||||
class InvalidKeyError extends Error {
|
||||
constructor(msg: string) {
|
||||
super(`[InvalidKeyError] : ${msg}`);
|
||||
this.name = "InvalidKeyError";
|
||||
Object.setPrototypeOf(this, InvalidKeyError);
|
||||
}
|
||||
}
|
129
src/proxy/ratelimit/BucketRatelimiter.ts
Normal file
129
src/proxy/ratelimit/BucketRatelimiter.ts
Normal file
|
@ -0,0 +1,129 @@
|
|||
import { Logger } from "../../logger.js";
|
||||
|
||||
export default class BucketRateLimiter {
|
||||
public capacity: number;
|
||||
public refillsPerMin: number;
|
||||
public keyMap: Map<string, KeyData>;
|
||||
public static readonly GC_TOLERANCE: number = 50;
|
||||
private sweeper: NodeJS.Timer;
|
||||
|
||||
constructor(capacity: number, refillsPerMin: number) {
|
||||
this.capacity = capacity;
|
||||
this.refillsPerMin = refillsPerMin;
|
||||
this.keyMap = new Map();
|
||||
this.sweeper = setInterval(() => {
|
||||
this.removeFull();
|
||||
}, 5000);
|
||||
}
|
||||
|
||||
public cleanUp() {
|
||||
clearInterval(this.sweeper);
|
||||
}
|
||||
|
||||
public consume(key: string, consumeTokens: number = 1): RateLimitData {
|
||||
if (this.keyMap.has(key)) {
|
||||
const bucket = this.keyMap.get(key);
|
||||
|
||||
const now = Date.now();
|
||||
if (now - bucket.lastRefillTime > 60000 && bucket.tokens < this.capacity) {
|
||||
const refillTimes = Math.floor((now - bucket.lastRefillTime) / 60000);
|
||||
bucket.tokens = Math.min(this.capacity, bucket.tokens + refillTimes * this.refillsPerMin);
|
||||
bucket.lastRefillTime = now - (refillTimes % 60000);
|
||||
} else if (now - bucket.lastRefillTime > 60000 && bucket.tokens >= this.capacity) bucket.lastRefillTime = now;
|
||||
|
||||
if (bucket.tokens >= consumeTokens) {
|
||||
bucket.tokens -= consumeTokens;
|
||||
return { success: true };
|
||||
} else {
|
||||
const difference = consumeTokens - bucket.tokens;
|
||||
return {
|
||||
success: false,
|
||||
missingTokens: difference,
|
||||
retryIn: Math.ceil(difference / this.refillsPerMin) * 60000 - ((now - bucket.lastRefillTime) % 60000),
|
||||
retryAt: Date.now() + Math.ceil(difference / this.refillsPerMin) * 60000 - ((now - bucket.lastRefillTime) % 60000),
|
||||
};
|
||||
}
|
||||
} else {
|
||||
const bucket: KeyData = {
|
||||
tokens: this.capacity,
|
||||
lastRefillTime: Date.now(),
|
||||
};
|
||||
if (bucket.tokens >= consumeTokens) {
|
||||
bucket.tokens -= consumeTokens;
|
||||
this.keyMap.set(key, bucket);
|
||||
return { success: true };
|
||||
} else {
|
||||
const difference = consumeTokens - bucket.tokens;
|
||||
const now = Date.now();
|
||||
return {
|
||||
success: false,
|
||||
missingTokens: difference,
|
||||
retryIn: Math.ceil(difference / this.refillsPerMin) * 60000 - ((now - bucket.lastRefillTime) % 60000),
|
||||
retryAt: Date.now() + Math.ceil(difference / this.refillsPerMin) * 60000 - ((now - bucket.lastRefillTime) % 60000),
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public addToBucket(key: string, amount: number) {
|
||||
if (this.keyMap.has(key)) {
|
||||
this.keyMap.get(key).tokens += amount;
|
||||
} else {
|
||||
this.keyMap.set(key, {
|
||||
tokens: this.capacity + amount,
|
||||
lastRefillTime: Date.now(),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public setBucketSize(key: string, amount: number) {
|
||||
if (this.keyMap.has(key)) {
|
||||
this.keyMap.get(key).tokens = amount;
|
||||
} else {
|
||||
this.keyMap.set(key, {
|
||||
tokens: amount,
|
||||
lastRefillTime: Date.now(),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public subtractFromBucket(key: string, amount: number) {
|
||||
if (this.keyMap.has(key)) {
|
||||
const bucket = this.keyMap.get(key);
|
||||
bucket.tokens -= amount;
|
||||
} else {
|
||||
this.keyMap.set(key, {
|
||||
tokens: this.capacity - amount,
|
||||
lastRefillTime: Date.now(),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public removeFull() {
|
||||
let remove: string[] = [];
|
||||
const now = Date.now();
|
||||
this.keyMap.forEach((v, k) => {
|
||||
if (now - v.lastRefillTime > 60000 && v.tokens < this.capacity) {
|
||||
const refillTimes = Math.floor((now - v.lastRefillTime) / 60000);
|
||||
v.tokens = Math.min(this.capacity, v.tokens + refillTimes * this.refillsPerMin);
|
||||
v.lastRefillTime = now - (refillTimes % 60000);
|
||||
} else if (now - v.lastRefillTime > 60000 && v.tokens >= this.capacity) v.lastRefillTime = now;
|
||||
if (v.tokens == this.capacity) {
|
||||
remove.push(k);
|
||||
}
|
||||
});
|
||||
remove.forEach((v) => this.keyMap.delete(v));
|
||||
}
|
||||
}
|
||||
|
||||
type RateLimitData = {
|
||||
success: boolean;
|
||||
missingTokens?: number;
|
||||
retryIn?: number;
|
||||
retryAt?: number;
|
||||
};
|
||||
|
||||
type KeyData = {
|
||||
tokens: number;
|
||||
lastRefillTime: number;
|
||||
};
|
63
src/proxy/ratelimit/ExponentialBackoffRequestController.ts
Normal file
63
src/proxy/ratelimit/ExponentialBackoffRequestController.ts
Normal file
|
@ -0,0 +1,63 @@
|
|||
const wait = (ms: number) => new Promise((res) => setTimeout(res, ms));
|
||||
|
||||
export default class ExponentialBackoffRequestController {
|
||||
public queue: Task[];
|
||||
public flushQueueAfterTries: number;
|
||||
public baseDelay: number;
|
||||
ended: boolean;
|
||||
aborted: boolean;
|
||||
|
||||
constructor(baseDelay: number = 3000, triesBeforeFlush: number = 10) {
|
||||
this.flushQueueAfterTries = triesBeforeFlush;
|
||||
this.baseDelay = baseDelay;
|
||||
this.queue = [];
|
||||
this.ended = false;
|
||||
this.aborted = false;
|
||||
setTimeout(() => this.tick(), 0);
|
||||
}
|
||||
|
||||
private async tick() {
|
||||
while (true) {
|
||||
if (this.ended) break;
|
||||
for (const task of this.queue) {
|
||||
if (this.ended || this.aborted) break;
|
||||
let times = 0,
|
||||
breakOut = false;
|
||||
while (true) {
|
||||
try {
|
||||
await task();
|
||||
break;
|
||||
} catch (err) {
|
||||
times++;
|
||||
await wait(this.baseDelay * 2 ** times);
|
||||
if (times > this.flushQueueAfterTries) {
|
||||
this.queue.forEach((task) => task(new Error("Controller overload!")));
|
||||
breakOut = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (breakOut) break;
|
||||
}
|
||||
if (this.aborted) this.aborted = false;
|
||||
this.queue = [];
|
||||
await wait(1);
|
||||
}
|
||||
}
|
||||
|
||||
public end() {
|
||||
this.ended = true;
|
||||
}
|
||||
|
||||
public flush() {
|
||||
this.aborted = false;
|
||||
this.queue.forEach((task) => task(new Error("Aborted")));
|
||||
this.queue = [];
|
||||
}
|
||||
|
||||
public queueTask(task: Task): void {
|
||||
this.queue.push(task);
|
||||
}
|
||||
}
|
||||
|
||||
type Task = (err?: object) => void | Promise<void>;
|
21
src/proxy/ratelimit/ProxyRatelimitManager.ts
Normal file
21
src/proxy/ratelimit/ProxyRatelimitManager.ts
Normal file
|
@ -0,0 +1,21 @@
|
|||
import { Config } from "../../launcher_types.js";
|
||||
import { Player } from "../Player.js";
|
||||
import BucketRateLimiter from "./BucketRatelimiter.js";
|
||||
|
||||
export default class ProxyRatelimitManager {
|
||||
http: BucketRateLimiter;
|
||||
motd: BucketRateLimiter;
|
||||
ws: BucketRateLimiter;
|
||||
connect: BucketRateLimiter;
|
||||
skinsIP: BucketRateLimiter;
|
||||
skinsConnection: BucketRateLimiter;
|
||||
|
||||
constructor(config: Config["adapter"]["ratelimits"]) {
|
||||
this.http = new BucketRateLimiter(config.limits.http, config.limits.http);
|
||||
this.ws = new BucketRateLimiter(config.limits.ws, config.limits.ws);
|
||||
this.motd = new BucketRateLimiter(config.limits.motd, config.limits.motd);
|
||||
this.connect = new BucketRateLimiter(config.limits.connect, config.limits.connect);
|
||||
this.skinsIP = new BucketRateLimiter(config.limits.skinsIp, config.limits.skinsIp);
|
||||
this.skinsConnection = new BucketRateLimiter(config.limits.skins, config.limits.skins);
|
||||
}
|
||||
}
|
|
@ -9,6 +9,9 @@ import { CSChannelMessagePacket } from "../packets/channel/CSChannelMessage.js";
|
|||
import { SCChannelMessagePacket } from "../packets/channel/SCChannelMessage.js";
|
||||
import { Logger } from "../../logger.js";
|
||||
import fetch from "node-fetch";
|
||||
import Jimp from "jimp";
|
||||
import { ImageEditor } from "./ImageEditor.js";
|
||||
import ExponentialBackoffRequestController from "../ratelimit/ExponentialBackoffRequestController.js";
|
||||
|
||||
// TODO: convert all functions to use MineProtocol's UUID manipulation functions
|
||||
|
||||
|
@ -67,26 +70,22 @@ export namespace EaglerSkins {
|
|||
};
|
||||
|
||||
export async function skinUrlFromUuid(uuid: string): Promise<string> {
|
||||
const response = (await (
|
||||
await fetch(
|
||||
`https://sessionserver.mojang.com/session/minecraft/profile/${uuid}`
|
||||
)
|
||||
).json()) as unknown as MojangFetchProfileResponse;
|
||||
const parsed = JSON.parse(
|
||||
Buffer.from(response.properties[0].value, "base64").toString()
|
||||
) as unknown as MojangTextureResponse;
|
||||
const response = (await (await fetch(`https://sessionserver.mojang.com/session/minecraft/profile/${uuid}`)).json()) as unknown as MojangFetchProfileResponse;
|
||||
const parsed = JSON.parse(Buffer.from(response.properties[0].value, "base64").toString()) as unknown as MojangTextureResponse;
|
||||
console.log(parsed.textures.SKIN.url);
|
||||
return parsed.textures.SKIN.url;
|
||||
}
|
||||
|
||||
export function downloadSkin(skinUrl: string): Promise<Buffer> {
|
||||
const url = new URL(skinUrl);
|
||||
if (url.protocol != "https:" && url.protocol != "http:")
|
||||
throw new Error("Invalid skin URL protocol!");
|
||||
if (url.protocol != "https:" && url.protocol != "http:") throw new Error("Invalid skin URL protocol!");
|
||||
return new Promise<Buffer>(async (res, rej) => {
|
||||
const skin = await fetch(skinUrl);
|
||||
if (skin.status != 200) {
|
||||
rej(`Tried to fetch ${skinUrl}, got HTTP ${skin.status} instead!`);
|
||||
rej({
|
||||
url: skinUrl,
|
||||
status: skin.status,
|
||||
});
|
||||
return;
|
||||
} else {
|
||||
res(Buffer.from(await skin.arrayBuffer()));
|
||||
|
@ -94,9 +93,21 @@ export namespace EaglerSkins {
|
|||
});
|
||||
}
|
||||
|
||||
export function readClientDownloadSkinRequestPacket(
|
||||
message: Buffer
|
||||
): ClientDownloadSkinRequest {
|
||||
export function safeDownloadSkin(skinUrl: string, backoff: ExponentialBackoffRequestController): Promise<Buffer> {
|
||||
return new Promise((res, rej) => {
|
||||
backoff.queueTask(async (err) => {
|
||||
if (err) return rej(err);
|
||||
try {
|
||||
res(await downloadSkin(skinUrl));
|
||||
} catch (err) {
|
||||
if (err.status == 429) throw new Error("Ratelimited!");
|
||||
else rej("Unexpected HTTP status code: " + err.status);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export function readClientDownloadSkinRequestPacket(message: Buffer): ClientDownloadSkinRequest {
|
||||
const ret: ClientDownloadSkinRequest = {
|
||||
id: null,
|
||||
uuid: null,
|
||||
|
@ -111,23 +122,11 @@ export namespace EaglerSkins {
|
|||
return ret;
|
||||
}
|
||||
|
||||
export function writeClientDownloadSkinRequestPacket(
|
||||
uuid: string | Buffer,
|
||||
url: string
|
||||
): Buffer {
|
||||
return Buffer.concat(
|
||||
[
|
||||
[Enums.EaglerSkinPacketId.CFetchSkinReq],
|
||||
MineProtocol.writeUUID(uuid),
|
||||
[0x0],
|
||||
MineProtocol.writeString(url),
|
||||
].map((arr) => (arr instanceof Uint8Array ? arr : Buffer.from(arr)))
|
||||
);
|
||||
export function writeClientDownloadSkinRequestPacket(uuid: string | Buffer, url: string): Buffer {
|
||||
return Buffer.concat([[Enums.EaglerSkinPacketId.CFetchSkinReq], MineProtocol.writeUUID(uuid), [0x0], MineProtocol.writeString(url)].map((arr) => (arr instanceof Uint8Array ? arr : Buffer.from(arr))));
|
||||
}
|
||||
|
||||
export function readServerFetchSkinResultBuiltInPacket(
|
||||
message: Buffer
|
||||
): ServerFetchSkinResultBuiltIn {
|
||||
export function readServerFetchSkinResultBuiltInPacket(message: Buffer): ServerFetchSkinResultBuiltIn {
|
||||
const ret: ServerFetchSkinResultBuiltIn = {
|
||||
id: null,
|
||||
uuid: null,
|
||||
|
@ -135,31 +134,20 @@ export namespace EaglerSkins {
|
|||
};
|
||||
const id = MineProtocol.readVarInt(message),
|
||||
uuid = MineProtocol.readUUID(id.newBuffer),
|
||||
skinId = MineProtocol.readVarInt(
|
||||
id.newBuffer.subarray(id.newBuffer.length)
|
||||
);
|
||||
skinId = MineProtocol.readVarInt(id.newBuffer.subarray(id.newBuffer.length));
|
||||
ret.id = id.value;
|
||||
ret.uuid = uuid.value;
|
||||
ret.skinId = skinId.value;
|
||||
return this;
|
||||
}
|
||||
|
||||
export function writeServerFetchSkinResultBuiltInPacket(
|
||||
uuid: string | Buffer,
|
||||
skinId: number
|
||||
): Buffer {
|
||||
export function writeServerFetchSkinResultBuiltInPacket(uuid: string | Buffer, skinId: number): Buffer {
|
||||
uuid = typeof uuid == "string" ? Util.uuidStringToBuffer(uuid) : uuid;
|
||||
console.log(1);
|
||||
return Buffer.concat([
|
||||
Buffer.from([Enums.EaglerSkinPacketId.SFetchSkinBuiltInRes]),
|
||||
uuid as Buffer,
|
||||
Buffer.from([skinId >> 24, skinId >> 16, skinId >> 8, skinId & 0xff]),
|
||||
]);
|
||||
return Buffer.concat([Buffer.from([Enums.EaglerSkinPacketId.SFetchSkinBuiltInRes]), uuid as Buffer, Buffer.from([skinId >> 24, skinId >> 16, skinId >> 8, skinId & 0xff])]);
|
||||
}
|
||||
|
||||
export function readServerFetchSkinResultCustomPacket(
|
||||
message: Buffer
|
||||
): ServerFetchSkinResultCustom {
|
||||
export function readServerFetchSkinResultCustomPacket(message: Buffer): ServerFetchSkinResultCustom {
|
||||
const ret: ServerFetchSkinResultCustom = {
|
||||
id: null,
|
||||
uuid: null,
|
||||
|
@ -167,22 +155,19 @@ export namespace EaglerSkins {
|
|||
};
|
||||
const id = MineProtocol.readVarInt(message),
|
||||
uuid = MineProtocol.readUUID(id.newBuffer),
|
||||
skin = uuid.newBuffer.subarray(
|
||||
0,
|
||||
Constants.EAGLERCRAFT_SKIN_CUSTOM_LENGTH
|
||||
);
|
||||
skin = uuid.newBuffer.subarray(0, Constants.EAGLERCRAFT_SKIN_CUSTOM_LENGTH);
|
||||
ret.id = id.value;
|
||||
ret.uuid = uuid.value;
|
||||
ret.skin = skin;
|
||||
return this;
|
||||
}
|
||||
|
||||
// TODO: fix bug where some people are missing left arm and leg
|
||||
export function writeServerFetchSkinResultCustomPacket(
|
||||
uuid: string | Buffer,
|
||||
skin: Buffer,
|
||||
downloaded: boolean
|
||||
): Buffer {
|
||||
export function writeClientFetchEaglerSkin(uuid: string | Buffer, url: string): Buffer {
|
||||
uuid = typeof uuid == "string" ? Util.uuidStringToBuffer(uuid) : uuid;
|
||||
return Buffer.concat([[Enums.EaglerSkinPacketId.CFetchSkinEaglerPlayerReq], uuid, [0x00], MineProtocol.writeString(url)].map((arr) => (arr instanceof Uint8Array ? arr : Buffer.from(arr))));
|
||||
}
|
||||
|
||||
export function writeServerFetchSkinResultCustomPacket(uuid: string | Buffer, skin: Buffer, downloaded: boolean): Buffer {
|
||||
uuid = typeof uuid == "string" ? Util.uuidStringToBuffer(uuid) : uuid;
|
||||
return Buffer.concat(
|
||||
[
|
||||
|
@ -194,9 +179,7 @@ export namespace EaglerSkins {
|
|||
);
|
||||
}
|
||||
|
||||
export function readClientFetchEaglerSkinPacket(
|
||||
buff: Buffer
|
||||
): ClientFetchEaglerSkin {
|
||||
export function readClientFetchEaglerSkinPacket(buff: Buffer): ClientFetchEaglerSkin {
|
||||
const ret: ClientFetchEaglerSkin = {
|
||||
id: null,
|
||||
uuid: null,
|
||||
|
@ -208,391 +191,6 @@ export namespace EaglerSkins {
|
|||
return ret;
|
||||
}
|
||||
|
||||
export function writeClientFetchEaglerSkin(
|
||||
uuid: string | Buffer,
|
||||
url: string
|
||||
): Buffer {
|
||||
uuid = typeof uuid == "string" ? Util.uuidStringToBuffer(uuid) : uuid;
|
||||
return Buffer.concat(
|
||||
[
|
||||
[Enums.EaglerSkinPacketId.CFetchSkinEaglerPlayerReq],
|
||||
uuid,
|
||||
[0x00],
|
||||
MineProtocol.writeString(url),
|
||||
].map((arr) => (arr instanceof Uint8Array ? arr : Buffer.from(arr)))
|
||||
);
|
||||
}
|
||||
|
||||
export async function copyRawPixels(
|
||||
imageIn: sharp.Sharp,
|
||||
imageOut: sharp.Sharp,
|
||||
dx1: number,
|
||||
dy1: number,
|
||||
dx2: number,
|
||||
dy2: number,
|
||||
sx1: number,
|
||||
sy1: number,
|
||||
sx2: number,
|
||||
sy2: number
|
||||
): Promise<sharp.Sharp> {
|
||||
const inMeta = await imageIn.metadata(),
|
||||
outMeta = await imageOut.metadata();
|
||||
|
||||
if (dx1 > dx2) {
|
||||
return _copyRawPixels(
|
||||
imageIn,
|
||||
imageOut,
|
||||
sx1,
|
||||
sy1,
|
||||
dx2,
|
||||
dy1,
|
||||
sx2 - sx1,
|
||||
sy2 - sy1,
|
||||
inMeta.width!,
|
||||
outMeta.width!,
|
||||
true
|
||||
);
|
||||
} else {
|
||||
return _copyRawPixels(
|
||||
imageIn,
|
||||
imageOut,
|
||||
sx1,
|
||||
sy1,
|
||||
dx1,
|
||||
dy1,
|
||||
sx2 - sx1,
|
||||
sy2 - sy1,
|
||||
inMeta.width!,
|
||||
outMeta.width!,
|
||||
false
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async function _copyRawPixels(
|
||||
imageIn: sharp.Sharp,
|
||||
imageOut: sharp.Sharp,
|
||||
srcX: number,
|
||||
srcY: number,
|
||||
dstX: number,
|
||||
dstY: number,
|
||||
width: number,
|
||||
height: number,
|
||||
imgSrcWidth: number,
|
||||
imgDstWidth: number,
|
||||
flip: boolean
|
||||
): Promise<sharp.Sharp> {
|
||||
const inData = await imageIn.raw().toBuffer();
|
||||
const outData = await imageOut.raw().toBuffer();
|
||||
const outMeta = await imageOut.metadata();
|
||||
|
||||
for (let y = 0; y < height; y++) {
|
||||
for (let x = 0; x < width; x++) {
|
||||
let srcIndex = (srcY + y) * imgSrcWidth + srcX + x;
|
||||
let dstIndex = (dstY + y) * imgDstWidth + dstX + x;
|
||||
|
||||
if (flip) {
|
||||
srcIndex = (srcY + y) * imgSrcWidth + srcX + (width - x - 1);
|
||||
}
|
||||
|
||||
for (let c = 0; c < 4; c++) {
|
||||
// Assuming RGBA channels
|
||||
outData[dstIndex * 4 + c] = inData[srcIndex * 4 + c];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return sharp(outData, {
|
||||
raw: {
|
||||
width: outMeta.width!,
|
||||
height: outMeta.height!,
|
||||
channels: 4,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function toEaglerSkin(
|
||||
image: Buffer
|
||||
): Promise<
|
||||
Util.BoundedBuffer<typeof Constants.EAGLERCRAFT_SKIN_CUSTOM_LENGTH>
|
||||
> {
|
||||
const meta = await sharp(image).metadata();
|
||||
let sharpImage = sharp(image);
|
||||
if (meta.height != 64) {
|
||||
// assume 32 height skin
|
||||
let imageOut = sharp(
|
||||
await sharpImage
|
||||
.extend({ bottom: 32, background: { r: 0, g: 0, b: 0, alpha: 0 } })
|
||||
.toBuffer()
|
||||
);
|
||||
|
||||
imageOut = await copyRawPixels(
|
||||
sharpImage,
|
||||
imageOut,
|
||||
24,
|
||||
48,
|
||||
20,
|
||||
52,
|
||||
4,
|
||||
16,
|
||||
8,
|
||||
20
|
||||
);
|
||||
imageOut = await copyRawPixels(
|
||||
sharpImage,
|
||||
imageOut,
|
||||
28,
|
||||
48,
|
||||
24,
|
||||
52,
|
||||
8,
|
||||
16,
|
||||
12,
|
||||
20
|
||||
);
|
||||
imageOut = await copyRawPixels(
|
||||
sharpImage,
|
||||
imageOut,
|
||||
20,
|
||||
52,
|
||||
16,
|
||||
64,
|
||||
8,
|
||||
20,
|
||||
12,
|
||||
32
|
||||
);
|
||||
imageOut = await copyRawPixels(
|
||||
sharpImage,
|
||||
imageOut,
|
||||
24,
|
||||
52,
|
||||
20,
|
||||
64,
|
||||
4,
|
||||
20,
|
||||
8,
|
||||
32
|
||||
);
|
||||
imageOut = await copyRawPixels(
|
||||
sharpImage,
|
||||
imageOut,
|
||||
28,
|
||||
52,
|
||||
24,
|
||||
64,
|
||||
0,
|
||||
20,
|
||||
4,
|
||||
32
|
||||
);
|
||||
imageOut = await copyRawPixels(
|
||||
sharpImage,
|
||||
imageOut,
|
||||
32,
|
||||
52,
|
||||
28,
|
||||
64,
|
||||
12,
|
||||
20,
|
||||
16,
|
||||
32
|
||||
);
|
||||
imageOut = await copyRawPixels(
|
||||
sharpImage,
|
||||
imageOut,
|
||||
40,
|
||||
48,
|
||||
36,
|
||||
52,
|
||||
44,
|
||||
16,
|
||||
48,
|
||||
20
|
||||
);
|
||||
imageOut = await copyRawPixels(
|
||||
sharpImage,
|
||||
imageOut,
|
||||
44,
|
||||
48,
|
||||
40,
|
||||
52,
|
||||
48,
|
||||
16,
|
||||
52,
|
||||
20
|
||||
);
|
||||
imageOut = await copyRawPixels(
|
||||
sharpImage,
|
||||
imageOut,
|
||||
36,
|
||||
52,
|
||||
32,
|
||||
64,
|
||||
48,
|
||||
20,
|
||||
52,
|
||||
32
|
||||
);
|
||||
imageOut = await copyRawPixels(
|
||||
sharpImage,
|
||||
imageOut,
|
||||
40,
|
||||
52,
|
||||
36,
|
||||
64,
|
||||
44,
|
||||
20,
|
||||
48,
|
||||
32
|
||||
);
|
||||
imageOut = await copyRawPixels(
|
||||
sharpImage,
|
||||
imageOut,
|
||||
44,
|
||||
52,
|
||||
40,
|
||||
64,
|
||||
40,
|
||||
20,
|
||||
44,
|
||||
32
|
||||
);
|
||||
imageOut = await copyRawPixels(
|
||||
sharpImage,
|
||||
imageOut,
|
||||
48,
|
||||
52,
|
||||
44,
|
||||
64,
|
||||
52,
|
||||
20,
|
||||
56,
|
||||
32
|
||||
);
|
||||
|
||||
sharpImage = imageOut;
|
||||
}
|
||||
|
||||
const r = await sharpImage
|
||||
.extractChannel("red")
|
||||
.raw({ depth: "uchar" })
|
||||
.toBuffer();
|
||||
const g = await sharpImage
|
||||
.extractChannel("green")
|
||||
.raw({ depth: "uchar" })
|
||||
.toBuffer();
|
||||
const b = await sharpImage
|
||||
.extractChannel("blue")
|
||||
.raw({ depth: "uchar" })
|
||||
.toBuffer();
|
||||
const a = await sharpImage
|
||||
.ensureAlpha()
|
||||
.extractChannel(3)
|
||||
.toColorspace("b-w")
|
||||
.raw({ depth: "uchar" })
|
||||
.toBuffer();
|
||||
const newBuff = Buffer.alloc(Constants.EAGLERCRAFT_SKIN_CUSTOM_LENGTH);
|
||||
for (let i = 1; i < 64 ** 2; i++) {
|
||||
const bytePos = i * 4;
|
||||
newBuff[bytePos] = a[i];
|
||||
newBuff[bytePos + 1] = b[i];
|
||||
newBuff[bytePos + 2] = g[i];
|
||||
newBuff[bytePos + 3] = r[i];
|
||||
}
|
||||
return newBuff;
|
||||
}
|
||||
|
||||
export class SkinServer {
|
||||
public allowedSkinDomains: string[];
|
||||
public proxy: Proxy;
|
||||
private _logger: Logger;
|
||||
|
||||
constructor(proxy: Proxy, allowedSkinDomains?: string[]) {
|
||||
this.allowedSkinDomains = allowedSkinDomains ?? [
|
||||
"textures.minecraft.net",
|
||||
];
|
||||
this.proxy = proxy ?? PROXY;
|
||||
this._logger = new Logger("SkinServer");
|
||||
this._logger.info("Started EaglercraftX skin server.");
|
||||
}
|
||||
|
||||
public async handleRequest(packet: CSChannelMessagePacket, caller: Player) {
|
||||
if (packet.messageType == Enums.ChannelMessageType.SERVER)
|
||||
throw new Error("Server message was passed to client message handler!");
|
||||
else if (packet.channel != Constants.EAGLERCRAFT_SKIN_CHANNEL_NAME)
|
||||
throw new Error("Cannot handle non-EaglerX skin channel messages!");
|
||||
switch (packet.data[0] as Enums.EaglerSkinPacketId) {
|
||||
default:
|
||||
throw new Error("Unknown operation!");
|
||||
break;
|
||||
case Enums.EaglerSkinPacketId.CFetchSkinEaglerPlayerReq:
|
||||
const parsedPacket_0 = EaglerSkins.readClientFetchEaglerSkinPacket(
|
||||
packet.data
|
||||
);
|
||||
const player = this.proxy.fetchUserByUUID(parsedPacket_0.uuid);
|
||||
if (player) {
|
||||
if (player.skin.type == Enums.SkinType.BUILTIN) {
|
||||
const response = new SCChannelMessagePacket();
|
||||
response.channel = Constants.EAGLERCRAFT_SKIN_CHANNEL_NAME;
|
||||
response.data =
|
||||
EaglerSkins.writeServerFetchSkinResultBuiltInPacket(
|
||||
player.uuid,
|
||||
player.skin.builtInSkin
|
||||
);
|
||||
caller.write(response);
|
||||
} else if (player.skin.type == Enums.SkinType.CUSTOM) {
|
||||
const response = new SCChannelMessagePacket();
|
||||
response.channel = Constants.EAGLERCRAFT_SKIN_CHANNEL_NAME;
|
||||
response.data =
|
||||
EaglerSkins.writeServerFetchSkinResultCustomPacket(
|
||||
player.uuid,
|
||||
player.skin.skin,
|
||||
false
|
||||
);
|
||||
caller.write(response);
|
||||
} else
|
||||
this._logger.warn(
|
||||
`Player ${caller.username} attempted to fetch player ${player.uuid}'s skin, but their skin hasn't loaded yet!`
|
||||
);
|
||||
}
|
||||
break;
|
||||
case Enums.EaglerSkinPacketId.CFetchSkinReq:
|
||||
const parsedPacket_1 =
|
||||
EaglerSkins.readClientDownloadSkinRequestPacket(packet.data),
|
||||
url = new URL(parsedPacket_1.url).hostname;
|
||||
if (
|
||||
!this.allowedSkinDomains.some((domain) =>
|
||||
Util.areDomainsEqual(domain, url)
|
||||
)
|
||||
) {
|
||||
this._logger.warn(
|
||||
`Player ${caller.username} tried to download a skin with a disallowed domain name(${url})!`
|
||||
);
|
||||
break;
|
||||
}
|
||||
try {
|
||||
const fetched = await EaglerSkins.downloadSkin(parsedPacket_1.url),
|
||||
processed = await EaglerSkins.toEaglerSkin(fetched),
|
||||
response = new SCChannelMessagePacket();
|
||||
response.channel = Constants.EAGLERCRAFT_SKIN_CHANNEL_NAME;
|
||||
response.data = EaglerSkins.writeServerFetchSkinResultCustomPacket(
|
||||
parsedPacket_1.uuid,
|
||||
processed,
|
||||
true
|
||||
);
|
||||
caller.write(response);
|
||||
} catch (err) {
|
||||
this._logger.warn(
|
||||
`Failed to fetch skin URL ${parsedPacket_1.url} for player ${
|
||||
caller.username
|
||||
}: ${err.stack ?? err}`
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export class EaglerSkin {
|
||||
owner: Player;
|
||||
type: Enums.SkinType;
|
||||
|
|
244
src/proxy/skins/ImageEditor.ts
Normal file
244
src/proxy/skins/ImageEditor.ts
Normal file
|
@ -0,0 +1,244 @@
|
|||
import { Constants } from "../Constants.js";
|
||||
import { Enums } from "../Enums.js";
|
||||
import { MineProtocol } from "../Protocol.js";
|
||||
import { Util } from "../Util.js";
|
||||
import Jimp from "jimp";
|
||||
import fs from "fs/promises";
|
||||
|
||||
// let Jimp: Jimp = null;
|
||||
// type Jimp = any;
|
||||
|
||||
let sharp: any = null;
|
||||
type Sharp = any;
|
||||
|
||||
export namespace ImageEditor {
|
||||
let loadedLibraries: boolean = false;
|
||||
|
||||
export async function loadLibraries(native: boolean) {
|
||||
if (loadedLibraries) return;
|
||||
if (native) sharp = (await import("sharp")).default;
|
||||
else {
|
||||
// Jimp = (await import("jimp")).default;
|
||||
Jimp.appendConstructorOption(
|
||||
"Custom Bitmap Constructor",
|
||||
(args) => args[0] && args[0].width != null && args[0].height != null && args[0].data != null,
|
||||
(res, rej, args) => {
|
||||
this.bitmap = args[0];
|
||||
res();
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
loadedLibraries = true;
|
||||
}
|
||||
|
||||
export async function copyRawPixelsJS(imageIn: Jimp, imageOut: Jimp, dx1: number, dy1: number, dx2: number, dy2: number, sx1: number, sy1: number, sx2: number, sy2: number): Promise<Jimp> {
|
||||
console.log(imageOut);
|
||||
if (dx1 > dx2) {
|
||||
return _copyRawPixelsJS(imageIn, imageOut, sx1, sy1, dx2, dy1, sx2 - sx1, sy2 - sy1, imageIn.getWidth(), imageOut.getWidth(), true);
|
||||
} else {
|
||||
return _copyRawPixelsJS(imageIn, imageOut, sx1, sy1, dx2, dy1, sx2 - sx1, sy2 - sy1, imageIn.getWidth(), imageOut.getWidth(), false);
|
||||
}
|
||||
}
|
||||
|
||||
// async function _copyRawPixelsJS(imageIn: Jimp, imageOut: Jimp, srcX: number, srcY: number, dstX: number, dstY: number, width: number, height: number, imgSrcWidth: number, imgDstWidth: number, flip: boolean): Promise<Jimp> {
|
||||
// const inData = imageIn.bitmap.data,
|
||||
// outData = imageOut.bitmap.data;
|
||||
|
||||
// for (let y = 0; y < height; y++) {
|
||||
// for (let x = 0; x < width; x++) {
|
||||
// let srcIndex = (srcY + y) * imgSrcWidth + srcX + x;
|
||||
// let dstIndex = (dstY + y) * imgDstWidth + dstX + x;
|
||||
|
||||
// if (flip) {
|
||||
// srcIndex = (srcY + y) * imgSrcWidth + srcX + (width - x - 1);
|
||||
// }
|
||||
|
||||
// for (let c = 0; c < 4; c++) {
|
||||
// // Assuming RGBA channels
|
||||
// outData[dstIndex * 4 + c] = inData[srcIndex * 4 + c];
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// return imageOut;
|
||||
|
||||
// // return sharp(outData, {
|
||||
// // raw: {
|
||||
// // width: outMeta.width!,
|
||||
// // height: outMeta.height!,
|
||||
// // channels: 4,
|
||||
// // },
|
||||
// // });
|
||||
// }
|
||||
|
||||
async function _copyRawPixelsJS(imageIn: Jimp, imageOut: Jimp, srcX: number, srcY: number, dstX: number, dstY: number, width: number, height: number, imgSrcWidth: number, imgDstWidth: number, flip: boolean) {
|
||||
for (let y = 0; y < height; y++) {
|
||||
for (let x = 0; x < width; x++) {
|
||||
let srcIndex = (srcY + y) * imgSrcWidth + srcX + x;
|
||||
|
||||
if (flip) {
|
||||
srcIndex = (srcY + y) * imgSrcWidth + srcX + (width - x - 1);
|
||||
}
|
||||
|
||||
const pixelColor = imageIn.getPixelColor(srcX + x, srcY + y);
|
||||
const rgba = Jimp.intToRGBA(pixelColor);
|
||||
|
||||
imageOut.setPixelColor(Jimp.rgbaToInt(rgba.r, rgba.g, rgba.b, rgba.a), dstX + x, dstY + y);
|
||||
}
|
||||
}
|
||||
|
||||
return imageOut;
|
||||
}
|
||||
|
||||
export async function copyRawPixels(imageIn: Sharp, imageOut: Sharp, dx1: number, dy1: number, dx2: number, dy2: number, sx1: number, sy1: number, sx2: number, sy2: number): Promise<Sharp> {
|
||||
const inMeta = await imageIn.metadata(),
|
||||
outMeta = await imageOut.metadata();
|
||||
|
||||
if (dx1 > dx2) {
|
||||
return _copyRawPixels(imageIn, imageOut, sx1, sy1, dx2, dy1, sx2 - sx1, sy2 - sy1, inMeta.width!, outMeta.width!, true);
|
||||
} else {
|
||||
return _copyRawPixels(imageIn, imageOut, sx1, sy1, dx1, dy1, sx2 - sx1, sy2 - sy1, inMeta.width!, outMeta.width!, false);
|
||||
}
|
||||
}
|
||||
|
||||
async function _copyRawPixels(imageIn: Sharp, imageOut: Sharp, srcX: number, srcY: number, dstX: number, dstY: number, width: number, height: number, imgSrcWidth: number, imgDstWidth: number, flip: boolean): Promise<Sharp> {
|
||||
const inData = await imageIn.raw().toBuffer();
|
||||
const outData = await imageOut.raw().toBuffer();
|
||||
const outMeta = await imageOut.metadata();
|
||||
|
||||
for (let y = 0; y < height; y++) {
|
||||
for (let x = 0; x < width; x++) {
|
||||
let srcIndex = (srcY + y) * imgSrcWidth + srcX + x;
|
||||
let dstIndex = (dstY + y) * imgDstWidth + dstX + x;
|
||||
|
||||
if (flip) {
|
||||
srcIndex = (srcY + y) * imgSrcWidth + srcX + (width - x - 1);
|
||||
}
|
||||
|
||||
for (let c = 0; c < 4; c++) {
|
||||
// Assuming RGBA channels
|
||||
outData[dstIndex * 4 + c] = inData[srcIndex * 4 + c];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return sharp(outData, {
|
||||
raw: {
|
||||
width: outMeta.width!,
|
||||
height: outMeta.height!,
|
||||
channels: 4,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function toEaglerSkinJS(image: Buffer): Promise<Util.BoundedBuffer<typeof Constants.EAGLERCRAFT_SKIN_CUSTOM_LENGTH>> {
|
||||
let jimpImage = await Jimp.read(image),
|
||||
height = jimpImage.getHeight();
|
||||
if (height != 64) {
|
||||
// assume 32 height skin
|
||||
let imageOut = await Jimp.create(64, 64, 0x0);
|
||||
|
||||
for (let x = 0; x < jimpImage.getWidth(); x++) {
|
||||
for (let y = 0; y < jimpImage.getHeight(); y++) {
|
||||
imageOut.setPixelColor(jimpImage.getPixelColor(x, y), x, y);
|
||||
}
|
||||
}
|
||||
|
||||
imageOut = await copyRawPixelsJS(jimpImage, imageOut, 24, 48, 20, 52, 4, 16, 8, 20);
|
||||
imageOut = await copyRawPixelsJS(jimpImage, imageOut, 28, 48, 24, 52, 8, 16, 12, 20);
|
||||
imageOut = await copyRawPixelsJS(jimpImage, imageOut, 20, 52, 16, 64, 8, 20, 12, 32);
|
||||
imageOut = await copyRawPixelsJS(jimpImage, imageOut, 24, 52, 20, 64, 4, 20, 8, 32);
|
||||
imageOut = await copyRawPixelsJS(jimpImage, imageOut, 28, 52, 24, 64, 0, 20, 4, 32);
|
||||
imageOut = await copyRawPixelsJS(jimpImage, imageOut, 32, 52, 28, 64, 12, 20, 16, 32);
|
||||
imageOut = await copyRawPixelsJS(jimpImage, imageOut, 40, 48, 36, 52, 44, 16, 48, 20);
|
||||
imageOut = await copyRawPixelsJS(jimpImage, imageOut, 44, 48, 40, 52, 48, 16, 52, 20);
|
||||
imageOut = await copyRawPixelsJS(jimpImage, imageOut, 36, 52, 32, 64, 48, 20, 52, 32);
|
||||
imageOut = await copyRawPixelsJS(jimpImage, imageOut, 40, 52, 36, 64, 44, 20, 48, 32);
|
||||
imageOut = await copyRawPixelsJS(jimpImage, imageOut, 44, 52, 40, 64, 40, 20, 44, 32);
|
||||
imageOut = await copyRawPixelsJS(jimpImage, imageOut, 48, 52, 44, 64, 52, 20, 56, 32);
|
||||
|
||||
jimpImage = imageOut;
|
||||
}
|
||||
|
||||
const newBuff = Buffer.alloc(Constants.EAGLERCRAFT_SKIN_CUSTOM_LENGTH);
|
||||
const bitmap = jimpImage.bitmap.data;
|
||||
for (let i = 1; i < 64 ** 2; i++) {
|
||||
const bytePos = i * 4;
|
||||
// red, green, blue, alpha => alpha, blue, green, red
|
||||
newBuff[bytePos] = bitmap[bytePos + 3];
|
||||
newBuff[bytePos + 1] = bitmap[bytePos + 2];
|
||||
newBuff[bytePos + 2] = bitmap[bytePos + 1];
|
||||
newBuff[bytePos + 3] = bitmap[bytePos];
|
||||
}
|
||||
return newBuff;
|
||||
}
|
||||
|
||||
export async function toEaglerSkin(image: Buffer): Promise<Util.BoundedBuffer<typeof Constants.EAGLERCRAFT_SKIN_CUSTOM_LENGTH>> {
|
||||
const meta = await sharp(image).metadata();
|
||||
let sharpImage = sharp(image);
|
||||
if (meta.height != 64) {
|
||||
// assume 32 height skin
|
||||
let imageOut = sharp(await sharpImage.extend({ bottom: 32, background: { r: 0, g: 0, b: 0, alpha: 0 } }).toBuffer());
|
||||
|
||||
imageOut = await copyRawPixels(sharpImage, imageOut, 24, 48, 20, 52, 4, 16, 8, 20);
|
||||
imageOut = await copyRawPixels(sharpImage, imageOut, 28, 48, 24, 52, 8, 16, 12, 20);
|
||||
imageOut = await copyRawPixels(sharpImage, imageOut, 20, 52, 16, 64, 8, 20, 12, 32);
|
||||
imageOut = await copyRawPixels(sharpImage, imageOut, 24, 52, 20, 64, 4, 20, 8, 32);
|
||||
imageOut = await copyRawPixels(sharpImage, imageOut, 28, 52, 24, 64, 0, 20, 4, 32);
|
||||
imageOut = await copyRawPixels(sharpImage, imageOut, 32, 52, 28, 64, 12, 20, 16, 32);
|
||||
imageOut = await copyRawPixels(sharpImage, imageOut, 40, 48, 36, 52, 44, 16, 48, 20);
|
||||
imageOut = await copyRawPixels(sharpImage, imageOut, 44, 48, 40, 52, 48, 16, 52, 20);
|
||||
imageOut = await copyRawPixels(sharpImage, imageOut, 36, 52, 32, 64, 48, 20, 52, 32);
|
||||
imageOut = await copyRawPixels(sharpImage, imageOut, 40, 52, 36, 64, 44, 20, 48, 32);
|
||||
imageOut = await copyRawPixels(sharpImage, imageOut, 44, 52, 40, 64, 40, 20, 44, 32);
|
||||
imageOut = await copyRawPixels(sharpImage, imageOut, 48, 52, 44, 64, 52, 20, 56, 32);
|
||||
|
||||
sharpImage = imageOut;
|
||||
}
|
||||
|
||||
const r = await sharpImage.extractChannel("red").raw({ depth: "uchar" }).toBuffer();
|
||||
const g = await sharpImage.extractChannel("green").raw({ depth: "uchar" }).toBuffer();
|
||||
const b = await sharpImage.extractChannel("blue").raw({ depth: "uchar" }).toBuffer();
|
||||
const a = await sharpImage.ensureAlpha().extractChannel(3).toColorspace("b-w").raw({ depth: "uchar" }).toBuffer();
|
||||
const newBuff = Buffer.alloc(Constants.EAGLERCRAFT_SKIN_CUSTOM_LENGTH);
|
||||
for (let i = 1; i < 64 ** 2; i++) {
|
||||
const bytePos = i * 4;
|
||||
newBuff[bytePos] = a[i];
|
||||
newBuff[bytePos + 1] = b[i];
|
||||
newBuff[bytePos + 2] = g[i];
|
||||
newBuff[bytePos + 3] = r[i];
|
||||
}
|
||||
return newBuff;
|
||||
}
|
||||
|
||||
export function generateEaglerMOTDImage(file: string | Buffer): Promise<Buffer> {
|
||||
return new Promise<Buffer>((res, rej) => {
|
||||
sharp(file)
|
||||
.resize(Constants.ICON_SQRT, Constants.ICON_SQRT, {
|
||||
kernel: "nearest",
|
||||
})
|
||||
.raw({
|
||||
depth: "uchar",
|
||||
})
|
||||
.toBuffer()
|
||||
.then((buff) => {
|
||||
for (const pixel of buff) {
|
||||
if ((pixel & 0xffffff) == 0) {
|
||||
buff[buff.indexOf(pixel)] = 0;
|
||||
}
|
||||
}
|
||||
res(buff);
|
||||
})
|
||||
.catch(rej);
|
||||
});
|
||||
}
|
||||
|
||||
export function generateEaglerMOTDImageJS(file: string | Buffer): Promise<Buffer> {
|
||||
return new Promise<Buffer>(async (res, rej) => {
|
||||
Jimp.read(typeof file == "string" ? await fs.readFile(file) : file, async (err, image) => {
|
||||
image = image.resize(Constants.ICON_SQRT, Constants.ICON_SQRT, Jimp.RESIZE_NEAREST_NEIGHBOR);
|
||||
res(image.bitmap.data);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
|
@ -1,75 +0,0 @@
|
|||
export default class SimpleRatelimit<T> {
|
||||
readonly requestCount: number;
|
||||
readonly resetInterval: number;
|
||||
private entries: Map<T, Ratelimit>;
|
||||
|
||||
constructor(requestCount: number, resetInterval: number) {
|
||||
this.requestCount = requestCount;
|
||||
this.resetInterval = resetInterval;
|
||||
this.entries = new Map();
|
||||
}
|
||||
|
||||
public get(key: T): Ratelimit {
|
||||
return (
|
||||
this.entries.get(key) ?? {
|
||||
remainingRequests: this.requestCount,
|
||||
resetTime: new Date(0),
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
public consume(key: T, count?: number): Ratelimit | never {
|
||||
if (this.entries.has(key)) {
|
||||
const ratelimit = this.entries.get(key);
|
||||
if (ratelimit.remainingRequests - (count ?? 1) < 0) {
|
||||
if (this.requestCount - (count ?? 1) < 0) {
|
||||
throw new RatelimitExceededError(
|
||||
`Consume request count is higher than default available request count!`
|
||||
);
|
||||
} else {
|
||||
throw new RatelimitExceededError(
|
||||
`Ratelimit exceeded, try again in ${
|
||||
ratelimit.resetTime.getDate() - Date.now()
|
||||
} ms!`
|
||||
);
|
||||
}
|
||||
}
|
||||
ratelimit.remainingRequests -= count ?? 1;
|
||||
return ratelimit;
|
||||
} else {
|
||||
if (this.requestCount - (count ?? 1) < 0) {
|
||||
throw new RatelimitExceededError(
|
||||
`Consume request count is higher than default available request count!`
|
||||
);
|
||||
}
|
||||
const ratelimit: Ratelimit = {
|
||||
remainingRequests: this.requestCount - (count ?? 1),
|
||||
resetTime: new Date(Date.now() + this.resetInterval),
|
||||
timer: null,
|
||||
};
|
||||
this.entries.set(key, ratelimit);
|
||||
ratelimit.timer = this._onAdd(ratelimit);
|
||||
return ratelimit;
|
||||
}
|
||||
}
|
||||
|
||||
private _onAdd(ratelimit: Ratelimit): NodeJS.Timer {
|
||||
return setInterval(() => {
|
||||
// TODO: work on
|
||||
}, this.resetInterval);
|
||||
}
|
||||
}
|
||||
|
||||
export type Ratelimit = {
|
||||
remainingRequests: number;
|
||||
resetTime: Date;
|
||||
timer?: NodeJS.Timer;
|
||||
};
|
||||
|
||||
export class RatelimitExceededError extends Error {
|
||||
constructor(message: { toString: () => string }) {
|
||||
super(message.toString());
|
||||
this.name = "RatelimitExceededError";
|
||||
Object.setPrototypeOf(this, RatelimitExceededError.prototype);
|
||||
}
|
||||
}
|
146
src/proxy/skins/SkinServer.ts
Normal file
146
src/proxy/skins/SkinServer.ts
Normal file
|
@ -0,0 +1,146 @@
|
|||
import DiskDB from "../databases/DiskDB.js";
|
||||
import crypto from "crypto";
|
||||
import { Logger } from "../../logger.js";
|
||||
import { Constants } from "../Constants.js";
|
||||
import { Enums } from "../Enums.js";
|
||||
import { Player } from "../Player.js";
|
||||
import { Proxy } from "../Proxy.js";
|
||||
import { Util } from "../Util.js";
|
||||
import { CSChannelMessagePacket } from "../packets/channel/CSChannelMessage.js";
|
||||
import { SCChannelMessagePacket } from "../packets/channel/SCChannelMessage.js";
|
||||
import { EaglerSkins } from "./EaglerSkins.js";
|
||||
import { ImageEditor } from "./ImageEditor.js";
|
||||
import { MineProtocol } from "../Protocol.js";
|
||||
import ExponentialBackoffRequestController from "../ratelimit/ExponentialBackoffRequestController.js";
|
||||
|
||||
export class SkinServer {
|
||||
public allowedSkinDomains: string[];
|
||||
public cache: DiskDB<CachedSkin>;
|
||||
public proxy: Proxy;
|
||||
public backoffController: ExponentialBackoffRequestController;
|
||||
public usingNative: boolean;
|
||||
public usingCache: boolean;
|
||||
private _logger: Logger;
|
||||
private deleteTask: NodeJS.Timer;
|
||||
private lifetime: number;
|
||||
|
||||
constructor(proxy: Proxy, native: boolean, sweepInterval: number, cacheLifetime: number, cacheFolder: string = "./skinCache", useCache: boolean = true, allowedSkinDomains?: string[]) {
|
||||
this.allowedSkinDomains = allowedSkinDomains ?? ["textures.minecraft.net"];
|
||||
if (useCache) {
|
||||
this.cache = new DiskDB(
|
||||
cacheFolder,
|
||||
(v) => exportCachedSkin(v),
|
||||
(b) => readCachedSkin(b),
|
||||
(k) => k.replaceAll("-", "")
|
||||
);
|
||||
}
|
||||
this.proxy = proxy ?? PROXY;
|
||||
this.usingCache = useCache;
|
||||
this.usingNative = native;
|
||||
this.lifetime = cacheLifetime;
|
||||
this.backoffController = new ExponentialBackoffRequestController();
|
||||
this._logger = new Logger("SkinServer");
|
||||
this._logger.info("Started EaglercraftX skin server.");
|
||||
if (useCache) this.deleteTask = setInterval(async () => await this.cache.filter((ent) => Date.now() < ent.expires), sweepInterval);
|
||||
}
|
||||
|
||||
public unload() {
|
||||
if (this.deleteTask != null) clearInterval(this.deleteTask);
|
||||
}
|
||||
|
||||
public async handleRequest(packet: CSChannelMessagePacket, caller: Player, proxy: Proxy) {
|
||||
if (packet.messageType == Enums.ChannelMessageType.SERVER) throw new Error("Server message was passed to client message handler!");
|
||||
else if (packet.channel != Constants.EAGLERCRAFT_SKIN_CHANNEL_NAME) throw new Error("Cannot handle non-EaglerX skin channel messages!");
|
||||
|
||||
{
|
||||
const rl = proxy.ratelimit.skinsConnection.consume(caller.username),
|
||||
rlip = proxy.ratelimit.skinsIP.consume(caller.ws._socket.remoteAddress);
|
||||
if (!rl.success || !rlip.success) return;
|
||||
}
|
||||
|
||||
switch (packet.data[0] as Enums.EaglerSkinPacketId) {
|
||||
default:
|
||||
throw new Error("Unknown operation!");
|
||||
break;
|
||||
case Enums.EaglerSkinPacketId.CFetchSkinEaglerPlayerReq:
|
||||
const parsedPacket_0 = EaglerSkins.readClientFetchEaglerSkinPacket(packet.data);
|
||||
const player = this.proxy.fetchUserByUUID(parsedPacket_0.uuid);
|
||||
if (player) {
|
||||
if (player.skin.type == Enums.SkinType.BUILTIN) {
|
||||
const response = new SCChannelMessagePacket();
|
||||
response.channel = Constants.EAGLERCRAFT_SKIN_CHANNEL_NAME;
|
||||
response.data = EaglerSkins.writeServerFetchSkinResultBuiltInPacket(player.uuid, player.skin.builtInSkin);
|
||||
caller.write(response);
|
||||
} else if (player.skin.type == Enums.SkinType.CUSTOM) {
|
||||
const response = new SCChannelMessagePacket();
|
||||
response.channel = Constants.EAGLERCRAFT_SKIN_CHANNEL_NAME;
|
||||
response.data = EaglerSkins.writeServerFetchSkinResultCustomPacket(player.uuid, player.skin.skin, false);
|
||||
caller.write(response);
|
||||
} else this._logger.warn(`Player ${caller.username} attempted to fetch player ${player.uuid}'s skin, but their skin hasn't loaded yet!`);
|
||||
}
|
||||
break;
|
||||
case Enums.EaglerSkinPacketId.CFetchSkinReq:
|
||||
const parsedPacket_1 = EaglerSkins.readClientDownloadSkinRequestPacket(packet.data),
|
||||
url = new URL(parsedPacket_1.url).hostname;
|
||||
if (!this.allowedSkinDomains.some((domain) => Util.areDomainsEqual(domain, url))) {
|
||||
this._logger.warn(`Player ${caller.username} tried to download a skin with a disallowed domain name(${url})!`);
|
||||
break;
|
||||
}
|
||||
try {
|
||||
let cacheHit = null,
|
||||
skin = null;
|
||||
if (this.usingCache) {
|
||||
(cacheHit = await this.cache.get(parsedPacket_1.uuid)), (skin = cacheHit != null ? cacheHit.data : null);
|
||||
|
||||
if (!skin) {
|
||||
const fetched = await EaglerSkins.safeDownloadSkin(parsedPacket_1.url, this.backoffController);
|
||||
skin = fetched;
|
||||
await this.cache.set(parsedPacket_1.uuid, {
|
||||
uuid: parsedPacket_1.uuid,
|
||||
expires: Date.now() + this.lifetime,
|
||||
data: fetched,
|
||||
});
|
||||
}
|
||||
} else {
|
||||
skin = await EaglerSkins.downloadSkin(parsedPacket_1.url);
|
||||
}
|
||||
|
||||
const processed = this.usingNative ? await ImageEditor.toEaglerSkin(skin) : await ImageEditor.toEaglerSkinJS(skin),
|
||||
response = new SCChannelMessagePacket();
|
||||
response.channel = Constants.EAGLERCRAFT_SKIN_CHANNEL_NAME;
|
||||
response.data = EaglerSkins.writeServerFetchSkinResultCustomPacket(parsedPacket_1.uuid, processed, true);
|
||||
caller.write(response);
|
||||
} catch (err) {
|
||||
this._logger.warn(`Failed to fetch skin URL ${parsedPacket_1.url} for player ${caller.username}: ${err.stack ?? err}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export type CachedSkin = {
|
||||
uuid: string;
|
||||
expires: number;
|
||||
data: Buffer;
|
||||
};
|
||||
|
||||
function digestMd5Hex(data: Buffer | string): string {
|
||||
return crypto.createHash("md5").update(data).digest("hex");
|
||||
}
|
||||
|
||||
function exportCachedSkin(skin: CachedSkin): Buffer {
|
||||
const endUuid = MineProtocol.writeString(skin.uuid),
|
||||
encExp = MineProtocol.writeVarLong(skin.expires),
|
||||
encSkin = MineProtocol.writeBinary(skin.data);
|
||||
return Buffer.concat([endUuid, encExp, encSkin]);
|
||||
}
|
||||
|
||||
function readCachedSkin(data: Buffer): CachedSkin {
|
||||
const readUuid = MineProtocol.readString(data),
|
||||
readExp = MineProtocol.readVarLong(readUuid.newBuffer),
|
||||
readSkin = MineProtocol.readBinary(readExp.newBuffer);
|
||||
return {
|
||||
uuid: readUuid.value,
|
||||
expires: readExp.value,
|
||||
data: readSkin.value,
|
||||
};
|
||||
}
|
Loading…
Reference in New Issue
Block a user