feat: remove pslist and use sudo-prompt to close game if needed

This commit is contained in:
Zamitto 2024-07-03 15:29:59 -03:00
parent 0f5db4f34e
commit 1397e3932d
14 changed files with 51 additions and 114 deletions

View file

@ -14,12 +14,3 @@ export const logsPath = path.join(app.getPath("appData"), "hydra", "logs");
export const seedsPath = app.isPackaged
? path.join(process.resourcesPath, "seeds")
: path.join(__dirname, "..", "..", "seeds");
export const windowsStartupPath = path.join(
app.getPath("appData"),
"Microsoft",
"Windows",
"Start Menu",
"Programs",
"Startup"
);

View file

@ -1,39 +1,45 @@
import path from "node:path";
import { gameRepository } from "@main/repository";
import { getProcesses } from "@main/helpers";
import { registerEvent } from "../register-event";
import { RPCManager, logger } from "@main/services";
import sudo from "sudo-prompt";
import { app } from "electron";
const getKillCommand = (pid: number) => {
if (process.platform == "win32") {
return `taskkill /PID ${pid}`;
}
return `kill -9 ${pid}`;
};
const closeGame = async (
_event: Electron.IpcMainInvokeEvent,
gameId: number
) => {
const processes = await getProcesses();
const processes = await RPCManager.getProcessList();
const game = await gameRepository.findOne({
where: { id: gameId, isDeleted: false },
});
if (!game) return false;
const executablePath = game.executablePath!;
const basename = path.win32.basename(executablePath);
const basenameWithoutExtension = path.win32.basename(
executablePath,
path.extname(executablePath)
);
if (!game) return;
const gameProcess = processes.find((runningProcess) => {
if (process.platform === "win32") {
return runningProcess.name === basename;
}
return [basename, basenameWithoutExtension].includes(runningProcess.name);
return runningProcess.exe === game.executablePath;
});
if (gameProcess) return process.kill(gameProcess.pid);
return false;
if (gameProcess) {
try {
process.kill(gameProcess.pid);
} catch (err) {
sudo.exec(
getKillCommand(gameProcess.pid),
{ name: app.getName() },
(error, _stdout, _stderr) => {
logger.error(error);
}
);
}
}
};
registerEvent("closeGame", closeGame);

View file

@ -1,9 +1,6 @@
import { windowsStartupPath } from "@main/constants";
import { registerEvent } from "../register-event";
import AutoLaunch from "auto-launch";
import { app } from "electron";
import fs from "node:fs";
import path from "node:path";
const autoLaunch = async (
_event: Electron.IpcMainInvokeEvent,
@ -15,23 +12,10 @@ const autoLaunch = async (
name: app.getName(),
});
if (process.platform == "win32") {
const destination = path.join(windowsStartupPath, "Hydra.vbs");
if (enabled) {
const scriptPath = path.join(process.resourcesPath, "hydralauncher.vbs");
fs.copyFileSync(scriptPath, destination);
} else {
appLauncher.disable().catch();
fs.rmSync(destination);
}
if (enabled) {
appLauncher.enable().catch();
} else {
if (enabled) {
appLauncher.enable().catch();
} else {
appLauncher.disable().catch();
}
appLauncher.disable().catch();
}
};

View file

@ -57,5 +57,4 @@ export const requestWebPage = async (url: string) => {
.then((response) => response.data);
};
export * from "./ps";
export * from "./download-source";

View file

@ -1,41 +0,0 @@
import psList from "ps-list";
import path from "node:path";
import childProcess from "node:child_process";
import { promisify } from "node:util";
import { app } from "electron";
const TEN_MEGABYTES = 1000 * 1000 * 10;
const execFile = promisify(childProcess.execFile);
export const getProcesses = async () => {
if (process.platform == "win32") {
const binaryPath = app.isPackaged
? path.join(process.resourcesPath, "fastlist.exe")
: path.join(
__dirname,
"..",
"..",
"node_modules",
"ps-list",
"vendor",
"fastlist-0.3.0-x64.exe"
);
const { stdout } = await execFile(binaryPath, {
maxBuffer: TEN_MEGABYTES,
windowsHide: true,
});
return stdout
.trim()
.split("\r\n")
.map((line) => line.split("\t"))
.map(([pid, ppid, name]) => ({
pid: Number.parseInt(pid, 10),
ppid: Number.parseInt(ppid, 10),
name,
}));
} else {
return psList();
}
};

View file

@ -17,6 +17,7 @@ import {
PauseDownloadPayload,
LibtorrentStatus,
LibtorrentPayload,
ProcessPayload,
} from "./types";
export class RPCManager {
@ -49,8 +50,10 @@ export class RPCManager {
}
}
public static async getProccessList() {
return (await this.rpc.get<string[] | null>("/process-list")).data || [];
public static async getProcessList() {
return (
(await this.rpc.get<ProcessPayload[] | null>("/process-list")).data || []
);
}
public static async getStatus() {

View file

@ -31,3 +31,8 @@ export interface LibtorrentPayload {
status: LibtorrentStatus;
gameId: number;
}
export interface ProcessPayload {
exe: string;
pid: number;
}

View file

@ -19,14 +19,13 @@ export const watchProcesses = async () => {
});
if (games.length === 0) return;
const processes = await RPCManager.getProccessList();
const processes = await RPCManager.getProcessList();
for (const game of games) {
const executablePath = game.executablePath!;
const gameProcess = processes.find((runningProcess) => {
return executablePath == runningProcess;
return executablePath == runningProcess.exe;
});
if (gameProcess) {