forked from opencast/opencast-admin-interface
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxyServer.js
134 lines (117 loc) · 3.44 KB
/
proxyServer.js
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
const path = require("path");
const express = require("express");
const bodyParser = require('body-parser');
const { createProxyMiddleware } = require('http-proxy-middleware');
const requestDigest = require('request-digest');
const urlParser = require('url-parse');
const app = express();
const port = process.env.PORT || 5000;
// Get values of proxy host, username and password from npm command
let args = process.argv,
host = args[2],
username = args[3],
password = args[4];
// Validate settings
if (host === undefined) {
throw new Error("Runtime Parameter host is undefined!");
}
if (username === undefined) {
throw new Error("Runtime Parameter username is undefined!");
}
if (password === undefined) {
throw new Error("Runtime Parameter password is undefined!");
}
// Set up static files
// TODO: maybe future adjustments necessary when code is further implemented
app.use(
"/styles",
express.static(path.join(__dirname, "app/build/static/css"))
);
app.use(
"/modules",
express.static(path.join(__dirname, "app/src/components"))
);
app.use(
"/shared",
express.static(path.join(__dirname, "app/src/components"))
);
app.use(
"/public",
express.static(path.join(__dirname, "app/public/"))
);
app.use(
"/img",
express.static(path.join(__dirname, "app/src/img"))
);
app.use(
"/info",
express.static(path.join(__dirname, "test/app/GET/info"))
);
app.use(
"/i18n",
express.static(path.join(__dirname, "test/app/GET/i18n"))
);
app.use(
"/sysinfo",
express.static(path.join(__dirname, "test/app/GET/sysinfo"))
);
app.use('', (req, res, next) => {
console.log('Proxy ' + req.method + ' ' + req.url + ' -> ' + host + req.url);
let onReadFromBackend = function (error, response, body) {
if (error && (typeof error != 'object' || !error.hasOwnProperty('statusCode') || !error.hasOwnProperty('body'))) {
throw error;
}
// forward to client
res.statusCode = (response || error).statusCode;
body = body || (error ? error.body : '');
res.write(body);
res.end();
};
let parsed = urlParser(req.url);
let parsedHost = urlParser(host);
let escapedQuery = parsed.query.replace(',', '%2C');
let onForwardToBackend = function (body) {
let authConfig = {
host: parsedHost.protocol + '//' + parsedHost.hostname,
port: parsedHost.port,
path: parsed.pathname + escapedQuery,
method: req.method,
headers: {
'content-type': req.headers['content-type'],
'X-Requested-Auth': 'Digest'
},
jar: true
};
if (body.length) {
authConfig.body = body;
}
requestDigest(username, password).request(authConfig, onReadFromBackend);
};
let buffer = [];
req.on('data', function (chunk) {
buffer.push(chunk);
});
req.on('end', function () {
onForwardToBackend(Buffer.concat(buffer));
});
});
// todo: not sure if really needed
app.use(
"/admin-ng",
createProxyMiddleware({
target: host,
headers: {
'X-Requested-Auth': 'Digest'
},
changeOrigin: true
}));
app.use(
"/services",
createProxyMiddleware({
target: host,
headers: {
'X-Requested-Auth': 'Digest'
},
changeOrigin: true
}));
app.listen(port, () => console.log(`Listening on port ${port}`));