Skip to content

Commit 5514116

Browse files
committed
Added status subcommand
Fixed 403 for landing page
1 parent 0dacac8 commit 5514116

File tree

3 files changed

+59
-38
lines changed

3 files changed

+59
-38
lines changed

bin.js

Lines changed: 50 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import { spawn } from 'node:child_process';
44
import { existsSync, readFileSync, unlinkSync, writeFileSync } from 'node:fs';
5+
import { createServer } from 'node:http';
56
import { tmpdir } from 'node:os';
67
import { join } from 'node:path';
78
import { parseArgs } from 'node:util';
@@ -28,12 +29,33 @@ Commands:
2829
run Run the CORS proxy server in the foreground
2930
start Start the CORS proxy server daemon
3031
stop Stop the CORS proxy server daemon
31-
32+
status Show the status of the CORS proxy server daemon
3233
Options:
3334
-h, --help Show this help message
3435
-p, --port <port> Port to listen on (default: 9999)
35-
--pid <path> Path to PID file (default: ${join(temp, 'cors-proxy.pid')}
36-
`);
36+
--pid <path> Path to PID file (default: ${join(temp, 'cors-proxy.pid')}`);
37+
}
38+
39+
function getPID(strict) {
40+
let pid;
41+
try {
42+
pid = parseInt(readFileSync(pidFile, 'utf8'));
43+
} catch (e) {
44+
if (e.code === 'ENOENT') {
45+
console.error('No PID file');
46+
process.exit(strict ? 2 : 0);
47+
}
48+
49+
console.error('Found existing PID file but could not read it');
50+
process.exit(strict ? 13 : 0);
51+
}
52+
53+
try {
54+
process.kill(pid, 0);
55+
return [pid, true];
56+
} catch (e) {
57+
return [pid, false];
58+
}
3759
}
3860

3961
switch (cmd) {
@@ -43,49 +65,47 @@ switch (cmd) {
4365
process.on('exit', () => {
4466
console.log('Shutting down server');
4567
server.close();
68+
unlinkSync(pidFile);
4669
});
4770
break;
4871
}
49-
5072
case 'start': {
5173
if (existsSync(pidFile)) {
52-
let pid;
53-
try {
54-
pid = parseInt(readFileSync(pidFile, 'utf8'));
55-
} catch (e) {
56-
console.error('Found existing PID file but could not read it');
57-
process.exit(13);
58-
}
74+
const [pid, processExists] = getPID(true);
5975

60-
try {
61-
process.kill(pid, 0);
62-
} catch {
63-
console.error(`Server is already running (pid is ${pid})`);
76+
if (processExists) {
77+
console.error(`Daemon is already running (pid is ${pid})`);
6478
process.exit(16);
79+
} else {
80+
unlinkSync(pidFile);
81+
console.error('Removed stale PID file');
6582
}
66-
67-
console.error('Removing stale PID file');
68-
unlinkSync(pidFile);
6983
}
7084

7185
const daemon = spawn(
72-
process.execPath,
86+
import.meta.filename,
7387
['run', port && `--port=${port}`].filter((a) => a),
74-
{
75-
stdio: 'ignore',
76-
detached: true,
77-
},
88+
{ stdio: 'ignore', detached: true },
7889
);
7990
daemon.unref();
8091
console.log('Started CORS proxy server with PID', daemon.pid);
8192
writeFileSync(pidFile, daemon.pid.toString());
82-
process.exit(0);
93+
break;
8394
}
84-
case 'stop':
85-
try {
86-
const pid = parseInt(readFileSync(pidFile, 'utf8'));
87-
process.kill(pid);
88-
} catch (e) {
89-
console.error('Invalid or missing PID file');
95+
case 'stop': {
96+
const [pid, processExists] = getPID(true);
97+
98+
if (processExists) process.kill(pid);
99+
else {
100+
unlinkSync(pidFile);
101+
console.error('Removed stale PID file');
90102
}
103+
break;
104+
}
105+
case 'status': {
106+
const [pid, processExists] = getPID(false);
107+
if (processExists) console.error('Daemon is running as pid', pid);
108+
else console.error('Not running, stale PID file');
109+
break;
110+
}
91111
}

index.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,6 @@ function isAllowed(req, u) {
8484
export default function handleRequest(req, res) {
8585
const u = parse(req.url, true);
8686

87-
if (!isAllowed(req, u)) {
88-
res.statusCode = 403;
89-
res.end();
90-
return;
91-
}
92-
93-
if (process.env.DEBUG) console.log(req.method, req.url);
94-
9587
// CORS
9688

9789
res.setHeader('Access-Control-Allow-Origin', origin);
@@ -123,6 +115,14 @@ export default function handleRequest(req, res) {
123115
return;
124116
}
125117

118+
if (!isAllowed(req, u)) {
119+
res.statusCode = 403;
120+
res.end();
121+
return;
122+
}
123+
124+
if (process.env.DEBUG) console.log(req.method, req.url);
125+
126126
let headers = {};
127127
for (let h of allowHeaders) {
128128
if (req.headers[h]) {

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"description": "Proxy clone and push requests for the browser",
55
"main": "index.js",
66
"bin": "bin.js",
7+
"type": "module",
78
"scripts": {
89
"start": "cors-proxy start",
910
"dev": "cors-proxy run",

0 commit comments

Comments
 (0)