mirror of
https://github.com/hydralauncher/hydra.git
synced 2025-02-03 00:33:49 +03:00
feat: crete kill-torrent
This commit is contained in:
parent
75c8f69e81
commit
0f5db4f34e
@ -24,7 +24,7 @@ const signOut = async (_event: Electron.IpcMainInvokeEvent) => {
|
|||||||
Sentry.setUser(null);
|
Sentry.setUser(null);
|
||||||
|
|
||||||
/* Disconnects libtorrent */
|
/* Disconnects libtorrent */
|
||||||
RPCManager.kill();
|
RPCManager.killTorrent();
|
||||||
|
|
||||||
await Promise.all([
|
await Promise.all([
|
||||||
databaseOperations,
|
databaseOperations,
|
||||||
|
@ -1,7 +1,11 @@
|
|||||||
import cp from "node:child_process";
|
import cp from "node:child_process";
|
||||||
|
|
||||||
import { Game } from "@main/entity";
|
import { Game } from "@main/entity";
|
||||||
import { RPC_PASSWORD, RPC_PORT, startTorrentClient } from "./torrent-client";
|
import {
|
||||||
|
RPC_PASSWORD,
|
||||||
|
RPC_PORT,
|
||||||
|
startTorrentClient as startRPCClient,
|
||||||
|
} from "./torrent-client";
|
||||||
import { gameRepository } from "@main/repository";
|
import { gameRepository } from "@main/repository";
|
||||||
import { DownloadProgress } from "@types";
|
import { DownloadProgress } from "@types";
|
||||||
import { QueryDeepPartialEntity } from "typeorm/query-builder/QueryPartialEntity";
|
import { QueryDeepPartialEntity } from "typeorm/query-builder/QueryPartialEntity";
|
||||||
@ -27,7 +31,7 @@ export class RPCManager {
|
|||||||
});
|
});
|
||||||
|
|
||||||
public static spawn(args?: StartDownloadPayload) {
|
public static spawn(args?: StartDownloadPayload) {
|
||||||
this.rpcProcess = startTorrentClient(args);
|
this.rpcProcess = startRPCClient(args);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static kill() {
|
public static kill() {
|
||||||
@ -38,8 +42,15 @@ export class RPCManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static killTorrent() {
|
||||||
|
if (this.rpcProcess) {
|
||||||
|
this.rpc.post("/action", { action: "kill-torrent" });
|
||||||
|
this.downloadingGameId = -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static async getProccessList() {
|
public static async getProccessList() {
|
||||||
return (await this.rpc.get<string[] | null>("/process-list")).data;
|
return (await this.rpc.get<string[] | null>("/process-list")).data || [];
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async getStatus() {
|
public static async getStatus() {
|
||||||
|
@ -20,7 +20,7 @@ export const watchProcesses = async () => {
|
|||||||
|
|
||||||
if (games.length === 0) return;
|
if (games.length === 0) return;
|
||||||
|
|
||||||
const processes = (await RPCManager.getProccessList()) || [];
|
const processes = await RPCManager.getProccessList();
|
||||||
|
|
||||||
for (const game of games) {
|
for (const game of games) {
|
||||||
const executablePath = game.executablePath!;
|
const executablePath = game.executablePath!;
|
||||||
|
@ -30,6 +30,15 @@ class Downloader:
|
|||||||
self.torrent_handles[game_id] = None
|
self.torrent_handles[game_id] = None
|
||||||
self.downloading_game_id = -1
|
self.downloading_game_id = -1
|
||||||
|
|
||||||
|
def cancel_all_downloads(self):
|
||||||
|
for game_id in self.torrent_handles:
|
||||||
|
torrent_handle = self.torrent_handles[game_id]
|
||||||
|
torrent_handle.pause()
|
||||||
|
self.session.remove_torrent(torrent_handle)
|
||||||
|
|
||||||
|
self.torrent_handles = {}
|
||||||
|
self.downloading_game_id = -1
|
||||||
|
|
||||||
def get_download_status(self):
|
def get_download_status(self):
|
||||||
if self.downloading_game_id == -1:
|
if self.downloading_game_id == -1:
|
||||||
return None
|
return None
|
||||||
|
@ -33,23 +33,28 @@ class Handler(BaseHTTPRequestHandler):
|
|||||||
status = downloader.get_download_status()
|
status = downloader.get_download_status()
|
||||||
|
|
||||||
self.wfile.write(json.dumps(status).encode('utf-8'))
|
self.wfile.write(json.dumps(status).encode('utf-8'))
|
||||||
if self.path == "/healthcheck":
|
|
||||||
|
elif self.path == "/healthcheck":
|
||||||
self.send_response(200)
|
self.send_response(200)
|
||||||
self.end_headers()
|
self.end_headers()
|
||||||
|
|
||||||
if self.path == "/process-list":
|
elif self.path == "/process-list":
|
||||||
if self.headers.get(self.rpc_password_header) != rpc_password:
|
if self.headers.get(self.rpc_password_header) != rpc_password:
|
||||||
self.send_response(401)
|
self.send_response(401)
|
||||||
self.end_headers()
|
self.end_headers()
|
||||||
return
|
return
|
||||||
|
|
||||||
process_path = list(set([proc.info["exe"] for proc in psutil.process_iter(['exe'])]))
|
process_path = list(set([proc.info["exe"] for proc in psutil.process_iter(['exe'])]))
|
||||||
|
|
||||||
self.send_response(200)
|
self.send_response(200)
|
||||||
self.send_header("Content-type", "application/json")
|
self.send_header("Content-type", "application/json")
|
||||||
self.end_headers()
|
self.end_headers()
|
||||||
|
|
||||||
self.wfile.write(json.dumps(process_path).encode('utf-8'))
|
self.wfile.write(json.dumps(process_path).encode('utf-8'))
|
||||||
|
|
||||||
def do_POST(self):
|
def do_POST(self):
|
||||||
|
global downloader
|
||||||
|
|
||||||
if self.path == "/action":
|
if self.path == "/action":
|
||||||
if self.headers.get(self.rpc_password_header) != rpc_password:
|
if self.headers.get(self.rpc_password_header) != rpc_password:
|
||||||
self.send_response(401)
|
self.send_response(401)
|
||||||
@ -69,6 +74,9 @@ class Handler(BaseHTTPRequestHandler):
|
|||||||
downloader.pause_download(data['game_id'])
|
downloader.pause_download(data['game_id'])
|
||||||
elif data['action'] == 'cancel':
|
elif data['action'] == 'cancel':
|
||||||
downloader.cancel_download(data['game_id'])
|
downloader.cancel_download(data['game_id'])
|
||||||
|
elif data['action'] == 'kill-torrent':
|
||||||
|
downloader.cancel_all_downloads()
|
||||||
|
downloader = None
|
||||||
|
|
||||||
self.send_response(200)
|
self.send_response(200)
|
||||||
self.end_headers()
|
self.end_headers()
|
||||||
|
Loading…
Reference in New Issue
Block a user