import { ServerDeviceCodeResponse, auth } from "../auth.js";
import { config } from "../config.js";
const LINK_STR = `
EagPAAS - Connect
EagPAAS - Connect to Server
Hello there! To connect to this Minecraft server through a server list entry,
use this URL: (connect from any recent
EaglercraftX client via Multiplayer > Direct Connect)
loading...
If your Eaglercraft client supports vanilla:// URLs, this will also work:
loading...
Note that your session will eventually expire, and you will need to log back in again.
`;
export async function registerEndpoints() {
const proxy = PLUGIN_MANAGER.proxy;
proxy.on("httpConnection", (req, res, ctx) => {
if (req.url.startsWith("/eagpaas/link")) {
ctx.handled = true;
res.setHeader("Content-Type", "text/html").writeHead(200).end(LINK_STR);
} else if (req.url.startsWith("/eagpaas/metadata")) {
ctx.handled = true;
res.writeHead(200).end(
JSON.stringify({
branding: "EagProxyAAS",
version: "1",
})
);
} else if (req.url.startsWith("/eagpaas/validate")) {
ctx.handled = true;
if (config.authentication.enabled) {
if (req.headers["authorization"] !== `Basic ${config.authentication.password}`) {
return res.writeHead(403).end(
JSON.stringify({
success: false,
reason: "Access Denied",
})
);
}
}
res.writeHead(200).end(
JSON.stringify({
success: true,
})
);
}
});
proxy.on("wsConnection", (ws, req, ctx) => {
try {
if (req.url.startsWith("/eagpaas/token")) {
ctx.handled = true;
if (config.authentication.enabled) {
if (req.headers.authorization !== `Basic ${config.authentication.password}`) {
ws.send(
JSON.stringify({
type: "ERROR",
error: "Access Denied",
})
);
ws.close();
return;
}
}
const quit = { quit: false },
authHandler = auth(quit),
codeCallback = (code: ServerDeviceCodeResponse) => {
ws.send(
JSON.stringify({
type: "CODE",
data: code,
})
);
};
ws.once("close", () => {
quit.quit = true;
});
authHandler
.on("code", codeCallback)
.on("error", (err) => {
ws.send(
JSON.stringify({
type: "ERROR",
reason: err,
})
);
ws.close();
})
.on("done", (result) => {
ws.send(
JSON.stringify({
type: "COMPLETE",
data: result,
})
);
ws.close();
});
} else if (req.url.startsWith("/eagpaas/ping")) {
ctx.handled = true;
if (config.authentication.enabled) {
if (req.headers.authorization !== `Basic ${config.authentication.password}`) {
ws.send(
JSON.stringify({
type: "ERROR",
error: "Access Denied",
})
);
ws.close();
return;
}
}
ws.once("message", (_) => {
ws.send(_);
ws.close();
});
}
} catch (err) {}
});
}