import { Server, type ServerChannel } from "ssh2"; import Front, { newLine } from "./front"; import { $ } from "bun"; import { eraseScreen, cursorTo, cursorHide, cursorShow, clearTerminal } from "ansi-escapes"; import ContactForm from "./contact/ContactForm"; const inputCodes = { int8: { backspace: 127, tab: 9, enter: 13, space: 32, escape: 27, ctrlC: 3, a: 97, p: 112, c: 99, q: 113, }, base64: { arrows: { up: "G1tB", down: "G1tC", right: "G1tD", left: "G1tE", }, }, }; // If ./host.key doesn't exist, generate a new one try { await Bun.file("./host.key").stat(); } catch { await $`ssh-keygen -t rsa -b 4096 -f host.key -N "password"`; } const server = new Server( { hostKeys: [{ key: await Bun.file("./host.key").text(), passphrase: "password" }], ident: "portfolio.sh", }, (client) => { console.log("Client connected!"); const userSpecs: { userName?: string; } = {}; client .on("authentication", (ctx) => { userSpecs.userName = ctx.username; ctx.accept(); // On accepte toutes les connexions }) .on("ready", () => { client.on("session", (accept) => { const session = accept(); const userWindow: { columns: number; rows: number; selectedTab: "About" | "Projects" | "Contact"; scrollPosition: number; stream: ServerChannel | null; contactFormData: { name: string; email: string; message: string; }; contactFormSelectedField: "name" | "email" | "message" | "submit"; } = { columns: 80, rows: 24, selectedTab: "About", scrollPosition: 0, stream: null, contactFormData: { name: "", email: "", message: "", }, contactFormSelectedField: "name", }; const render = () => { userWindow.stream?.write(eraseScreen); userWindow.stream?.write(cursorTo(0, 0)); userWindow.stream?.write(cursorHide); if (userSpecs.userName === "contact") { userWindow.stream?.write(ContactForm(userWindow)); return; } userWindow.stream?.write(Front(userWindow)); }; session.on("pty", (accept, reject, info) => { console.log(info); userWindow.columns = info.cols; userWindow.rows = info.rows; accept(); }); session.on("window-change", (_accept, _reject, info) => { console.log(info); userWindow.columns = info.cols; userWindow.rows = info.rows; render(); }); session.on("shell", (accept) => { const stream = accept(); // stream.write(` // 🔥 Welcome to My Portfolio CLI 🔥 // ------------------------------ // Commands: about | projects | contact | exit // `); userWindow.stream = stream; render(); stream.on("data", (data: Buffer) => { const cmd = data.toString(); const cmdAsInt = data.readInt8(); const cmdAsBase64 = data.toBase64(); if ( cmdAsInt == inputCodes.int8.ctrlC || (cmdAsInt === inputCodes.int8.q && userSpecs.userName !== "contact") ) { userWindow.stream?.write(clearTerminal); userWindow.stream?.write(cursorTo(0, 0)); stream.write("Goodbye!\n"); stream.write(cursorShow); stream.close(); } if (userSpecs.userName === "contact") { if (cmdAsInt == inputCodes.int8.tab) { if (userWindow.contactFormSelectedField === "name") { userWindow.contactFormSelectedField = "email"; } else if (userWindow.contactFormSelectedField === "email") { userWindow.contactFormSelectedField = "message"; } else if (userWindow.contactFormSelectedField === "message") { userWindow.contactFormSelectedField = "submit"; } else if (userWindow.contactFormSelectedField === "submit") { userWindow.contactFormSelectedField = "name"; } render(); } if (cmdAsInt == inputCodes.int8.backspace) { if (userWindow.contactFormSelectedField === "name") { userWindow.contactFormData.name = userWindow.contactFormData.name.slice(0, -1); } else if (userWindow.contactFormSelectedField === "email") { userWindow.contactFormData.email = userWindow.contactFormData.email.slice( 0, -1 ); } else if (userWindow.contactFormSelectedField === "message") { userWindow.contactFormData.message = userWindow.contactFormData.message.slice( 0, -1 ); } render(); } if (cmdAsInt == inputCodes.int8.space) { if (userWindow.contactFormSelectedField === "name") { userWindow.contactFormData.name += " "; } else if (userWindow.contactFormSelectedField === "email") { userWindow.contactFormData.email += " "; } else if (userWindow.contactFormSelectedField === "message") { userWindow.contactFormData.message += " "; } render(); } if (cmdAsInt == inputCodes.int8.enter) { if (userWindow.contactFormSelectedField === "message") { userWindow.contactFormData.message += newLine; } else if (userWindow.contactFormSelectedField === "submit") { if (!process.env.DISCORD_WEBHOOK_URL || !process.env.DISCORD_USER_ID) { console.error("Discord webhook URL or user ID not set"); stream.write("Error sending message to Discord webhook\n"); stream.close(); return; } const webhookUrl = process.env.DISCORD_WEBHOOK_URL!; const messageData = { content: `<@${process.env.DISCORD_USER_ID}>\nName: ${ userWindow.contactFormData.name }\nEmail: ${ userWindow.contactFormData.email }\nMessage: ${userWindow.contactFormData.message.replace(newLine, "\n")}`, }; fetch(webhookUrl, { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify(messageData), }) .then((response) => { if (!response.ok) { throw new Error("Failed to send message to Discord webhook"); } console.log("Message sent to Discord webhook successfully"); }) .catch((error) => { console.error("Error sending message to Discord webhook:", error); }); userWindow.stream?.write(eraseScreen); userWindow.stream?.write(cursorTo(0, 0)); stream.write(cursorShow); userWindow.stream?.write("Message sent!\n"); stream.close(); } render(); } if ( cmdAsInt != inputCodes.int8.backspace && cmdAsInt != inputCodes.int8.escape && cmdAsInt != inputCodes.int8.tab && cmdAsInt != inputCodes.int8.space && cmdAsInt != inputCodes.int8.enter ) { if (userWindow.contactFormSelectedField === "name") { userWindow.contactFormData.name += cmd.trim(); } else if (userWindow.contactFormSelectedField === "email") { userWindow.contactFormData.email += cmd.trim(); } else if (userWindow.contactFormSelectedField === "message") { userWindow.contactFormData.message += cmd.trim(); } render(); } return; } switch (cmdAsInt) { case inputCodes.int8.a: userWindow.selectedTab = "About"; userWindow.scrollPosition = 0; render(); break; case inputCodes.int8.p: userWindow.selectedTab = "Projects"; userWindow.scrollPosition = 0; render(); break; case inputCodes.int8.c: userWindow.selectedTab = "Contact"; userWindow.scrollPosition = 0; render(); break; } switch (cmdAsBase64) { case inputCodes.base64.arrows.up: userWindow.scrollPosition -= 1; if (userWindow.scrollPosition < 0) { userWindow.scrollPosition = 0; } render(); break; case inputCodes.base64.arrows.down: userWindow.scrollPosition += 1; render(); break; } }); }); }); }); client.on("end", () => { console.log("Client disconnected"); }); } ); server.listen(+(process.env.PORT || 2222), "0.0.0.0", () => { console.log(`🚀 SSH server running on port ${process.env.PORT || 2222}`); });