Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
282 lines (235 loc) · 9.65 KB
/
Copy pathserver.js
File metadata and controls
282 lines (235 loc) · 9.65 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
'use strict';
const express = require('express');
const path = require('path');
const os = require('os');
const readline = require('readline/promises');
const { execFile, spawn } = require('child_process');
const steam = require('./src/steam');
const io = require('./src/io');
let PORT = 3000; // chosen interactively at startup (see promptStartup)
steam.init();
io.loadSettings();
console.log('[IO] Settings loaded');
const app = express();
const unsubbed = new Set(); // workshop IDs unsubscribed this session
app.use(express.json());
// ---- CSRF / DNS-rebinding guard ----
// This server performs destructive deletes. Without this, any website the user
// visits while ModScan is running could POST to localhost and wipe their mods.
// Requiring a localhost Host (and a matching Origin when present) blocks both
// cross-origin CSRF and DNS-rebinding attacks.
app.use('/api', (req, res, next) => {
const allowedHosts = [`localhost:${PORT}`, `127.0.0.1:${PORT}`];
const allowedOrigins = allowedHosts.map(h => `http://${h}`);
if (!allowedHosts.includes(req.headers.host)) {
return res.status(403).json({ error: 'Forbidden: unexpected Host header' });
}
if (req.headers.origin && !allowedOrigins.includes(req.headers.origin)) {
return res.status(403).json({ error: 'Forbidden: cross-origin request blocked' });
}
next();
});
app.use(express.static(path.join(__dirname, 'public')));
// ---- Status ----
app.get('/api/status', (_req, res) => {
res.json({ steam: steam.status(), paths: io.currentPaths() });
});
// ---- Settings ----
app.post('/api/settings', (req, res) => {
const { listPath, modPath } = req.body;
try {
io.saveSettings(listPath || '', modPath || '');
res.json({ ok: true });
} catch (err) {
console.error('[ERR]', err);
res.status(500).json({ error: err.message });
}
});
// ---- Scan ----
app.post('/api/scan', async (_req, res) => {
try {
io.loadSettings();
const { presets } = await io.scanPresets();
const whitelist = io.readWhitelist();
const allMods = await io.scanAllMods();
allMods.forEach(m => { m.unsubbed = unsubbed.has(m.id); });
const wlEntries = Object.entries(whitelist).map(([name, id]) => ({ name, id }));
res.json({ presets, allMods, whitelist: wlEntries });
} catch (err) {
console.error('[ERR]', err);
res.status(500).json({ error: err.message });
}
});
// ---- Unsubscribe ----
app.post('/api/unsubscribe', async (req, res) => {
const { ids } = req.body;
if (!Array.isArray(ids) || ids.length === 0) {
return res.status(400).json({ error: 'No IDs provided' });
}
try {
const results = await steam.unsubscribeItems(ids);
const succeeded = results.filter(r => r.success).map(r => r.id);
succeeded.forEach(id => unsubbed.add(id));
if (succeeded.length) {
await io.deleteModFolders(succeeded);
io.removeIdsFromAcf(succeeded);
}
res.json({ results });
} catch (err) {
console.error('[ERR]', err);
res.status(500).json({ error: err.message });
}
});
// ---- Whitelist ----
app.post('/api/whitelist/add', (req, res) => {
try {
io.addToWhitelist(req.body.entries || []);
res.json({ ok: true });
} catch (err) {
console.error('[ERR]', err);
res.status(500).json({ error: err.message });
}
});
app.post('/api/whitelist/remove', (req, res) => {
try {
io.removeFromWhitelist(req.body.names || []);
res.json({ ok: true });
} catch (err) {
console.error('[ERR]', err);
res.status(500).json({ error: err.message });
}
});
// ---- Folder picker ----
app.get('/api/pick-folder', (_req, res) => {
openFolderDialog()
.then(p => res.json({ path: p }))
.catch(err => res.status(500).json({ error: err.message }));
});
function openFolderDialog() {
if (process.platform === 'win32') {
return new Promise((resolve, reject) => {
const cmd = [
'Add-Type -AssemblyName System.Windows.Forms;',
'$f = New-Object System.Windows.Forms.FolderBrowserDialog;',
'$f.Description = \'Select folder\';',
'if ($f.ShowDialog() -eq \'OK\') { Write-Output $f.SelectedPath }',
].join(' ');
execFile('powershell', ['-NonInteractive', '-WindowStyle', 'Hidden', '-Command', cmd],
(err, stdout) => {
if (err) return reject(new Error('Could not open folder picker'));
resolve(stdout.trim() || null);
});
});
}
return tryExec('zenity', ['--file-selection', '--directory', '--title=Select Folder'])
.catch(() => tryExec('kdialog', ['--getexistingdirectory', os.homedir()]))
.catch(() => { throw new Error('No folder picker found — install zenity or kdialog'); });
}
function tryExec(cmd, args) {
return new Promise((resolve, reject) => {
execFile(cmd, args, (err, stdout) => {
if (err && err.code === 'ENOENT') return reject(new Error(`${cmd} not found`));
resolve(stdout.trim() || null);
});
});
}
// ---- Browser launch ----
// Resolve the user's browser choice into a spawnable command per platform.
// Linux candidates are tried in order so e.g. "chrome" works whether the
// binary is google-chrome or google-chrome-stable.
function browserCommand(choice, url) {
const p = process.platform;
if (choice === 'default' || !choice) {
if (p === 'win32') return [{ cmd: 'cmd', args: ['/c', 'start', '', url] }];
if (p === 'darwin') return [{ cmd: 'open', args: [url] }];
return [{ cmd: 'xdg-open', args: [url] }];
}
const known = {
firefox: { win: 'firefox', mac: 'Firefox', lin: ['firefox'] },
chrome: { win: 'chrome', mac: 'Google Chrome', lin: ['google-chrome', 'google-chrome-stable'] },
chromium: { win: 'chromium', mac: 'Chromium', lin: ['chromium', 'chromium-browser'] },
edge: { win: 'msedge', mac: 'Microsoft Edge', lin: ['microsoft-edge', 'microsoft-edge-stable'] },
brave: { win: 'brave', mac: 'Brave Browser', lin: ['brave', 'brave-browser'] },
};
const b = known[choice];
if (!b) {
// Unrecognised name — treat it as a raw executable / app name.
if (p === 'win32') return [{ cmd: 'cmd', args: ['/c', 'start', '', choice, url] }];
if (p === 'darwin') return [{ cmd: 'open', args: ['-a', choice, url] }];
return [{ cmd: choice, args: [url] }];
}
if (p === 'win32') return [{ cmd: 'cmd', args: ['/c', 'start', '', b.win, url] }];
if (p === 'darwin') return [{ cmd: 'open', args: ['-a', b.mac, url] }];
return b.lin.map(cmd => ({ cmd, args: [url] }));
}
function launchBrowser(choice, url) {
if (choice === 'none') {
console.log('Open that URL in your browser manually.\n');
return;
}
const candidates = browserCommand(choice, url);
const tryNext = i => {
if (i >= candidates.length) {
console.log(`Could not launch "${choice}". Open ${url} in your browser manually.\n`);
return;
}
const { cmd, args } = candidates[i];
const child = spawn(cmd, args, { detached: true, stdio: 'ignore' });
child.once('error', () => tryNext(i + 1));
child.unref();
};
tryNext(0);
}
// ---- Interactive startup (runs on every launch) ----
// Read a flag from argv, supporting both "--flag value" and "--flag=value".
function argVal(name) {
const i = process.argv.indexOf(`--${name}`);
if (i !== -1 && i + 1 < process.argv.length) return process.argv[i + 1];
const pref = `--${name}=`;
const eq = process.argv.find(a => a.startsWith(pref));
return eq ? eq.slice(pref.length) : undefined;
}
function parsePort(raw) {
const n = parseInt(raw, 10);
return (Number.isInteger(n) && n >= 1 && n <= 65535) ? n : null;
}
// CLI flags and env vars take precedence and let the app run headless (no TTY):
// --port <n> / MODSCAN_PORT --browser <name> / MODSCAN_BROWSER
async function promptStartup() {
let port = parsePort(argVal('port') ?? process.env.MODSCAN_PORT);
let browser = (argVal('browser') ?? process.env.MODSCAN_BROWSER ?? '').trim().toLowerCase() || null;
// Only prompt for values not already supplied, and only with an interactive stdin.
if ((port === null || browser === null) && process.stdin.isTTY) {
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
try {
if (port === null) {
port = parsePort((await rl.question('Port to run on [3000]: ')).trim());
}
if (browser === null) {
browser = (await rl.question(
'Open in browser (default/firefox/chrome/chromium/edge/brave/none) [default]: '
)).trim().toLowerCase() || null;
}
} finally {
rl.close();
}
}
PORT = port ?? 3000;
return { browser: browser || 'default' };
}
// ---- Start ----
(async () => {
const { browser } = await promptStartup();
const url = `http://localhost:${PORT}`;
app.listen(PORT, '127.0.0.1', () => {
console.log(`\nArma 3 Mod Cleaner → ${url}`);
launchBrowser(browser, url);
}).on('error', err => {
if (err.code === 'EADDRINUSE') {
console.error(`\nPort ${PORT} is already in use. Restart and choose a different port.`);
} else {
console.error('\nServer error:', err.message);
}
process.exit(1);
});
})();