-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
171 lines (134 loc) · 5.78 KB
/
server.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
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
162
163
164
165
166
167
168
169
170
171
/**
* Created by meissner on 05.01.17.
* Main script for the api-proxy server
*/
// Enable unsecure/self signed certificates
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
// WebServer
var http = require('http');
// Serve static pages
var finalhandler = require('finalhandler');
var serveStatic = require('serve-static');
// To parse urls
var parser = require('url');
var path = require('path');
// To parse post data
var querystring = require('querystring');
// Http proxy
var httpProxy = require('http-proxy');
// Serve static files of the webUi
var serve = serveStatic("./webui/");
// Make requests (used to obtain the token)
var request = require("request");
// Function to return json for internal api
function jsonResponse(responseData, errorMessage, errorCode) {
var response = {data:responseData};
if (errorMessage) {
response.error = true;
response.error_message = errorMessage;
response.errorCode = errorCode;
} else {
response.error = false;
}
return JSON.stringify(response);
}
var proxy = httpProxy.createProxyServer({ws: true});
// Create http server
var httpServer = http.createServer(function (req, res) {
// Parse the request url
var url = parser.parse(req.url, true);
// Decide action based on path
switch(url.pathname.split("/")[1]) {
case 'requestToken':
// TODO: Clean this up and move it into a module or at least function
// Get token from api
console.log("Request for token");
res.setHeader('Content-Type', 'application/json');
// Check if post request
if (req.method != 'POST') {
res.end(jsonResponse(null, 'Only POST-Requests allowed'));
}
var postBody = "";
req.on('data', function (chunk) {
// Append to post
postBody += chunk;
});
req.on('end', function () {
var postData = querystring.parse(postBody);
// TODO: Check format of apiServer url and check if all values are set and valid
// Obtain token from OpenShift API
var url = 'https://' + postData.username + ':' + postData.password + '@' + postData.server + '/oauth/authorize?client_id=openshift-challenging-client&response_type=token';
request({url: url}, function (error, response, body) {
if (typeof response !== 'undefined' && typeof response.request !== 'undefined' && typeof response.request.href !== 'undefined') {
// Redirect works and got url to token
// Quick & dirty method to parse the access_token
var redirectUrl = response.request.href;
// Check if redirect contains requestToken
if (redirectUrl.indexOf("access_token=") > 1) {
var accessToken = redirectUrl.substring(redirectUrl.indexOf("access_token=")+13, redirectUrl.indexOf("&"));
console.log('Got token ' + accessToken);
res.end(jsonResponse({userToken: accessToken}));
} else {
res.end(jsonResponse(null, "Did not got an access_token, propably access denied", 403));
}
} else {
// TODO: Error handling to display wrong username/password
res.end(jsonResponse(null, "Did not got redirected and got no token", 500));
}
});
});
break;
case 'api-proxy':
// Simple proxy to api
console.log('api-proxy to server ' + url.query._server);
// Rewrite URL to original request
req.url = 'https://' + url.query._server + url.path.substr(10);
// Proxy Request
proxy.web(req, res, { target: 'https://' + url.query._server , secure: false});
break;
default:
// Serve static files from webgui
// Prepare the file-path (and append index.html if root dir)
var filePath = '.' + req.url;
if (filePath == './') {
filePath = './index.html';
}
var extname = path.extname(filePath);
switch (extname) {
case '.png':
case '.ico':
case '.svg':
case '.woff':
// dont log assets
break;
case '.js':
case '.html':
// TODO: Remove this in non-dev version or make variable to toggle it
// For debugging/dev purposes: no caching of js files
res.setHeader('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0');
res.setHeader('Pragma', 'no-cache');
// console.log('Serve static page: ' + req.url + ' (Without caching for dev)');
break;
default:
// Access log
// console.log('Serve static page: ' + req.url);
break;
}
var done = finalhandler(req, res);
serve(req, res, done);
}
});
httpServer.listen(8080);
// Proxy webSocket connections
httpServer.on('upgrade', function (req, socket, head) {
console.log('-- httpServer Upgrade detected');
// Simple proxy to api
var url = parser.parse(req.url, true);
console.log('api-proxy to server ' + url.query._server);
console.log(req.url);
// Rewrite URL to original request
req.url = 'wss://' + url.query._server + req.url.substr(10);
// Proxy upgrade request
proxy.ws(req, socket, head, {target: req.url, secure: false});
});
console.log("Now serving on http://localhost:8080");