-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathserver.js
More file actions
102 lines (81 loc) · 2.97 KB
/
Copy pathserver.js
File metadata and controls
102 lines (81 loc) · 2.97 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
const http = require('http');
const https = require('https');
const fs = require('fs');
const url = require('url');
const path = require('path');
// Load configuration
const config = JSON.parse(fs.readFileSync(path.join(__dirname, 'rr-config.json'), 'utf8'));
const replaceRules = JSON.parse(fs.readFileSync(path.join(__dirname, 'req-replace.json'), 'utf8'));
const PORT = config.port || 3030;
const BASE_URL = config.base_url || 'http://127.0.0.1:3000';
function applyReplacements(body) {
let result = body;
for (const [key, value] of Object.entries(replaceRules)) {
result = result.split(key).join(value);
}
return result;
}
const server = http.createServer((clientReq, clientRes) => {
console.log(`[${new Date().toISOString()}] ${clientReq.method} ${clientReq.url}`);
let body = [];
clientReq.on('data', (chunk) => {
body.push(chunk);
});
clientReq.on('end', () => {
let bodyString = Buffer.concat(body).toString();
if (bodyString) {
bodyString = applyReplacements(bodyString);
console.log(`[Replaced] Request body processed with ${Object.keys(replaceRules).length} rule(s)`);
}
const targetUrl = new URL(clientReq.url, BASE_URL);
const isHttps = targetUrl.protocol === 'https:';
const httpModule = isHttps ? https : http;
const headers = { ...clientReq.headers };
headers['host'] = targetUrl.host;
if (bodyString) {
headers['content-length'] = Buffer.byteLength(bodyString);
}
const options = {
hostname: targetUrl.hostname,
port: targetUrl.port || (isHttps ? 443 : 80),
path: targetUrl.pathname + targetUrl.search,
method: clientReq.method,
headers: headers
};
const proxyReq = httpModule.request(options, (proxyRes) => {
console.log(`[Response] ${proxyRes.statusCode} ${clientReq.url}`);
clientRes.writeHead(proxyRes.statusCode, proxyRes.headers);
proxyRes.pipe(clientRes);
});
proxyReq.on('error', (err) => {
console.error(`[Error] ${err.message}`);
if (!clientRes.headersSent) {
clientRes.writeHead(502, { 'Content-Type': 'text/plain' });
}
clientRes.end(`Proxy Error: ${err.message}`);
});
if (bodyString) {
proxyReq.write(bodyString);
}
proxyReq.end();
});
clientReq.on('error', (err) => {
console.error(`[Client Error] ${err.message}`);
if (!clientRes.headersSent) {
clientRes.writeHead(400, { 'Content-Type': 'text/plain' });
}
clientRes.end(`Client Request Error: ${err.message}`);
});
});
server.listen(PORT, () => {
console.log(`===========================================`);
console.log(`Proxy Server Running`);
console.log(`Listening on: http://127.0.0.1:${PORT}`);
console.log(`Forwarding to: ${BASE_URL}`);
console.log(`Replace rules: ${Object.keys(replaceRules).length} rule(s) loaded`);
console.log(`===========================================`);
});
server.on('error', (err) => {
console.error(`Server Error: ${err.message}`);
process.exit(1);
});