forked from land007/docker_theia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy.js
50 lines (42 loc) · 1.1 KB
/
proxy.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
const httpProxy = require('http-proxy');
const http = require('http');
const basicAuth = require('basic-auth');
const crypto = require('crypto');
var username = process.env['username'] || 'land007';
var password = process.env['password'] || 'fcea920f7412b5da7be0cf42b8c93759';
var proxy = httpProxy.createProxyServer({
target: {
host: 'localhost',
port: 3000
},
ws: true
});
var send401 = function(res) {
res.statusCode = 401;
res.setHeader('WWW-Authenticate', 'Basic realm=Authorization Required');
res.end('<html><body>Need some creds son</body></html>');
};
var proxyServer = http.createServer(function (req, res) {
var user = basicAuth(req);
if (!user) {
send401(res);
return;
}
var md5 = crypto.createHash('md5');
if (user.pass === undefined) {
md5.update('undefined');
} else {
md5.update(user.pass);
}
var pass = md5.digest('hex');
if (user.name !== username || pass !== password) {
send401(res);
return;
}
proxy.web(req, res);
});
proxyServer.on('upgrade', function (req, socket, head) {
proxy.ws(req, socket, head);
});
proxyServer.listen(5050);
console.log("listen 5050");