-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·161 lines (138 loc) · 4.47 KB
/
index.js
File metadata and controls
executable file
·161 lines (138 loc) · 4.47 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
#!/usr/bin/env node
import express from 'express';
import { createProxyMiddleware } from 'http-proxy-middleware';
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
// ========== Default Config ==========
const defaultPort = 9017;
const defaultTarget = 'https://api.openai.com';
// ========== Core Proxy App ==========
function createProxyApp(target, host) {
const app = express();
app.use('/', createProxyMiddleware(
{
...target,
changeOrigin: true,
on: {
proxyReq: (proxyReq, req, res) => {
proxyReq.removeHeader('x-forwarded-for');
proxyReq.removeHeader('x-real-ip');
if (host) {
proxyReq.setHeader('Host', host);
}
},
proxyRes: (proxyRes, req, res) => {
proxyRes.headers['Access-Control-Allow-Origin'] = '*';
proxyRes.headers['Access-Control-Allow-Headers'] = 'Origin, X-Requested-With, Content-Type, Accept, api_key, Authorization';
},
error: (err, req, res) => {
console.error('Proxy error:', err);
res.status(500).json({
error: 'Proxy Error',
message: err.message
});
},
},
}));
return app;
}
// ========== Main App ==========
const parseArguments = () => {
const argv = yargs(hideBin(process.argv))
.options({
port: {
type: 'number',
demandOption: false,
describe: 'Server port number (valid range: 1-65535)',
alias: 'P'
},
target: {
type: 'string',
demandOption: false,
describe: 'Target URL or API endpoint to connect to',
alias: 'T'
},
host: {
type: 'string',
demandOption: false,
describe: 'Host header specifying the domain name',
alias: 'H'
},
secure: {
type: 'boolean',
demandOption: false,
describe: 'Enables security features, such as TLS certificate validation',
alias: 'S'
}
})
.check(argv => {
if (argv.port !== undefined) {
if (!Number.isInteger(argv.port) || argv.port <= 0 || argv.port > 65535) {
throw new Error('Port must be an integer between 1 and 65535');
}
}
if (argv.security !== undefined && argv.security !== false) {
throw new Error('Do not set --security to true. If provided, --security must be false (use --security=false) because security features are enabled by default.');
}
return true;
}).strict()
.help().alias('help', 'h')
.argv;
return argv;
};
const validatePort = (port) => {
const portNum = parseInt(port);
if (isNaN(portNum)) throw new Error(`Invalid port: ${port}`);
if (portNum < 1 || portNum > 65535) throw new Error(`Port out of range: ${port}`);
return portNum;
};
const validateTarget = (target) => {
try {
new URL(target);
return target;
} catch {
throw new Error(`Invalid target URL: ${target}`);
}
};
const runProxy = () => {
try {
const args = parseArguments();
const port = args.port || process.env.PORT || defaultPort;
const target = args.target || process.env.TARGET || defaultTarget;
const host = args.host || process.env.HOST;
const secure = args.secure ?? process.env.SECURE
const validatedPort = validatePort(port);
const validatedTarget = validateTarget(target);
const validatedProxyParams = {
target: validatedTarget
}
if (secure === 'false' || secure === false) {
validatedProxyParams.secure = false
}
const app = createProxyApp(validatedProxyParams, host);
const server = app.listen(validatedPort, () => {
console.log('API Proxy running:');
console.log(` Local : http://localhost:${validatedPort}`);
console.log(` Target : ${validatedTarget}`);
console.log(` Host : ${host ? host : 'Inherited from the target URL'}`);
console.log(` Secure : ${validatedProxyParams.secure ?? 'Enabled by default'}`);
console.log('\nPress Ctrl+C to stop');
});
server.on('error', (err) => {
if (err.code === 'EADDRINUSE') {
console.error(`❌ Port ${validatedPort} is already in use`);
} else {
console.error(`❌ Server error: ${err.message}`);
}
process.exit(1);
});
process.on('SIGINT', () => {
console.log('\nShutting down proxy...');
server.close(() => process.exit(0));
});
} catch (error) {
console.error(`❌ Error: ${error.message}`);
process.exit(1);
}
};
runProxy();