Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
vincelwt committed Dec 24, 2024
1 parent 04d4b63 commit ea429ec
Showing 1 changed file with 154 additions and 175 deletions.
329 changes: 154 additions & 175 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { readFileSync } from "fs";

import { v4 as uuidv4 } from "uuid";
import cron from "node-cron";
import sql from "./db";
// import sql from "./db";
import {
Message,
ClientSession as IClientSession,
Expand Down Expand Up @@ -723,180 +723,159 @@ const server = new SSH2.Server(
},
},
async (client) => {
console.log("Client connected");

const sessionId = `session_${++sessionCounter}`;
let autoLoginUsername: string | null = null;

const handleStream = async (stream) => {
console.log(`Stream opened for session ${sessionId}`);
let autoLoginInfo: AutoLoginInfo | null = null;

if (autoLoginUsername) {
try {
const [userInfo] = await sql<[{ id: string; credits: number }]>`
SELECT id, credits FROM accounts
WHERE username = ${autoLoginUsername}
`;
if (userInfo) {
autoLoginInfo = {
username: autoLoginUsername,
userId: userInfo.id,
credits: userInfo.credits,
};
}
} catch (error) {
console.error("Error fetching user info for auto-login:", error);
}
}

const session = new ClientSession(sessionId, stream, autoLoginInfo);
sessions.set(sessionId, session);

// Welcome message with badass ASCII art
const welcomeMessage = `
\x1b[35m
__ _ __
____ _ __ __ ___ _____ / /_ (_)____ ____ _____ / /_
/ __ \`// / / // _ \ / ___// __// // __ \ / __ \ / ___// __ \\
/ /_/ // /_/ // __/(__ )/ /_ / // /_/ // / / /_ (__ )/ / / /
\__, / \__,_/ \___//____/ \__//_/ \____//_/ /_/(_)/____//_/ /_/
/_/
\x1b[0m
🤖 Welcome to \x1b[1mquestion.sh\x1b[0m - Your AI-powered SSH interface!
Create shared rooms and reusable agents.
${
autoLoginInfo
? `\x1b[32mYou are logged in as ${autoLoginInfo.username}. You have ${autoLoginInfo.credits} credits.\x1b[0m`
: "You are not logged in. Use /register or /login to access all features."
}
Type your message and press Enter. Commands:
- Type "exit" to quit
- Type "/help" for available commands`;

session.writeCommandOutput(welcomeMessage);

// If auto-login was successful, notify the user
if (autoLoginUsername) {
session.writeCommandOutput(
`Automatically logged in as ${autoLoginUsername}.`
);
}

stream.on("data", async (data) => {
const input = data.toString();

// Handle backspace
if (input === "\x7f") {
if (session.buffer.length > 0) {
session.buffer = session.buffer.slice(0, -1);
stream.write("\b \b");
}
return;
}

// Handle enter key
if (input === "\r") {
stream.write("\n");

const trimmedBuffer = session.buffer.trim();
if (trimmedBuffer.toLowerCase() === "exit") {
stream.write("Goodbye! 👋\r\n");
sessions.delete(sessionId);
stream.end();
return;
}

if (trimmedBuffer) {
await session.handleMessage(trimmedBuffer);
} else {
// If the buffer is empty or only contains whitespace, just show the prompt
session.writeToStream("", true);
}

session.buffer = "";
return;
}

// Regular character input
session.buffer += input;
stream.write(input);
});

stream.on("error", (err) => {
console.error(`Stream error for session ${sessionId}:`, err);
sessions.delete(sessionId);
});

stream.on("close", () => {
console.log(`Stream closed for session ${sessionId}`);
sessions.delete(sessionId);
});
};

client.on("authentication", async (ctx) => {
if (ctx.method === "password" && ctx.username) {
try {
const result = await sql`
SELECT id, credits FROM accounts
WHERE username = ${ctx.username}
`;
if (result.length > 0) {
autoLoginUsername = ctx.username;
console.log(`Auto-login successful for user: ${ctx.username}`);
} else {
console.log(`Auto-login failed: User ${ctx.username} not found`);
}
} catch (error) {
console.error("Auto-login error:", error);
}
}
ctx.accept();
});

client.on("ready", () => {
console.log(`Client authenticated! (Session: ${sessionId})`);
client.on("session", (accept) => {
const session = accept();
session.on("pty", (accept) => accept());
session.on("shell", (accept) => {
const stream = accept();
handleStream(stream);

// If auto-login was successful, update the session
if (autoLoginUsername) {
const clientSession = sessions.get(sessionId);
if (clientSession) {
sql`
SELECT id, credits FROM accounts
WHERE username = ${autoLoginUsername}
`
.then(([result]) => {
clientSession.userId = result.id;
clientSession.username = autoLoginUsername;
clientSession.credits = result.credits;
})
.catch((error) => {
console.error(
"Error updating session after auto-login:",
error
);
});
}
}
});
});
});

client.on("error", (err) => {
console.error("Client error:", err);
sessions.delete(sessionId);
});

client.on("close", () => {
console.log(`Client disconnected (Session: ${sessionId})`);
sessions.delete(sessionId);
});
// console.log("Client connected");
// const sessionId = `session_${++sessionCounter}`;
// let autoLoginUsername: string | null = null;
// const handleStream = async (stream) => {
// console.log(`Stream opened for session ${sessionId}`);
// let autoLoginInfo: AutoLoginInfo | null = null;
// if (autoLoginUsername) {
// try {
// const [userInfo] = await sql<[{ id: string; credits: number }]>`
// SELECT id, credits FROM accounts
// WHERE username = ${autoLoginUsername}
// `;
// if (userInfo) {
// autoLoginInfo = {
// username: autoLoginUsername,
// userId: userInfo.id,
// credits: userInfo.credits,
// };
// }
// } catch (error) {
// console.error("Error fetching user info for auto-login:", error);
// }
// }
// const session = new ClientSession(sessionId, stream, autoLoginInfo);
// sessions.set(sessionId, session);
// // Welcome message with badass ASCII art
// const welcomeMessage = `
// \x1b[35m
// __ _ __
// ____ _ __ __ ___ _____ / /_ (_)____ ____ _____ / /_
// / __ \`// / / // _ \ / ___// __// // __ \ / __ \ / ___// __ \\
// / /_/ // /_/ // __/(__ )/ /_ / // /_/ // / / /_ (__ )/ / / /
// \__, / \__,_/ \___//____/ \__//_/ \____//_/ /_/(_)/____//_/ /_/
// /_/
// \x1b[0m
// 🤖 Welcome to \x1b[1mquestion.sh\x1b[0m - Your AI-powered SSH interface!
// Create shared rooms and reusable agents.
// ${
// autoLoginInfo
// ? `\x1b[32mYou are logged in as ${autoLoginInfo.username}. You have ${autoLoginInfo.credits} credits.\x1b[0m`
// : "You are not logged in. Use /register or /login to access all features."
// }
// Type your message and press Enter. Commands:
// - Type "exit" to quit
// - Type "/help" for available commands`;
// session.writeCommandOutput(welcomeMessage);
// // If auto-login was successful, notify the user
// if (autoLoginUsername) {
// session.writeCommandOutput(
// `Automatically logged in as ${autoLoginUsername}.`
// );
// }
// stream.on("data", async (data) => {
// const input = data.toString();
// // Handle backspace
// if (input === "\x7f") {
// if (session.buffer.length > 0) {
// session.buffer = session.buffer.slice(0, -1);
// stream.write("\b \b");
// }
// return;
// }
// // Handle enter key
// if (input === "\r") {
// stream.write("\n");
// const trimmedBuffer = session.buffer.trim();
// if (trimmedBuffer.toLowerCase() === "exit") {
// stream.write("Goodbye! 👋\r\n");
// sessions.delete(sessionId);
// stream.end();
// return;
// }
// if (trimmedBuffer) {
// await session.handleMessage(trimmedBuffer);
// } else {
// // If the buffer is empty or only contains whitespace, just show the prompt
// session.writeToStream("", true);
// }
// session.buffer = "";
// return;
// }
// // Regular character input
// session.buffer += input;
// stream.write(input);
// });
// stream.on("error", (err) => {
// console.error(`Stream error for session ${sessionId}:`, err);
// sessions.delete(sessionId);
// });
// stream.on("close", () => {
// console.log(`Stream closed for session ${sessionId}`);
// sessions.delete(sessionId);
// });
// };
// client.on("authentication", async (ctx) => {
// if (ctx.method === "password" && ctx.username) {
// try {
// const result = await sql`
// SELECT id, credits FROM accounts
// WHERE username = ${ctx.username}
// `;
// if (result.length > 0) {
// autoLoginUsername = ctx.username;
// console.log(`Auto-login successful for user: ${ctx.username}`);
// } else {
// console.log(`Auto-login failed: User ${ctx.username} not found`);
// }
// } catch (error) {
// console.error("Auto-login error:", error);
// }
// }
// ctx.accept();
// });
// client.on("ready", () => {
// console.log(`Client authenticated! (Session: ${sessionId})`);
// client.on("session", (accept) => {
// const session = accept();
// session.on("pty", (accept) => accept());
// session.on("shell", (accept) => {
// const stream = accept();
// handleStream(stream);
// // If auto-login was successful, update the session
// if (autoLoginUsername) {
// const clientSession = sessions.get(sessionId);
// if (clientSession) {
// sql`
// SELECT id, credits FROM accounts
// WHERE username = ${autoLoginUsername}
// `
// .then(([result]) => {
// clientSession.userId = result.id;
// clientSession.username = autoLoginUsername;
// clientSession.credits = result.credits;
// })
// .catch((error) => {
// console.error(
// "Error updating session after auto-login:",
// error
// );
// });
// }
// }
// });
// });
// });
// client.on("error", (err) => {
// console.error("Client error:", err);
// sessions.delete(sessionId);
// });
// client.on("close", () => {
// console.log(`Client disconnected (Session: ${sessionId})`);
// sessions.delete(sessionId);
// });
}
);

Expand Down

0 comments on commit ea429ec

Please sign in to comment.