forked from TerriaJS/RaPPMap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
415 lines (339 loc) · 13 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
"use strict";
var url = require('url');
var configSettings = require('./wwwroot/config.json');
var proxyAuth = require('./proxyAuth.json');
var protocolRegex = /^\w+:\//;
var upstreamProxy;
var bypassUpstreamProxyHosts;
var dontProxyHeaderRegex = /^(?:Host|X-Forwarded-Host|Proxy-Connection|Connection|Keep-Alive|Transfer-Encoding|TE|Trailer|Proxy-Authorization|Proxy-Authenticate|Upgrade)$/i;
function filterHeaders(req, headers) {
var result = {};
// filter out headers that are listed in the regex above
Object.keys(headers).forEach(function(name) {
if (!dontProxyHeaderRegex.test(name)) {
result[name] = headers[name];
}
});
return result;
}
function filterResponseHeaders(req, headers, maxAgeSeconds) {
var result = filterHeaders(req, headers);
result['Cache-Control'] = 'public,max-age=' + maxAgeSeconds;
result['Access-Control-Allow-Origin'] ='*';
delete result['Expires'];
delete result['pragma'];
return result;
}
var proxyDomains = configSettings.proxyDomains;
//Non CORS hosts and domains we proxy to
function proxyAllowedHost(host) {
host = host.toLowerCase();
//check that host is from one of these domains
for (var i = 0; i < proxyDomains.length; i++) {
if (host.indexOf(proxyDomains[i], host.length - proxyDomains[i].length) !== -1) {
return true;
}
}
return false;
}
var durationRegex = /^([\d.]+)(ms|s|m|h|d|w|y)$/;
var durationUnits = {
ms: 1.0 / 1000,
s: 1.0,
m: 60.0,
h: 60.0 * 60.0,
d: 24.0 * 60.0 * 60.0,
w: 7.0 * 24.0 * 60.0 * 60.0,
y: 365.0 * 24.0 * 60.0 * 60.0
};
function doProxy(req, res, next, callback) {
var maxAgeSeconds = 1209600; // two weeks
var remoteUrlString = req.params[0];
if (!remoteUrlString || remoteUrlString.length === 0) {
return res.status(400).send('No url specified.');
}
// Does the proxy URL include a max age?
if (remoteUrlString[0] === '_') {
var slashIndex = remoteUrlString.indexOf('/');
if (slashIndex < 0) {
return res.status(400).send('No url specified.');
}
var maxAgeString = remoteUrlString.substring(1, slashIndex);
remoteUrlString = remoteUrlString.substring(slashIndex + 1);
if (remoteUrlString.length === 0) {
return res.status(400).send('No url specified.');
}
// Interpret the max age as a duration in Varnish notation.
// https://www.varnish-cache.org/docs/trunk/reference/vcl.html#durations
var parsedMaxAge = durationRegex.exec(maxAgeString);
if (!parsedMaxAge || parsedMaxAge.length < 3) {
return res.status(400).send('Invalid duration.');
}
var value = parseFloat(parsedMaxAge[1]);
if (value !== value) {
return res.status(400).send('Invalid duration.');
}
var unitConversion = durationUnits[parsedMaxAge[2]];
if (!unitConversion) {
return res.status(400).send('Invalid duration unit ' + parsedMaxAge[2]);
}
maxAgeSeconds = value * unitConversion;
}
// Add http:// if no protocol is specified.
var protocolMatch = protocolRegex.exec(remoteUrlString);
if (!protocolMatch || protocolMatch.length < 1) {
remoteUrlString = 'http://' + remoteUrlString;
} else {
var matchedPart = protocolMatch[0];
// If the protocol portion of the URL only has a single slash after it, the extra slash was probably stripped off by someone
// along the way (NGINX will do this). Add it back.
if (remoteUrlString[matchedPart.length] !== '/') {
remoteUrlString = matchedPart + '/' + remoteUrlString.substring(matchedPart.length);
}
}
var remoteUrl = url.parse(remoteUrlString);
// Copy the query string
remoteUrl.search = url.parse(req.url).search;
if (!remoteUrl.protocol) {
remoteUrl.protocol = 'http:';
}
var proxy;
if (upstreamProxy && !(remoteUrl.host in bypassUpstreamProxyHosts)) {
proxy = upstreamProxy;
}
// Are we allowed to proxy for this host?
if (!proxyAllowedHost(remoteUrl.host)) {
res.status(400).send('Host is not in list of allowed hosts: ' + remoteUrl.host);
return;
}
// encoding : null means "body" passed to the callback will be raw bytes
var proxiedRequest;
req.on('close', function() {
if (proxiedRequest) {
proxiedRequest.abort();
}
});
var filteredReqHeaders = filterHeaders(req, req.headers);
if (!filteredReqHeaders['x-forwarded-for']) {
filteredReqHeaders['x-forwarded-for'] = req.connection.remoteAddress;
}
// http basic auth
var authRequired = proxyAuth[remoteUrl.host];
if (authRequired) {
filteredReqHeaders['authorization'] = authRequired.authorization;
}
proxiedRequest = callback(remoteUrl, filteredReqHeaders, proxy, maxAgeSeconds);
}
// Include the cluster module
var cluster = require('cluster');
// Code to run if we're in the master process
if (cluster.isMaster) {
// Count the machine's CPUs
var cpuCount = require('os').cpus().length;
// cpuCount = 1; //testing
console.log('Cores Used:', cpuCount);
// Create a worker for each CPU
for (var i = 0; i < cpuCount; i += 1) {
cluster.fork();
}
// Listen for dying workers
cluster.on('exit', function (worker) {
// Replace the dead worker, we're not sentimental
console.log('Worker ' + worker.id + ' died :(');
cluster.fork();
});
// Code to run if we're in a worker process
} else {
/*global console,require,__dirname*/
/*jshint es3:false*/
var express = require('express');
var fs = require('fs');
var compression = require('compression');
var request = require('request');
var path = require('path');
var cors = require('cors');
var formidable = require('formidable');
var ogr2ogr = require('ogr2ogr');
var proj4 = require('proj4');
//TODO: check if this loads the file into each core and if so then,
require('proj4js-defs/epsg')(proj4);
var yargs = require('yargs').options({
'port' : {
'default' : 3001,
'description' : 'Port to listen on.'
},
'public' : {
'type' : 'boolean',
'default' : true,
'description' : 'Run a public server that listens on all interfaces.'
},
'upstream-proxy' : {
'description' : 'A standard proxy server that will be used to retrieve data. Specify a URL including port, e.g. "http://proxy:8000".'
},
'bypass-upstream-proxy-hosts' : {
'description' : 'A comma separated list of hosts that will bypass the specified upstream_proxy, e.g. "lanhost1,lanhost2"'
},
'help' : {
'alias' : 'h',
'type' : 'boolean',
'description' : 'Show this help.'
}
});
var argv = yargs.argv;
if (argv.help) {
return yargs.showHelp();
}
// eventually this mime type configuration will need to change
// https://github.com/visionmedia/send/commit/d2cb54658ce65948b0ed6e5fb5de69d022bef941
var mime = express.static.mime;
mime.define({
'application/json' : ['czml', 'json', 'geojson'],
'text/plain' : ['glsl']
});
var app = express();
app.use(compression());
app.use(cors());
app.disable('etag');
app.use(express.static(path.join(__dirname, 'wwwroot')));
upstreamProxy = argv['upstream-proxy'];
bypassUpstreamProxyHosts = {};
if (argv['bypass-upstream-proxy-hosts']) {
argv['bypass-upstream-proxy-hosts'].split(',').forEach(function(host) {
bypassUpstreamProxyHosts[host.toLowerCase()] = true;
});
}
app.get('/ping', function(req, res){
res.status(200).send('OK');
});
app.get('/proxy/*', function(req, res, next) {
doProxy(req, res, next, function(remoteUrl, filteredRequestHeaders, proxy, maxAgeSeconds) {
return request.get({
url : url.format(remoteUrl),
headers : filteredRequestHeaders,
encoding : null,
proxy : proxy
}, function(error, response, body) {
var code = 500;
if (response) {
code = response.statusCode;
res.header(filterResponseHeaders(req, response.headers, maxAgeSeconds));
}
res.status(code).send(body);
});
});
});
app.post('/proxy/*', function(req, res, next) {
doProxy(req, res, next, function(remoteUrl, filteredRequestHeaders, proxy, maxAgeSeconds) {
req.pipe(request.post({
url : url.format(remoteUrl),
headers : filteredRequestHeaders,
encoding : null,
proxy : proxy
}, function(error, response, body) {
var code = 500;
if (response) {
code = response.statusCode;
res.header(filterResponseHeaders(req, response.headers, maxAgeSeconds));
}
res.status(code).send(body);
}));
});
});
//provide REST service for proj4 definition strings
app.get('/proj4def/:crs', function(req, res, next) {
var crs = req.param('crs');
var epsg = proj4.defs[crs.toUpperCase()];
if (epsg !== undefined) {
res.status(200).send(epsg);
} else {
res.status(500).send('no proj4 definition');
}
});
// provide conversion to geojson service
// reguires install of gdal on server: sudo apt-get install gdal-bin
app.post('/convert', function(req, res, next) {
var form = new formidable.IncomingForm();
form.parse(req, function(err, fields, files) {
var fname, fpath, inputStream;
var maxSize = 1000000;
if (fields.input_url !== undefined) {
if (fields.input_url.indexOf('http') === 0) {
fpath = fields.input_url;
fname = fpath;
}
} else if (files.input_file !== undefined) {
if (files.input_file.size <= maxSize) {
fpath = files.input_file.path;
fname = files.input_file.name;
} else {
console.log('Input file is too large', files.input_file.size);
}
}
if (fpath === undefined) {
res.status(500).send('Unable to convert data');
return;
}
console.log('Converting', fname);
var hint = '';
//simple hint for now, might need to crack zip files going forward
if (fname.toLowerCase().indexOf('.zip') === fname.length-4) {
hint = 'shp';
}
if (fpath.indexOf('http') === 0) {
inputStream = request.get({url: fpath}).on('response', function(response) {
var request = this, len = 0;
response.on('data', function (chunk) {
len += chunk.length;
if (len > maxSize) {
request.abort();
}
});
response.on('end', function() {
console.log('Convert download size', len);
});
});
} else {
inputStream = fs.createReadStream(fpath);
}
var ogr = ogr2ogr(inputStream, hint)
.skipfailures()
.options(['-t_srs', 'EPSG:4326']);
ogr.exec(function (er, data) {
if (er) {
console.error(er);
}
if (data !== undefined) {
res.status(200).send(JSON.stringify(data));
} else {
res.status(500).send('Unable to convert data');
}
});
});
});
//Share record storage
app.post('/upload', function(req, res, next) {
});
app.get('/get/:id', function(req, res, next) {
});
//sample simple NM service
app.post('/nm_service_1', function(req, res, next) {
//receive the posted object
var form = new formidable.IncomingForm();
form.parse(req, function(err, fields, files) {
//create a layer for NM to display
var obj = {
name: 'Bikes Available',
type: 'DATA',
proxy: false,
url: 'http://nationalmap.nicta.com.au/test/bike_racks.geojson'
};
//send a response with the object and display text
res.json({ displayHtml: 'Here are the available bike racks.', layer: obj});
});
});
// Redirect unknown pages back home. We don't actually have a 404 page, for starters.
app.use(function(req, res, next) {
res.redirect(303, '/');
});
app.listen(argv.port, argv.public ? undefined : 'localhost');
}