|
| 1 | +import { exec } from 'node:child_process' |
| 2 | +import { ipcMain } from 'electron' |
| 3 | +import { EventsKeys } from '../../shared/constants/eventsKeys.constant' |
| 4 | + |
| 5 | +interface ScheduledShutdown { |
| 6 | + id: string |
| 7 | + scheduledTime: Date |
| 8 | + isActive: boolean |
| 9 | + description?: string |
| 10 | + timeoutId?: NodeJS.Timeout |
| 11 | +} |
| 12 | + |
| 13 | +const activeShutdowns: Map<string, ScheduledShutdown> = new Map() |
| 14 | + |
| 15 | +ipcMain.handle( |
| 16 | + EventsKeys.SCHEDULE_SHUTDOWN, |
| 17 | + async ( |
| 18 | + _, |
| 19 | + data: { |
| 20 | + delay: number |
| 21 | + scheduledTime: Date |
| 22 | + description?: string |
| 23 | + }, |
| 24 | + ) => { |
| 25 | + try { |
| 26 | + const id = Date.now().toString() |
| 27 | + |
| 28 | + await cancelAllScheduledShutdowns() |
| 29 | + |
| 30 | + const shutdownCommand = getShutdownCommand(Math.floor(data.delay / 1000)) |
| 31 | + |
| 32 | + return new Promise<string>((resolve, reject) => { |
| 33 | + exec(shutdownCommand, (error) => { |
| 34 | + if (error) { |
| 35 | + console.error('Failed to schedule shutdown:', error) |
| 36 | + reject(new Error('Failed to schedule shutdown')) |
| 37 | + return |
| 38 | + } |
| 39 | + |
| 40 | + const shutdown: ScheduledShutdown = { |
| 41 | + id, |
| 42 | + scheduledTime: new Date(data.scheduledTime), |
| 43 | + isActive: true, |
| 44 | + description: data.description, |
| 45 | + } |
| 46 | + |
| 47 | + activeShutdowns.set(id, shutdown) |
| 48 | + resolve(id) |
| 49 | + }) |
| 50 | + }) |
| 51 | + } catch (error) { |
| 52 | + throw new Error(`Failed to schedule shutdown: ${error}`) |
| 53 | + } |
| 54 | + }, |
| 55 | +) |
| 56 | + |
| 57 | +ipcMain.handle( |
| 58 | + EventsKeys.CANCEL_SCHEDULED_SHUTDOWN, |
| 59 | + async (_, shutdownId: string) => { |
| 60 | + try { |
| 61 | + const shutdown = activeShutdowns.get(shutdownId) |
| 62 | + if (!shutdown) { |
| 63 | + throw new Error('Shutdown not found') |
| 64 | + } |
| 65 | + |
| 66 | + const cancelCommand = getCancelShutdownCommand() |
| 67 | + |
| 68 | + return new Promise<boolean>((resolve, reject) => { |
| 69 | + exec(cancelCommand, (error) => { |
| 70 | + if (error) { |
| 71 | + console.error('Failed to cancel shutdown:', error) |
| 72 | + reject(new Error('Failed to cancel shutdown')) |
| 73 | + return |
| 74 | + } |
| 75 | + |
| 76 | + activeShutdowns.delete(shutdownId) |
| 77 | + resolve(true) |
| 78 | + }) |
| 79 | + }) |
| 80 | + } catch (error) { |
| 81 | + throw new Error(`Failed to cancel shutdown: ${error}`) |
| 82 | + } |
| 83 | + }, |
| 84 | +) |
| 85 | + |
| 86 | +ipcMain.handle(EventsKeys.CLEAR_ALL_SHUTDOWNS, async () => { |
| 87 | + try { |
| 88 | + await cancelAllScheduledShutdowns() |
| 89 | + return { success: true } |
| 90 | + } catch (error) { |
| 91 | + throw new Error(`Failed to clear all shutdowns: ${error}`) |
| 92 | + } |
| 93 | +}) |
| 94 | + |
| 95 | +async function cancelAllScheduledShutdowns(): Promise<void> { |
| 96 | + const cancelCommand = getCancelShutdownCommand() |
| 97 | + |
| 98 | + return new Promise((resolve) => { |
| 99 | + exec(cancelCommand, (error) => { |
| 100 | + if (error) { |
| 101 | + console.log('No existing shutdown to cancel or failed to cancel') |
| 102 | + } |
| 103 | + // Clear our tracking |
| 104 | + activeShutdowns.clear() |
| 105 | + resolve() |
| 106 | + }) |
| 107 | + }) |
| 108 | +} |
| 109 | + |
| 110 | +function getShutdownCommand(delayInSeconds: number): string { |
| 111 | + const platform = process.platform |
| 112 | + |
| 113 | + switch (platform) { |
| 114 | + case 'win32': |
| 115 | + return `shutdown /s /t ${delayInSeconds} /c "Scheduled shutdown from DNS Changer"` |
| 116 | + case 'darwin': // macOS |
| 117 | + return `sudo shutdown -h +${Math.ceil(delayInSeconds / 60)}` // macOS uses minutes |
| 118 | + case 'linux': |
| 119 | + return `shutdown -h +${Math.ceil(delayInSeconds / 60)}` // Linux uses minutes |
| 120 | + default: |
| 121 | + throw new Error(`Unsupported platform: ${platform}`) |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +function getCancelShutdownCommand(): string { |
| 126 | + const platform = process.platform |
| 127 | + |
| 128 | + switch (platform) { |
| 129 | + case 'win32': |
| 130 | + return 'shutdown /a' |
| 131 | + case 'darwin': // macOS |
| 132 | + return 'sudo killall shutdown' |
| 133 | + case 'linux': |
| 134 | + return 'shutdown -c' |
| 135 | + default: |
| 136 | + throw new Error(`Unsupported platform: ${platform}`) |
| 137 | + } |
| 138 | +} |
0 commit comments