-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
94 lines (76 loc) · 2.65 KB
/
Copy pathserver.js
File metadata and controls
94 lines (76 loc) · 2.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
const chalk = require('chalk');
const { loadConfig } = require('./src/loader');
const { createProxyServer } = require('./src/proxy');
const { RequestLogger } = require('./src/request-logger');
function parseCliArgs() {
const args = process.argv.slice(2);
// Parse --log-group with its value
let logGroup = null;
const logGroupIndex = args.findIndex(arg => arg === '--log-group');
if (logGroupIndex !== -1 && args[logGroupIndex + 1]) {
logGroup = args[logGroupIndex + 1];
}
// Parse --port with its value
let port = null;
const portIndex = args.findIndex(arg => arg === '--port' || arg === '-p');
if (portIndex !== -1 && args[portIndex + 1]) {
port = parseInt(args[portIndex + 1], 10);
}
return {
logGroup,
port,
help: args.includes('--help') || args.includes('-h')
};
}
function showHelp() {
console.log(`
${chalk.bold('Interceptor CLI - Proxy Server')}
${chalk.bold('Usage:')}
node server.js [options]
${chalk.bold('Options:')}
--port, -p <port> Override the port from config file
--log-group <name> Save all requests/responses to logs/<name> directory
--help, -h Show this help message
${chalk.bold('Environment Variables:')}
CONFIG_FILE Path to config file (default: config.yaml)
${chalk.bold('Examples:')}
node server.js --port 8000
node server.js --log-group session1 # saves to logs/session1/
npm run dev -- --port 8000 --log-group test # saves to logs/test/
`);
}
function main() {
const cliArgs = parseCliArgs();
if (cliArgs.help) {
showHelp();
process.exit(0);
}
const config = loadConfig();
if (!config.port || !config.target) {
console.error(chalk.red('Missing required configuration: port and target must be specified in config file!'));
process.exit(1);
}
// Initialize request logger - log-group is always a subdirectory within logs/
const logDir = cliArgs.logGroup ? `logs/${cliArgs.logGroup}` : null;
const requestLogger = new RequestLogger(logDir);
// Override port from CLI if provided
const proxyConfig = {
port: cliArgs.port || config.port,
target: config.target
};
try {
const server = createProxyServer(proxyConfig, config.logging || {}, requestLogger);
process.on('SIGINT', () => {
console.log(chalk.yellow('\n\n👋 Shutting down proxy server...'));
if (requestLogger.enabled) {
console.log(chalk.green(`Saved ${requestLogger.getRequestCount()} requests to ${requestLogger.getLogDir()}`));
}
server.close();
process.exit(0);
});
} catch (error) {
console.error(chalk.red(`Failed to start proxy server:`, error.message));
process.exit(1);
}
}
main();