q13x-eaglerproxy/index.ts

95 lines
3.4 KiB
TypeScript
Raw Permalink Normal View History

2022-12-18 05:39:38 -08:00
import { readFileSync } from "fs";
import * as http from "http"
import * as https from "https"
import { WebSocketServer } from "ws";
import { BRANDING, config, NETWORK_VERSION, VERSION } from "./config.js";
2022-12-18 05:39:38 -08:00
import { handlePacket } from "./listener.js";
import { Logger } from "./logger.js";
import { disconnect, generateMOTDImage } from "./utils.js";
import { ChatColor, ProxiedPlayer, State } from "./types.js";
2022-12-18 05:39:38 -08:00
import { genUUID } from "./utils.js";
const logger = new Logger("EagXProxy")
const connectionLogger = new Logger("ConnectionHandler")
global.PROXY = {
brand: BRANDING,
version: VERSION,
MOTDVersion: NETWORK_VERSION,
serverName: config.name,
secure: false,
proxyUUID: genUUID(config.name),
MOTD: {
icon: config.motd.iconURL ? await generateMOTDImage(readFileSync(config.motd.iconURL)) : null,
2022-12-18 05:39:38 -08:00
motd: [config.motd.l1, config.motd.l2]
},
wsServer: null,
players: new Map(),
logger: logger,
config: config
}
let server: WebSocketServer
if (PROXY.config.security.enabled) {
logger.info(`Starting SECURE WebSocket proxy on port ${config.bindPort}...`)
2022-12-18 05:39:38 -08:00
if (process.env.REPL_SLUG) {
logger.warn("You appear to be running the proxy on Repl.it with encryption enabled. Please note that Repl.it by default provides encryption, and enabling encryption may or may not prevent you from connecting to the server.")
}
server = new WebSocketServer({
server: https.createServer({
key: readFileSync(config.security.key),
cert: readFileSync(config.security.cert)
}).listen(config.bindPort, config.bindHost)
2022-12-18 05:39:38 -08:00
})
} else {
logger.info(`Starting INSECURE WebSocket proxy on port ${config.bindPort}...`)
2022-12-18 05:39:38 -08:00
server = new WebSocketServer({
port: config.bindPort,
host: config.bindHost
2022-12-18 05:39:38 -08:00
})
}
PROXY.wsServer = server
server.addListener('connection', c => {
connectionLogger.debug(`[CONNECTION] New inbound WebSocket connection from [/${(c as any)._socket.remoteAddress}:${(c as any)._socket.remotePort}]. (${(c as any)._socket.remotePort} -> ${config.bindPort})`)
2022-12-18 05:39:38 -08:00
const plr = new ProxiedPlayer()
plr.ws = c
plr.ip = (c as any)._socket.remoteAddress
plr.remotePort = (c as any)._socket.remotePort
plr.state = State.PRE_HANDSHAKE
plr.queuedEaglerSkinPackets = []
2022-12-18 05:39:38 -08:00
c.on('message', msg => {
handlePacket(msg as Buffer, plr)
})
})
server.on('listening', () => {
logger.info(`Successfully started${config.security.enabled ? " [secure]" : ""} WebSocket proxy on port ${config.bindPort}!`)
})
process.on('uncaughtException', err => {
logger.error(`An uncaught exception was caught! Exception: ${err.stack ?? err}`)
})
process.on('unhandledRejection', err => {
logger.error(`An unhandled promise rejection was caught! Rejection: ${(err != null ? (err as any).stack : err) ?? err}`)
})
process.on('SIGTERM', () => {
logger.info("Cleaning up before exiting...")
for (const [username, plr] of PROXY.players) {
if (plr.remoteConnection != null) plr.remoteConnection.end()
disconnect(plr, ChatColor.YELLOW + "Proxy is shutting down.")
}
process.exit(0)
})
process.on('SIGINT', () => {
logger.info("Cleaning up before exiting...")
for (const [username, plr] of PROXY.players) {
if (plr.remoteConnection != null) plr.remoteConnection.end()
disconnect(plr, ChatColor.YELLOW + "Proxy is shutting down.")
}
process.exit(0)
2022-12-18 05:39:38 -08:00
})