|
| 1 | +import type { Command } from "@commander-js/extra-typings"; |
| 2 | +import console from "node:console"; |
| 3 | +import process from "node:process"; |
| 4 | +import { Shescape } from "shescape"; |
| 5 | +import { execvp } from "@alphahydrae/exec"; |
| 6 | +import { |
| 7 | + logAndQuit, |
| 8 | + logSessionTokenExpiredAndQuit, |
| 9 | +} from "../helpers/errors.ts"; |
| 10 | +import { getApiUrl } from "../helpers/urls.ts"; |
| 11 | +import { getAuthToken } from "../helpers/config.ts"; |
| 12 | + |
| 13 | +type SshHostKey = { |
| 14 | + keyType: string; |
| 15 | + base64EncodedKey: string; |
| 16 | +}; |
| 17 | + |
| 18 | +export function registerSsh(program: Command) { |
| 19 | + program |
| 20 | + .command("ssh") |
| 21 | + .option("-q, --quiet", "Quiet mode", false) |
| 22 | + .option( |
| 23 | + "--use-host-keys [value]", |
| 24 | + "Use API provided SSH server host keys if known", |
| 25 | + true, |
| 26 | + ) |
| 27 | + .argument( |
| 28 | + "<destination>", |
| 29 | + "USERNAME@VM_ID The (optional) username, and VM id to SSH into.", |
| 30 | + ) |
| 31 | + .allowExcessArguments(false) |
| 32 | + .action(async (destination, options) => { |
| 33 | + const splitDestination = destination.split("@"); |
| 34 | + let vmId: string; |
| 35 | + let sshUsername: string | undefined; |
| 36 | + if (splitDestination.length == 1) { |
| 37 | + sshUsername = undefined; |
| 38 | + vmId = splitDestination[0]; |
| 39 | + } else if (splitDestination.length == 2) { |
| 40 | + sshUsername = splitDestination[0]; |
| 41 | + vmId = splitDestination[1]; |
| 42 | + } else { |
| 43 | + logAndQuit(`Invalid SSH destination string: ${destination}`); |
| 44 | + } |
| 45 | + |
| 46 | + const response = await fetch(await getApiUrl("vms_ssh_get"), { |
| 47 | + method: "POST", |
| 48 | + headers: { |
| 49 | + "Content-Type": "application/json", |
| 50 | + Authorization: `Bearer ${await getAuthToken()}`, |
| 51 | + }, |
| 52 | + body: JSON.stringify({ vm_id: vmId }), |
| 53 | + }); |
| 54 | + |
| 55 | + if (!response.ok) { |
| 56 | + if (response.status === 401) { |
| 57 | + logSessionTokenExpiredAndQuit(); |
| 58 | + } |
| 59 | + |
| 60 | + logAndQuit( |
| 61 | + `Failed to retrieve ssh information: ${response.statusText}`, |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + const data = (await response.json()) as { |
| 66 | + ssh_hostname: string; |
| 67 | + ssh_port: number; |
| 68 | + ssh_host_keys: SshHostKey[] | undefined; |
| 69 | + }; |
| 70 | + const sshHostname = data.ssh_hostname; |
| 71 | + const sshPort = data.ssh_port; |
| 72 | + const sshHostKeys = data.ssh_host_keys || []; |
| 73 | + |
| 74 | + let sshDestination = sshHostname; |
| 75 | + if (sshUsername !== undefined) { |
| 76 | + sshDestination = `${sshUsername}@${sshDestination}`; |
| 77 | + } |
| 78 | + if (sshPort !== undefined) { |
| 79 | + sshDestination = `${sshDestination}:${sshPort}`; |
| 80 | + } |
| 81 | + sshDestination = `ssh://${sshDestination}`; |
| 82 | + |
| 83 | + let cmd = ["ssh"]; |
| 84 | + |
| 85 | + if (sshHostKeys.length > 0 && options.useHostKeys) { |
| 86 | + let knownHostsCommand = ["/usr/bin/env", "printf", "%s %s %s\\n"]; |
| 87 | + for (const sshHostKey of sshHostKeys) { |
| 88 | + knownHostsCommand = knownHostsCommand.concat([ |
| 89 | + sshHostname, |
| 90 | + sshHostKey.keyType, |
| 91 | + sshHostKey.base64EncodedKey, |
| 92 | + ]); |
| 93 | + } |
| 94 | + // Escape all characters for proper pass through |
| 95 | + for (const i in knownHostsCommand) { |
| 96 | + knownHostsCommand[i] = knownHostsCommand[i].replaceAll("%", "%%"); |
| 97 | + knownHostsCommand[i] = knownHostsCommand[i].replaceAll('"', '\\"'); |
| 98 | + knownHostsCommand[i] = '"' + knownHostsCommand[i] + '"'; |
| 99 | + } |
| 100 | + const knownHostsCommand_str = knownHostsCommand.join(" "); |
| 101 | + cmd = cmd.concat(["-o", `KnownHostsCommand=${knownHostsCommand_str}`]); |
| 102 | + } |
| 103 | + |
| 104 | + cmd = cmd.concat([sshDestination]); |
| 105 | + |
| 106 | + let shescape: undefined | Shescape = undefined; |
| 107 | + let shell: undefined | string = undefined; |
| 108 | + if (process.env.SHELL !== undefined) { |
| 109 | + try { |
| 110 | + shescape = new Shescape({ |
| 111 | + flagProtection: false, |
| 112 | + shell: process.env.SHELL, |
| 113 | + }); |
| 114 | + shell = process.env.SHELL; |
| 115 | + } catch { |
| 116 | + // shescape will stay undefined |
| 117 | + } |
| 118 | + } |
| 119 | + if (shescape === undefined) { |
| 120 | + shescape = new Shescape({ |
| 121 | + flagProtection: false, |
| 122 | + shell: "/bin/sh", |
| 123 | + }); |
| 124 | + shell = "/bin/sh"; |
| 125 | + } |
| 126 | + const shell_cmd = shescape.quoteAll(cmd).join(" "); |
| 127 | + if (!options.quiet) { |
| 128 | + console.log(`Executing (${shell} style output): ${shell_cmd}`); |
| 129 | + } |
| 130 | + |
| 131 | + execvp(cmd[0], cmd.slice(1)); |
| 132 | + }); |
| 133 | +} |
0 commit comments