2025-04-14 09:50:42 +02:00
|
|
|
import fs from "fs";
|
|
|
|
|
import { spawn, exec } from "child_process";
|
|
|
|
|
|
2025-04-09 15:25:49 +02:00
|
|
|
export class ProcessControl {
|
|
|
|
|
constructor(configService, shellService, osService, fsService) {
|
2025-04-14 11:27:58 +02:00
|
|
|
this.configService = configService;
|
2025-04-09 15:25:49 +02:00
|
|
|
this.config = configService.get();
|
|
|
|
|
this.shell = shellService;
|
|
|
|
|
this.os = osService;
|
|
|
|
|
this.fs = fsService;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getPublicIp = async () => {
|
|
|
|
|
if (this.os.isWindows()) {
|
|
|
|
|
const result = await this.shell.run(
|
|
|
|
|
"for /f \"delims=\" %a in ('curl -s --ssl-reqd ip.codex.storage') do @echo %a",
|
|
|
|
|
);
|
|
|
|
|
return result.trim();
|
|
|
|
|
} else {
|
|
|
|
|
return await this.shell.run("curl -s https://ip.codex.storage");
|
|
|
|
|
}
|
2025-04-14 11:27:58 +02:00
|
|
|
};
|
2025-04-09 15:25:49 +02:00
|
|
|
|
|
|
|
|
doThing = async () => {
|
2025-04-14 11:27:58 +02:00
|
|
|
if (this.config.dataDir.length < 1)
|
|
|
|
|
throw new Error("Missing config: dataDir");
|
|
|
|
|
if (this.config.logsDir.length < 1)
|
|
|
|
|
throw new Error("Missing config: logsDir");
|
2025-04-09 15:25:49 +02:00
|
|
|
|
|
|
|
|
console.log("start a codex detached");
|
|
|
|
|
|
2025-04-14 11:27:58 +02:00
|
|
|
console.log("nat: " + (await this.getPublicIp()));
|
2025-04-09 15:25:49 +02:00
|
|
|
console.log("logs dir: " + this.getLogFile());
|
2025-04-14 09:50:42 +02:00
|
|
|
console.log("data dir: " + this.config.dataDir);
|
|
|
|
|
console.log("api port: " + this.config.ports.apiPort);
|
|
|
|
|
console.log("codex exe: " + this.config.codexExe);
|
|
|
|
|
console.log("quota: " + this.config.storageQuota);
|
2025-04-09 15:25:49 +02:00
|
|
|
|
2025-04-14 09:50:42 +02:00
|
|
|
const executable = this.config.codexExe;
|
|
|
|
|
const args = [
|
|
|
|
|
`--data-dir=${this.config.dataDir}`,
|
|
|
|
|
// `--log-level=DEBUG`,
|
|
|
|
|
// `--log-file="${this.getLogFile()}"`,
|
|
|
|
|
`--storage-quota=${this.config.storageQuota}`,
|
|
|
|
|
`--disc-port=${this.config.ports.discPort}`,
|
|
|
|
|
`--listen-addrs=/ip4/0.0.0.0/tcp/${this.config.ports.listenPort}`,
|
|
|
|
|
`--api-port=${this.config.ports.apiPort}`,
|
|
|
|
|
`--nat=extip:${await this.getPublicIp()}`,
|
|
|
|
|
`--api-cors-origin="*"`,
|
|
|
|
|
`--bootstrap-node=spr:CiUIAhIhAiJvIcA_ZwPZ9ugVKDbmqwhJZaig5zKyLiuaicRcCGqLEgIDARo8CicAJQgCEiECIm8hwD9nA9n26BUoNuarCEllqKDnMrIuK5qJxFwIaosQ3d6esAYaCwoJBJ_f8zKRAnU6KkYwRAIgM0MvWNJL296kJ9gWvfatfmVvT-A7O2s8Mxp8l9c8EW0CIC-h-H-jBVSgFjg3Eny2u33qF7BDnWFzo7fGfZ7_qc9P`,
|
|
|
|
|
];
|
2025-04-09 15:25:49 +02:00
|
|
|
|
2025-04-14 09:50:42 +02:00
|
|
|
const command = `"${executable}" ${args.join(" ")}`;
|
|
|
|
|
console.log("command: " + command);
|
|
|
|
|
console.log("\n\n");
|
2025-04-09 15:25:49 +02:00
|
|
|
|
2025-04-14 11:27:58 +02:00
|
|
|
this.configService.writeCodexConfigFile();
|
|
|
|
|
|
|
|
|
|
var child = spawn(executable, args, {
|
|
|
|
|
detached: true,
|
|
|
|
|
stdio: ["ignore", "ignore", "ignore"],
|
|
|
|
|
});
|
2025-04-14 09:50:42 +02:00
|
|
|
child.unref();
|
2025-04-09 15:25:49 +02:00
|
|
|
|
2025-04-14 09:50:42 +02:00
|
|
|
await new Promise((resolve) => setTimeout(resolve, 2000));
|
|
|
|
|
return;
|
2025-04-14 11:27:58 +02:00
|
|
|
};
|
2025-04-09 15:25:49 +02:00
|
|
|
}
|