From d7e29c5c00f836e1247b0fb7825e6c0d5ff45bda Mon Sep 17 00:00:00 2001 From: q13x <84165981+WorldEditAxe@users.noreply.github.com> Date: Tue, 4 Jul 2023 19:01:02 -0700 Subject: [PATCH] ran prettier --- example_plugin/index.ts | 4 +- example_plugin/metadata.json | 16 +- replit_runtime/config.js | 11 +- replit_runtime/index.js | 236 +++--- replit_runtime/package.json | 8 +- src/config.ts | 54 +- src/globals.d.ts | 17 +- src/index.ts | 28 +- src/launcher_types.ts | 74 +- src/logger.ts | 114 ++- src/meta.ts | 12 +- src/plugins/EagProxyAAS/auth.ts | 129 +-- src/plugins/EagProxyAAS/config.ts | 2 +- src/plugins/EagProxyAAS/index.ts | 100 +-- src/plugins/EagProxyAAS/metadata.json | 57 +- src/plugins/EagProxyAAS/types.ts | 5 +- src/plugins/EagProxyAAS/utils.ts | 414 ++++++++-- src/proxy/BungeeUtil.ts | 288 +++---- src/proxy/Chat.ts | 97 +-- src/proxy/Constants.ts | 22 +- src/proxy/Enums.ts | 130 ++-- src/proxy/Motd.ts | 269 ++++--- src/proxy/Packet.ts | 65 +- src/proxy/Protocol.ts | 141 ++-- src/proxy/Proxy.ts | 732 +++++++++++------- src/proxy/Util.ts | 379 +++++---- src/proxy/packets/CSLoginPacket.ts | 88 ++- src/proxy/packets/CSReadyPacket.ts | 22 +- src/proxy/packets/CSSetSkinPacket.ts | 108 +-- src/proxy/packets/CSUsernamePacket.ts | 44 +- src/proxy/packets/SCDisconnectPacket.ts | 51 +- src/proxy/packets/SCIdentifyPacket.ts | 72 +- src/proxy/packets/SCReadyPacket.ts | 22 +- src/proxy/packets/SCSyncUuidPacket.ts | 48 +- src/proxy/packets/channel/CSChannelMessage.ts | 47 +- src/proxy/packets/channel/SCChannelMessage.ts | 49 +- src/proxy/pluginLoader/PluginLoaderTypes.ts | 124 +-- src/proxy/pluginLoader/PluginManager.ts | 599 ++++++++------ src/proxy/skins/SimpleRatelimit.ts | 114 +-- tsconfig.json | 11 +- 40 files changed, 2827 insertions(+), 1976 deletions(-) diff --git a/example_plugin/index.ts b/example_plugin/index.ts index 4bb6b6b..2152e93 100644 --- a/example_plugin/index.ts +++ b/example_plugin/index.ts @@ -1,2 +1,2 @@ -const logger = new PLUGIN_MANAGER.Logger("ExamplePlugin") -logger.info("Hi!") \ No newline at end of file +const logger = new PLUGIN_MANAGER.Logger("ExamplePlugin"); +logger.info("Hi!"); diff --git a/example_plugin/metadata.json b/example_plugin/metadata.json index 91b5e63..825af5d 100644 --- a/example_plugin/metadata.json +++ b/example_plugin/metadata.json @@ -1,9 +1,9 @@ { - "name": "Example Plugin", - "id": "examplePlugin", - "version": "1.0.0", - "entry_point": "index.js", - "requirements": [], - "incompatibilities": [], - "load_after": [] -} \ No newline at end of file + "name": "Example Plugin", + "id": "examplePlugin", + "version": "1.0.0", + "entry_point": "index.js", + "requirements": [], + "incompatibilities": [], + "load_after": [] +} diff --git a/replit_runtime/config.js b/replit_runtime/config.js index 33a9cbf..c588a77 100644 --- a/replit_runtime/config.js +++ b/replit_runtime/config.js @@ -1,6 +1,9 @@ -const path = require("path") -const os = require("os") +const path = require("path"); +const os = require("os"); module.exports = { - sourceDir: path.resolve(os.homedir(), path.join(process.env.REPL_SLUG, "src")) -} \ No newline at end of file + sourceDir: path.resolve( + os.homedir(), + path.join(process.env.REPL_SLUG, "src") + ), +}; diff --git a/replit_runtime/index.js b/replit_runtime/index.js index 14b3bcc..b162f19 100644 --- a/replit_runtime/index.js +++ b/replit_runtime/index.js @@ -1,135 +1,147 @@ // libraries -const fs = require("fs/promises") -const path = require("path") -const crypto = require("crypto") -const { - sourceDir -} = require("./config.js") - +const fs = require("fs/promises"); +const path = require("path"); +const crypto = require("crypto"); +const { sourceDir } = require("./config.js"); class Logger { - constructor({ name, logDebug }) { - this.name = name - this.debug = logDebug - } + constructor({ name, logDebug }) { + this.name = name; + this.debug = logDebug; + } - _log(logType, data, method) { - console[method](`[${this.name}] [${logType}] ${typeof data == "string" ? data : data.toString()}`) - } + _log(logType, data, method) { + console[method]( + `[${this.name}] [${logType}] ${ + typeof data == "string" ? data : data.toString() + }` + ); + } - info(data) { - this._log("info", data, "log") - } + info(data) { + this._log("info", data, "log"); + } - warn(data) { - this._log("warn", data, "error") - } + warn(data) { + this._log("warn", data, "error"); + } - error(data) { - this._log("error", data, "error") - } + error(data) { + this._log("error", data, "error"); + } - debug(data) { - if (this.debug) { - this._log("debug", data, "error") - } + debug(data) { + if (this.debug) { + this._log("debug", data, "error"); } + } } async function recursiveFileSearch(dir) { - const fileList = [] - for (const file of await fs.readdir(dir, { withFileTypes: true })) { - let pathDir = path.resolve(dir, file.name) - if (file.isFile()) { - fileList.push(pathDir) - } else if (file.isDirectory()) { - fileList.push(...(await recursiveFileSearch(pathDir))) - } else { - logger.warn(`Found directory entry that is neither a file or directory (${pathDir}), ignoring!`) - } + const fileList = []; + for (const file of await fs.readdir(dir, { withFileTypes: true })) { + let pathDir = path.resolve(dir, file.name); + if (file.isFile()) { + fileList.push(pathDir); + } else if (file.isDirectory()) { + fileList.push(...(await recursiveFileSearch(pathDir))); + } else { + logger.warn( + `Found directory entry that is neither a file or directory (${pathDir}), ignoring!` + ); } - return fileList + } + return fileList; } const logger = new Logger({ - name: "launcher", - logDebug: process.env.DEBUG == "true" -}), - LINE_SEPERATOR = "-----------------------------------" + name: "launcher", + logDebug: process.env.DEBUG == "true", + }), + LINE_SEPERATOR = "-----------------------------------"; if (!process.env.REPL_SLUG) { - logger.error(LINE_SEPERATOR) - logger.error("Repl not detected!") - logger.error("") - logger.error("This file is meant to be ran in a Repl") - logger.error(LINE_SEPERATOR) + logger.error(LINE_SEPERATOR); + logger.error("Repl not detected!"); + logger.error(""); + logger.error("This file is meant to be ran in a Repl"); + logger.error(LINE_SEPERATOR); } -logger.info(LINE_SEPERATOR) -logger.info("Checking if the proxy needs to be recompiled...") -logger.info(LINE_SEPERATOR) +logger.info(LINE_SEPERATOR); +logger.info("Checking if the proxy needs to be recompiled..."); +logger.info(LINE_SEPERATOR); fs.readFile(path.join(__dirname, ".sourcehash")) - .then(data => { - let oldHash = data.toString() - logger.info("Found old hash, calculating hash of source files...") - recursiveFileSearch(sourceDir) - .then(files => { - Promise.all(files.map(f => fs.readFile(f))) - .then(data => { - const hash = crypto.createHash("sha256") - data.forEach(d => hash.update(d)) - let sourceHash = hash.digest().toString() + .then((data) => { + let oldHash = data.toString(); + logger.info("Found old hash, calculating hash of source files..."); + recursiveFileSearch(sourceDir) + .then((files) => { + Promise.all(files.map((f) => fs.readFile(f))).then((data) => { + const hash = crypto.createHash("sha256"); + data.forEach((d) => hash.update(d)); + let sourceHash = hash.digest().toString(); - if (sourceHash === oldHash) { - logger.info("Source hasn't been changed, skipping compilation...") - process.exit(0) - } else { - logger.info("Source has been changed, recompiling...") - fs.writeFile(path.join(__dirname, ".sourcehash"), sourceHash) - .then(() => { - process.exit(2) - }) - .catch(err => { - logger.error(`Could not write new hash to disk!\n${err.stack}`) - process.exit(1) - }) - } - }) - }) - .catch(err => { - logger.error(`Could not calculate file hashes for files in directory ${sourceDir}!\n${err.stack}`) - process.exit(1) - }) - }) - .catch(err => { - if (err.code == "ENOENT") { - logger.warn("Previous source hash not found! Assuming a clean install is being used.") - logger.info("Calculating hash...") - recursiveFileSearch(sourceDir) - .then(files => { - Promise.all(files.map(f => fs.readFile(f))) - .then(data => { - const hash = crypto.createHash("sha256") - data.forEach(d => hash.update(d)) - let sourceHash = hash.digest().toString() - fs.writeFile(path.join(__dirname, ".sourcehash"), sourceHash) - .then(() => { - logger.info("Saved hash to disk.") - process.exit(2) - }) - .catch(err => { - logger.error(`Could not write new hash to disk!\n${err.stack}`) - process.exit(1) - }) - }) - }) - .catch(err => { - logger.error(`Could not calculate file hashes for files in directory ${sourceDir}!\n${err.stack}`) - process.exit(1) - }) - } else { - logger.error(`Could not read .sourcehash file in ${path.join(__dirname, ".sourcehash")} due to an unknown error! Try again with a clean repl?\n${err.stack}`) - process.exit(1) - } - }) \ No newline at end of file + if (sourceHash === oldHash) { + logger.info("Source hasn't been changed, skipping compilation..."); + process.exit(0); + } else { + logger.info("Source has been changed, recompiling..."); + fs.writeFile(path.join(__dirname, ".sourcehash"), sourceHash) + .then(() => { + process.exit(2); + }) + .catch((err) => { + logger.error(`Could not write new hash to disk!\n${err.stack}`); + process.exit(1); + }); + } + }); + }) + .catch((err) => { + logger.error( + `Could not calculate file hashes for files in directory ${sourceDir}!\n${err.stack}` + ); + process.exit(1); + }); + }) + .catch((err) => { + if (err.code == "ENOENT") { + logger.warn( + "Previous source hash not found! Assuming a clean install is being used." + ); + logger.info("Calculating hash..."); + recursiveFileSearch(sourceDir) + .then((files) => { + Promise.all(files.map((f) => fs.readFile(f))).then((data) => { + const hash = crypto.createHash("sha256"); + data.forEach((d) => hash.update(d)); + let sourceHash = hash.digest().toString(); + fs.writeFile(path.join(__dirname, ".sourcehash"), sourceHash) + .then(() => { + logger.info("Saved hash to disk."); + process.exit(2); + }) + .catch((err) => { + logger.error(`Could not write new hash to disk!\n${err.stack}`); + process.exit(1); + }); + }); + }) + .catch((err) => { + logger.error( + `Could not calculate file hashes for files in directory ${sourceDir}!\n${err.stack}` + ); + process.exit(1); + }); + } else { + logger.error( + `Could not read .sourcehash file in ${path.join( + __dirname, + ".sourcehash" + )} due to an unknown error! Try again with a clean repl?\n${err.stack}` + ); + process.exit(1); + } + }); diff --git a/replit_runtime/package.json b/replit_runtime/package.json index 75e7ea6..401c9a7 100644 --- a/replit_runtime/package.json +++ b/replit_runtime/package.json @@ -1,5 +1,5 @@ { - "name": "replit_runtime", - "description": "Runtime preparation utility for Repls. Determines whether or not a recompilation is necessary.", - "type": "commonjs" -} \ No newline at end of file + "name": "replit_runtime", + "description": "Runtime preparation utility for Repls. Determines whether or not a recompilation is necessary.", + "type": "commonjs" +} diff --git a/src/config.ts b/src/config.ts index 023bb59..90c4457 100644 --- a/src/config.ts +++ b/src/config.ts @@ -4,30 +4,32 @@ import { Config } from "./launcher_types.js"; export const config: Config = { - bridge: { - enabled: false, - motd: null + bridge: { + enabled: false, + motd: null, + }, + adapter: { + name: "EaglerProxy", + bindHost: "0.0.0.0", + bindPort: 8080, + maxConcurrentClients: 20, + skinUrlWhitelist: undefined, + motd: true + ? "FORWARD" + : { + iconURL: "logo.png", + l1: "yes", + l2: "no", + }, + origins: { + allowOfflineDownloads: true, + originWhitelist: null, + originBlacklist: null, }, - adapter: { - name: "EaglerProxy", - bindHost: "0.0.0.0", - bindPort: 8080, - maxConcurrentClients: 20, - skinUrlWhitelist: undefined, - motd: true ? "FORWARD" : { - iconURL: "logo.png", - l1: "yes", - l2: "no" - }, - origins: { - allowOfflineDownloads: true, - originWhitelist: null, - originBlacklist: null - }, - server: { - host: "no", - port: 46625 - }, - tls: undefined - } -} \ No newline at end of file + server: { + host: "no", + port: 46625, + }, + tls: undefined, + }, +}; diff --git a/src/globals.d.ts b/src/globals.d.ts index fd097cc..e7deaf4 100644 --- a/src/globals.d.ts +++ b/src/globals.d.ts @@ -4,10 +4,13 @@ import { Config } from "./launcher_types.js"; import { PluginManager } from "./proxy/pluginLoader/PluginManager.js"; declare global { - var CONFIG: Config - var PROXY: Proxy - var PLUGIN_MANAGER: PluginManager - var PACKET_REGISTRY: Map -} \ No newline at end of file + var CONFIG: Config; + var PROXY: Proxy; + var PLUGIN_MANAGER: PluginManager; + var PACKET_REGISTRY: Map< + number, + Packet & { + class: any; + } + >; +} diff --git a/src/index.ts b/src/index.ts index 26038cc..14fefab 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,26 +2,28 @@ import * as dotenv from "dotenv"; import process from "process"; import { Proxy } from "./proxy/Proxy.js"; import { config } from "./config.js"; -dotenv.config() +dotenv.config(); import { Logger } from "./logger.js"; import { PROXY_BRANDING } from "./meta.js"; import { PluginManager } from "./proxy/pluginLoader/PluginManager.js"; import { dirname, join } from "path"; import { fileURLToPath } from "url"; -const logger = new Logger("Launcher") -let proxy: Proxy +const logger = new Logger("Launcher"); +let proxy: Proxy; -global.CONFIG = config +global.CONFIG = config; -logger.info("Loading plugins...") -const pluginManager = new PluginManager(join(dirname(fileURLToPath(import.meta.url)), "plugins")) -global.PLUGIN_MANAGER = pluginManager -await pluginManager.loadPlugins() +logger.info("Loading plugins..."); +const pluginManager = new PluginManager( + join(dirname(fileURLToPath(import.meta.url)), "plugins") +); +global.PLUGIN_MANAGER = pluginManager; +await pluginManager.loadPlugins(); -proxy = new Proxy(config.adapter, pluginManager) -pluginManager.proxy = proxy +proxy = new Proxy(config.adapter, pluginManager); +pluginManager.proxy = proxy; -logger.info(`Launching ${PROXY_BRANDING}...`) -await proxy.init() -global.PROXY = proxy \ No newline at end of file +logger.info(`Launching ${PROXY_BRANDING}...`); +await proxy.init(); +global.PROXY = proxy; diff --git a/src/launcher_types.ts b/src/launcher_types.ts index c2a34b6..3673ab3 100644 --- a/src/launcher_types.ts +++ b/src/launcher_types.ts @@ -1,40 +1,44 @@ export type Config = { - bridge: BridgeOptions, - adapter: AdapterOptions -} + bridge: BridgeOptions; + adapter: AdapterOptions; +}; export type BridgeOptions = { - enabled: boolean, - motd: 'FORWARD' | { - iconURL?: string, - l1: string, - l2?: string - } -} + enabled: boolean; + motd: + | "FORWARD" + | { + iconURL?: string; + l1: string; + l2?: string; + }; +}; export type AdapterOptions = { - name: "EaglerProxy", - bindHost: string, - bindPort: number, - maxConcurrentClients: 20, - skinUrlWhitelist?: string[], - origins: { - allowOfflineDownloads: boolean, - originWhitelist: string[], - originBlacklist: string[] - }, - motd: 'FORWARD' | { - iconURL?: string, - l1: string, - l2?: string - }, - server: { - host: string, - port: number - }, - tls?: { - enabled: boolean, - key: null, - cert: null - } -} \ No newline at end of file + name: "EaglerProxy"; + bindHost: string; + bindPort: number; + maxConcurrentClients: 20; + skinUrlWhitelist?: string[]; + origins: { + allowOfflineDownloads: boolean; + originWhitelist: string[]; + originBlacklist: string[]; + }; + motd: + | "FORWARD" + | { + iconURL?: string; + l1: string; + l2?: string; + }; + server: { + host: string; + port: number; + }; + tls?: { + enabled: boolean; + key: null; + cert: null; + }; +}; diff --git a/src/logger.ts b/src/logger.ts index 27c65a8..91a5049 100644 --- a/src/logger.ts +++ b/src/logger.ts @@ -1,63 +1,95 @@ -import { Chalk } from "chalk" +import { Chalk } from "chalk"; -const color = new Chalk({ level: 2 }) +const color = new Chalk({ level: 2 }); -let global_verbose: boolean = false +let global_verbose: boolean = false; -type JsonLogType = "info" | "warn" | "error" | "fatal" | "debug" +type JsonLogType = "info" | "warn" | "error" | "fatal" | "debug"; type JsonOutput = { - type: JsonLogType, - message: string -} + type: JsonLogType; + message: string; +}; export function verboseLogging(newVal?: boolean) { - global_verbose = newVal ?? global_verbose ? false : true + global_verbose = newVal ?? global_verbose ? false : true; } function jsonLog(type: JsonLogType, message: string): string { - return JSON.stringify({ - type: type, - message: message + return ( + JSON.stringify({ + type: type, + message: message, }) + "\n" + ); } export class Logger { - loggerName: string - verbose: boolean - private jsonLog: boolean = process.argv.includes("--json") || process.argv.includes("-j") + loggerName: string; + verbose: boolean; + private jsonLog: boolean = + process.argv.includes("--json") || process.argv.includes("-j"); - constructor(name: string, verbose?: boolean) { - this.loggerName = name - if (verbose) this.verbose = verbose - else this.verbose = global_verbose - } + constructor(name: string, verbose?: boolean) { + this.loggerName = name; + if (verbose) this.verbose = verbose; + else this.verbose = global_verbose; + } - info(s: string) { - if (!this.jsonLog) process.stdout.write(`${color.green("I")} ${color.gray(new Date().toISOString())} ${color.reset(`${color.yellow(`${this.loggerName}:`)} ${s}`)}\n`) - else process.stdout.write(jsonLog("info", s)) - } + info(s: string) { + if (!this.jsonLog) + process.stdout.write( + `${color.green("I")} ${color.gray( + new Date().toISOString() + )} ${color.reset(`${color.yellow(`${this.loggerName}:`)} ${s}`)}\n` + ); + else process.stdout.write(jsonLog("info", s)); + } - warn(s: string) { - if (!this.jsonLog) process.stdout.write(`${color.yellow("W")} ${color.gray(new Date().toISOString())} ${color.yellow(`${color.yellow(`${this.loggerName}:`)} ${s}`)}\n`) - else process.stderr.write(jsonLog("warn", s)) - } + warn(s: string) { + if (!this.jsonLog) + process.stdout.write( + `${color.yellow("W")} ${color.gray( + new Date().toISOString() + )} ${color.yellow(`${color.yellow(`${this.loggerName}:`)} ${s}`)}\n` + ); + else process.stderr.write(jsonLog("warn", s)); + } - error(s: string) { - if (!this.jsonLog) process.stderr.write(`* ${color.red("E")} ${color.gray(new Date().toISOString())} ${color.redBright(`${color.red(`${this.loggerName}:`)} ${s}`)}\n`) - else process.stderr.write(jsonLog("error", s)) - } + error(s: string) { + if (!this.jsonLog) + process.stderr.write( + `* ${color.red("E")} ${color.gray( + new Date().toISOString() + )} ${color.redBright(`${color.red(`${this.loggerName}:`)} ${s}`)}\n` + ); + else process.stderr.write(jsonLog("error", s)); + } - fatal(s: string) { - if (!this.jsonLog) process.stderr.write(`** ${color.red("F!")} ${color.gray(new Date().toISOString())} ${color.bgRedBright(color.redBright(`${color.red(`${this.loggerName}:`)} ${s}`))}\n`) - else process.stderr.write(jsonLog("fatal", s)) - } + fatal(s: string) { + if (!this.jsonLog) + process.stderr.write( + `** ${color.red("F!")} ${color.gray( + new Date().toISOString() + )} ${color.bgRedBright( + color.redBright(`${color.red(`${this.loggerName}:`)} ${s}`) + )}\n` + ); + else process.stderr.write(jsonLog("fatal", s)); + } - debug(s: string) { - if (this.verbose || global_verbose) { - if (!this.jsonLog) process.stderr.write(`${color.gray("D")} ${color.gray(new Date().toISOString())} ${color.gray(`${color.gray(`${this.loggerName}:`)} ${s}`)}\n`) - else process.stderr.write(jsonLog("debug", s)) - } + debug(s: string) { + if (this.verbose || global_verbose) { + if (!this.jsonLog) + process.stderr.write( + `${color.gray("D")} ${color.gray( + new Date().toISOString() + )} ${color.gray(`${color.gray(`${this.loggerName}:`)} ${s}`)}\n` + ); + else process.stderr.write(jsonLog("debug", s)); } + } } -verboseLogging(process.env.DEBUG != null && process.env.DEBUG != "false" ? true : false) \ No newline at end of file +verboseLogging( + process.env.DEBUG != null && process.env.DEBUG != "false" ? true : false +); diff --git a/src/meta.ts b/src/meta.ts index 39ee95f..0e1d862 100644 --- a/src/meta.ts +++ b/src/meta.ts @@ -1,11 +1,11 @@ -const f = Object.freeze +const f = Object.freeze; // bridge meta -export const BRIDGE_VERSION: Readonly = f(1) +export const BRIDGE_VERSION: Readonly = f(1); // adapter meta -export const PROXY_BRANDING: Readonly = f("EaglerProxy") -export const PROXY_VERSION: Readonly = f("1.0.7") +export const PROXY_BRANDING: Readonly = f("EaglerProxy"); +export const PROXY_VERSION: Readonly = f("1.0.7"); -export const NETWORK_VERSION: Readonly = f(0x03) -export const VANILLA_PROTOCOL_VERSION: Readonly = f(47) +export const NETWORK_VERSION: Readonly = f(0x03); +export const VANILLA_PROTOCOL_VERSION: Readonly = f(47); diff --git a/src/plugins/EagProxyAAS/auth.ts b/src/plugins/EagProxyAAS/auth.ts index b3ca240..5160565 100644 --- a/src/plugins/EagProxyAAS/auth.ts +++ b/src/plugins/EagProxyAAS/auth.ts @@ -1,64 +1,85 @@ -import { randomUUID } from "crypto" -import EventEmitter from "events" -import pauth from "prismarine-auth" -import debug from "debug" +import { randomUUID } from "crypto"; +import EventEmitter from "events"; +import pauth from "prismarine-auth"; +import debug from "debug"; const { Authflow, Titles } = pauth; -const Enums = PLUGIN_MANAGER.Enums +const Enums = PLUGIN_MANAGER.Enums; export type ServerDeviceCodeResponse = { - user_code: string - device_code: string - verification_uri: string - expires_in: number - interval: number - message: string -} + user_code: string; + device_code: string; + verification_uri: string; + expires_in: number; + interval: number; + message: string; +}; class InMemoryCache { - private cache = {} - async getCached () { - return this.cache - } - async setCached (value) { - this.cache = value - } - async setCachedPartial (value) { - this.cache = { - ...this.cache, - ...value - } - } + private cache = {}; + async getCached() { + return this.cache; + } + async setCached(value) { + this.cache = value; + } + async setCachedPartial(value) { + this.cache = { + ...this.cache, + ...value, + }; + } } export function auth(): EventEmitter { - const emitter = new EventEmitter() - const userIdentifier = randomUUID() - const flow = new Authflow(userIdentifier, ({ username, cacheName }) => new InMemoryCache(), { - authTitle: Titles.MinecraftJava, - flow: 'sisu', - deviceType: "Win32" - }, code => { - console.log = () => {} - emitter.emit('code', code) + const emitter = new EventEmitter(); + const userIdentifier = randomUUID(); + const flow = new Authflow( + userIdentifier, + ({ username, cacheName }) => new InMemoryCache(), + { + authTitle: Titles.MinecraftJava, + flow: "sisu", + deviceType: "Win32", + }, + (code) => { + console.log = () => {}; + emitter.emit("code", code); + } + ); + flow + .getMinecraftJavaToken({ fetchProfile: true }) + .then(async (data) => { + 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?" + ) + ); + emitter.emit("done", { + accessToken: data.token, + expiresOn: _data.obtainedOn + _data.expires_in * 1000, + selectedProfile: data.profile, + availableProfiles: [data.profile], + }); }) - flow.getMinecraftJavaToken({ fetchProfile: true }) - .then(async data => { - 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?")) - emitter.emit('done', { - accessToken: data.token, - expiresOn: _data.obtainedOn + _data.expires_in * 1000, - selectedProfile: data.profile, - availableProfiles: [data.profile] - }) - }) - .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())) - }) - return emitter -} \ No newline at end of file + .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()) + ); + }); + return emitter; +} diff --git a/src/plugins/EagProxyAAS/config.ts b/src/plugins/EagProxyAAS/config.ts index c3a2951..a85b385 100644 --- a/src/plugins/EagProxyAAS/config.ts +++ b/src/plugins/EagProxyAAS/config.ts @@ -1,5 +1,5 @@ export const config = { bindInternalServerPort: 25569, bindInternalServerIp: "127.0.0.1", - allowCustomPorts: false, + allowCustomPorts: true, }; diff --git a/src/plugins/EagProxyAAS/index.ts b/src/plugins/EagProxyAAS/index.ts index 7bfaeb5..5a88841 100644 --- a/src/plugins/EagProxyAAS/index.ts +++ b/src/plugins/EagProxyAAS/index.ts @@ -1,60 +1,68 @@ -import metadata from "./metadata.json" assert { type: "json" } -import { config } from "./config.js" -import { createServer } from "minecraft-protocol" -import { ClientState, ConnectionState, ServerGlobals } from "./types.js" -import { handleConnect, setSG } from "./utils.js" +import metadata from "./metadata.json" assert { type: "json" }; +import { config } from "./config.js"; +import { createServer } from "minecraft-protocol"; +import { ClientState, ConnectionState, ServerGlobals } from "./types.js"; +import { handleConnect, setSG } from "./utils.js"; -const PluginManager = PLUGIN_MANAGER +const PluginManager = PLUGIN_MANAGER; -const Logger = PluginManager.Logger -const Enums = PluginManager.Enums -const Chat = PluginManager.Chat -const Constants = PluginManager.Constants -const Motd = PluginManager.Motd -const Player = PluginManager.Player -const MineProtocol = PluginManager.MineProtocol -const EaglerSkins = PluginManager.EaglerSkins -const Util = PluginManager.Util +const Logger = PluginManager.Logger; +const Enums = PluginManager.Enums; +const Chat = PluginManager.Chat; +const Constants = PluginManager.Constants; +const Motd = PluginManager.Motd; +const Player = PluginManager.Player; +const MineProtocol = PluginManager.MineProtocol; +const EaglerSkins = PluginManager.EaglerSkins; +const Util = PluginManager.Util; -const logger = new Logger("EaglerProxyAAS") -logger.info(`Starting ${metadata.name} v${metadata.version}...`) -logger.info(`(internal server port: ${config.bindInternalServerPort}, internal server IP: ${config.bindInternalServerPort})`) +const logger = new Logger("EaglerProxyAAS"); +logger.info(`Starting ${metadata.name} v${metadata.version}...`); +logger.info( + `(internal server port: ${config.bindInternalServerPort}, internal server IP: ${config.bindInternalServerPort})` +); -logger.info("Starting internal server...") +logger.info("Starting internal server..."); let server = createServer({ host: config.bindInternalServerIp, port: config.bindInternalServerPort, motdMsg: `${Enums.ChatColor.GOLD}EaglerProxy as a Service`, "online-mode": false, - version: '1.8.9' -}), sGlobals: ServerGlobals = { + version: "1.8.9", + }), + sGlobals: ServerGlobals = { server: server, - players: new Map() -} -setSG(sGlobals) + players: new Map(), + }; +setSG(sGlobals); -server.on('login', client => { - logger.info(`Client ${client.username} has connected to the authentication server.`) - client.on('end', () => { - sGlobals.players.delete(client.username) - logger.info(`Client ${client.username} has disconnected from the authentication server.`) - }) - const cs: ClientState = { - state: ConnectionState.AUTH, - gameClient: client, - token: null, - lastStatusUpdate: null - } - sGlobals.players.set(client.username, cs) - handleConnect(cs) -}) +server.on("login", (client) => { + logger.info( + `Client ${client.username} has connected to the authentication server.` + ); + client.on("end", () => { + sGlobals.players.delete(client.username); + logger.info( + `Client ${client.username} has disconnected from the authentication server.` + ); + }); + const cs: ClientState = { + state: ConnectionState.AUTH, + gameClient: client, + token: null, + lastStatusUpdate: null, + }; + sGlobals.players.set(client.username, cs); + handleConnect(cs); +}); - -logger.info("Redirecting backend server IP... (this is required for the plugin to function)") +logger.info( + "Redirecting backend server IP... (this is required for the plugin to function)" +); CONFIG.adapter.server = { - host: config.bindInternalServerIp, - port: config.bindInternalServerPort -} + host: config.bindInternalServerIp, + port: config.bindInternalServerPort, +}; CONFIG.adapter.motd = { - l1: Enums.ChatColor.GOLD + "EaglerProxy as a Service" -} + l1: Enums.ChatColor.GOLD + "EaglerProxy as a Service", +}; diff --git a/src/plugins/EagProxyAAS/metadata.json b/src/plugins/EagProxyAAS/metadata.json index aba0a02..389a674 100644 --- a/src/plugins/EagProxyAAS/metadata.json +++ b/src/plugins/EagProxyAAS/metadata.json @@ -1,27 +1,34 @@ { - "name": "EaglerProxy as a Service", - "id": "eagpaas", - "version": "1.1.0", - "entry_point": "index.js", - "requirements": [{ - "id": "eaglerproxy", - "version": "any" - }, { - "id": "module:vec3", - "version": "^0.1.0" - }, { - "id": "module:prismarine-chunk", - "version": "^1.33.0" - }, { - "id": "module:prismarine-block", - "version": "^1.16.0" - }, { - "id": "module:prismarine-registry", - "version": "^1.6.0" - }, { - "id": "module:minecraft-protocol", - "version": "^1.40.0" - }], - "load_after": [], - "incompatibilities": [] + "name": "EaglerProxy as a Service", + "id": "eagpaas", + "version": "1.1.0", + "entry_point": "index.js", + "requirements": [ + { + "id": "eaglerproxy", + "version": "any" + }, + { + "id": "module:vec3", + "version": "^0.1.0" + }, + { + "id": "module:prismarine-chunk", + "version": "^1.33.0" + }, + { + "id": "module:prismarine-block", + "version": "^1.16.0" + }, + { + "id": "module:prismarine-registry", + "version": "^1.6.0" + }, + { + "id": "module:minecraft-protocol", + "version": "^1.40.0" + } + ], + "load_after": [], + "incompatibilities": [] } diff --git a/src/plugins/EagProxyAAS/types.ts b/src/plugins/EagProxyAAS/types.ts index 4bdfd1f..7e2ac54 100644 --- a/src/plugins/EagProxyAAS/types.ts +++ b/src/plugins/EagProxyAAS/types.ts @@ -45,6 +45,7 @@ export enum ChatColor { } export enum ConnectType { - ONLINE, - OFFLINE, + ONLINE = "ONLINE", + OFFLINE = "OFFLINE", + EASYMC = "EASYMC", } diff --git a/src/plugins/EagProxyAAS/utils.ts b/src/plugins/EagProxyAAS/utils.ts index 9b9ca73..03b4b7e 100644 --- a/src/plugins/EagProxyAAS/utils.ts +++ b/src/plugins/EagProxyAAS/utils.ts @@ -7,6 +7,8 @@ import { Client } from "minecraft-protocol"; import { ClientState, ConnectionState } from "./types.js"; import { auth, ServerDeviceCodeResponse } from "./auth.js"; import { config } from "./config.js"; +import { handleCommand } from "./commands.js"; +import { getTokenProfileEasyMc } from "./auth_easymc.js"; const { Vec3 } = vec3 as any; const Enums = PLUGIN_MANAGER.Enums; @@ -50,7 +52,7 @@ export function handleConnect(client: ClientState) { client.gameClient.write("login", { entityId: 1, gameMode: 2, - dimension: 0, + dimension: 1, difficulty: 1, maxPlayers: 1, levelType: "flat", @@ -190,7 +192,7 @@ export function sendMessageLogin(client: Client, url: string, token: string) { export function updateState( client: Client, - newState: "CONNECTION_TYPE" | "AUTH" | "SERVER", + newState: "CONNECTION_TYPE" | "AUTH_EASYMC" | "AUTH" | "SERVER", uri?: string, code?: string ) { @@ -201,7 +203,17 @@ export function updateState( text: ` ${Enums.ChatColor.GOLD}EaglerProxy Authentication Server `, }), footer: JSON.stringify({ - text: `${Enums.ChatColor.RED}Choose the connection type: 1 = online, 2 = offline.`, + text: `${Enums.ChatColor.RED}Choose the connection type: 1 = online, 2 = offline, 3 = EasyMC.`, + }), + }); + break; + case "AUTH_EASYMC": + client.write("playerlist_header", { + header: JSON.stringify({ + text: ` ${Enums.ChatColor.GOLD}EaglerProxy Authentication Server `, + }), + footer: JSON.stringify({ + text: `${Enums.ChatColor.RED}easymc.io/get${Enums.ChatColor.GOLD} | ${Enums.ChatColor.RED}/login `, }), }); break; @@ -234,6 +246,18 @@ export function updateState( } } +// assuming that the player will always stay at the same pos +export function playSelectSound(client: Client) { + client.write("named_sound_effect", { + soundName: "note.hat", + x: 8.5, + y: 65, + z: 8.5, + volume: 100, + pitch: 63, + }); +} + export async function onConnect(client: ClientState) { try { client.state = ConnectionState.AUTH; @@ -267,7 +291,7 @@ export async function onConnect(client: ClientState) { }, clickEvent: { action: "run_command", - value: "1", + value: "$1", }, }); sendChatComponent(client.gameClient, { @@ -285,12 +309,30 @@ export async function onConnect(client: ClientState) { }, clickEvent: { action: "run_command", - value: "2", + value: "$2", + }, + }); + sendChatComponent(client.gameClient, { + text: "3) ", + color: "gold", + extra: [ + { + text: "Connect to an online server via EasyMC account pool (no Minecraft account needed)", + color: "white", + }, + ], + hoverEvent: { + action: "show_text", + value: Enums.ChatColor.GOLD + "Click me to select!", + }, + clickEvent: { + action: "run_command", + value: "$3", }, }); sendCustomMessage( client.gameClient, - "Select an option from the above (1 = online, 2 = offline), either by clicking or manually typing out the option.", + "Select an option from the above (1 = online, 2 = offline, 3 = EasyMC), either by clicking or manually typing out the option.", "green" ); updateState(client.gameClient, "CONNECTION_TYPE"); @@ -298,21 +340,28 @@ export async function onConnect(client: ClientState) { let chosenOption: ConnectType | null = null; while (true) { const option = await awaitCommand(client.gameClient, (msg) => true); - switch (option) { + switch (option.replace(/\$/gim, "")) { default: sendCustomMessage( client.gameClient, `I don't understand what you meant by "${option}", please reply with a valid option!`, "red" ); + break; case "1": chosenOption = ConnectType.ONLINE; break; case "2": chosenOption = ConnectType.OFFLINE; break; + case "3": + chosenOption = ConnectType.EASYMC; + break; + } + if (chosenOption != null) { + if (option.startsWith("$")) playSelectSound(client.gameClient); + break; } - if (chosenOption != null) break; } if (chosenOption == ConnectType.ONLINE) { @@ -347,6 +396,7 @@ export async function onConnect(client: ClientState) { authHandler.on("code", codeCallback); await new Promise((res) => authHandler.once("done", (result) => { + console.log(result); savedAuth = result; res(result); }) @@ -407,26 +457,81 @@ export async function onConnect(client: ClientState) { } } try { - await PLUGIN_MANAGER.proxy.players - .get(client.gameClient.username) - .switchServers({ - host: host, - port: port, - version: "1.8.8", - username: savedAuth.selectedProfile.name, - auth: "mojang", - keepAlive: false, - session: { - accessToken: savedAuth.accessToken, - clientToken: savedAuth.selectedProfile.id, - selectedProfile: { - id: savedAuth.selectedProfile.id, - name: savedAuth.selectedProfile.name, + sendChatComponent(client.gameClient, { + text: `Joining server under ${savedAuth.selectedProfile.name}/your Minecraft account's username! Run `, + color: "aqua", + extra: [ + { + text: "/eag-help", + color: "gold", + hoverEvent: { + action: "show_text", + value: Enums.ChatColor.GOLD + "Click me to run this command!", + }, + clickEvent: { + action: "run_command", + value: "/eag-help", }, }, - skipValidation: true, - hideErrors: true, - }); + { + text: " for a list of proxy commands.", + color: "aqua", + }, + ], + }); + sendCustomMessage( + client.gameClient, + "Attempting to switch servers, please wait... (if you don't get connected to the target server after a while, the server might not be a Minecraft server at all)", + "gray" + ); + const player = PLUGIN_MANAGER.proxy.players.get( + client.gameClient.username + ); + player.on("vanillaPacket", (packet, origin) => { + if ( + origin == "CLIENT" && + packet.name == "chat" && + (packet.params.message as string) + .toLowerCase() + .startsWith("/eag-") && + !packet.cancel + ) { + packet.cancel = true; + handleCommand(player, packet.params.message as string); + } + }); + + (player as any)._onlineSession = { + auth: "mojang", + username: savedAuth.selectedProfile.name, + session: { + accessToken: savedAuth.accessToken, + clientToken: savedAuth.selectedProfile.id, + selectedProfile: { + id: savedAuth.selectedProfile.id, + name: savedAuth.selectedProfile.name, + }, + }, + }; + + await player.switchServers({ + host: host, + port: port, + version: "1.8.8", + username: savedAuth.selectedProfile.name, + auth: "mojang", + keepAlive: false, + session: { + accessToken: savedAuth.accessToken, + clientToken: savedAuth.selectedProfile.id, + selectedProfile: { + id: savedAuth.selectedProfile.id, + name: savedAuth.selectedProfile.name, + }, + }, + skipValidation: true, + hideErrors: true, + }); } catch (err) { if (!client.gameClient.ended) { client.gameClient.end( @@ -441,7 +546,169 @@ export async function onConnect(client: ClientState) { ); } } - } else { + } else if (chosenOption == ConnectType.EASYMC) { + const EASYMC_GET_TOKEN_URL = "easymc.io/get"; + client.state = ConnectionState.AUTH; + client.lastStatusUpdate = Date.now(); + updateState(client.gameClient, "AUTH_EASYMC"); + + sendMessageWarning( + client.gameClient, + `WARNING: You've chosen to use an account from EasyMC's account pool. Please note that accounts and shared, and may be banned from whatever server you are attempting to join.` + ); + sendChatComponent(client.gameClient, { + text: "Please generate an alt token at ", + color: "white", + extra: [ + { + text: EASYMC_GET_TOKEN_URL, + color: "gold", + hoverEvent: { + action: "show_text", + value: Enums.ChatColor.GOLD + "Click me to open in a new window!", + }, + clickEvent: { + action: "open_url", + value: `https://${EASYMC_GET_TOKEN_URL}`, + }, + }, + { + text: ", and then run ", + color: "white", + }, + { + text: "/login ", + color: "gold", + hoverEvent: { + action: "show_text", + value: Enums.ChatColor.GOLD + "Copy me to chat!", + }, + clickEvent: { + action: "suggest_command", + value: `/login `, + }, + }, + { + text: " to log in.", + color: "white", + }, + ], + }); + + let appendOptions: any; + while (true) { + const tokenResponse = await awaitCommand(client.gameClient, (msg) => + msg.toLowerCase().startsWith("/login") + ), + splitResponse = tokenResponse.split(/ /gim, 2).slice(1); + if (splitResponse.length != 1) { + sendChatComponent(client.gameClient, { + text: "Invalid usage! Please use the command as follows: ", + color: "red", + extra: [ + { + text: "/login ", + color: "gold", + hoverEvent: { + action: "show_text", + value: Enums.ChatColor.GOLD + "Copy me to chat!", + }, + clickEvent: { + action: "suggest_command", + value: `/login `, + }, + }, + { + text: ".", + color: "red", + }, + ], + }); + } else { + const token = splitResponse[0]; + if (token.length != 20) { + sendChatComponent(client.gameClient, { + text: "Please provide a valid token (you can get one ", + color: "red", + extra: [ + { + text: "here", + color: "white", + hoverEvent: { + action: "show_text", + value: + Enums.ChatColor.GOLD + + "Click me to open in a new window!", + }, + clickEvent: { + action: "open_url", + value: `https://${EASYMC_GET_TOKEN_URL}`, + }, + }, + { + text: "). ", + color: "red", + }, + { + text: "/login ", + color: "gold", + hoverEvent: { + action: "show_text", + value: Enums.ChatColor.GOLD + "Copy me to chat!", + }, + clickEvent: { + action: "suggest_command", + value: `/login `, + }, + }, + { + text: ".", + color: "red", + }, + ], + }); + } else { + sendCustomMessage( + client.gameClient, + "Validating alt token...", + "gray" + ); + try { + appendOptions = await getTokenProfileEasyMc(token); + sendCustomMessage( + client.gameClient, + `Successfully validated your alt token and retrieved your session profile! You'll be joining to your preferred server as ${appendOptions.username}.`, + "green" + ); + break; + } catch (err) { + sendChatComponent(client.gameClient, { + text: `EasyMC's servers replied with an error (${err.message}), please try again! `, + color: "red", + extra: [ + { + text: "/login ", + color: "gold", + hoverEvent: { + action: "show_text", + value: Enums.ChatColor.GOLD + "Copy me to chat!", + }, + clickEvent: { + action: "suggest_command", + value: `/login `, + }, + }, + { + text: ".", + color: "red", + }, + ], + }); + } + } + } + } + client.state = ConnectionState.SUCCESS; client.lastStatusUpdate = Date.now(); updateState(client.gameClient, "SERVER"); @@ -493,23 +760,63 @@ export async function onConnect(client: ClientState) { } } try { + sendChatComponent(client.gameClient, { + text: `Joining server under ${appendOptions.username}/EasyMC account username! Run `, + color: "aqua", + extra: [ + { + text: "/eag-help", + color: "gold", + hoverEvent: { + action: "show_text", + value: Enums.ChatColor.GOLD + "Click me to run this command!", + }, + clickEvent: { + action: "run_command", + value: "/eag-help", + }, + }, + { + text: " for a list of proxy commands.", + color: "aqua", + }, + ], + }); sendCustomMessage( client.gameClient, - "Attempting to switch servers, please wait... (if you don't get connected to the target server after a while, the server might be online only)", + "Attempting to switch servers, please wait... (if you don't get connected to the target server for a while, the server might be online only)", "gray" ); - await PLUGIN_MANAGER.proxy.players - .get(client.gameClient.username) - .switchServers({ - host: host, - port: port, - version: "1.8.8", - username: client.gameClient.username, - auth: "offline", - keepAlive: false, - skipValidation: true, - hideErrors: true, - }); + const player = PLUGIN_MANAGER.proxy.players.get( + client.gameClient.username + ); + player.on("vanillaPacket", (packet, origin) => { + if ( + origin == "CLIENT" && + packet.name == "chat" && + (packet.params.message as string) + .toLowerCase() + .startsWith("/eag-") && + !packet.cancel + ) { + packet.cancel = true; + handleCommand(player, packet.params.message as string); + } + }); + (player as any)._onlineSession = { + ...appendOptions, + isEasyMC: true, + }; + + await player.switchServers({ + host: host, + port: port, + version: "1.8.8", + keepAlive: false, + skipValidation: true, + hideErrors: true, + ...appendOptions, + }); } catch (err) { if (!client.gameClient.ended) { client.gameClient.end( @@ -546,15 +853,15 @@ export function generateSpawnChunk(): Chunk.PCChunk { () => new McBlock( REGISTRY.blocksByName.air.id, - REGISTRY.biomesByName.plains.id, + REGISTRY.biomesByName.the_end.id, 0 ) ); chunk.setBlock( new Vec3(8, 64, 8), new McBlock( - REGISTRY.blocksByName.barrier.id, - REGISTRY.biomesByName.plains.id, + REGISTRY.blocksByName.sea_lantern.id, + REGISTRY.biomesByName.the_end.id, 0 ) ); @@ -562,7 +869,7 @@ export function generateSpawnChunk(): Chunk.PCChunk { new Vec3(8, 67, 8), new McBlock( REGISTRY.blocksByName.barrier.id, - REGISTRY.biomesByName.plains.id, + REGISTRY.biomesByName.the_end.id, 0 ) ); @@ -570,7 +877,7 @@ export function generateSpawnChunk(): Chunk.PCChunk { new Vec3(7, 65, 8), new McBlock( REGISTRY.blocksByName.barrier.id, - REGISTRY.biomesByName.plains.id, + REGISTRY.biomesByName.the_end.id, 0 ) ); @@ -578,7 +885,7 @@ export function generateSpawnChunk(): Chunk.PCChunk { new Vec3(7, 66, 8), new McBlock( REGISTRY.blocksByName.barrier.id, - REGISTRY.biomesByName.plains.id, + REGISTRY.biomesByName.the_end.id, 0 ) ); @@ -586,7 +893,7 @@ export function generateSpawnChunk(): Chunk.PCChunk { new Vec3(9, 65, 8), new McBlock( REGISTRY.blocksByName.barrier.id, - REGISTRY.biomesByName.plains.id, + REGISTRY.biomesByName.the_end.id, 0 ) ); @@ -594,7 +901,7 @@ export function generateSpawnChunk(): Chunk.PCChunk { new Vec3(9, 66, 8), new McBlock( REGISTRY.blocksByName.barrier.id, - REGISTRY.biomesByName.plains.id, + REGISTRY.biomesByName.the_end.id, 0 ) ); @@ -602,7 +909,7 @@ export function generateSpawnChunk(): Chunk.PCChunk { new Vec3(8, 65, 7), new McBlock( REGISTRY.blocksByName.barrier.id, - REGISTRY.biomesByName.plains.id, + REGISTRY.biomesByName.the_end.id, 0 ) ); @@ -610,7 +917,7 @@ export function generateSpawnChunk(): Chunk.PCChunk { new Vec3(8, 66, 7), new McBlock( REGISTRY.blocksByName.barrier.id, - REGISTRY.biomesByName.plains.id, + REGISTRY.biomesByName.the_end.id, 0 ) ); @@ -618,7 +925,7 @@ export function generateSpawnChunk(): Chunk.PCChunk { new Vec3(8, 65, 9), new McBlock( REGISTRY.blocksByName.barrier.id, - REGISTRY.biomesByName.plains.id, + REGISTRY.biomesByName.the_end.id, 0 ) ); @@ -626,10 +933,11 @@ export function generateSpawnChunk(): Chunk.PCChunk { new Vec3(8, 66, 9), new McBlock( REGISTRY.blocksByName.barrier.id, - REGISTRY.biomesByName.plains.id, + REGISTRY.biomesByName.the_end.id, 0 ) ); - chunk.setSkyLight(new Vec3(8, 66, 8), 15); + // chunk.setBlockLight(new Vec3(8, 65, 8), 15); + chunk.setBlockLight(new Vec3(8, 66, 8), 15); return chunk; } diff --git a/src/proxy/BungeeUtil.ts b/src/proxy/BungeeUtil.ts index 67d158c..6fe151b 100644 --- a/src/proxy/BungeeUtil.ts +++ b/src/proxy/BungeeUtil.ts @@ -1,147 +1,153 @@ -import { Logger } from "../logger.js" -import mcp, { states } from "minecraft-protocol" +import { Logger } from "../logger.js"; +import mcp, { states } from "minecraft-protocol"; -const { createSerializer, createDeserializer } = mcp +const { createSerializer, createDeserializer } = mcp; export namespace BungeeUtil { - export class PacketUUIDTranslator { - public serverSidePlayerUUID: string - public clientSidePlayerUUID: string - - static readonly CAST_UUID_SERVER: string[] = [ - 'update_attributes', - 'named_entity_spawn', - // drop this packet (twitch.tv integration not available anymore) - 'player_info' - ] - static readonly CAST_UUID_CLIENT: string[] = [ - 'spectate' - ] - - private _logger: Logger - private _serverSerializer: any - private _clientSerializer: any - private _clientDeserializer: any - private _serverDeserializer: any - - constructor(ssPlayerUUID: string, csPlayerUUID: string) { - this.serverSidePlayerUUID = ssPlayerUUID - this.clientSidePlayerUUID = csPlayerUUID - this._logger = new Logger("PacketTranslator") - this._serverSerializer = createSerializer({ - state: states.PLAY, - isServer: true, - version: "1.8.8", - customPackets: null - }) - this._clientSerializer = createSerializer({ - state: states.PLAY, - isServer: false, - version: "1.8.8", - customPackets: null - }) - this._clientDeserializer = createDeserializer({ - state: states.PLAY, - isServer: false, - version: "1.8.8", - customPackets: null - }) - this._serverDeserializer = createDeserializer({ - state: states.PLAY, - isServer: true, - version: "1.8.8", - customPackets: null - }) - } - - public onClientWrite(packet: Buffer): Buffer /* write to server */ { - const { name, params } = this._serverDeserializer.parsePacketBuffer(packet).data - return this._clientSerializer.createPacketBuffer(this._translatePacketClient(params, { name })) - } - - public onServerWrite(packet: any, meta: any): Buffer /* write to client */ { - return this._serverSerializer.createPacketBuffer(this._translatePacketServer(packet, meta)) - } - - private _translatePacketClient(packet: any, meta: any): any | null { - if (PacketUUIDTranslator.CAST_UUID_CLIENT.some(id => id == meta.name)) { - if (meta.name == 'spectate') { - if (packet.target == this.clientSidePlayerUUID) { - packet.target = this.serverSidePlayerUUID - } else if (packet.target == this.serverSidePlayerUUID) { - packet.target = this.clientSidePlayerUUID - } - } - } - return { - name: meta.name, - params: packet - } - } - - private _translatePacketServer(packet: any, meta: any): any | null { - if (PacketUUIDTranslator.CAST_UUID_SERVER.some(id => id == meta.name)) { - if (meta.name == 'update_attributes') { - for (const prop of packet.properties) { - for (const modifier of prop.modifiers) { - if (modifier.uuid == this.serverSidePlayerUUID) { - modifier.uuid = this.clientSidePlayerUUID - } else if (modifier.uuid == this.clientSidePlayerUUID) { - modifier.uuid = this.serverSidePlayerUUID - } - } - } - } else if (meta.name == 'named_entity_spawn') { - if (packet.playerUUID == this.serverSidePlayerUUID) { - packet.playerUUID = this.clientSidePlayerUUID - } else if (packet.playerUUID == this.clientSidePlayerUUID) { - packet.playerUUID = this.serverSidePlayerUUID - } - } else if (meta.name == 'player_info') { - for (const player of packet.data) { - if (player.UUID == this.serverSidePlayerUUID) { - player.UUID = this.clientSidePlayerUUID - } else if (player.UUID == this.clientSidePlayerUUID) { - player.UUID = this.serverSidePlayerUUID - } - } - } - } - return { - name: meta.name, - params: packet - } - } + export class PacketUUIDTranslator { + public serverSidePlayerUUID: string; + public clientSidePlayerUUID: string; + + static readonly CAST_UUID_SERVER: string[] = [ + "update_attributes", + "named_entity_spawn", + // drop this packet (twitch.tv integration not available anymore) + "player_info", + ]; + static readonly CAST_UUID_CLIENT: string[] = ["spectate"]; + + private _logger: Logger; + private _serverSerializer: any; + private _clientSerializer: any; + private _clientDeserializer: any; + private _serverDeserializer: any; + + constructor(ssPlayerUUID: string, csPlayerUUID: string) { + this.serverSidePlayerUUID = ssPlayerUUID; + this.clientSidePlayerUUID = csPlayerUUID; + this._logger = new Logger("PacketTranslator"); + this._serverSerializer = createSerializer({ + state: states.PLAY, + isServer: true, + version: "1.8.8", + customPackets: null, + }); + this._clientSerializer = createSerializer({ + state: states.PLAY, + isServer: false, + version: "1.8.8", + customPackets: null, + }); + this._clientDeserializer = createDeserializer({ + state: states.PLAY, + isServer: false, + version: "1.8.8", + customPackets: null, + }); + this._serverDeserializer = createDeserializer({ + state: states.PLAY, + isServer: true, + version: "1.8.8", + customPackets: null, + }); } - - export function getRespawnSequence(login: any, serializer: any): [Buffer, Buffer] { - const dimset = getDimSets(login.dimension) - return [ - serializer.createPacketBuffer({ - name: 'respawn', - params: { - dimension: dimset[0], - difficulty: login.difficulty, - gamemode: login.gameMode, - levelType: login.levelType - } - }), - serializer.createPacketBuffer({ - name: 'respawn', - params: { - dimension: dimset[1], - difficulty: login.difficulty, - gamemode: login.gameMode, - levelType: login.levelType - } - }) - ] + + public onClientWrite(packet: Buffer): Buffer /* write to server */ { + const { name, params } = + this._serverDeserializer.parsePacketBuffer(packet).data; + return this._clientSerializer.createPacketBuffer( + this._translatePacketClient(params, { name }) + ); } - - function getDimSets(loginDim: number): [number, number] { - return [ - loginDim == -1 ? 0 : loginDim == 0 ? -1 : loginDim == 1 ? 0 : 0, - loginDim - ] + + public onServerWrite(packet: any, meta: any): Buffer /* write to client */ { + return this._serverSerializer.createPacketBuffer( + this._translatePacketServer(packet, meta) + ); } -} \ No newline at end of file + + private _translatePacketClient(packet: any, meta: any): any | null { + if (PacketUUIDTranslator.CAST_UUID_CLIENT.some((id) => id == meta.name)) { + if (meta.name == "spectate") { + if (packet.target == this.clientSidePlayerUUID) { + packet.target = this.serverSidePlayerUUID; + } else if (packet.target == this.serverSidePlayerUUID) { + packet.target = this.clientSidePlayerUUID; + } + } + } + return { + name: meta.name, + params: packet, + }; + } + + private _translatePacketServer(packet: any, meta: any): any | null { + if (PacketUUIDTranslator.CAST_UUID_SERVER.some((id) => id == meta.name)) { + if (meta.name == "update_attributes") { + for (const prop of packet.properties) { + for (const modifier of prop.modifiers) { + if (modifier.uuid == this.serverSidePlayerUUID) { + modifier.uuid = this.clientSidePlayerUUID; + } else if (modifier.uuid == this.clientSidePlayerUUID) { + modifier.uuid = this.serverSidePlayerUUID; + } + } + } + } else if (meta.name == "named_entity_spawn") { + if (packet.playerUUID == this.serverSidePlayerUUID) { + packet.playerUUID = this.clientSidePlayerUUID; + } else if (packet.playerUUID == this.clientSidePlayerUUID) { + packet.playerUUID = this.serverSidePlayerUUID; + } + } else if (meta.name == "player_info") { + for (const player of packet.data) { + if (player.UUID == this.serverSidePlayerUUID) { + player.UUID = this.clientSidePlayerUUID; + } else if (player.UUID == this.clientSidePlayerUUID) { + player.UUID = this.serverSidePlayerUUID; + } + } + } + } + return { + name: meta.name, + params: packet, + }; + } + } + + export function getRespawnSequence( + login: any, + serializer: any + ): [Buffer, Buffer] { + const dimset = getDimSets(login.dimension); + return [ + serializer.createPacketBuffer({ + name: "respawn", + params: { + dimension: dimset[0], + difficulty: login.difficulty, + gamemode: login.gameMode, + levelType: login.levelType, + }, + }), + serializer.createPacketBuffer({ + name: "respawn", + params: { + dimension: dimset[1], + difficulty: login.difficulty, + gamemode: login.gameMode, + levelType: login.levelType, + }, + }), + ]; + } + + function getDimSets(loginDim: number): [number, number] { + return [ + loginDim == -1 ? 0 : loginDim == 0 ? -1 : loginDim == 1 ? 0 : 0, + loginDim, + ]; + } +} diff --git a/src/proxy/Chat.ts b/src/proxy/Chat.ts index bc54865..1c333ec 100644 --- a/src/proxy/Chat.ts +++ b/src/proxy/Chat.ts @@ -1,50 +1,57 @@ -import { Enums } from "./Enums.js" +import { Enums } from "./Enums.js"; export namespace Chat { - export type ChatExtra = { - text: string, - bold?: boolean, - italic?: boolean, - underlined?: boolean, - strikethrough?: boolean, - obfuscated?: boolean, - color?: Enums.ChatColor | 'reset' - } - - export type Chat = { - text?: string, - bold?: boolean, - italic?: boolean, - underlined?: boolean, - strikethrough?: boolean, - obfuscated?: boolean, - color?: Enums.ChatColor | 'reset', - extra?: ChatExtra[] - } - - export function chatToPlainString(chat: Chat): string { - let ret = '' - if (chat.text != null) ret += chat.text - if (chat.extra != null) { - chat.extra.forEach(extra => { - let append = "" - if (extra.bold) append += Enums.ChatColor.BOLD - if (extra.italic) append += Enums.ChatColor.ITALIC - if (extra.underlined) append += Enums.ChatColor.UNDERLINED - if (extra.strikethrough) append += Enums.ChatColor.STRIKETHROUGH - if (extra.obfuscated) append += Enums.ChatColor.OBFUSCATED - if (extra.color) append += extra.color == 'reset' ? Enums.ChatColor.RESET : resolveColor(extra.color) - append += extra.text - ret += append - }) - } - return ret - } + export type ChatExtra = { + text: string; + bold?: boolean; + italic?: boolean; + underlined?: boolean; + strikethrough?: boolean; + obfuscated?: boolean; + color?: Enums.ChatColor | "reset"; + }; - const ccValues = Object.values(Enums.ChatColor) - const ccKeys = Object.keys(Enums.ChatColor).map(str => str.toLowerCase()) + export type Chat = { + text?: string; + bold?: boolean; + italic?: boolean; + underlined?: boolean; + strikethrough?: boolean; + obfuscated?: boolean; + color?: Enums.ChatColor | "reset"; + extra?: ChatExtra[]; + }; - function resolveColor(colorStr: string) { - return Object.values(Enums.ChatColor)[ccKeys.indexOf(colorStr.toLowerCase())] ?? colorStr + export function chatToPlainString(chat: Chat): string { + let ret = ""; + if (chat.text != null) ret += chat.text; + if (chat.extra != null) { + chat.extra.forEach((extra) => { + let append = ""; + if (extra.bold) append += Enums.ChatColor.BOLD; + if (extra.italic) append += Enums.ChatColor.ITALIC; + if (extra.underlined) append += Enums.ChatColor.UNDERLINED; + if (extra.strikethrough) append += Enums.ChatColor.STRIKETHROUGH; + if (extra.obfuscated) append += Enums.ChatColor.OBFUSCATED; + if (extra.color) + append += + extra.color == "reset" + ? Enums.ChatColor.RESET + : resolveColor(extra.color); + append += extra.text; + ret += append; + }); } -} \ No newline at end of file + return ret; + } + + const ccValues = Object.values(Enums.ChatColor); + const ccKeys = Object.keys(Enums.ChatColor).map((str) => str.toLowerCase()); + + function resolveColor(colorStr: string) { + return ( + Object.values(Enums.ChatColor)[ccKeys.indexOf(colorStr.toLowerCase())] ?? + colorStr + ); + } +} diff --git a/src/proxy/Constants.ts b/src/proxy/Constants.ts index 7ba18d5..2e63125 100644 --- a/src/proxy/Constants.ts +++ b/src/proxy/Constants.ts @@ -1,13 +1,17 @@ -import * as meta from "../meta.js" +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 EAGLERCRAFT_SKIN_CUSTOM_LENGTH = 64 ** 2 * 4 - - export const JOIN_SERVER_PACKET = 0x01 - export const PLAYER_LOOK_PACKET = 0x08 + 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 EAGLERCRAFT_SKIN_CUSTOM_LENGTH = 64 ** 2 * 4; + + export const JOIN_SERVER_PACKET = 0x01; + export const PLAYER_LOOK_PACKET = 0x08; } -export const UPGRADE_REQUIRED_RESPONSE = ` EaglerProxy landing page

