First Commit
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
import { exec, execSync, type ExecSyncOptions } from "child_process";
|
||||
import grabSSHPrefix from "./grab-ssh-prefix";
|
||||
import _ from "lodash";
|
||||
|
||||
type Param = {
|
||||
cmd: string | string[];
|
||||
debug?: boolean;
|
||||
ip: string;
|
||||
user?: string;
|
||||
options?: ExecSyncOptions;
|
||||
detached?: boolean;
|
||||
return_cmd_only?: boolean;
|
||||
cmd_prefix?: string;
|
||||
};
|
||||
|
||||
export default async function execSSH(
|
||||
params: Param,
|
||||
): Promise<string | undefined> {
|
||||
const {
|
||||
cmd,
|
||||
debug,
|
||||
ip,
|
||||
user = "root",
|
||||
options,
|
||||
detached,
|
||||
return_cmd_only,
|
||||
cmd_prefix,
|
||||
} = params;
|
||||
|
||||
try {
|
||||
let cmdPrefix = cmd_prefix || grabSSHPrefix();
|
||||
|
||||
let finalCmd = `${cmdPrefix}`;
|
||||
finalCmd += ` ${user}@${ip}`;
|
||||
|
||||
const parsedCmd =
|
||||
typeof cmd == "string"
|
||||
? cmd
|
||||
: Array.isArray(cmd)
|
||||
? cmd.join("\n")
|
||||
: undefined;
|
||||
|
||||
finalCmd += ` << 'TURBOCIEXEC' \n${parsedCmd}\nTURBOCIEXEC`;
|
||||
|
||||
if (debug) {
|
||||
console.log("finalCmd", finalCmd);
|
||||
}
|
||||
|
||||
if (return_cmd_only) {
|
||||
return finalCmd;
|
||||
}
|
||||
|
||||
const str = detached
|
||||
? ""
|
||||
: execSync(finalCmd, {
|
||||
stdio: "pipe",
|
||||
...options,
|
||||
encoding: "utf-8",
|
||||
});
|
||||
|
||||
if (debug) {
|
||||
console.log(str);
|
||||
}
|
||||
|
||||
if (detached) {
|
||||
exec(finalCmd);
|
||||
}
|
||||
|
||||
return str.trim();
|
||||
} catch (error: any) {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
import path from "path";
|
||||
|
||||
export default function grabDirNames() {
|
||||
const APP_DIR = path.resolve(__dirname, "../../");
|
||||
const TURBOCI_DIR = `/root/.turboci`;
|
||||
const TURBOCI_CONFIG_DIR = path.join(TURBOCI_DIR, ".config");
|
||||
const TURBOCI_CONFIG_JSON_FILE = path.join(
|
||||
TURBOCI_CONFIG_DIR,
|
||||
"turboci.json",
|
||||
);
|
||||
|
||||
const TURBOCI_SSH_DIR = path.join(TURBOCI_DIR, ".ssh");
|
||||
const TURBOCI_SSH_KEY_FILE = path.join(TURBOCI_SSH_DIR, "turboci");
|
||||
|
||||
return {
|
||||
APP_DIR,
|
||||
TURBOCI_CONFIG_DIR,
|
||||
TURBOCI_CONFIG_JSON_FILE,
|
||||
TURBOCI_DIR,
|
||||
TURBOCI_SSH_DIR,
|
||||
TURBOCI_SSH_KEY_FILE,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
import grabDirNames from "./grab-dir-names";
|
||||
|
||||
type Params = {};
|
||||
|
||||
export default function grabSSHPrefix(params?: Params) {
|
||||
const { TURBOCI_SSH_KEY_FILE } = grabDirNames();
|
||||
return `ssh -i ${TURBOCI_SSH_KEY_FILE} -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -C -c aes128-ctr`;
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
type Params = {
|
||||
cmd: string;
|
||||
cwd?: string;
|
||||
flags?: string[];
|
||||
};
|
||||
|
||||
export default function grabTtydCmd({ cmd: ttydCmd, cwd, flags }: Params) {
|
||||
const port = 8080;
|
||||
|
||||
let cmd = ``;
|
||||
cmd += `${AppData["TerminalBinName"]}`;
|
||||
cmd += ` --writable --max-clients 1`;
|
||||
cmd += ` --client-option 'theme={"background":"#0c0e11"}'`;
|
||||
cmd += ` --client-option fontSize=14`;
|
||||
|
||||
if (cwd) {
|
||||
cmd += ` --cwd ${cwd}`;
|
||||
}
|
||||
|
||||
if (flags?.[0]) {
|
||||
for (let i = 0; i < flags.length; i++) {
|
||||
const flag = flags[i];
|
||||
cmd += ` ${flag}`;
|
||||
}
|
||||
}
|
||||
|
||||
cmd += ` -p ${port}`;
|
||||
cmd += ` ${ttydCmd}`;
|
||||
|
||||
return { cmd, port };
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
import fs from "fs";
|
||||
import grabDirNames from "./grab-dir-names";
|
||||
import { TCIGlobalConfig } from "@/types";
|
||||
|
||||
export default function grabTurboCiConfig() {
|
||||
const { TURBOCI_CONFIG_JSON_FILE } = grabDirNames();
|
||||
|
||||
if (!fs.existsSync(TURBOCI_CONFIG_JSON_FILE)) {
|
||||
throw new Error(`TurboCI config JSON file not found!`);
|
||||
}
|
||||
|
||||
const config = JSON.parse(
|
||||
fs.readFileSync(TURBOCI_CONFIG_JSON_FILE, "utf-8"),
|
||||
) as TCIGlobalConfig;
|
||||
|
||||
return config;
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import datasquirel from "@moduletrace/datasquirel";
|
||||
import { NextApiRequest } from "next";
|
||||
|
||||
type Params = {
|
||||
req: NextApiRequest;
|
||||
};
|
||||
|
||||
export default async function userAuth({ req }: Params) {
|
||||
const auth = datasquirel.user.auth.auth({ req });
|
||||
const user = auth.payload;
|
||||
return { user };
|
||||
}
|
||||
Reference in New Issue
Block a user