-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.js
125 lines (104 loc) · 4.13 KB
/
index.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
'use strict';
const config = require('config');
const express = require('express');
const app = express();
const https = require('https');
const path = require('path');
const http = require('http');
const fs = require('fs');
const _ = require('lodash');
const { expressCspHeader, INLINE, NONE, SELF } = require('express-csp-header');
const prerender = require('prerender-node');
const configFe = config.util.loadFileConfigs('./public/config');
const pathSettings = path.resolve('./public/settings.js');
try {
var settingsFileTxt = '(function (window) { window.__config = window.__config || {};';
_(configFe).forEach(function (value, key) {
settingsFileTxt += ' window.__config.' + key + ' = ' + JSON.stringify(value) + ';';
});
settingsFileTxt += '}(this));';
fs.writeFileSync(pathSettings, settingsFileTxt);
} catch (err) {
console.log('Settings.json write FAILED to ' + pathSettings, err);
process.exit(1);
}
//TODO: list whitelisted urls to github with description
const cspConfig = config.csp;
const cspOptions = _.cloneDeep(cspConfig);
if (cspConfig) {
if (cspConfig.directives) {
if (typeof cspConfig.directives === 'string') {
cspConfig.directives = JSON.parse(cspConfig.directives);
cspOptions.directives = {};
}
Object.keys(cspConfig.directives).forEach(function(key, index) {
cspConfig.directives[key].forEach(function (value, k) {
if (k === 0) {
cspOptions.directives[key] = [];
}
if (value === 'none') {
cspOptions.directives[key].push(NONE);
} else if (value === 'self') {
cspOptions.directives[key].push(SELF);
} else if (value === 'inline') {
cspOptions.directives[key].push(INLINE);
} else {
cspOptions.directives[key].push(value);
}
});
});
}
app.use(expressCspHeader(cspOptions));
}
app.use(prerender.set('prerenderToken', 'CrrAflHAEiF44KMFkrs7'));
app.use(express.static(__dirname + '/public'));
const browserDetect = (req, res, next) => {
const ua = req.headers['user-agent'];
const msie = ua.indexOf('MSIE ');
if (msie > 0) {
// IE 10 or older => return version number
var IEversion = parseInt(ua.substring(msie + 5, ua.indexOf('.', msie)), 10);
console.log('IEversion', IEversion);
res.sendFile(__dirname + '/public/views/unknown_device.html');
return res.set('Permissions-Policy', 'interest-cohort=()'); // Opt-out of Google FLoC
}
const trident = ua.indexOf('Trident/');
if (trident > 0) {
// IE 11 => return version number
var rv = ua.indexOf('rv:');
var version = parseInt(ua.substring(rv + 3, ua.indexOf('.', rv)), 10);
console.log('IE 11');
res.sendFile(__dirname + '/public/views/unknown_device.html');
return res.set('Permissions-Policy', 'interest-cohort=()'); // Opt-out of Google FLoC
}
// other browser
next();
}
app.use(browserDetect);
app.get('/*', browserDetect, function (req, res) {
res.sendFile(__dirname + '/public/index.html');
res.set('Permissions-Policy', 'interest-cohort=()'); // Opt-out of Google FLoC
});
const host = process.env.HOST || null;
const portHttp = process.env.PORT || 3000;
http.createServer(app).listen(portHttp, host, function (err, res) {
if (err) {
console.log('Failed to start HTTP server on port' + portHttp, err);
return;
}
console.log('HTTP server listening on port ' + portHttp);
});
if (app.get('env') === 'development') {
const portHttps = process.env.PORT_SSL || 3001;
const options = {
key: fs.readFileSync('./config/certs/dev.citizenos.com.key'),
cert: fs.readFileSync('./config/certs/dev.citizenos.com.crt')
};
https.createServer(options, app).listen(portHttps, host, function (err, res) {
if (err) {
console.log('Failed to start HTTPS server on port' + portHttps, err);
return;
}
console.log('HTTPS server listening on port ' + portHttps);
});
}