forked from billiegoose/cors-buster
-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathbin.js
More file actions
executable file
·124 lines (112 loc) · 3.83 KB
/
Copy pathbin.js
File metadata and controls
executable file
·124 lines (112 loc) · 3.83 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
#!/usr/bin/env node
import { spawn } from 'node:child_process';
import { existsSync, readFileSync, unlinkSync, writeFileSync } from 'node:fs';
import { createServer } from 'node:http';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { parseArgs } from 'node:util';
import handleRequest from './index.js';
const temp = tmpdir();
const {
positionals: [cmd],
values: { port, help, pid: pidFile, restart },
} = parseArgs({
options: {
help: { type: 'boolean', short: 'h', default: false },
port: { type: 'string', short: 'p', default: process.env.PORT || '9999' },
pid: { type: 'string', default: join(temp, 'cors-proxy.pid') },
restart: { type: 'boolean', short: 'r', default: false },
},
allowPositionals: true,
});
if (cmd == 'help' || help || !cmd) {
console.log(`Usage: ${process.argv0} [...options] <command>
Commands:
run Run the CORS proxy server in the foreground
start Start the CORS proxy server daemon
stop Stop the CORS proxy server daemon
status Show the status of the CORS proxy server daemon
Options:
-h, --help Show this help message
-p, --port <port> Port to listen on (default: 9999)
--pid <path> Path to PID file (default: ${join(temp, 'cors-proxy.pid')}
-r, --restart Restart the daemon if it is already running. Only valid with start`);
}
function getPID(strict) {
let pid;
try {
pid = parseInt(readFileSync(pidFile, 'utf8'));
} catch (e) {
if (e.code === 'ENOENT') {
console.error('No PID file');
process.exit(strict ? 2 : 0);
}
console.error('Found existing PID file but could not read it');
process.exit(strict ? 13 : 0);
}
try {
process.kill(pid, 0);
return [pid, true];
} catch (e) {
return [pid, false];
}
}
switch (cmd) {
case 'run': {
const server = createServer(handleRequest);
server.listen(port, () => console.log('Listening on port', port));
process.on('beforeExit', () => {
console.log('Shutting down server');
server.close();
unlinkSync(pidFile);
});
break;
}
case 'start': {
if (existsSync(pidFile)) {
const [pid, processExists] = getPID(true);
if (!processExists) {
unlinkSync(pidFile);
console.error('Removed stale PID file');
} else if (restart) {
process.kill(pid);
console.error('Stopped existing daemon');
} else {
console.error(`Daemon is already running (pid is ${pid})`);
process.exit(16);
}
}
// Spawn `node <this script> run [...]` rather than `<this script> run`
// directly. Invoking the .js file as the executable relies on the
// `#!/usr/bin/env node` shebang and therefore only works on Unix-like
// shells; on Windows, .js files are not directly executable through
// CreateProcess and the spawn call fails. Using `process.execPath`
// (the currently-running `node` binary) is portable across platforms.
// `windowsHide: true` prevents the detached child from flashing a
// console window on Windows.
const daemon = spawn(
process.execPath,
[import.meta.filename, 'run', port && `--port=${port}`].filter((a) => a),
{ stdio: 'ignore', detached: true, windowsHide: true },
);
daemon.unref();
console.log('Started CORS proxy daemon with PID', daemon.pid);
writeFileSync(pidFile, daemon.pid.toString());
break;
}
case 'stop': {
const [pid, processExists] = getPID(true);
if (processExists) process.kill(pid);
else {
unlinkSync(pidFile);
console.error('Removed stale PID file');
}
break;
}
case 'status': {
const [pid, processExists] = getPID(false);
if (processExists) console.error('Daemon is running as pid', pid);
else console.error('Not running, stale PID file');
break;
}
}