426 - Upgrade Required

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: loading... (connect from any recent EaglercraftX client via Multiplayer > Direct Connect)

` +export const UPGRADE_REQUIRED_RESPONSE = ` EaglerProxy landing page

426 - Upgrade Required

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: loading... (connect from any recent EaglercraftX client via Multiplayer > Direct Connect)

`; diff --git a/src/proxy/Enums.ts b/src/proxy/Enums.ts index 35c477b..277ddf8 100644 --- a/src/proxy/Enums.ts +++ b/src/proxy/Enums.ts @@ -1,71 +1,71 @@ export namespace Enums { - export enum PacketId { - CSLoginPacket = 0x01, - SCIdentifyPacket = 0x02, - SCDisconnectPacket = 0xff, - SCChannelMessagePacket = 0x3f, - CSChannelMessagePacket = 0x17, - CSUsernamePacket = 0x04, - SCSyncUuidPacket = 0x05, - CSSetSkinPacket = 0x07, - CSReadyPacket = 0x08, - SCReadyPacket = 0x09 - } + export enum PacketId { + CSLoginPacket = 0x01, + SCIdentifyPacket = 0x02, + SCDisconnectPacket = 0xff, + SCChannelMessagePacket = 0x3f, + CSChannelMessagePacket = 0x17, + CSUsernamePacket = 0x04, + SCSyncUuidPacket = 0x05, + CSSetSkinPacket = 0x07, + CSReadyPacket = 0x08, + SCReadyPacket = 0x09, + } - export enum ChannelMessageType { - CLIENT = 0x17, - SERVER = 0x3f - } + export enum ChannelMessageType { + CLIENT = 0x17, + SERVER = 0x3f, + } - export enum EaglerSkinPacketId { - CFetchSkinEaglerPlayerReq = 0x03, - SFetchSkinBuiltInRes = 0x04, - SFetchSkinRes = 0x05, - CFetchSkinReq = 0x06 - } - - export enum ClientState { - PRE_HANDSHAKE = "PRE_HANDSHAKE", - POST_HANDSHAKE = "POST_HANDSHAKE", - DISCONNECTED = "DISCONNECTED" - } + export enum EaglerSkinPacketId { + CFetchSkinEaglerPlayerReq = 0x03, + SFetchSkinBuiltInRes = 0x04, + SFetchSkinRes = 0x05, + CFetchSkinReq = 0x06, + } - export enum PacketBounds { - C = "C", - S = "S" - } + export enum ClientState { + PRE_HANDSHAKE = "PRE_HANDSHAKE", + POST_HANDSHAKE = "POST_HANDSHAKE", + DISCONNECTED = "DISCONNECTED", + } - export enum SkinType { - BUILTIN, - CUSTOM - } + export enum PacketBounds { + C = "C", + S = "S", + } - export enum ChatColor { - AQUA = "§b", - BLACK = "§0", - DARK_BLUE = "§1", - DARK_GREEN = "§2", - DARK_CYAN = "§3", - DARK_RED = "§4", - PURPLE = "§5", - GOLD = "§6", - GRAY = "§7", - GREEN = "§a", - DARK_GRAY = "§8", - BLUE = "§9", - BRIGHT_GREEN = "§a", - LIGHT_PURPLE = "§d", - CYAN = "§b", - RED = "§c", - PINK = "§d", - YELLOW = "§e", - WHITE = "§f", - // text styling - OBFUSCATED = '§k', - BOLD = '§l', - STRIKETHROUGH = '§m', - UNDERLINED = '§n', - ITALIC = '§o', - RESET = '§r' - } -} \ No newline at end of file + export enum SkinType { + BUILTIN, + CUSTOM, + } + + export enum ChatColor { + AQUA = "§b", + BLACK = "§0", + DARK_BLUE = "§1", + DARK_GREEN = "§2", + DARK_CYAN = "§3", + DARK_RED = "§4", + PURPLE = "§5", + GOLD = "§6", + GRAY = "§7", + GREEN = "§a", + DARK_GRAY = "§8", + BLUE = "§9", + BRIGHT_GREEN = "§a", + LIGHT_PURPLE = "§d", + CYAN = "§b", + RED = "§c", + PINK = "§d", + YELLOW = "§e", + WHITE = "§f", + // text styling + OBFUSCATED = "§k", + BOLD = "§l", + STRIKETHROUGH = "§m", + UNDERLINED = "§n", + ITALIC = "§o", + RESET = "§r", + } +} diff --git a/src/proxy/Motd.ts b/src/proxy/Motd.ts index 4075738..6662f7c 100644 --- a/src/proxy/Motd.ts +++ b/src/proxy/Motd.ts @@ -1,132 +1,153 @@ -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'; -const { ping } = pkg +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"; +const { ping } = pkg; export namespace Motd { - const ICON_SQRT = 64 - const IMAGE_DATA_PREPEND = "data:image/png;base64," + const ICON_SQRT = 64; + const IMAGE_DATA_PREPEND = "data:image/png;base64,"; - export class MOTD { - public jsonMotd: JSONMotd - public image?: Buffer + export class MOTD { + public jsonMotd: JSONMotd; + public image?: Buffer; - constructor(motd: JSONMotd, image?: Buffer) { - this.jsonMotd = motd - this.image = image - } - - public static async generateMOTDFromPing(host: string, port: number): Promise { - const pingRes = await ping({ host: host, port: port }) - 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')) - } - - return new MOTD({ - brand: PROXY_BRANDING, - cracked: true, - data: { - cache: true, - icon: newPingRes.favicon != null ? true : false, - max: newPingRes.players.max, - 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) : [], - }, - name: "placeholder name", - secure: false, - time: Date.now(), - type: "motd", - uuid: randomUUID(), // replace placeholder with global. cached UUID - vers: `${PROXY_BRANDING}/${PROXY_VERSION}` - }, image) - } - } - - public static async generateMOTDFromConfig(config: Config['adapter']): Promise { - 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: [] - }, - name: config.name, - secure: false, - time: Date.now(), - type: 'motd', - uuid: randomUUID(), - vers: `${PROXY_BRANDING}/${PROXY_VERSION}` - }) - if (config.motd.iconURL != null) { - motd.image = await this.generateEaglerMOTDImage(config.motd.iconURL) - } - 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 { - return new Promise((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 - ] - } + constructor(motd: JSONMotd, image?: Buffer) { + this.jsonMotd = motd; + this.image = image; } - export type JSONMotd = { - brand: string, - cracked: true, - data: { + public static async generateMOTDFromPing( + host: string, + port: number + ): Promise { + const pingRes = await ping({ host: host, port: port }); + 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" + ) + ); + } + + return new MOTD( + { + brand: PROXY_BRANDING, + cracked: true, + data: { + cache: true, + icon: newPingRes.favicon != null ? true : false, + max: newPingRes.players.max, + 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) + : [], + }, + name: "placeholder name", + secure: false, + time: Date.now(), + type: "motd", + uuid: randomUUID(), // replace placeholder with global. cached UUID + vers: `${PROXY_BRANDING}/${PROXY_VERSION}`, + }, + image + ); + } + } + + public static async generateMOTDFromConfig( + config: Config["adapter"] + ): Promise { + if (typeof config.motd != "string") { + const motd = new MOTD({ + brand: PROXY_BRANDING, + cracked: true, + data: { cache: true, - icon: boolean, - max: number, - motd: [string, string], - online: number, - players: string[], - }, - name: string, - secure: false, - time: ReturnType, - type: "motd", - uuid: ReturnType, - vers: string + 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}`, + }); + if (config.motd.iconURL != null) { + motd.image = await this.generateEaglerMOTDImage(config.motd.iconURL); + } + return motd; + } else throw new Error("MOTD is set to be forwarded in the config!"); } -} \ No newline at end of file + + // TODO: fix not working + public static generateEaglerMOTDImage( + file: string | Buffer + ): Promise { + return new Promise((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]; + } + } + + export type JSONMotd = { + brand: string; + cracked: true; + data: { + cache: true; + icon: boolean; + max: number; + motd: [string, string]; + online: number; + players: string[]; + }; + name: string; + secure: false; + time: ReturnType; + type: "motd"; + uuid: ReturnType; + vers: string; + }; +} diff --git a/src/proxy/Packet.ts b/src/proxy/Packet.ts index c7cde06..db5fd43 100644 --- a/src/proxy/Packet.ts +++ b/src/proxy/Packet.ts @@ -1,35 +1,44 @@ -import { readdir } from "fs/promises" -import { dirname, join, resolve } from "path" -import { fileURLToPath, pathToFileURL } from "url" -import { Enums } from "./Enums.js" -import { Util } from "./Util.js" +import { readdir } from "fs/promises"; +import { dirname, join, resolve } from "path"; +import { fileURLToPath, pathToFileURL } from "url"; +import { Enums } from "./Enums.js"; +import { Util } from "./Util.js"; export default interface Packet { - packetId: Enums.PacketId - type: "packet" - boundTo: Enums.PacketBounds - sentAfterHandshake: boolean + packetId: Enums.PacketId; + type: "packet"; + boundTo: Enums.PacketBounds; + sentAfterHandshake: boolean; - serialize: () => Buffer - deserialize: (packet: Buffer) => this + serialize: () => Buffer; + deserialize: (packet: Buffer) => this; } -export async function loadPackets(dir?: string): Promise> { - const files = (await Util.recursiveFileSearch(dir ?? join(dirname(fileURLToPath(import.meta.url)), "packets"))).filter(f => f.endsWith(".js") && !f.endsWith(".disabled.js")) - const packetRegistry = new Map() - for (const file of files) { - const imp = await import(process.platform == 'win32' ? pathToFileURL(file).toString() : file) - for (const val of Object.values(imp)) { - if (val != null) { - let e: Packet - try { e = new (val as any)() } - catch {} - if (e != null && e.type == 'packet') { - ;(e as any).class = val - packetRegistry.set(e.packetId, e) - } - } +export async function loadPackets( + dir?: string +): Promise> { + const files = ( + await Util.recursiveFileSearch( + dir ?? join(dirname(fileURLToPath(import.meta.url)), "packets") + ) + ).filter((f) => f.endsWith(".js") && !f.endsWith(".disabled.js")); + const packetRegistry = new Map(); + for (const file of files) { + const imp = await import( + process.platform == "win32" ? pathToFileURL(file).toString() : file + ); + for (const val of Object.values(imp)) { + if (val != null) { + let e: Packet; + try { + e = new (val as any)(); + } catch {} + if (e != null && e.type == "packet") { + (e as any).class = val; + packetRegistry.set(e.packetId, e); } + } } - return packetRegistry -} \ No newline at end of file + } + return packetRegistry; +} diff --git a/src/proxy/Protocol.ts b/src/proxy/Protocol.ts index 6ca0dd5..60d5ade 100644 --- a/src/proxy/Protocol.ts +++ b/src/proxy/Protocol.ts @@ -1,9 +1,9 @@ import { - encodeULEB128 as _encodeVarInt, - decodeULEB128 as _decodeVarInt -} from "@thi.ng/leb128" -import { Enums } from "./Enums.js" -import { Util } from "./Util.js" + encodeULEB128 as _encodeVarInt, + decodeULEB128 as _decodeVarInt, +} from "@thi.ng/leb128"; +import { Enums } from "./Enums.js"; +import { Util } from "./Util.js"; // reference: https://wiki.vg/index.php?title=Protocol&oldid=7368 (id: 73) // use https://hexed.it/ for hex analysis, dumps.ts for example dumps @@ -11,68 +11,77 @@ import { Util } from "./Util.js" // datatypes being used thus far. There may be more, but however, they will be added here as needed. export namespace MineProtocol { - export type ReadResult = { - value: T, - // the new buffer, but with the bytes being read being completely removed - // very useful when it comes to chaining - newBuffer: Buffer - } + export type ReadResult = { + value: T; + // the new buffer, but with the bytes being read being completely removed + // very useful when it comes to chaining + newBuffer: Buffer; + }; - export type UUID = string + export type UUID = string; - export function writeVarInt(int: number): Buffer { - return Buffer.from(_encodeVarInt(int)) - } - - export function readVarInt(buff: Buffer, offset?: number): ReadResult { - buff = offset ? buff.subarray(offset) : buff - const read = _decodeVarInt(buff), len = read[1] - return { - // potential oversight? - value: Number(read[0]), - newBuffer: buff.subarray(len) - } - } - - 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 { - buff = offset ? buff.subarray(offset) : buff - const len = readVarInt(buff), str = len.newBuffer.subarray(0, len.value).toString('utf8') - return { - value: str, - newBuffer: len.newBuffer.subarray(len.value) - } - } - - const _readShort = (a: number, b: number) => a << 8 | b << 0 - - export function readShort(buff: Buffer, offset?: number): ReadResult { - buff = offset ? buff.subarray(offset) : buff - return { - value: _readShort(buff[0], buff[1]), - newBuffer: buff.subarray(2) - } - } - - export function writeShort(num: number): Buffer { - const alloc = Buffer.alloc(2) - alloc.writeInt16BE(num) - return alloc - } + export function writeVarInt(int: number): Buffer { + return Buffer.from(_encodeVarInt(int)); + } - export function readUUID(buff: Buffer, offset?: number): ReadResult { - buff = offset ? buff.subarray(offset) : buff - return { - value: Util.uuidBufferToString(buff.subarray(0, 16)), - newBuffer: buff.subarray(16) - } - } + export function readVarInt( + buff: Buffer, + offset?: number + ): ReadResult { + buff = offset ? buff.subarray(offset) : buff; + const read = _decodeVarInt(buff), + len = read[1]; + return { + // potential oversight? + value: Number(read[0]), + newBuffer: buff.subarray(len), + }; + } - export function writeUUID(uuid: string | Buffer): Buffer { - return typeof uuid == 'string' ? Util.uuidStringToBuffer(uuid) : uuid - } -} \ No newline at end of file + 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 { + buff = offset ? buff.subarray(offset) : buff; + const len = readVarInt(buff), + str = len.newBuffer.subarray(0, len.value).toString("utf8"); + return { + value: str, + newBuffer: len.newBuffer.subarray(len.value), + }; + } + + const _readShort = (a: number, b: number) => (a << 8) | (b << 0); + + export function readShort(buff: Buffer, offset?: number): ReadResult { + buff = offset ? buff.subarray(offset) : buff; + return { + value: _readShort(buff[0], buff[1]), + newBuffer: buff.subarray(2), + }; + } + + export function writeShort(num: number): Buffer { + const alloc = Buffer.alloc(2); + alloc.writeInt16BE(num); + return alloc; + } + + export function readUUID(buff: Buffer, offset?: number): ReadResult { + buff = offset ? buff.subarray(offset) : buff; + return { + value: Util.uuidBufferToString(buff.subarray(0, 16)), + newBuffer: buff.subarray(16), + }; + } + + export function writeUUID(uuid: string | Buffer): Buffer { + return typeof uuid == "string" ? Util.uuidStringToBuffer(uuid) : uuid; + } +} diff --git a/src/proxy/Proxy.ts b/src/proxy/Proxy.ts index ead959d..e2b4623 100644 --- a/src/proxy/Proxy.ts +++ b/src/proxy/Proxy.ts @@ -2,18 +2,23 @@ import { WebSocket, WebSocketServer } from "ws"; import { Config } from "../launcher_types.js"; import { Logger } from "../logger.js"; import Packet, { loadPackets } from "./Packet.js"; -import * as http from "http" -import * as https from "https" +import * as http from "http"; +import * as https from "https"; import { readFile } from "fs/promises"; import { Duplex } from "stream"; -import { parseDomain, ParseResultType } from "parse-domain" +import { parseDomain, ParseResultType } from "parse-domain"; import { Util } from "./Util.js"; import CSLoginPacket from "./packets/CSLoginPacket.js"; import SCIdentifyPacket from "./packets/SCIdentifyPacket.js"; import { Motd } from "./Motd.js"; import { Player } from "./Player.js"; import { Enums } from "./Enums.js"; -import { NETWORK_VERSION, PROXY_BRANDING, PROXY_VERSION, VANILLA_PROTOCOL_VERSION } from "../meta.js"; +import { + NETWORK_VERSION, + PROXY_BRANDING, + PROXY_VERSION, + VANILLA_PROTOCOL_VERSION, +} from "../meta.js"; import { CSUsernamePacket } from "./packets/CSUsernamePacket.js"; import { SCSyncUuidPacket } from "./packets/SCSyncUuidPacket.js"; import { SCReadyPacket } from "./packets/SCReadyPacket.js"; @@ -26,312 +31,457 @@ import { CSChannelMessagePacket } from "./packets/channel/CSChannelMessage.js"; import { Constants, UPGRADE_REQUIRED_RESPONSE } from "./Constants.js"; import { PluginManager } from "./pluginLoader/PluginManager.js"; -let instanceCount = 0 -const chalk = new Chalk({ level: 2 }) +let instanceCount = 0; +const chalk = new Chalk({ level: 2 }); export class Proxy extends EventEmitter { - public packetRegistry: Map - public players = new Map() - public pluginManager: PluginManager - public config: Config['adapter'] - public wsServer: WebSocketServer - public httpServer: http.Server - public skinServer: EaglerSkins.SkinServer - public broadcastMotd?: Motd.MOTD - - private _logger: Logger - private initalHandlerLogger: Logger - - private loaded: boolean - - constructor(config: Config['adapter'], pluginManager: PluginManager) { - super() - this._logger = new Logger(`EaglerProxy-${instanceCount}`) - this.initalHandlerLogger = new Logger(`EaglerProxy-InitialHandler`) - // hijack the initial handler logger to append [InitialHandler] to the beginning - ;(this.initalHandlerLogger as any)._info = this.initalHandlerLogger.info - this.initalHandlerLogger.info = (msg: string) => { - ;(this.initalHandlerLogger as any)._info(`${chalk.blue("[InitialHandler]")} ${msg}`) - } - ;(this.initalHandlerLogger as any)._warn = this.initalHandlerLogger.warn - this.initalHandlerLogger.warn = (msg: string) => { - ;(this.initalHandlerLogger as any)._warn(`${chalk.blue("[InitialHandler]")} ${msg}`) - } - ;(this.initalHandlerLogger as any)._error = this.initalHandlerLogger.error - this.initalHandlerLogger.error = (msg: string) => { - ;(this.initalHandlerLogger as any)._error(`${chalk.blue("[InitialHandler]")} ${msg}`) - } - ;(this.initalHandlerLogger as any)._fatal = this.initalHandlerLogger.fatal - this.initalHandlerLogger.fatal = (msg: string) => { - ;(this.initalHandlerLogger as any)._fatal(`${chalk.blue("[InitialHandler]")} ${msg}`) - } - ;(this.initalHandlerLogger as any)._debug = this.initalHandlerLogger.debug - this.initalHandlerLogger.debug = (msg: string) => { - ;(this.initalHandlerLogger as any)._debug(`${chalk.blue("[InitialHandler]")} ${msg}`) - } - this.config = config - this.pluginManager = pluginManager - instanceCount++ - - process.on('uncaughtException', err => { - this._logger.warn(`An uncaught exception was caught! Error: ${err.stack}`) - }) - - process.on('unhandledRejection', err => { - this._logger.warn(`An unhandled rejection was caught! Rejection: ${err}`) - }) + public packetRegistry: Map< + number, + Packet & { + class: any; } + >; + public players = new Map(); + public pluginManager: PluginManager; + public config: Config["adapter"]; + public wsServer: WebSocketServer; + public httpServer: http.Server; + public skinServer: EaglerSkins.SkinServer; + public broadcastMotd?: Motd.MOTD; - public async init() { - this._logger.info(`Starting ${PROXY_BRANDING} v${PROXY_VERSION}...`) - global.PROXY = this - 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) - 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) - ;(broadcastMOTD as any)._static = true - this.broadcastMotd = broadcastMOTD - // playercount will be dynamically updated - } - if (this.config.tls && this.config.tls.enabled) { - this.httpServer = https.createServer({ - key: await readFile(this.config.tls.key), - cert: await readFile(this.config.tls.cert) - }, (req, res) => this._handleNonWSRequest(req, res, this.config)).listen(this.config.bindPort || 8080, this.config.bindHost || '127.0.0.1') - this.wsServer = new WebSocketServer({ - noServer: true - }) - } else { - this.httpServer = http.createServer((req, res) => this._handleNonWSRequest(req, res, this.config)).listen(this.config.bindPort || 8080, this.config.bindHost || '127.0.0.1') - this.wsServer = new WebSocketServer({ - noServer: true - }) - } - this.httpServer.on('error', err => { - this._logger.warn(`HTTP server threw an error: ${err.stack}`) - }) - this.wsServer.on('error', err => { - this._logger.warn(`WebSocket server threw an error: ${err.stack}`) - }) - this.httpServer.on('upgrade', async (r, s, h) => { - try { - await this._handleWSConnectionReq(r, s, h) - } catch (err) { - this._logger.error(`Error was caught whilst trying to handle WebSocket upgrade! Error: ${err.stack ?? err}`) - } - }) - 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 _logger: Logger; + private initalHandlerLogger: Logger; + + private loaded: boolean; + + constructor(config: Config["adapter"], pluginManager: PluginManager) { + super(); + this._logger = new Logger(`EaglerProxy-${instanceCount}`); + this.initalHandlerLogger = new Logger(`EaglerProxy-InitialHandler`); + // hijack the initial handler logger to append [InitialHandler] to the beginning + (this.initalHandlerLogger as any)._info = this.initalHandlerLogger.info; + this.initalHandlerLogger.info = (msg: string) => { + (this.initalHandlerLogger as any)._info( + `${chalk.blue("[InitialHandler]")} ${msg}` + ); + }; + (this.initalHandlerLogger as any)._warn = this.initalHandlerLogger.warn; + this.initalHandlerLogger.warn = (msg: string) => { + (this.initalHandlerLogger as any)._warn( + `${chalk.blue("[InitialHandler]")} ${msg}` + ); + }; + (this.initalHandlerLogger as any)._error = this.initalHandlerLogger.error; + this.initalHandlerLogger.error = (msg: string) => { + (this.initalHandlerLogger as any)._error( + `${chalk.blue("[InitialHandler]")} ${msg}` + ); + }; + (this.initalHandlerLogger as any)._fatal = this.initalHandlerLogger.fatal; + this.initalHandlerLogger.fatal = (msg: string) => { + (this.initalHandlerLogger as any)._fatal( + `${chalk.blue("[InitialHandler]")} ${msg}` + ); + }; + (this.initalHandlerLogger as any)._debug = this.initalHandlerLogger.debug; + this.initalHandlerLogger.debug = (msg: string) => { + (this.initalHandlerLogger as any)._debug( + `${chalk.blue("[InitialHandler]")} ${msg}` + ); + }; + this.config = config; + this.pluginManager = pluginManager; + instanceCount++; + + process.on("uncaughtException", (err) => { + this._logger.warn( + `An uncaught exception was caught! Error: ${err.stack}` + ); + }); + + process.on("unhandledRejection", (err) => { + this._logger.warn(`An unhandled rejection was caught! Rejection: ${err}`); + }); + } + + public async init() { + this._logger.info(`Starting ${PROXY_BRANDING} v${PROXY_VERSION}...`); + global.PROXY = this; + 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 + ); + 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); + (broadcastMOTD as any)._static = true; + this.broadcastMotd = broadcastMOTD; + // playercount will be dynamically updated } + if (this.config.tls && this.config.tls.enabled) { + this.httpServer = https + .createServer( + { + key: await readFile(this.config.tls.key), + cert: await readFile(this.config.tls.cert), + }, + (req, res) => this._handleNonWSRequest(req, res, this.config) + ) + .listen( + this.config.bindPort || 8080, + this.config.bindHost || "127.0.0.1" + ); + this.wsServer = new WebSocketServer({ + noServer: true, + }); + } else { + this.httpServer = http + .createServer((req, res) => + this._handleNonWSRequest(req, res, this.config) + ) + .listen( + this.config.bindPort || 8080, + this.config.bindHost || "127.0.0.1" + ); + this.wsServer = new WebSocketServer({ + noServer: true, + }); + } + this.httpServer.on("error", (err) => { + this._logger.warn(`HTTP server threw an error: ${err.stack}`); + }); + this.wsServer.on("error", (err) => { + this._logger.warn(`WebSocket server threw an error: ${err.stack}`); + }); + this.httpServer.on("upgrade", async (r, s, h) => { + try { + await this._handleWSConnectionReq(r, s, h); + } catch (err) { + this._logger.error( + `Error was caught whilst trying to handle WebSocket upgrade! Error: ${ + err.stack ?? err + }` + ); + } + }); + 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']) { - res.setHeader("Content-Type", "text/html") - .writeHead(426) - .end(UPGRADE_REQUIRED_RESPONSE) - } + private _handleNonWSRequest( + req: http.IncomingMessage, + res: http.ServerResponse, + config: Config["adapter"] + ) { + res + .setHeader("Content-Type", "text/html") + .writeHead(426) + .end(UPGRADE_REQUIRED_RESPONSE); + } - readonly LOGIN_TIMEOUT = 30000 + readonly LOGIN_TIMEOUT = 30000; - private async _handleWSConnection(ws: WebSocket) { - const firstPacket = await Util.awaitPacket(ws) - let player: Player, handled: boolean - setTimeout(() => { - if (!handled) { - this.initalHandlerLogger.warn(`Disconnecting client ${player ? player.username ?? `[/${(ws as any)._socket.remoteAddress}:${(ws as any)._socket.remotePort}` : `[/${(ws as any)._socket.remoteAddress}:${(ws as any)._socket.remotePort}`} due to connection timing out.`) - if (player) player.disconnect(`${Enums.ChatColor.YELLOW} Your connection timed out whilst processing handshake, please try again.`) - else ws.close() - } - }, this.LOGIN_TIMEOUT) + private async _handleWSConnection(ws: WebSocket) { + const firstPacket = await Util.awaitPacket(ws); + let player: Player, handled: boolean; + setTimeout(() => { + if (!handled) { + this.initalHandlerLogger.warn( + `Disconnecting client ${ + player + ? player.username ?? + `[/${(ws as any)._socket.remoteAddress}:${ + (ws as any)._socket.remotePort + }` + : `[/${(ws as any)._socket.remoteAddress}:${ + (ws as any)._socket.remotePort + }` + } due to connection timing out.` + ); + if (player) + player.disconnect( + `${Enums.ChatColor.YELLOW} Your connection timed out whilst processing handshake, please try again.` + ); + else ws.close(); + } + }, this.LOGIN_TIMEOUT); + try { + if (firstPacket.toString() === "Accept: MOTD") { + if (this.broadcastMotd) { + if ((this.broadcastMotd as any)._static) { + this.broadcastMotd.jsonMotd.data.online = this.players.size; + // sample for players + this.broadcastMotd.jsonMotd.data.players = []; + const playerSample = [...this.players.keys()] + .filter((sample) => !sample.startsWith("!phs_")) + .slice(0, 5); + this.broadcastMotd.jsonMotd.data.players = playerSample; + if (this.players.size - playerSample.length > 0) + this.broadcastMotd.jsonMotd.data.players.push( + `${Enums.ChatColor.GRAY}${Enums.ChatColor.ITALIC}(and ${ + this.players.size - playerSample.length + } more)` + ); + + const bufferized = this.broadcastMotd.toBuffer(); + ws.send(bufferized[0]); + if (bufferized[1] != null) ws.send(bufferized[1]); + } else { + const motd = this.broadcastMotd.toBuffer(); + ws.send(motd[0]); + if (motd[1] != null) ws.send(motd[1]); + } + } + handled = true; + ws.close(); + } else { + player = new Player(ws); + const loginPacket = new CSLoginPacket().deserialize(firstPacket); + player.state = Enums.ClientState.PRE_HANDSHAKE; + if (loginPacket.gameVersion != VANILLA_PROTOCOL_VERSION) { + player.disconnect( + `${Enums.ChatColor.RED}Please connect to this proxy on EaglercraftX 1.8.9.` + ); + return; + } else if (loginPacket.networkVersion != NETWORK_VERSION) { + player.disconnect( + `${Enums.ChatColor.RED}Your EaglercraftX version is too ${ + loginPacket.networkVersion > NETWORK_VERSION ? "new" : "old" + }! Please ${ + loginPacket.networkVersion > NETWORK_VERSION + ? "downgrade" + : "update" + }.` + ); + return; + } try { - if (firstPacket.toString() === "Accept: MOTD") { - if (this.broadcastMotd) { - if ((this.broadcastMotd as any)._static) { - this.broadcastMotd.jsonMotd.data.online = this.players.size - // sample for players - this.broadcastMotd.jsonMotd.data.players = [] - const playerSample = [...this.players.keys()] - .filter(sample => !sample.startsWith("!phs_")) - .slice(0, 5) - this.broadcastMotd.jsonMotd.data.players = playerSample - if (this.players.size - playerSample.length > 0) this.broadcastMotd.jsonMotd.data.players.push(`${Enums.ChatColor.GRAY}${Enums.ChatColor.ITALIC}(and ${this.players.size - playerSample.length} more)`) - - const bufferized = this.broadcastMotd.toBuffer() - ws.send(bufferized[0]) - if (bufferized[1] != null) ws.send(bufferized[1]) - } else { - const motd = this.broadcastMotd.toBuffer() - ws.send(motd[0]) - if (motd[1] != null) ws.send(motd[1]) - } - } - handled = true - ws.close() - } else { - player = new Player(ws) - const loginPacket = new CSLoginPacket().deserialize(firstPacket) - player.state = Enums.ClientState.PRE_HANDSHAKE - if (loginPacket.gameVersion != VANILLA_PROTOCOL_VERSION) { - player.disconnect(`${Enums.ChatColor.RED}Please connect to this proxy on EaglercraftX 1.8.9.`) - return - } else if (loginPacket.networkVersion != NETWORK_VERSION) { - player.disconnect(`${Enums.ChatColor.RED}Your EaglercraftX version is too ${loginPacket.networkVersion > NETWORK_VERSION ? "new" : "old"}! Please ${loginPacket.networkVersion > NETWORK_VERSION ? "downgrade" : "update"}.`) - return - } - try { Util.validateUsername(loginPacket.username) } - catch (err) { - player.disconnect(`${Enums.ChatColor.RED}${err.reason || err}`) - return - } - player.username = loginPacket.username - player.uuid = Util.generateUUIDFromPlayer(player.username) - if (this.players.size > this.config.maxConcurrentClients) { - player.disconnect(`${Enums.ChatColor.YELLOW}Proxy is full! Please try again later.`) - return - } else if (this.players.get(player.username) != null|| this.players.get(`!phs.${player.uuid}`) != null) { - player.disconnect(`${Enums.ChatColor.YELLOW}Someone under your username (${player.username}) is already connected to the proxy!`) - return - } - this.players.set(`!phs.${player.uuid}`, player) - this._logger.info(`Player ${loginPacket.username} (${Util.generateUUIDFromPlayer(loginPacket.username)}) running ${loginPacket.brand}/${loginPacket.version} (net ver: ${loginPacket.networkVersion}, game ver: ${loginPacket.gameVersion}) is attempting to connect!`) - player.write(new SCIdentifyPacket()) - const usernamePacket: CSUsernamePacket = await player.read(Enums.PacketId.CSUsernamePacket) as any - if (usernamePacket.username !== player.username) { - player.disconnect(`${Enums.ChatColor.YELLOW}Failed to complete handshake. Your game version may be too old or too new.`) - return - } - const syncUuid = new SCSyncUuidPacket() - syncUuid.username = player.username - syncUuid.uuid = player.uuid - player.write(syncUuid) - - const prom = await Promise.all([player.read(Enums.PacketId.CSReadyPacket), await player.read(Enums.PacketId.CSSetSkinPacket) as CSSetSkinPacket]), - skin = prom[1], - obj = new EaglerSkins.EaglerSkin() - obj.owner = player - obj.type = skin.skinType as any - if (skin.skinType == Enums.SkinType.CUSTOM) obj.skin = skin.skin - else obj.builtInSkin = skin.skinId - player.skin = obj - - player.write(new SCReadyPacket()) - this.players.delete(`!phs.${player.uuid}`) - this.players.set(player.username, player) - player.initListeners() - this._bindListenersToPlayer(player) - 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, - username: player.username - }) - this._logger.info(`Player ${player.username} successfully connected to server.`) - this.emit('playerConnect', player) - } + Util.validateUsername(loginPacket.username); } catch (err) { - this.initalHandlerLogger.warn(`Error occurred whilst handling handshake: ${err.stack ?? err}`) - handled = true - ws.close() - if (player && player.uuid && this.players.has(`!phs.${player.uuid}`)) - this.players.delete(`!phs.${player.uuid}`) - if (player && player.uuid && this.players.has(player.username)) - this.players.delete(player.username) + player.disconnect(`${Enums.ChatColor.RED}${err.reason || err}`); + return; } - } + player.username = loginPacket.username; + player.uuid = Util.generateUUIDFromPlayer(player.username); + if (this.players.size > this.config.maxConcurrentClients) { + player.disconnect( + `${Enums.ChatColor.YELLOW}Proxy is full! Please try again later.` + ); + return; + } else if ( + this.players.get(player.username) != null || + this.players.get(`!phs.${player.uuid}`) != null + ) { + player.disconnect( + `${Enums.ChatColor.YELLOW}Someone under your username (${player.username}) is already connected to the proxy!` + ); + return; + } + this.players.set(`!phs.${player.uuid}`, player); + this._logger.info( + `Player ${loginPacket.username} (${Util.generateUUIDFromPlayer( + loginPacket.username + )}) running ${loginPacket.brand}/${loginPacket.version} (net ver: ${ + loginPacket.networkVersion + }, game ver: ${loginPacket.gameVersion}) is attempting to connect!` + ); + player.write(new SCIdentifyPacket()); + const usernamePacket: CSUsernamePacket = (await player.read( + Enums.PacketId.CSUsernamePacket + )) as any; + if (usernamePacket.username !== player.username) { + player.disconnect( + `${Enums.ChatColor.YELLOW}Failed to complete handshake. Your game version may be too old or too new.` + ); + return; + } + const syncUuid = new SCSyncUuidPacket(); + syncUuid.username = player.username; + syncUuid.uuid = player.uuid; + player.write(syncUuid); - private _bindListenersToPlayer(player: Player) { - let sentDisconnectMsg = false - player.on('disconnect', () => { - if (this.players.has(player.username)) - this.players.delete(player.username) - this.initalHandlerLogger.info(`DISCONNECT ${player.username} <=> DISCONNECTED`) - if (!sentDisconnectMsg) this._logger.info(`Player ${player.username} (${player.uuid}) disconnected from the proxy server.`) - }) - player.on('proxyPacket', async packet => { - if (packet.packetId == Enums.PacketId.CSChannelMessagePacket) { - try { - const msg: CSChannelMessagePacket = packet as any - if (msg.channel == Constants.EAGLERCRAFT_SKIN_CHANNEL_NAME) { - await this.skinServer.handleRequest(msg, player) - } - } catch (err) { - this._logger.error(`Failed to process channel message packet! Error: ${err.stack || err}`) - } - } - }) - player.on('switchServer', client => { - this.initalHandlerLogger.info(`SWITCH_SERVER ${player.username} <=> ${client.socket.remoteAddress}:${client.socket.remotePort}`) - }) - player.on('joinServer', client => { - this.initalHandlerLogger.info(`SERVER_CONNECTED ${player.username} <=> ${client.socket.remoteAddress}:${client.socket.remotePort}`) - }) - } + const prom = await Promise.all([ + player.read(Enums.PacketId.CSReadyPacket), + (await player.read( + Enums.PacketId.CSSetSkinPacket + )) as CSSetSkinPacket, + ]), + skin = prom[1], + obj = new EaglerSkins.EaglerSkin(); + obj.owner = player; + obj.type = skin.skinType as any; + if (skin.skinType == Enums.SkinType.CUSTOM) obj.skin = skin.skin; + else obj.builtInSkin = skin.skinId; + player.skin = obj; - static readonly POLL_INTERVAL: number = 10000 + player.write(new SCReadyPacket()); + this.players.delete(`!phs.${player.uuid}`); + this.players.set(player.username, player); + player.initListeners(); + this._bindListenersToPlayer(player); + 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, + username: player.username, + }); + this._logger.info( + `Player ${player.username} successfully connected to server.` + ); + this.emit("playerConnect", player); + } + } catch (err) { + this.initalHandlerLogger.warn( + `Error occurred whilst handling handshake: ${err.stack ?? err}` + ); + handled = true; + ws.close(); + if (player && player.uuid && this.players.has(`!phs.${player.uuid}`)) + this.players.delete(`!phs.${player.uuid}`); + if (player && player.uuid && this.players.has(player.username)) + this.players.delete(player.username); + } + } - private _pollServer(host: string, port: number, interval?: number) { - ;(async () => { - while (true) { - const motd = await Motd.MOTD.generateMOTDFromPing(host, port) - .catch(err => { - this._logger.warn(`Error polling ${host}:${port} for MOTD: ${err.stack ?? err}`) - }) - if (motd) this.broadcastMotd = motd - await new Promise(res => setTimeout(res, interval ?? Proxy.POLL_INTERVAL)) - } - })() - } + private _bindListenersToPlayer(player: Player) { + let sentDisconnectMsg = false; + player.on("disconnect", () => { + if (this.players.has(player.username)) + this.players.delete(player.username); + this.initalHandlerLogger.info( + `DISCONNECT ${player.username} <=> DISCONNECTED` + ); + if (!sentDisconnectMsg) + this._logger.info( + `Player ${player.username} (${player.uuid}) disconnected from the proxy server.` + ); + }); + player.on("proxyPacket", async (packet) => { + if (packet.packetId == Enums.PacketId.CSChannelMessagePacket) { + try { + const msg: CSChannelMessagePacket = packet as any; + if (msg.channel == Constants.EAGLERCRAFT_SKIN_CHANNEL_NAME) { + await this.skinServer.handleRequest(msg, player); + } + } catch (err) { + this._logger.error( + `Failed to process channel message packet! Error: ${ + err.stack || err + }` + ); + } + } + }); + player.on("switchServer", (client) => { + this.initalHandlerLogger.info( + `SWITCH_SERVER ${player.username} <=> ${client.socket.remoteAddress}:${client.socket.remotePort}` + ); + }); + player.on("joinServer", (client) => { + this.initalHandlerLogger.info( + `SERVER_CONNECTED ${player.username} <=> ${client.socket.remoteAddress}:${client.socket.remotePort}` + ); + }); + } - private async _handleWSConnectionReq(req: http.IncomingMessage, socket: Duplex, head: Buffer) { - const origin = req.headers.origin == null || req.headers.origin == 'null' ? null : req.headers.origin - if (!this.config.origins.allowOfflineDownloads && origin == null) { - socket.destroy() - return - } - if (this.config.origins.originBlacklist != null && this.config.origins.originBlacklist.some(host => Util.areDomainsEqual(host, origin))) { - socket.destroy() - return - } - if (this.config.origins.originWhitelist != null && !this.config.origins.originWhitelist.some(host => Util.areDomainsEqual(host, origin))) { - socket.destroy() - return - } - try { await this.wsServer.handleUpgrade(req, socket, head, ws => this._handleWSConnection(ws)) } - catch (err) { - this._logger.error(`Error was caught whilst trying to handle WebSocket connection request! Error: ${err.stack ?? err}`) - socket.destroy() - } - } + static readonly POLL_INTERVAL: number = 10000; - public fetchUserByUUID(uuid: MineProtocol.UUID): Player | null { - for (const [username, player] of this.players) { - if (player.uuid == uuid) - return player - } - return null - } + private _pollServer(host: string, port: number, interval?: number) { + (async () => { + while (true) { + const motd = await Motd.MOTD.generateMOTDFromPing(host, port).catch( + (err) => { + this._logger.warn( + `Error polling ${host}:${port} for MOTD: ${err.stack ?? err}` + ); + } + ); + if (motd) this.broadcastMotd = motd; + await new Promise((res) => + setTimeout(res, interval ?? Proxy.POLL_INTERVAL) + ); + } + })(); + } + + private async _handleWSConnectionReq( + req: http.IncomingMessage, + socket: Duplex, + head: Buffer + ) { + const origin = + req.headers.origin == null || req.headers.origin == "null" + ? null + : req.headers.origin; + if (!this.config.origins.allowOfflineDownloads && origin == null) { + socket.destroy(); + return; + } + if ( + this.config.origins.originBlacklist != null && + this.config.origins.originBlacklist.some((host) => + Util.areDomainsEqual(host, origin) + ) + ) { + socket.destroy(); + return; + } + if ( + this.config.origins.originWhitelist != null && + !this.config.origins.originWhitelist.some((host) => + Util.areDomainsEqual(host, origin) + ) + ) { + socket.destroy(); + return; + } + try { + await this.wsServer.handleUpgrade(req, socket, head, (ws) => + this._handleWSConnection(ws) + ); + } catch (err) { + this._logger.error( + `Error was caught whilst trying to handle WebSocket connection request! Error: ${ + err.stack ?? err + }` + ); + socket.destroy(); + } + } + + public fetchUserByUUID(uuid: MineProtocol.UUID): Player | null { + for (const [username, player] of this.players) { + if (player.uuid == uuid) return player; + } + return null; + } } interface ProxyEvents { - 'playerConnect': (player: Player) => void, - 'playerDisconnect': (player: Player) => void + playerConnect: (player: Player) => void; + playerDisconnect: (player: Player) => void; } export declare interface Proxy { - on( - event: U, listener: ProxyEvents[U] - ): this; - - emit( - event: U, ...args: Parameters - ): boolean; -} \ No newline at end of file + on(event: U, listener: ProxyEvents[U]): this; + + emit( + event: U, + ...args: Parameters + ): boolean; +} diff --git a/src/proxy/Util.ts b/src/proxy/Util.ts index 782b145..236b15d 100644 --- a/src/proxy/Util.ts +++ b/src/proxy/Util.ts @@ -1,198 +1,247 @@ import { createHash } from "crypto"; -import { - encodeULEB128, - decodeULEB128, -} from "@thi.ng/leb128" +import { encodeULEB128, decodeULEB128 } from "@thi.ng/leb128"; import { Chat } from "./Chat.js"; import { WebSocket } from "ws"; import { Enums } from "./Enums.js"; import { Player } from "./Player.js"; -import * as http from "http" +import * as http from "http"; import { Config } from "../launcher_types.js"; import { parseDomain, ParseResultType } from "parse-domain"; import { access, readdir } from "fs/promises"; import { resolve } from "path"; export namespace Util { - export const encodeVarInt: typeof encodeULEB128 = encodeULEB128 - export const decodeVarInt: typeof decodeULEB128 = decodeULEB128 + export const encodeVarInt: typeof encodeULEB128 = encodeULEB128; + export const decodeVarInt: typeof decodeULEB128 = decodeULEB128; - // annotation for range - // b = beginning, e = end - export type Range = number + // annotation for range + // b = beginning, e = end + export type Range = number; - export type BoundedBuffer = Buffer + export type BoundedBuffer = Buffer; - const USERNAME_REGEX = /[^0-9^a-z^A-Z^_]/gi + const USERNAME_REGEX = /[^0-9^a-z^A-Z^_]/gi; - export function generateUUIDFromPlayer(user: string): string { - const str = `OfflinePlayer:${user}` - let md5Bytes = createHash('md5').update(str).digest() - md5Bytes[6] &= 0x0f; /* clear version */ - md5Bytes[6] |= 0x30; /* set to version 3 */ - md5Bytes[8] &= 0x3f; /* clear variant */ - md5Bytes[8] |= 0x80; /* set to IETF variant */ - return uuidBufferToString(md5Bytes) - } + export function generateUUIDFromPlayer(user: string): string { + const str = `OfflinePlayer:${user}`; + let md5Bytes = createHash("md5").update(str).digest(); + md5Bytes[6] &= 0x0f; /* clear version */ + md5Bytes[6] |= 0x30; /* set to version 3 */ + md5Bytes[8] &= 0x3f; /* clear variant */ + md5Bytes[8] |= 0x80; /* set to IETF variant */ + return uuidBufferToString(md5Bytes); + } - // excerpt from uuid-buffer + // excerpt from uuid-buffer - export function uuidStringToBuffer(uuid: string): Buffer { - if (!uuid) return Buffer.alloc(16); // Return empty buffer - const hexStr = uuid.replace(/-/g, ''); - if (uuid.length != 36 || hexStr.length != 32) throw new Error(`Invalid UUID string: ${uuid}`); - return Buffer.from(hexStr, 'hex'); - } + export function uuidStringToBuffer(uuid: string): Buffer { + if (!uuid) return Buffer.alloc(16); // Return empty buffer + const hexStr = uuid.replace(/-/g, ""); + if (uuid.length != 36 || hexStr.length != 32) + throw new Error(`Invalid UUID string: ${uuid}`); + return Buffer.from(hexStr, "hex"); + } - export function uuidBufferToString(buffer: Buffer): string { - if (buffer.length != 16) throw new Error(`Invalid buffer length for uuid: ${buffer.length}`); - if (buffer.equals(Buffer.alloc(16))) return null; // If buffer is all zeros, return null - const str = buffer.toString('hex'); - return `${str.slice(0, 8)}-${str.slice(8, 12)}-${str.slice(12, 16)}-${str.slice(16, 20)}-${str.slice(20)}`; - } + export function uuidBufferToString(buffer: Buffer): string { + if (buffer.length != 16) + throw new Error(`Invalid buffer length for uuid: ${buffer.length}`); + if (buffer.equals(Buffer.alloc(16))) return null; // If buffer is all zeros, return null + const str = buffer.toString("hex"); + return `${str.slice(0, 8)}-${str.slice(8, 12)}-${str.slice( + 12, + 16 + )}-${str.slice(16, 20)}-${str.slice(20)}`; + } - export function awaitPacket(ws: WebSocket, filter?: (msg: Buffer) => boolean): Promise { - return new Promise((res, rej) => { - let resolved = false - const msgCb = (msg: any) => { - if (filter != null && filter(msg)) { - resolved = true - ws.removeListener('message', msgCb) - ws.removeListener('close', discon) - ws.setMaxListeners(ws.getMaxListeners() - 2 < 0 ? 5 : ws.getMaxListeners() - 2) - res(msg) - } else if (filter == null) { - resolved = true - ws.removeListener('message', msgCb) - ws.removeListener('close', discon) - ws.setMaxListeners(ws.getMaxListeners() - 2 < 0 ? 5 : ws.getMaxListeners() - 2) - res(msg) - } - } - const discon = () => { - resolved = true - ws.removeListener('message', msgCb) - ws.removeListener('close', discon) - ws.setMaxListeners(ws.getMaxListeners() - 2 < 0 ? 5 : ws.getMaxListeners() - 2) - rej("Connection closed") - } - ws.setMaxListeners(ws.getMaxListeners() + 2) - ws.on('message', msgCb) - ws.on('close', discon) - setTimeout(() => { - ws.removeListener('message', msgCb) - ws.removeListener('close', discon) - ws.setMaxListeners(ws.getMaxListeners() - 2 < 0 ? 5 : ws.getMaxListeners() - 2) - rej("Timed out") - }, 10000) - }) - } + export function awaitPacket( + ws: WebSocket, + filter?: (msg: Buffer) => boolean + ): Promise { + return new Promise((res, rej) => { + let resolved = false; + const msgCb = (msg: any) => { + if (filter != null && filter(msg)) { + resolved = true; + ws.removeListener("message", msgCb); + ws.removeListener("close", discon); + ws.setMaxListeners( + ws.getMaxListeners() - 2 < 0 ? 5 : ws.getMaxListeners() - 2 + ); + res(msg); + } else if (filter == null) { + resolved = true; + ws.removeListener("message", msgCb); + ws.removeListener("close", discon); + ws.setMaxListeners( + ws.getMaxListeners() - 2 < 0 ? 5 : ws.getMaxListeners() - 2 + ); + res(msg); + } + }; + const discon = () => { + resolved = true; + ws.removeListener("message", msgCb); + ws.removeListener("close", discon); + ws.setMaxListeners( + ws.getMaxListeners() - 2 < 0 ? 5 : ws.getMaxListeners() - 2 + ); + rej("Connection closed"); + }; + ws.setMaxListeners(ws.getMaxListeners() + 2); + ws.on("message", msgCb); + ws.on("close", discon); + setTimeout(() => { + ws.removeListener("message", msgCb); + ws.removeListener("close", discon); + ws.setMaxListeners( + ws.getMaxListeners() - 2 < 0 ? 5 : ws.getMaxListeners() - 2 + ); + rej("Timed out"); + }, 10000); + }); + } - export function validateUsername(user: string): void | never { - if (user.length > 20) - throw new Error("Username is too long!") - if (user.length < 3) - throw new Error("Username is too short!") - if (!!user.match(USERNAME_REGEX)) - throw new Error("Invalid username. Username can only contain alphanumeric characters, and the underscore (_) character.") - } + export function validateUsername(user: string): void | never { + if (user.length > 20) throw new Error("Username is too long!"); + if (user.length < 3) throw new Error("Username is too short!"); + if (!!user.match(USERNAME_REGEX)) + throw new Error( + "Invalid username. Username can only contain alphanumeric characters, and the underscore (_) character." + ); + } - export function areDomainsEqual(d1: string, d2: string): boolean { - if (d1.endsWith("*.")) d1 = d1.replace("*.", "WILDCARD-LOL-EXTRA-LONG-SUBDOMAIN-TO-LOWER-CHANCES-OF-COLLISION.") - const parseResult1 = parseDomain(d1), parseResult2 = parseDomain(d2) - if (parseResult1.type != ParseResultType.Invalid && parseResult2.type != ParseResultType.Invalid) { - if (parseResult1.type == ParseResultType.Ip && parseResult2.type == ParseResultType.Ip) { - return parseResult1.hostname == parseResult2.hostname ? true : false - } else if (parseResult1.type == ParseResultType.Listed && parseResult2.type == ParseResultType.Listed) { - if (parseResult1.subDomains[0] == "WILDCARD-LOL-EXTRA-LONG-SUBDOMAIN-TO-LOWER-CHANCES-OF-COLLISION") { - // wildcard - const domainPlusTld1 = parseResult1.domain + ("." + parseResult1.topLevelDomains.join(".")) - const domainPlusTld2 = parseResult2.domain + ("." + parseResult2.topLevelDomains.join(".")) - return domainPlusTld1 == domainPlusTld2 ? true : false - } else { - // no wildcard - return d1 == d2 ? true : false - } - } else if (parseResult1.type == ParseResultType.NotListed && parseResult2.type == ParseResultType.NotListed) { - if (parseResult1.labels[0] == "WILDCARD-LOL-EXTRA-LONG-SUBDOMAIN-TO-LOWER-CHANCES-OF-COLLISION") { - // wildcard - const domainPlusTld1 = parseResult1.labels.slice(2).join('.') - const domainPlusTld2 = parseResult1.labels.slice(2).join('.') - return domainPlusTld1 == domainPlusTld2 ? true : false - } else { - // no wildcard - return d1 == d2 ? true : false - } - } else if (parseResult1.type == ParseResultType.Reserved && parseResult2.type == ParseResultType.Reserved) { - if (parseResult1.hostname == "" && parseResult1.hostname === parseResult2.hostname) - return true - else { - // uncertain, fallback to exact hostname matching - return d1 == d2 ? true : false - } - } + export function areDomainsEqual(d1: string, d2: string): boolean { + if (d1.endsWith("*.")) + d1 = d1.replace( + "*.", + "WILDCARD-LOL-EXTRA-LONG-SUBDOMAIN-TO-LOWER-CHANCES-OF-COLLISION." + ); + const parseResult1 = parseDomain(d1), + parseResult2 = parseDomain(d2); + if ( + parseResult1.type != ParseResultType.Invalid && + parseResult2.type != ParseResultType.Invalid + ) { + if ( + parseResult1.type == ParseResultType.Ip && + parseResult2.type == ParseResultType.Ip + ) { + return parseResult1.hostname == parseResult2.hostname ? true : false; + } else if ( + parseResult1.type == ParseResultType.Listed && + parseResult2.type == ParseResultType.Listed + ) { + if ( + parseResult1.subDomains[0] == + "WILDCARD-LOL-EXTRA-LONG-SUBDOMAIN-TO-LOWER-CHANCES-OF-COLLISION" + ) { + // wildcard + const domainPlusTld1 = + parseResult1.domain + + ("." + parseResult1.topLevelDomains.join(".")); + const domainPlusTld2 = + parseResult2.domain + + ("." + parseResult2.topLevelDomains.join(".")); + return domainPlusTld1 == domainPlusTld2 ? true : false; } else { - return false + // no wildcard + return d1 == d2 ? true : false; } - } - - async function* _getFiles(dir: string) { - const dirents = await readdir(dir, { withFileTypes: true }); - for (const dirent of dirents) { - const res = resolve(dir, dirent.name); - if (dirent.isDirectory()) { - yield* _getFiles(res); - } else { - yield res; - } + } else if ( + parseResult1.type == ParseResultType.NotListed && + parseResult2.type == ParseResultType.NotListed + ) { + if ( + parseResult1.labels[0] == + "WILDCARD-LOL-EXTRA-LONG-SUBDOMAIN-TO-LOWER-CHANCES-OF-COLLISION" + ) { + // wildcard + const domainPlusTld1 = parseResult1.labels.slice(2).join("."); + const domainPlusTld2 = parseResult1.labels.slice(2).join("."); + return domainPlusTld1 == domainPlusTld2 ? true : false; + } else { + // no wildcard + return d1 == d2 ? true : false; } - } - - export async function recursiveFileSearch(dir: string): Promise { - const ents = [] - for await (const f of _getFiles(dir)) { - ents.push(f) + } else if ( + parseResult1.type == ParseResultType.Reserved && + parseResult2.type == ParseResultType.Reserved + ) { + if ( + parseResult1.hostname == "" && + parseResult1.hostname === parseResult2.hostname + ) + return true; + else { + // uncertain, fallback to exact hostname matching + return d1 == d2 ? true : false; } - return ents + } + } else { + return false; } + } - export async function fsExists(path: string): Promise { - try { await access(path) } - catch (err) { - if (err.code == 'ENOENT') - return false - else return true - } - return true + async function* _getFiles(dir: string) { + const dirents = await readdir(dir, { withFileTypes: true }); + for (const dirent of dirents) { + const res = resolve(dir, dirent.name); + if (dirent.isDirectory()) { + yield* _getFiles(res); + } else { + yield res; + } } + } - export type PlayerPosition = { - x: number, - y: number, - z: number, - yaw: number, - pitch: number + export async function recursiveFileSearch(dir: string): Promise { + const ents = []; + for await (const f of _getFiles(dir)) { + ents.push(f); } + return ents; + } - export type PositionPacket = { - x: number, - y: number, - z: number, - yaw: number, - pitch: number, - flags: number + export async function fsExists(path: string): Promise { + try { + await access(path); + } catch (err) { + if (err.code == "ENOENT") return false; + else return true; } + return true; + } - export function generatePositionPacket(currentPos: PlayerPosition, newPos: PositionPacket): PositionPacket { - const DEFAULT_RELATIVITY = 0x01 // relative to X-axis - const newPosPacket = { - x: newPos.x - (currentPos.x * 2), - y: newPos.y, - z: newPos.z, - yaw: newPos.yaw, - pitch: newPos.pitch, - flags: DEFAULT_RELATIVITY - } - return newPosPacket - } -} \ No newline at end of file + export type PlayerPosition = { + x: number; + y: number; + z: number; + yaw: number; + pitch: number; + }; + + export type PositionPacket = { + x: number; + y: number; + z: number; + yaw: number; + pitch: number; + flags: number; + }; + + export function generatePositionPacket( + currentPos: PlayerPosition, + newPos: PositionPacket + ): PositionPacket { + const DEFAULT_RELATIVITY = 0x01; // relative to X-axis + const newPosPacket = { + x: newPos.x - currentPos.x * 2, + y: newPos.y, + z: newPos.z, + yaw: newPos.yaw, + pitch: newPos.pitch, + flags: DEFAULT_RELATIVITY, + }; + return newPosPacket; + } +} diff --git a/src/proxy/packets/CSLoginPacket.ts b/src/proxy/packets/CSLoginPacket.ts index b7ec60c..d97208a 100644 --- a/src/proxy/packets/CSLoginPacket.ts +++ b/src/proxy/packets/CSLoginPacket.ts @@ -4,46 +4,50 @@ import Packet from "../Packet.js"; import { MineProtocol } from "../Protocol.js"; export default class CSLoginPacket implements Packet { - packetId: Enums.PacketId = Enums.PacketId.CSLoginPacket - type: "packet" = "packet" - boundTo = Enums.PacketBounds.S - sentAfterHandshake = false - - networkVersion = NETWORK_VERSION - gameVersion = VANILLA_PROTOCOL_VERSION - brand: string - version: string - username: string - - private _getMagicSeq(): Buffer { - return Buffer.concat([ - [0x02, 0x00, 0x02, 0x00, 0x02, 0x00], - [this.networkVersion], - [0x00, 0x01, 0x00], - [this.gameVersion] - ].map(arr => Buffer.from(arr))) - } + packetId: Enums.PacketId = Enums.PacketId.CSLoginPacket; + type: "packet" = "packet"; + boundTo = Enums.PacketBounds.S; + sentAfterHandshake = false; - public serialize() { - return Buffer.concat( - [[Enums.PacketId.CSLoginPacket], - this._getMagicSeq(), - MineProtocol.writeString(this.brand), - MineProtocol.writeString(this.version), - [0x00], - MineProtocol.writeString(this.username)] - .map(arr => arr instanceof Uint8Array ? arr : Buffer.from(arr)) - ) - } - public deserialize(packet: Buffer) { - if (packet[0] != this.packetId) throw TypeError("Invalid packet ID detected!") - packet = packet.subarray(1 + this._getMagicSeq().length) - const brand = MineProtocol.readString(packet), - version = MineProtocol.readString(brand.newBuffer), - username = MineProtocol.readString(version.newBuffer, 1) - this.brand = brand.value - this.version = version.value - this.username = username.value - return this - } -} \ No newline at end of file + networkVersion = NETWORK_VERSION; + gameVersion = VANILLA_PROTOCOL_VERSION; + brand: string; + version: string; + username: string; + + private _getMagicSeq(): Buffer { + return Buffer.concat( + [ + [0x02, 0x00, 0x02, 0x00, 0x02, 0x00], + [this.networkVersion], + [0x00, 0x01, 0x00], + [this.gameVersion], + ].map((arr) => Buffer.from(arr)) + ); + } + + public serialize() { + return Buffer.concat( + [ + [Enums.PacketId.CSLoginPacket], + this._getMagicSeq(), + MineProtocol.writeString(this.brand), + MineProtocol.writeString(this.version), + [0x00], + MineProtocol.writeString(this.username), + ].map((arr) => (arr instanceof Uint8Array ? arr : Buffer.from(arr))) + ); + } + public deserialize(packet: Buffer) { + if (packet[0] != this.packetId) + throw TypeError("Invalid packet ID detected!"); + packet = packet.subarray(1 + this._getMagicSeq().length); + const brand = MineProtocol.readString(packet), + version = MineProtocol.readString(brand.newBuffer), + username = MineProtocol.readString(version.newBuffer, 1); + this.brand = brand.value; + this.version = version.value; + this.username = username.value; + return this; + } +} diff --git a/src/proxy/packets/CSReadyPacket.ts b/src/proxy/packets/CSReadyPacket.ts index 5dae3a6..a33ee74 100644 --- a/src/proxy/packets/CSReadyPacket.ts +++ b/src/proxy/packets/CSReadyPacket.ts @@ -2,16 +2,16 @@ import { Enums } from "../Enums.js"; import Packet from "../Packet.js"; export class CSReadyPacket implements Packet { - packetId: Enums.PacketId = Enums.PacketId.CSReadyPacket - type: "packet" = "packet" - boundTo = Enums.PacketBounds.S - sentAfterHandshake = false + packetId: Enums.PacketId = Enums.PacketId.CSReadyPacket; + type: "packet" = "packet"; + boundTo = Enums.PacketBounds.S; + sentAfterHandshake = false; - public serialize() { - return Buffer.from([this.packetId]) - } + public serialize() { + return Buffer.from([this.packetId]); + } - public deserialize(packet: Buffer) { - return this - } -} \ No newline at end of file + public deserialize(packet: Buffer) { + return this; + } +} diff --git a/src/proxy/packets/CSSetSkinPacket.ts b/src/proxy/packets/CSSetSkinPacket.ts index 6348da0..8670cf8 100644 --- a/src/proxy/packets/CSSetSkinPacket.ts +++ b/src/proxy/packets/CSSetSkinPacket.ts @@ -4,57 +4,67 @@ import Packet from "../Packet.js"; import { MineProtocol } from "../Protocol.js"; export class CSSetSkinPacket implements Packet { - packetId: Enums.PacketId = Enums.PacketId.CSSetSkinPacket - type: "packet" = "packet" - boundTo: Enums.PacketBounds = Enums.PacketBounds.S - sentAfterHandshake: boolean = false + packetId: Enums.PacketId = Enums.PacketId.CSSetSkinPacket; + type: "packet" = "packet"; + boundTo: Enums.PacketBounds = Enums.PacketBounds.S; + sentAfterHandshake: boolean = false; - version: string | 'skin_v1' = 'skin_v1' - skinType: Omit - skinDimensions?: number - skin?: Buffer - skinId?: number + version: string | "skin_v1" = "skin_v1"; + skinType: Omit; + skinDimensions?: number; + skin?: Buffer; + skinId?: number; - public serialize() { - if (this.skinType == Enums.SkinType.BUILTIN) { - return Buffer.concat([ - Buffer.from([this.packetId]), - MineProtocol.writeString(this.version), - MineProtocol.writeVarInt(this.skinDimensions), - this.skin - ]) - } else { - return Buffer.concat([ - [this.packetId], - MineProtocol.writeString(this.version), - Constants.MAGIC_ENDING_CLIENT_UPLOAD_SKIN_BUILTIN, - [this.skinId] - ].map(arr => arr instanceof Uint8Array ? arr : Buffer.from(arr))) - } + public serialize() { + if (this.skinType == Enums.SkinType.BUILTIN) { + return Buffer.concat([ + Buffer.from([this.packetId]), + MineProtocol.writeString(this.version), + MineProtocol.writeVarInt(this.skinDimensions), + this.skin, + ]); + } else { + return Buffer.concat( + [ + [this.packetId], + MineProtocol.writeString(this.version), + Constants.MAGIC_ENDING_CLIENT_UPLOAD_SKIN_BUILTIN, + [this.skinId], + ].map((arr) => (arr instanceof Uint8Array ? arr : Buffer.from(arr))) + ); } + } - public deserialize(packet: Buffer) { - packet = packet.subarray(1) - const version = MineProtocol.readString(packet) - let skinType: Enums.SkinType - if (!Constants.MAGIC_ENDING_CLIENT_UPLOAD_SKIN_BUILTIN.some((byte, index) => byte !== version.newBuffer[index])) { - // built in - skinType = Enums.SkinType.BUILTIN - const id = MineProtocol.readVarInt(version.newBuffer.subarray(Constants.MAGIC_ENDING_CLIENT_UPLOAD_SKIN_BUILTIN.length)) - this.version = version.value - this.skinType = skinType - this.skinId = id.value - return this - } else { - // custom - skinType = Enums.SkinType.CUSTOM - const dimensions = MineProtocol.readVarInt(version.newBuffer), - skin = dimensions.newBuffer.subarray(3).subarray(0, 16384) - this.version = version.value - this.skinType = skinType - this.skinDimensions = dimensions.value - this.skin = skin - return this - } + public deserialize(packet: Buffer) { + packet = packet.subarray(1); + const version = MineProtocol.readString(packet); + let skinType: Enums.SkinType; + if ( + !Constants.MAGIC_ENDING_CLIENT_UPLOAD_SKIN_BUILTIN.some( + (byte, index) => byte !== version.newBuffer[index] + ) + ) { + // built in + skinType = Enums.SkinType.BUILTIN; + const id = MineProtocol.readVarInt( + version.newBuffer.subarray( + Constants.MAGIC_ENDING_CLIENT_UPLOAD_SKIN_BUILTIN.length + ) + ); + this.version = version.value; + this.skinType = skinType; + this.skinId = id.value; + return this; + } else { + // custom + skinType = Enums.SkinType.CUSTOM; + const dimensions = MineProtocol.readVarInt(version.newBuffer), + skin = dimensions.newBuffer.subarray(3).subarray(0, 16384); + this.version = version.value; + this.skinType = skinType; + this.skinDimensions = dimensions.value; + this.skin = skin; + return this; } -} \ No newline at end of file + } +} diff --git a/src/proxy/packets/CSUsernamePacket.ts b/src/proxy/packets/CSUsernamePacket.ts index a3eeaa8..9458be5 100644 --- a/src/proxy/packets/CSUsernamePacket.ts +++ b/src/proxy/packets/CSUsernamePacket.ts @@ -3,27 +3,29 @@ import Packet from "../Packet.js"; import { MineProtocol } from "../Protocol.js"; export class CSUsernamePacket implements Packet { - packetId: Enums.PacketId = Enums.PacketId.CSUsernamePacket - type: "packet" = "packet" - boundTo = Enums.PacketBounds.S - sentAfterHandshake = false + packetId: Enums.PacketId = Enums.PacketId.CSUsernamePacket; + type: "packet" = "packet"; + boundTo = Enums.PacketBounds.S; + sentAfterHandshake = false; - username: string - static readonly DEFAULT = "default" + username: string; + static readonly DEFAULT = "default"; - public serialize() { - return Buffer.concat([ - [this.packetId], - MineProtocol.writeString(this.username), - MineProtocol.writeString(CSUsernamePacket.DEFAULT), - [0x0] - ].map(arr => arr instanceof Uint8Array ? arr : Buffer.from(arr))) - } + public serialize() { + return Buffer.concat( + [ + [this.packetId], + MineProtocol.writeString(this.username), + MineProtocol.writeString(CSUsernamePacket.DEFAULT), + [0x0], + ].map((arr) => (arr instanceof Uint8Array ? arr : Buffer.from(arr))) + ); + } - public deserialize(packet: Buffer) { - packet = packet.subarray(1) - const username = MineProtocol.readString(packet) - this.username = username.value - return this - } -} \ No newline at end of file + public deserialize(packet: Buffer) { + packet = packet.subarray(1); + const username = MineProtocol.readString(packet); + this.username = username.value; + return this; + } +} diff --git a/src/proxy/packets/SCDisconnectPacket.ts b/src/proxy/packets/SCDisconnectPacket.ts index 7429269..7f5f1d6 100644 --- a/src/proxy/packets/SCDisconnectPacket.ts +++ b/src/proxy/packets/SCDisconnectPacket.ts @@ -4,27 +4,34 @@ import Packet from "../Packet.js"; import { MineProtocol } from "../Protocol.js"; export default class SCDisconnectPacket implements Packet { - packetId: Enums.PacketId = Enums.PacketId.SCDisconnectPacket - type: "packet" = "packet" - boundTo = Enums.PacketBounds.C - sentAfterHandshake = false - static readonly REASON = 0x8 + packetId: Enums.PacketId = Enums.PacketId.SCDisconnectPacket; + type: "packet" = "packet"; + boundTo = Enums.PacketBounds.C; + sentAfterHandshake = false; + static readonly REASON = 0x8; - reason: string | Chat.Chat + reason: string | Chat.Chat; - public serialize() { - const msg = (typeof this.reason == 'string' ? this.reason : Chat.chatToPlainString(this.reason)) - return Buffer.concat([ - [0xff], - MineProtocol.writeVarInt(SCDisconnectPacket.REASON), - MineProtocol.writeString(" " + msg + " ") - ].map(arr => arr instanceof Uint8Array ? arr : Buffer.from(arr))) - } - public deserialize(packet: Buffer) { - if (packet[0] != this.packetId) throw new Error("Invalid packet ID!") - packet = packet.subarray(1 + MineProtocol.writeVarInt(SCDisconnectPacket.REASON).length) - const reason = MineProtocol.readString(packet) - this.reason = reason.value - return this - } -} \ No newline at end of file + public serialize() { + const msg = + typeof this.reason == "string" + ? this.reason + : Chat.chatToPlainString(this.reason); + return Buffer.concat( + [ + [0xff], + MineProtocol.writeVarInt(SCDisconnectPacket.REASON), + MineProtocol.writeString(" " + msg + " "), + ].map((arr) => (arr instanceof Uint8Array ? arr : Buffer.from(arr))) + ); + } + public deserialize(packet: Buffer) { + if (packet[0] != this.packetId) throw new Error("Invalid packet ID!"); + packet = packet.subarray( + 1 + MineProtocol.writeVarInt(SCDisconnectPacket.REASON).length + ); + const reason = MineProtocol.readString(packet); + this.reason = reason.value; + return this; + } +} diff --git a/src/proxy/packets/SCIdentifyPacket.ts b/src/proxy/packets/SCIdentifyPacket.ts index 6ede8f8..af68019 100644 --- a/src/proxy/packets/SCIdentifyPacket.ts +++ b/src/proxy/packets/SCIdentifyPacket.ts @@ -1,40 +1,48 @@ -import { NETWORK_VERSION, PROXY_BRANDING, PROXY_VERSION, VANILLA_PROTOCOL_VERSION } from "../../meta.js"; +import { + NETWORK_VERSION, + PROXY_BRANDING, + PROXY_VERSION, + VANILLA_PROTOCOL_VERSION, +} from "../../meta.js"; import { Enums } from "../Enums.js"; import Packet from "../Packet.js"; import { MineProtocol } from "../Protocol.js"; export default class SCIdentifyPacket implements Packet { - packetId: Enums.PacketId = Enums.PacketId.SCIdentifyPacket - type: "packet" = "packet" - boundTo = Enums.PacketBounds.C - sentAfterHandshake = false + packetId: Enums.PacketId = Enums.PacketId.SCIdentifyPacket; + type: "packet" = "packet"; + boundTo = Enums.PacketBounds.C; + sentAfterHandshake = false; - protocolVer = NETWORK_VERSION - gameVersion = VANILLA_PROTOCOL_VERSION - branding = PROXY_BRANDING - version = PROXY_VERSION + protocolVer = NETWORK_VERSION; + gameVersion = VANILLA_PROTOCOL_VERSION; + branding = PROXY_BRANDING; + version = PROXY_VERSION; - public serialize() { - return Buffer.concat([ - [0x02], - MineProtocol.writeShort(this.protocolVer), - MineProtocol.writeShort(this.gameVersion), - MineProtocol.writeString(this.branding), - MineProtocol.writeString(this.version), - [0x00, 0x00, 0x00] - ].map(arr => arr instanceof Uint8Array ? arr : Buffer.from(arr))) - } + public serialize() { + return Buffer.concat( + [ + [0x02], + MineProtocol.writeShort(this.protocolVer), + MineProtocol.writeShort(this.gameVersion), + MineProtocol.writeString(this.branding), + MineProtocol.writeString(this.version), + [0x00, 0x00, 0x00], + ].map((arr) => (arr instanceof Uint8Array ? arr : Buffer.from(arr))) + ); + } - public deserialize(packet: Buffer) { - if (packet[0] != this.packetId) throw TypeError("Invalid packet ID detected!") - packet = packet.subarray(1) - const protoVer = MineProtocol.readShort(packet), - gameVer = MineProtocol.readShort(protoVer.newBuffer), - branding = MineProtocol.readString(gameVer.newBuffer), - version = MineProtocol.readString(branding.newBuffer) - this.gameVersion = gameVer.value - this.branding = branding.value - this.version = version.value - return this - } -} \ No newline at end of file + public deserialize(packet: Buffer) { + if (packet[0] != this.packetId) + throw TypeError("Invalid packet ID detected!"); + packet = packet.subarray(1); + const protoVer = MineProtocol.readShort(packet), + gameVer = MineProtocol.readShort(protoVer.newBuffer), + branding = MineProtocol.readString(gameVer.newBuffer), + version = MineProtocol.readString(branding.newBuffer); + this.gameVersion = gameVer.value; + this.branding = branding.value; + this.version = version.value; + return this; + } +} diff --git a/src/proxy/packets/SCReadyPacket.ts b/src/proxy/packets/SCReadyPacket.ts index 815665b..970b94b 100644 --- a/src/proxy/packets/SCReadyPacket.ts +++ b/src/proxy/packets/SCReadyPacket.ts @@ -2,16 +2,16 @@ import { Enums } from "../Enums.js"; import Packet from "../Packet.js"; export class SCReadyPacket implements Packet { - packetId: Enums.PacketId = Enums.PacketId.SCReadyPacket - type: "packet" = "packet" - boundTo = Enums.PacketBounds.C - sentAfterHandshake = false + packetId: Enums.PacketId = Enums.PacketId.SCReadyPacket; + type: "packet" = "packet"; + boundTo = Enums.PacketBounds.C; + sentAfterHandshake = false; - public serialize() { - return Buffer.from([this.packetId]) - } + public serialize() { + return Buffer.from([this.packetId]); + } - public deserialize(packet: Buffer) { - return this - } -} \ No newline at end of file + public deserialize(packet: Buffer) { + return this; + } +} diff --git a/src/proxy/packets/SCSyncUuidPacket.ts b/src/proxy/packets/SCSyncUuidPacket.ts index ecdc804..34d34c4 100644 --- a/src/proxy/packets/SCSyncUuidPacket.ts +++ b/src/proxy/packets/SCSyncUuidPacket.ts @@ -4,28 +4,30 @@ import { MineProtocol } from "../Protocol.js"; import { Util } from "../Util.js"; export class SCSyncUuidPacket implements Packet { - packetId: Enums.PacketId = Enums.PacketId.SCSyncUuidPacket - type: "packet" = "packet" - boundTo = Enums.PacketBounds.C - sentAfterHandshake = false + packetId: Enums.PacketId = Enums.PacketId.SCSyncUuidPacket; + type: "packet" = "packet"; + boundTo = Enums.PacketBounds.C; + sentAfterHandshake = false; - username: string - uuid: string + username: string; + uuid: string; - public serialize() { - return Buffer.concat([ - [this.packetId], - MineProtocol.writeString(this.username), - Util.uuidStringToBuffer(this.uuid) - ].map(arr => arr instanceof Uint8Array ? arr : Buffer.from(arr))) - } - - public deserialize(packet: Buffer) { - packet = packet.subarray(1) - const username = MineProtocol.readString(packet), - uuid = username.newBuffer.subarray(0, 15) - this.username = username.value - this.uuid = Util.uuidBufferToString(uuid) - return this - } -} \ No newline at end of file + public serialize() { + return Buffer.concat( + [ + [this.packetId], + MineProtocol.writeString(this.username), + Util.uuidStringToBuffer(this.uuid), + ].map((arr) => (arr instanceof Uint8Array ? arr : Buffer.from(arr))) + ); + } + + public deserialize(packet: Buffer) { + packet = packet.subarray(1); + const username = MineProtocol.readString(packet), + uuid = username.newBuffer.subarray(0, 15); + this.username = username.value; + this.uuid = Util.uuidBufferToString(uuid); + return this; + } +} diff --git a/src/proxy/packets/channel/CSChannelMessage.ts b/src/proxy/packets/channel/CSChannelMessage.ts index 80c8c1e..e553669 100644 --- a/src/proxy/packets/channel/CSChannelMessage.ts +++ b/src/proxy/packets/channel/CSChannelMessage.ts @@ -3,29 +3,30 @@ import Packet from "../../Packet.js"; import { MineProtocol } from "../../Protocol.js"; export class CSChannelMessagePacket implements Packet { - packetId: Enums.PacketId = Enums.PacketId.CSChannelMessagePacket - type: "packet" = "packet" - boundTo = Enums.PacketBounds.S - sentAfterHandshake = true + packetId: Enums.PacketId = Enums.PacketId.CSChannelMessagePacket; + type: "packet" = "packet"; + boundTo = Enums.PacketBounds.S; + sentAfterHandshake = true; - readonly messageType: Enums.ChannelMessageType = Enums.ChannelMessageType.CLIENT - channel: string - data: Buffer + readonly messageType: Enums.ChannelMessageType = + Enums.ChannelMessageType.CLIENT; + channel: string; + data: Buffer; - public serialize() { - return Buffer.concat([ - [this.packetId], - MineProtocol.writeString(this.channel), - this.data - ].map(arr => arr instanceof Uint8Array ? arr : Buffer.from(arr))) - } + public serialize() { + return Buffer.concat( + [[this.packetId], MineProtocol.writeString(this.channel), this.data].map( + (arr) => (arr instanceof Uint8Array ? arr : Buffer.from(arr)) + ) + ); + } - public deserialize(packet: Buffer) { - packet = packet.subarray(1) - const channel = MineProtocol.readString(packet), - data = channel.newBuffer - this.channel = channel.value - this.data = data - return this - } -} \ No newline at end of file + public deserialize(packet: Buffer) { + packet = packet.subarray(1); + const channel = MineProtocol.readString(packet), + data = channel.newBuffer; + this.channel = channel.value; + this.data = data; + return this; + } +} diff --git a/src/proxy/packets/channel/SCChannelMessage.ts b/src/proxy/packets/channel/SCChannelMessage.ts index b606536..58e3475 100644 --- a/src/proxy/packets/channel/SCChannelMessage.ts +++ b/src/proxy/packets/channel/SCChannelMessage.ts @@ -3,29 +3,30 @@ import Packet from "../../Packet.js"; import { MineProtocol } from "../../Protocol.js"; export class SCChannelMessagePacket implements Packet { - packetId: Enums.PacketId = Enums.PacketId.SCChannelMessagePacket - type: "packet" = "packet" - boundTo = Enums.PacketBounds.C - sentAfterHandshake = true - - readonly messageType: Enums.ChannelMessageType = Enums.ChannelMessageType.SERVER - channel: string - data: Buffer + packetId: Enums.PacketId = Enums.PacketId.SCChannelMessagePacket; + type: "packet" = "packet"; + boundTo = Enums.PacketBounds.C; + sentAfterHandshake = true; - public serialize() { - return Buffer.concat([ - [this.packetId], - MineProtocol.writeString(this.channel), - this.data - ].map(arr => arr instanceof Uint8Array ? arr : Buffer.from(arr))) - } + readonly messageType: Enums.ChannelMessageType = + Enums.ChannelMessageType.SERVER; + channel: string; + data: Buffer; - public deserialize(packet: Buffer) { - packet = packet.subarray(1) - const channel = MineProtocol.readString(packet), - data = channel.newBuffer - this.channel = channel.value - this.data = data - return this - } -} \ No newline at end of file + public serialize() { + return Buffer.concat( + [[this.packetId], MineProtocol.writeString(this.channel), this.data].map( + (arr) => (arr instanceof Uint8Array ? arr : Buffer.from(arr)) + ) + ); + } + + public deserialize(packet: Buffer) { + packet = packet.subarray(1); + const channel = MineProtocol.readString(packet), + data = channel.newBuffer; + this.channel = channel.value; + this.data = data; + return this; + } +} diff --git a/src/proxy/pluginLoader/PluginLoaderTypes.ts b/src/proxy/pluginLoader/PluginLoaderTypes.ts index 5722488..a289102 100644 --- a/src/proxy/pluginLoader/PluginLoaderTypes.ts +++ b/src/proxy/pluginLoader/PluginLoaderTypes.ts @@ -1,68 +1,68 @@ export namespace PluginLoaderTypes { - /** - * ## SemVer - * Abstract typing to define a semantic version string. Refer to https://semver.org/ for more details. - */ - export type SemVer = string + /** + * ## SemVer + * Abstract typing to define a semantic version string. Refer to https://semver.org/ for more details. + */ + export type SemVer = string; - /** - * ## SemVerReq - * Abstract typing to define a semantic version requirement. Refer to https://semver.org/ for more details. - */ - export type SemVerReq = string + /** + * ## SemVerReq + * Abstract typing to define a semantic version requirement. Refer to https://semver.org/ for more details. + */ + export type SemVerReq = string; - /** - * ## PluginMetadata - * Data structure of the JSON plugin metadata stored in `metadata.json`. Below is an example plugin metadata object. - * @example - * { - * name: "ExamplePlugin", - * version: "1.0.0" - * } - * - * @property {string} name - The name of the plugin. Spaces are allowed, and this will be shown to the end user. - * @property {string} id - The internal ID of the plugin. Spaces are not allowed, and any ID conflicts will cause the proxy to not load. - * @property {PluginLoaderTypes.SemVer} version - The version of the plugin. Must follow SemVer guidelines. - * @property {string} entry_point - Reference to the entry point JS file of the plugin. Is relative to the file of the `metadata.json`. - * @property {PluginLoaderTypes.PluginRequirement[]} requirements - The plugin requirement(s) of the plugin. Proxy will not load if any requirement cannot be satisfied. - * @property {string[]} load_after - Defines what plugin(s) to be loaded first before this plugin is loaded. - */ - export type PluginMetadata = { - name: string, - id: string, - version: SemVer, - entry_point: string, - requirements: PluginRequirement[], - incompatibilities: PluginRequirement[], - load_after: string[] - } + /** + * ## PluginMetadata + * Data structure of the JSON plugin metadata stored in `metadata.json`. Below is an example plugin metadata object. + * @example + * { + * name: "ExamplePlugin", + * version: "1.0.0" + * } + * + * @property {string} name - The name of the plugin. Spaces are allowed, and this will be shown to the end user. + * @property {string} id - The internal ID of the plugin. Spaces are not allowed, and any ID conflicts will cause the proxy to not load. + * @property {PluginLoaderTypes.SemVer} version - The version of the plugin. Must follow SemVer guidelines. + * @property {string} entry_point - Reference to the entry point JS file of the plugin. Is relative to the file of the `metadata.json`. + * @property {PluginLoaderTypes.PluginRequirement[]} requirements - The plugin requirement(s) of the plugin. Proxy will not load if any requirement cannot be satisfied. + * @property {string[]} load_after - Defines what plugin(s) to be loaded first before this plugin is loaded. + */ + export type PluginMetadata = { + name: string; + id: string; + version: SemVer; + entry_point: string; + requirements: PluginRequirement[]; + incompatibilities: PluginRequirement[]; + load_after: string[]; + }; - /** - * ## PluginMetadataPathed - * Internal typing. Provides a path to the plugin metadata file. - */ - export type PluginMetadataPathed = PluginMetadata & { path: string } + /** + * ## PluginMetadataPathed + * Internal typing. Provides a path to the plugin metadata file. + */ + export type PluginMetadataPathed = PluginMetadata & { path: string }; - /** - * ## PluginLoadOrder - * Internal typing. Provides a loading order for plugin loading. - */ - export type PluginLoadOrder = string[] + /** + * ## PluginLoadOrder + * Internal typing. Provides a loading order for plugin loading. + */ + export type PluginLoadOrder = string[]; - /** - * ## PluginRequirement - * A plugin requirement used to define dependencies for a specific plugin. - * Semantic versions may be used for the attribute `version`, and you can - * use `eaglerproxy` to define a requirement for the proxy version. - * @example - * { - * id: "eaglerproxy" - * } - * @property {string} id - The ID of the plugin to be used as a requirement. - * @property {PluginLoaderTypes.SemVerReq} version - The SemVer requirement for the requirement. - */ - export type PluginRequirement = { - id: string, - version: SemVerReq | 'any' - } -} \ No newline at end of file + /** + * ## PluginRequirement + * A plugin requirement used to define dependencies for a specific plugin. + * Semantic versions may be used for the attribute `version`, and you can + * use `eaglerproxy` to define a requirement for the proxy version. + * @example + * { + * id: "eaglerproxy" + * } + * @property {string} id - The ID of the plugin to be used as a requirement. + * @property {PluginLoaderTypes.SemVerReq} version - The SemVer requirement for the requirement. + */ + export type PluginRequirement = { + id: string; + version: SemVerReq | "any"; + }; +} diff --git a/src/proxy/pluginLoader/PluginManager.ts b/src/proxy/pluginLoader/PluginManager.ts index ce58a30..81c6bc9 100644 --- a/src/proxy/pluginLoader/PluginManager.ts +++ b/src/proxy/pluginLoader/PluginManager.ts @@ -1,7 +1,7 @@ import { Stats } from "fs"; import * as fs from "fs/promises"; -import * as pathUtil from "path" -import * as semver from "semver" +import * as pathUtil from "path"; +import * as semver from "semver"; import { EventEmitter } from "events"; import { pathToFileURL } from "url"; import { Logger } from "../../logger.js"; @@ -10,7 +10,7 @@ import { Proxy } from "../Proxy.js"; import { Util } from "../Util.js"; import { PluginLoaderTypes } from "./PluginLoaderTypes.js"; import { Enums } from "../Enums.js"; -import { Chat } from "../Chat.js" +import { Chat } from "../Chat.js"; import { Constants } from "../Constants.js"; import { Motd } from "../Motd.js"; import { Player } from "../Player.js"; @@ -19,248 +19,401 @@ import { EaglerSkins } from "../skins/EaglerSkins.js"; import { BungeeUtil } from "../BungeeUtil.js"; export class PluginManager extends EventEmitter { - public plugins: Map - public proxy: Proxy + public plugins: Map< + string, + { exports: any; metadata: PluginLoaderTypes.PluginMetadataPathed } + >; + public proxy: Proxy; - public Logger: typeof Logger = Logger - public Enums: typeof Enums = Enums - public Chat: typeof Chat = Chat - public Constants: typeof Constants = Constants - public Motd: typeof Motd = Motd - public Player: typeof Player = Player - public MineProtocol: typeof MineProtocol = MineProtocol - public EaglerSkins: typeof EaglerSkins = EaglerSkins - public Util: typeof Util = Util - public BungeeUtil: typeof BungeeUtil = BungeeUtil + public Logger: typeof Logger = Logger; + public Enums: typeof Enums = Enums; + public Chat: typeof Chat = Chat; + public Constants: typeof Constants = Constants; + public Motd: typeof Motd = Motd; + public Player: typeof Player = Player; + public MineProtocol: typeof MineProtocol = MineProtocol; + public EaglerSkins: typeof EaglerSkins = EaglerSkins; + public Util: typeof Util = Util; + public BungeeUtil: typeof BungeeUtil = BungeeUtil; - private _loadDir: string - private _logger: Logger + private _loadDir: string; + private _logger: Logger; - constructor(loadDir: string) { - super() - this.setMaxListeners(0) - this._loadDir = loadDir - this.plugins = new Map() - this.Logger = Logger - this._logger = new this.Logger('PluginManager') + constructor(loadDir: string) { + super(); + this.setMaxListeners(0); + this._loadDir = loadDir; + this.plugins = new Map(); + this.Logger = Logger; + this._logger = new this.Logger("PluginManager"); + } + + public async loadPlugins() { + this._logger.info("Loading plugin metadata files..."); + const pluginMeta = await this._findPlugins(this._loadDir); + await this._validatePluginList(pluginMeta); + + let pluginsString = ""; + for (const [id, plugin] of pluginMeta) { + pluginsString += `${id}@${plugin.version}`; } + pluginsString = pluginsString.substring(0, pluginsString.length - 1); + this._logger.info(`Found ${pluginMeta.size} plugin(s): ${pluginsString}`); - public async loadPlugins() { - this._logger.info("Loading plugin metadata files...") - const pluginMeta = await this._findPlugins(this._loadDir) - await this._validatePluginList(pluginMeta) + this._logger.info(`Loading ${pluginMeta.size} plugin(s)...`); + const successLoadCount = await this._loadPlugins( + pluginMeta, + this._getLoadOrder(pluginMeta) + ); + this._logger.info(`Successfully loaded ${successLoadCount} plugin(s).`); + this.emit("pluginsFinishLoading", this); + } - let pluginsString = '' - for (const [id, plugin] of pluginMeta) { - pluginsString += `${id}@${plugin.version}` + private async _findPlugins( + dir: string + ): Promise> { + const ret: Map = new Map(); + const lsRes = (await Promise.all( + (await fs.readdir(dir)) + .filter((ent) => !ent.endsWith(".disabled")) + .map(async (res) => [ + pathUtil.join(dir, res), + await fs.stat(pathUtil.join(dir, res)), + ]) + )) as [string, Stats][]; + for (const [path, details] of lsRes) { + if (details.isFile()) { + if (path.endsWith(".jar")) { + this._logger.warn(`Non-EaglerProxy plugin found! (${path})`); + this._logger.warn( + `BungeeCord plugins are NOT supported! Only custom EaglerProxy plugins are allowed.` + ); + } else if (path.endsWith(".zip")) { + this._logger.warn(`.zip file found in plugin directory! (${path})`); + this._logger.warn( + `A .zip file was found in the plugins directory! Perhaps you forgot to unzip it?` + ); + } else + this._logger.debug(`Skipping file found in plugin folder: ${path}`); + } else { + const metadataPath = pathUtil.resolve( + pathUtil.join(path, "metadata.json") + ); + let metadata: PluginLoaderTypes.PluginMetadata; + try { + const file = await fs.readFile(metadataPath); + metadata = JSON.parse(file.toString()); + // do some type checking + if (typeof metadata.name != "string") + throw new TypeError( + ".name is either null or not of a string type!" + ); + if (typeof metadata.id != "string") + throw new TypeError( + ".id is either null or not of a string type!" + ); + if (/ /gm.test(metadata.id)) + throw new Error(`.id contains whitespace!`); + if (!semver.valid(metadata.version)) + throw new Error( + ".version is either null, not a string, or is not a valid SemVer!" + ); + if (typeof metadata.entry_point != "string") + throw new TypeError( + ".entry_point is either null or not a string!" + ); + if (!metadata.entry_point.endsWith(".js")) + throw new Error( + `.entry_point (${metadata.entry_point}) references a non-JavaScript file!` + ); + if ( + !(await Util.fsExists(pathUtil.resolve(path, metadata.entry_point))) + ) + throw new Error( + `.entry_point (${metadata.entry_point}) references a non-existent file!` + ); + if (metadata.requirements instanceof Array == false) + throw new TypeError( + ".requirements is either null or not an array!" + ); + for (const requirement of metadata.requirements as PluginLoaderTypes.PluginMetadata["requirements"]) { + if (typeof requirement != "object" || requirement == null) + throw new TypeError( + `.requirements[${( + metadata.requirements as any + ).indexOf(requirement)}] is either null or not an object!` + ); + if (typeof requirement.id != "string") + throw new TypeError( + `.requirements[${( + metadata.requirements as any + ).indexOf(requirement)}].id is either null or not a string!` + ); + if (/ /gm.test(requirement.id)) + throw new TypeError( + `.requirements[${( + metadata.requirements as any + ).indexOf(requirement)}].id contains whitespace!` + ); + if ( + semver.validRange(requirement.version) == null && + requirement.version != "any" + ) + throw new TypeError( + `.requirements[${( + metadata.requirements as any + ).indexOf( + requirement + )}].version is either null or not a valid SemVer!` + ); + } + if (metadata.load_after instanceof Array == false) + throw new TypeError( + ".load_after is either null or not an array!" + ); + for (const loadReq of metadata.load_after as string[]) { + if (typeof loadReq != "string") + throw new TypeError( + `.load_after[${(metadata.load_after as any).indexOf( + loadReq + )}] is either null, or not a valid ID!` + ); + if (/ /gm.test(loadReq)) + throw new TypeError( + `.load_after[${(metadata.load_after as any).indexOf( + loadReq + )}] contains whitespace!` + ); + } + if (metadata.incompatibilities instanceof Array == false) + throw new TypeError( + ".incompatibilities is either null or not an array!" + ); + for (const incompatibility of metadata.incompatibilities as PluginLoaderTypes.PluginMetadata["requirements"]) { + if (typeof incompatibility != "object" || incompatibility == null) + throw new TypeError( + `.incompatibilities[${( + metadata.load_after as any + ).indexOf(incompatibility)}] is either null or not an object!` + ); + if (typeof incompatibility.id != "string") + throw new TypeError( + `.incompatibilities[${( + metadata.load_after as any + ).indexOf(incompatibility)}].id is either null or not a string!` + ); + if (/ /gm.test(incompatibility.id)) + throw new TypeError( + `.incompatibilities[${( + metadata.load_after as any + ).indexOf(incompatibility)}].id contains whitespace!` + ); + if (semver.validRange(incompatibility.version) == null) + throw new TypeError( + `.incompatibilities[${( + metadata.load_after as any + ).indexOf( + incompatibility + )}].version is either null or not a valid SemVer!` + ); + } + if (ret.has(metadata.id)) + throw new Error( + `Duplicate plugin ID detected: ${metadata.id}. Are there duplicate plugins in the plugin folder?` + ); + ret.set(metadata.id, { + path: pathUtil.resolve(path), + ...metadata, + }); + } catch (err) { + this._logger.warn( + `Failed to load plugin metadata file at ${metadataPath}: ${ + err.stack ?? err + }` + ); + this._logger.warn("This plugin will skip loading due to an error."); } - pluginsString = pluginsString.substring(0, pluginsString.length - 1) - this._logger.info(`Found ${pluginMeta.size} plugin(s): ${pluginsString}`) - - this._logger.info(`Loading ${pluginMeta.size} plugin(s)...`) - const successLoadCount = await this._loadPlugins(pluginMeta, this._getLoadOrder(pluginMeta)) - this._logger.info(`Successfully loaded ${successLoadCount} plugin(s).`) - this.emit('pluginsFinishLoading', this) + } } + return ret; + } - private async _findPlugins(dir: string): Promise> { - const ret: Map = new Map() - const lsRes = await Promise.all((await fs.readdir(dir)) - .filter(ent => !ent.endsWith(".disabled")) - .map(async res => [pathUtil.join(dir, res), await fs.stat(pathUtil.join(dir, res))])) as [string, Stats][] - for (const [path, details] of lsRes) { - if (details.isFile()) { - if (path.endsWith('.jar')) { - this._logger.warn(`Non-EaglerProxy plugin found! (${path})`) - this._logger.warn(`BungeeCord plugins are NOT supported! Only custom EaglerProxy plugins are allowed.`) - } else if (path.endsWith('.zip')) { - this._logger.warn(`.zip file found in plugin directory! (${path})`) - this._logger.warn(`A .zip file was found in the plugins directory! Perhaps you forgot to unzip it?`) - } else this._logger.debug(`Skipping file found in plugin folder: ${path}`) - } else { - const metadataPath = pathUtil.resolve(pathUtil.join(path, 'metadata.json')) - let metadata: PluginLoaderTypes.PluginMetadata - try { - const file = await fs.readFile(metadataPath) - metadata = JSON.parse(file.toString()) - // do some type checking - if (typeof metadata.name != 'string') - throw new TypeError(".name is either null or not of a string type!") - if (typeof metadata.id != 'string') - throw new TypeError(".id is either null or not of a string type!") - if ((/ /gm).test(metadata.id)) - throw new Error(`.id contains whitespace!`) - if (!semver.valid(metadata.version)) - throw new Error(".version is either null, not a string, or is not a valid SemVer!") - if (typeof metadata.entry_point != 'string') - throw new TypeError(".entry_point is either null or not a string!") - if (!metadata.entry_point.endsWith('.js')) - throw new Error(`.entry_point (${metadata.entry_point}) references a non-JavaScript file!`) - if (!await Util.fsExists(pathUtil.resolve(path, metadata.entry_point))) - throw new Error(`.entry_point (${metadata.entry_point}) references a non-existent file!`) - if (metadata.requirements instanceof Array == false) - throw new TypeError(".requirements is either null or not an array!") - for (const requirement of metadata.requirements as PluginLoaderTypes.PluginMetadata["requirements"]) { - if (typeof requirement != 'object' || requirement == null) - throw new TypeError(`.requirements[${(metadata.requirements as any).indexOf(requirement)}] is either null or not an object!`) - if (typeof requirement.id != 'string') - throw new TypeError(`.requirements[${(metadata.requirements as any).indexOf(requirement)}].id is either null or not a string!`) - if (/ /gm.test(requirement.id)) - throw new TypeError(`.requirements[${(metadata.requirements as any).indexOf(requirement)}].id contains whitespace!`) - if (semver.validRange(requirement.version) == null && requirement.version != 'any') - throw new TypeError(`.requirements[${(metadata.requirements as any).indexOf(requirement)}].version is either null or not a valid SemVer!`) - } - if (metadata.load_after instanceof Array == false) - throw new TypeError(".load_after is either null or not an array!") - for (const loadReq of metadata.load_after as string[]) { - if (typeof loadReq != 'string') - throw new TypeError(`.load_after[${(metadata.load_after as any).indexOf(loadReq)}] is either null, or not a valid ID!`) - if (/ /gm.test(loadReq)) - throw new TypeError(`.load_after[${(metadata.load_after as any).indexOf(loadReq)}] contains whitespace!`) - } - if (metadata.incompatibilities instanceof Array == false) - throw new TypeError(".incompatibilities is either null or not an array!") - for (const incompatibility of metadata.incompatibilities as PluginLoaderTypes.PluginMetadata["requirements"]) { - if (typeof incompatibility != 'object' || incompatibility == null) - throw new TypeError(`.incompatibilities[${(metadata.load_after as any).indexOf(incompatibility)}] is either null or not an object!`) - if (typeof incompatibility.id != 'string') - throw new TypeError(`.incompatibilities[${(metadata.load_after as any).indexOf(incompatibility)}].id is either null or not a string!`) - if (/ /gm.test(incompatibility.id)) - throw new TypeError(`.incompatibilities[${(metadata.load_after as any).indexOf(incompatibility)}].id contains whitespace!`) - if (semver.validRange(incompatibility.version) == null) - throw new TypeError(`.incompatibilities[${(metadata.load_after as any).indexOf(incompatibility)}].version is either null or not a valid SemVer!`) - } - if (ret.has(metadata.id)) - throw new Error(`Duplicate plugin ID detected: ${metadata.id}. Are there duplicate plugins in the plugin folder?`) - ret.set(metadata.id, { - path: pathUtil.resolve(path), - ...metadata - }) - } catch (err) { - this._logger.warn(`Failed to load plugin metadata file at ${metadataPath}: ${err.stack ?? err}`) - this._logger.warn("This plugin will skip loading due to an error.") - } + private async _validatePluginList( + plugins: Map + ) { + for (const [id, plugin] of plugins) { + for (const req of plugin.requirements) { + if ( + !plugins.has(req.id) && + req.id != "eaglerproxy" && + !req.id.startsWith("module:") + ) { + this._logger.fatal( + `Error whilst loading plugins: Plugin ${plugin.name}@${plugin.version} requires plugin ${req.id}@${req.version}, but it is not found!` + ); + this._logger.fatal("Loading has halted due to missing dependencies."); + process.exit(1); + } + if (req.id == "eaglerproxy") { + if ( + !semver.satisfies(PROXY_VERSION, req.version) && + req.version != "any" + ) { + this._logger.fatal( + `Error whilst loading plugins: Plugin ${plugin.name}@${plugin.version} requires a proxy version that satisfies the SemVer requirement ${req.version}, but the proxy version is ${PROXY_VERSION} and does not satisfy the SemVer requirement!` + ); + this._logger.fatal("Loading has halted due to dependency issues."); + process.exit(1); + } + } else if (req.id.startsWith("module:")) { + const moduleName = req.id.replace("module:", ""); + try { + await import(moduleName); + } catch (err) { + if (err.code == "ERR_MODULE_NOT_FOUND") { + this._logger.fatal( + `Plugin ${plugin.name}@${ + plugin.version + } requires NPM module ${moduleName}${ + req.version == "any" ? "" : `@${req.version}` + } to be installed, but it is not found!` + ); + this._logger.fatal( + `Please install this missing package by running "npm install ${moduleName}${ + req.version == "any" ? "" : `@${req.version}` + }". If you're using yarn, run "yarn add ${moduleName}${ + req.version == "any" ? "" : `@${req.version}` + }" instead.` + ); + this._logger.fatal( + "Loading has halted due to dependency issues." + ); + process.exit(1); } + } + } else { + let dep = plugins.get(req.id); + if ( + !semver.satisfies(dep.version, req.version) && + req.version != "any" + ) { + this._logger.fatal( + `Error whilst loading plugins: Plugin ${plugin.name}@${plugin.version} requires a version of plugin ${dep.name} that satisfies the SemVer requirement ${req.version}, but the plugin ${dep.name}'s version is ${dep.version} and does not satisfy the SemVer requirement!` + ); + this._logger.fatal("Loading has halted due to dependency issues."); + process.exit(1); + } } - return ret + } + plugin.incompatibilities.forEach((incomp) => { + const plugin_incomp = plugins.get(incomp.id); + if (plugin_incomp) { + if (semver.satisfies(plugin_incomp.version, incomp.version)) { + this._logger.fatal( + `Error whilst loading plugins: Plugin incompatibility found! Plugin ${plugin.name}@${plugin.version} is incompatible with ${plugin_incomp.name}@${plugin_incomp.version} as it satisfies the SemVer requirement of ${incomp.version}!` + ); + this._logger.fatal( + "Loading has halted due to plugin incompatibility issues." + ); + process.exit(1); + } + } else if (incomp.id == "eaglerproxy") { + if (semver.satisfies(PROXY_VERSION, incomp.version)) { + this._logger.fatal( + `Error whilst loading plugins: Plugin ${plugin.name}@${plugin.version} is incompatible with proxy version ${PROXY_VERSION} as it satisfies the SemVer requirement of ${incomp.version}!` + ); + this._logger.fatal( + "Loading has halted due to plugin incompatibility issues." + ); + process.exit(1); + } + } + }); } + } - private async _validatePluginList(plugins: Map) { - for (const [id, plugin] of plugins) { - for (const req of plugin.requirements) { - if (!plugins.has(req.id) && req.id != 'eaglerproxy' && !req.id.startsWith("module:")) { - this._logger.fatal(`Error whilst loading plugins: Plugin ${plugin.name}@${plugin.version} requires plugin ${req.id}@${req.version}, but it is not found!`) - this._logger.fatal("Loading has halted due to missing dependencies.") - process.exit(1) - } - if (req.id == 'eaglerproxy') { - if (!semver.satisfies(PROXY_VERSION, req.version) && req.version != 'any') { - this._logger.fatal(`Error whilst loading plugins: Plugin ${plugin.name}@${plugin.version} requires a proxy version that satisfies the SemVer requirement ${req.version}, but the proxy version is ${PROXY_VERSION} and does not satisfy the SemVer requirement!`) - this._logger.fatal("Loading has halted due to dependency issues.") - process.exit(1) - } - } else if (req.id.startsWith("module:")) { - const moduleName = req.id.replace("module:", "") - try { await import(moduleName) } - catch (err) { - if (err.code == 'ERR_MODULE_NOT_FOUND') { - this._logger.fatal(`Plugin ${plugin.name}@${plugin.version} requires NPM module ${moduleName}${req.version == 'any' ? "" : `@${req.version}`} to be installed, but it is not found!`) - this._logger.fatal(`Please install this missing package by running "npm install ${moduleName}${req.version == 'any' ? "" : `@${req.version}`}". If you're using yarn, run "yarn add ${moduleName}${req.version == 'any' ? "" : `@${req.version}`}" instead.`) - this._logger.fatal("Loading has halted due to dependency issues.") - process.exit(1) - } - } - } else { - let dep = plugins.get(req.id) - if (!semver.satisfies(dep.version, req.version) && req.version != 'any') { - this._logger.fatal(`Error whilst loading plugins: Plugin ${plugin.name}@${plugin.version} requires a version of plugin ${dep.name} that satisfies the SemVer requirement ${req.version}, but the plugin ${dep.name}'s version is ${dep.version} and does not satisfy the SemVer requirement!`) - this._logger.fatal("Loading has halted due to dependency issues.") - process.exit(1) - } - } + private _getLoadOrder( + plugins: Map + ): PluginLoaderTypes.PluginLoadOrder { + let order = [], + lastPlugin: any; + plugins.forEach((v) => order.push(v.id)); + for (const [id, plugin] of plugins) { + const load = plugin.load_after.filter((dep) => plugins.has(dep)); + if (load.length < 0) { + order.push(plugin.id); + } else { + let mostLastIndexFittingDeps = -1; + for (const loadEnt of load) { + if (loadEnt != lastPlugin) { + if (order.indexOf(loadEnt) + 1 > mostLastIndexFittingDeps) { + mostLastIndexFittingDeps = order.indexOf(loadEnt) + 1; } - plugin.incompatibilities.forEach(incomp => { - const plugin_incomp = plugins.get(incomp.id) - if (plugin_incomp) { - if (semver.satisfies(plugin_incomp.version, incomp.version)) { - this._logger.fatal(`Error whilst loading plugins: Plugin incompatibility found! Plugin ${plugin.name}@${plugin.version} is incompatible with ${plugin_incomp.name}@${plugin_incomp.version} as it satisfies the SemVer requirement of ${incomp.version}!`) - this._logger.fatal("Loading has halted due to plugin incompatibility issues.") - process.exit(1) - } - } else if (incomp.id == 'eaglerproxy') { - if (semver.satisfies(PROXY_VERSION, incomp.version)) { - this._logger.fatal(`Error whilst loading plugins: Plugin ${plugin.name}@${plugin.version} is incompatible with proxy version ${PROXY_VERSION} as it satisfies the SemVer requirement of ${incomp.version}!`) - this._logger.fatal("Loading has halted due to plugin incompatibility issues.") - process.exit(1) - } - } - }) + } } + if (mostLastIndexFittingDeps != -1) { + order.splice(order.indexOf(plugin.id), 1); + order.splice(mostLastIndexFittingDeps - 1, 0, plugin.id); + lastPlugin = plugin; + } + } } + return order; + } - private _getLoadOrder(plugins: Map): PluginLoaderTypes.PluginLoadOrder { - let order = [], lastPlugin: any - plugins.forEach(v => order.push(v.id)) - for (const [id, plugin] of plugins) { - const load = plugin.load_after.filter(dep => plugins.has(dep)) - if (load.length < 0) { - order.push(plugin.id) - } else { - let mostLastIndexFittingDeps = -1 - for (const loadEnt of load) { - if (loadEnt != lastPlugin) { - if (order.indexOf(loadEnt) + 1 > mostLastIndexFittingDeps) { - mostLastIndexFittingDeps = order.indexOf(loadEnt) + 1 - } - } - } - if (mostLastIndexFittingDeps != -1) { - order.splice(order.indexOf(plugin.id), 1) - order.splice(mostLastIndexFittingDeps - 1, 0, plugin.id) - lastPlugin = plugin - } - } - } - return order - } - - private async _loadPlugins(plugins: Map, order: PluginLoaderTypes.PluginLoadOrder): Promise { - let successCount = 0 - for (const id of order) { - let pluginMeta = plugins.get(id) - try { - const imp = await import(process.platform == 'win32' ? pathToFileURL(pathUtil.join(pluginMeta.path, pluginMeta.entry_point)).toString() : pathUtil.join(pluginMeta.path, pluginMeta.entry_point)) - this.plugins.set(pluginMeta.id, { - exports: imp, - metadata: pluginMeta - }) - successCount++ - this.emit('pluginLoad', pluginMeta.id, imp) - } catch (err) { - this._logger.warn(`Failed to load plugin entry point for plugin (${pluginMeta.name}) at ${pluginMeta.path}: ${err.stack ?? err}`) - this._logger.warn("This plugin will skip loading due to an error.") - } - return successCount - } + private async _loadPlugins( + plugins: Map, + order: PluginLoaderTypes.PluginLoadOrder + ): Promise { + let successCount = 0; + for (const id of order) { + let pluginMeta = plugins.get(id); + try { + const imp = await import( + process.platform == "win32" + ? pathToFileURL( + pathUtil.join(pluginMeta.path, pluginMeta.entry_point) + ).toString() + : pathUtil.join(pluginMeta.path, pluginMeta.entry_point) + ); + this.plugins.set(pluginMeta.id, { + exports: imp, + metadata: pluginMeta, + }); + successCount++; + this.emit("pluginLoad", pluginMeta.id, imp); + } catch (err) { + this._logger.warn( + `Failed to load plugin entry point for plugin (${ + pluginMeta.name + }) at ${pluginMeta.path}: ${err.stack ?? err}` + ); + this._logger.warn("This plugin will skip loading due to an error."); + } + return successCount; } + } } interface PluginManagerEvents { - 'pluginLoad': (name: string, plugin: any) => void, - 'pluginsFinishLoading': (manager: PluginManager) => void, - 'proxyFinishLoading': (proxy: Proxy, manager: PluginManager) => void + pluginLoad: (name: string, plugin: any) => void; + pluginsFinishLoading: (manager: PluginManager) => void; + proxyFinishLoading: (proxy: Proxy, manager: PluginManager) => void; } export declare interface PluginManager { - on( - event: U, listener: PluginManagerEvents[U] - ): this; - - emit( - event: U, ...args: Parameters - ): boolean; + on( + event: U, + listener: PluginManagerEvents[U] + ): this; - once( - event: U, listener: PluginManagerEvents[U] - ): this; -} \ No newline at end of file + emit( + event: U, + ...args: Parameters + ): boolean; + + once( + event: U, + listener: PluginManagerEvents[U] + ): this; +} diff --git a/src/proxy/skins/SimpleRatelimit.ts b/src/proxy/skins/SimpleRatelimit.ts index f7fe279..3a41195 100644 --- a/src/proxy/skins/SimpleRatelimit.ts +++ b/src/proxy/skins/SimpleRatelimit.ts @@ -1,65 +1,75 @@ export default class SimpleRatelimit { - readonly requestCount: number - readonly resetInterval: number - private entries: Map + readonly requestCount: number; + readonly resetInterval: number; + private entries: Map; - constructor(requestCount: number, resetInterval: number) { - this.requestCount = requestCount - this.resetInterval = resetInterval - this.entries = new Map() - } + 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 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 + 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 { - 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 + 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) - } + private _onAdd(ratelimit: Ratelimit): NodeJS.Timer { + return setInterval(() => { + // TODO: work on + }, this.resetInterval); + } } export type Ratelimit = { - remainingRequests: number, - resetTime: Date, - timer?: NodeJS.Timer -} + 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) - } -} \ No newline at end of file + constructor(message: { toString: () => string }) { + super(message.toString()); + this.name = "RatelimitExceededError"; + Object.setPrototypeOf(this, RatelimitExceededError.prototype); + } +} diff --git a/tsconfig.json b/tsconfig.json index 9d02dc4..8973c8c 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -8,11 +8,6 @@ "outDir": "build", "resolveJsonModule": true }, - "include": [ - "src/**/*.json", - "src/**/*" - ], - "hooks": [ - "copy-files" - ] -} \ No newline at end of file + "include": ["src/**/*.json", "src/**/*"], + "hooks": ["copy-files"] +}