Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 38 additions & 14 deletions gates/_lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ let held = "";

const staged = new Map<string, Promise<Exec>>();

// a hoc script longer than this (quoted) goes up as a staged file
const SCRIPT_MAX = 4096;

const opened = new Map<number, Promise<Exec>>();

// Exec
Expand Down Expand Up @@ -150,7 +153,10 @@ export async function ssh(node: number, script: string,
}

// The hoc way sends each input up to the bastion once, and every session
// reads it there: 48 shards of one pack cost one upload, not 48.
// reads it there: 48 shards of one pack cost one upload, not 48. A long
// script goes up the same way: the mux client hands its command to the
// master in one message, and macOS refuses one near 8 KB ("mm_send_fd:
// sendmsg: Message too long"), so that session never started.
async function hoc_ssh(node: number, script: string,
input?: Buffer | string, timeout?: number): Promise<Exec> {
const open = await hoc_open(node);
Expand All @@ -159,24 +165,42 @@ async function hoc_ssh(node: number, script: string,
}
let from = "";
if (input !== undefined) {
const sum = crypto.createHash("sha1").update(input).digest("hex");
const up = staged.get(sum) ?? exec("ssh", [...hoc_mux(node), ...HOC_HUB,
"d=$HOME/.bend-gate; mkdir -p $d && { find $d -type f -mmin +60"
+ " -delete 2>/dev/null; cat > $d/" + sum + ".$$; } && mv $d/" + sum
+ ".$$ $d/" + sum], input);
staged.set(sum, up);
const got = await up;
const got = await hoc_stage(node, input);
if (got.code !== 0) {
return got;
}
from = " < " + got.out;
}
let arg = "'" + script.replaceAll("'", "'\\''") + "'";
if (arg.length > SCRIPT_MAX) {
const got = await hoc_stage(node, script);
if (got.code !== 0) {
if (staged.get(sum) === up) {
staged.delete(sum);
}
return got;
}
from = " < $HOME/.bend-gate/" + sum;
arg = "\"$(cat " + got.out + ")\"";
}
return node_exec([...hoc_mux(node), ...HOC_HUB, "sudo -n"
+ " /usr/local/bin/hoc-ssh " + String(node) + " '"
+ script.replaceAll("'", "'\\''") + "'" + from], undefined, timeout);
+ " /usr/local/bin/hoc-ssh " + String(node) + " " + arg + from],
undefined, timeout);
}

// Puts data on the bastion under its sha1 (once per run) and answers its
// path there in out.
async function hoc_stage(node: number, data: Buffer | string): Promise<Exec> {
const sum = crypto.createHash("sha1").update(data).digest("hex");
const up = staged.get(sum) ?? exec("ssh", [...hoc_mux(node), ...HOC_HUB,
"d=$HOME/.bend-gate; mkdir -p $d && { find $d -type f -mmin +60"
+ " -delete 2>/dev/null; cat > $d/" + sum + ".$$; } && mv $d/" + sum
+ ".$$ $d/" + sum], data);
staged.set(sum, up);
const got = await up;
if (got.code !== 0) {
if (staged.get(sum) === up) {
staged.delete(sum);
}
return got;
}
return { out: "$HOME/.bend-gate/" + sum, err: "", code: 0 };
}

function node_exec(args: string[], input?: Buffer | string,
Expand Down