Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/project/sshd.ts
Views: 687
/*1* This file is part of CoCalc: Copyright © 2022 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45/*6This starts an sshd server used for accessing the project via an ssh gateway server78Ref.: https://nodejs.org/docs/latest-v16.x/api/child_process.html#optionsdetached9*/1011import { spawn } from "node:child_process";12import { openSync } from "node:fs";13import { writeFile } from "node:fs/promises";14import { homedir } from "node:os";15import { join } from "node:path";1617import { isEmpty } from "lodash";1819import { getLogger } from "./logger";20import { SSH_LOG, SSH_ERR } from "./data";2122const { info, warn } = getLogger("sshd");2324// created by ./project-setup.ts25type EnvVars = { [key: string]: string };2627export async function init(envVars?: EnvVars) {28info("starting sshd");2930// SSHD does not inherit the environment variables.31// We write a file for the user, which contains the custom variables.32// Also, if there are no env variables define, we keep the comment and an otherwise empty file.33try {34const intro =35"# This file is autogenerated!\n# Configure SSH environment variables via Project Settings → Custom environment variables.";36const envData =37envVars && !isEmpty(envVars)38? Object.entries(envVars)39.map(([k, v]) => `${k.trim()}=${v}`) // no quotes around the value!40.join("\n")41: "";42const envFn = join(homedir(), ".ssh", "environment");43await writeFile(envFn, `${intro}\n${envData}\n`);44} catch (err) {45warn(`unable to write to ~/.ssh/environment -- ${err}`);46}4748const out = openSync(SSH_LOG, "w");49const err = openSync(SSH_ERR, "w");5051const sshd = spawn("bash", ["/cocalc/kucalc-start-sshd.sh"], {52detached: true,53stdio: ["ignore", out, err],54});5556sshd.unref();57}585960