@cocalc/project
Version:
CoCalc: project daemon
49 lines (48 loc) • 2.11 kB
JavaScript
;
/*
* This file is part of CoCalc: Copyright © 2022 Sagemath, Inc.
* License: AGPLv3 s.t. "Commons Clause" – see LICENSE.md for details
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.init = void 0;
/*
This starts an sshd server used for accessing the project via an ssh gateway server
Ref.: https://nodejs.org/docs/latest-v16.x/api/child_process.html#optionsdetached
*/
const node_child_process_1 = require("node:child_process");
const node_fs_1 = require("node:fs");
const promises_1 = require("node:fs/promises");
const node_os_1 = require("node:os");
const node_path_1 = require("node:path");
const lodash_1 = require("lodash");
const logger_1 = require("./logger");
const data_1 = require("./data");
const { info, warn } = (0, logger_1.getLogger)("sshd");
async function init(envVars) {
info("starting sshd");
// SSHD does not inherit the environment variables.
// We write a file for the user, which contains the custom variables.
// Also, if there are no env variables define, we keep the comment and an otherwise empty file.
try {
const intro = "# This file is autogenerated!\n# Configure SSH environment variables via Project Settings → Custom environment variables.";
const envData = envVars && !(0, lodash_1.isEmpty)(envVars)
? Object.entries(envVars)
.map(([k, v]) => `${k.trim()}=${v}`) // no quotes around the value!
.join("\n")
: "";
const envFn = (0, node_path_1.join)((0, node_os_1.homedir)(), ".ssh", "environment");
await (0, promises_1.writeFile)(envFn, `${intro}\n${envData}\n`);
}
catch (err) {
warn(`unable to write to ~/.ssh/environment -- ${err}`);
}
const out = (0, node_fs_1.openSync)(data_1.SSH_LOG, "w");
const err = (0, node_fs_1.openSync)(data_1.SSH_ERR, "w");
const sshd = (0, node_child_process_1.spawn)("bash", ["/cocalc/kucalc-start-sshd.sh"], {
detached: true,
stdio: ["ignore", out, err],
});
sshd.unref();
}
exports.init = init;
//# sourceMappingURL=sshd.js.map