-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy.conf.mjs
103 lines (92 loc) · 3.18 KB
/
proxy.conf.mjs
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
// Proxy configuration for the Angular development server
const baseUrl = process.env.data_portal_base_url;
const basicAuth = process.env.data_portal_basic_auth;
const checkCert = !process.env.data_portal_ignore_cert;
const mockApi = !process.env.data_portal_with_backend;
const mockOidc = !process.env.data_portal_with_oidc;
// filter out standard headers
function filterHeaders(headers) {
return Object.entries(headers).reduce((acc, [key, value]) => {
// show only custom headers
if (!/x-/.test(key)) return acc;
acc[key] = value;
return acc;
}, {});
}
// log headers
function logHeaders(headers) {
const out = [];
for (const [key, value] of Object.entries(headers)) {
out.push(`${key}: ${value}`);
}
console.log(out.join('\n'));
}
// configure proxy
function configure(proxy) {
proxy.on('proxyReq', (proxyReq, req, res) => {
console.log('\n', '==>', req.method, req.url);
logHeaders(filterHeaders(proxyReq.getHeaders()));
});
proxy.on('proxyRes', (proxyRes, req, res) => {
console.log('\n', '<==', req.method, req.url);
console.log('Status:', proxyRes.statusCode, proxyRes.statusMessage);
logHeaders(filterHeaders(proxyRes.headers));
});
}
let target = baseUrl || 'http://127.0.0.1';
if (!target.endsWith('/')) target += '/';
const useProxy = (!mockApi || !mockOidc) && !target.startsWith('http://127.');
const config = {};
if (useProxy) {
const targetHost = new URL(target).hostname;
if (!/^(\.\d{1,3}){4}$/.test(targetHost)) {
try {
const hostsFile = '/etc/hosts';
const fs = await import('fs');
const hostsContent = await fs.promises.readFile(hostsFile, 'utf8');
const lines = hostsContent.split('\n');
const hostEntry = lines.find((line) => line.includes(targetHost));
if (hostEntry) {
const resolvedAddress = hostEntry.split(/\s+/)[0];
console.log(
`The target hostname ${targetHost} resolves to ${resolvedAddress} using the hosts file.`,
);
} else {
console.log(
`The target hostname ${targetHost} is not found in the hosts file.`,
);
const dns = await import('dns');
const addresses = await dns.promises.lookup(targetHost);
const resolvedAddress = addresses.address;
if (resolvedAddress.startsWith('127.')) {
console.log(
`The target hostname ${targetHost} resolves to a local address on the host computer.`,
);
console.warn('Please check the hosts file on your host computer!');
} else {
console.log(
`The target hostname ${targetHost} resolves to ${resolvedAddress} on the host computer.`,
);
}
}
} catch (err) {
console.warn(`Cannot resolve ${targetHost}:`, err);
}
}
console.log(`\nRunning proxy server with target: ${target}`);
if (basicAuth) {
console.log('Using basic authentication');
}
config['/api'] = {
target,
changeOrigin: true,
secure: checkCert,
auth: basicAuth,
logLevel: 'debug',
configure,
};
}
if (!mockOidc && baseUrl && !baseUrl.startsWith('http://127.')) {
console.log(`\n\x1b[33mPlease point your browser to: ${baseUrl}\x1b[0m\n`);
}
export default config;