-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
103 lines (94 loc) · 3.58 KB
/
Copy pathserver.js
File metadata and controls
103 lines (94 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
const express = require('express');
const path = require('path');
const fs = require('fs');
const os = require('os');
const { spawn } = require('child_process');
const crypto = require('crypto');
const app = express();
const PORT = 3456;
const isSea = process.execPath.toLowerCase().endsWith('appnetworkcontroller.exe');
const appDir = isSea ? path.dirname(process.execPath) : __dirname;
app.use(express.json());
app.use(express.static(path.join(appDir, 'public')));
const psScriptPath = path.join(appDir, 'scripts', 'app-controller.ps1');
function runPowerShell(scriptPath, args) {
return new Promise((resolve) => {
const psArgs = [
'-ExecutionPolicy', 'Bypass',
'-NoProfile',
'-File', scriptPath,
...args
];
const child = spawn('powershell', psArgs, {
windowsHide: true,
stdio: ['ignore', 'pipe', 'pipe'],
timeout: 120000
});
let stdout = '';
let stderr = '';
child.stdout.on('data', (data) => { stdout += data.toString(); });
child.stderr.on('data', (data) => { stderr += data.toString(); });
child.on('close', (code) => {
resolve({ error: code !== 0 ? `Exit code ${code}` : null, stdout, stderr, code });
});
child.on('error', (err) => {
resolve({ error: err.message, stdout, stderr, code: -1 });
});
});
}
app.get('/api/apps', async (req, res) => {
try {
const outFile = path.join(os.tmpdir(), `anc_${crypto.randomBytes(4).toString('hex')}.json`);
const args = ['-Action', 'list', '-OutputFile', outFile];
const psResult = await runPowerShell(psScriptPath, args);
if (psResult.error) {
return res.status(500).json({
success: false,
error: `PowerShell error: ${psResult.error}`,
stderr: psResult.stderr ? psResult.stderr.slice(0, 500) : ''
});
}
if (fs.existsSync(outFile)) {
let data = fs.readFileSync(outFile, 'utf8');
fs.unlinkSync(outFile);
data = data.replace(/^\uFEFF/, '');
const apps = JSON.parse(data);
res.json({ success: true, apps });
} else {
res.status(500).json({
success: false,
error: 'Output file not found',
details: { psScriptPath, outFile, psResult }
});
}
} catch (err) {
res.status(500).json({ success: false, error: err.toString() });
}
});
app.post('/api/toggle', async (req, res) => {
try {
const { name, exePath, block } = req.body;
if (!name || !exePath) {
return res.status(400).json({ success: false, error: 'Missing name or exePath' });
}
const action = block ? 'block' : 'unblock';
const args = ['-Action', action, '-AppName', name, '-ExePath', exePath];
const psResult = await runPowerShell(psScriptPath, args);
if (psResult.error) {
return res.status(500).json({
success: false,
error: `PowerShell error: ${psResult.error}`,
stderr: psResult.stderr ? psResult.stderr.slice(0, 500) : ''
});
}
res.json({ success: true, result: psResult.stdout });
} catch (err) {
res.status(500).json({ success: false, error: err.toString() });
}
});
app.get('*', (req, res) => {
res.sendFile(path.join(appDir, 'public', 'index.html'));
});
app.listen(PORT, '127.0.0.1', () => {
console.log(`App Network Controller running at http://127.0.0.1:${PORT}`);
});