feature: Adds exec support.
This also lets us prepare for support for ssh-copy-id.
This commit is contained in:
parent
95b79ca364
commit
47b707e8cd
2 changed files with 57 additions and 13 deletions
|
@ -9,6 +9,7 @@ import type {
|
|||
ParameterReturnedValue,
|
||||
BackendBaseClass,
|
||||
} from "../base.js";
|
||||
|
||||
import { generateRandomData } from "../../libs/generateRandom.js";
|
||||
import { requestHandler } from "./socket.js";
|
||||
import { route } from "./routes.js";
|
||||
|
|
|
@ -39,17 +39,17 @@ if (!keyFile) throw new Error("Somehow failed to fetch the key file!");
|
|||
|
||||
const server: ssh2.Server = new ssh2.Server({
|
||||
hostKeys: [keyFile],
|
||||
|
||||
banner: "NextNet-LOM (c) NextNet project et al.",
|
||||
greeting: "NextNet LOM (beta)",
|
||||
banner: "NextNet-LOM (c) NextNet project et al."
|
||||
});
|
||||
|
||||
server.on("connection", client => {
|
||||
let token: string = "";
|
||||
|
||||
client.on("authentication", async auth => {
|
||||
if (auth.method != "password") return auth.reject(["password"]); // We need to know the password to auth with the API
|
||||
let username: string = "";
|
||||
let password: string = "";
|
||||
|
||||
client.on("authentication", async auth => {
|
||||
if (auth.method == "password") {
|
||||
const response = await axios.post("/api/v1/users/login", {
|
||||
username: auth.username,
|
||||
password: auth.password,
|
||||
|
@ -60,13 +60,56 @@ server.on("connection", client => {
|
|||
}
|
||||
|
||||
token = response.data.token;
|
||||
|
||||
username = auth.username;
|
||||
password = auth.password;
|
||||
|
||||
auth.accept();
|
||||
} else if (auth.method == "publickey") {
|
||||
return auth.reject();
|
||||
// todo
|
||||
} else {
|
||||
return auth.reject(["password", "publickey"]);
|
||||
}
|
||||
});
|
||||
|
||||
client.on("ready", () => {
|
||||
client.on("session", (accept, reject) => {
|
||||
const conn = accept();
|
||||
|
||||
conn.on("exec", async (accept, reject, info) => {
|
||||
const stream = accept();
|
||||
|
||||
// Matches on ; and &&
|
||||
const commandsRecv = info.command.split(/;|&&/).map((i) => i.trim());
|
||||
|
||||
function println(...data: any[]) {
|
||||
stream.write(format(...data).replaceAll("\n", "\r\n"));
|
||||
};
|
||||
|
||||
for (const command of commandsRecv) {
|
||||
const argv = parseArgsStringToArgv(command);
|
||||
|
||||
if (argv[0] == "exit") {
|
||||
stream.close();
|
||||
} else {
|
||||
const command = commands.find(i => i.name == argv[0]);
|
||||
|
||||
if (!command) {
|
||||
stream.write(
|
||||
`Unknown command ${argv[0]}.\r\n`,
|
||||
);
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
await command.run(argv, println, axios, token, (disableEcho) => readFromKeyboard(stream, disableEcho));
|
||||
}
|
||||
}
|
||||
|
||||
return stream.close();
|
||||
});
|
||||
|
||||
// We're dumb. We don't really care.
|
||||
conn.on("pty", accept => accept());
|
||||
conn.on("window-change", accept => {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue