This repository has been archived by the owner on Oct 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 169
/
server.js
229 lines (205 loc) · 8.17 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
'use strict';
var express = require('express');
var ejs = require('ejs');
var path = require('path');
var compression = require('compression');
loadEnv('.env');
var port = parseInt(process.env.PORT, 10) || 8080;
var walletHelperPort = port + 1;
var runWalletHelper = !Boolean(process.env.WALLET_HELPER_URL);
var dist = parseInt(process.env.DIST, 10) === 1;
var rootURL = process.env.ROOT_URL || 'https://blockchain.info';
var webSocketURL = process.env.WEB_SOCKET_URL || false;
var apiDomain = process.env.API_DOMAIN;
var production = Boolean(rootURL === 'https://blockchain.info');
var iSignThisDomain = production ? 'https://verify.isignthis.com/' : 'https://stage-verify.isignthis.com/';
var walletHelperFrameDomain = process.env.WALLET_HELPER_URL || `http://localhost:${ walletHelperPort }`;
var sfoxProduction = parseInt(process.env.SFOX_USE_PRODUCTION, 10) === 1;
var unocoinProduction = parseInt(process.env.UNOCOIN_USE_PRODUCTION, 10) === 1;
// App configuration
var rootApp = express();
var app = express();
var helperApp = runWalletHelper ? express() : null;
app.use(compression());
if (runWalletHelper) {
helperApp.use(compression());
}
rootApp.use('/:lang?/wallet', app);
rootApp.set('json spaces', 2);
rootApp.get('/:lang?/search', (req, res) => {
res.redirect(`${rootURL}/search?search=${req.query.search}`);
});
app.use(function (req, res, next) {
var cspHeader;
if (req.url === '/') {
cspHeader = ([
"img-src 'self' " + rootURL + ' data: blob: android-webview-video-poster:',
// echo -n "outline: 0;" | openssl dgst -sha256 -binary | base64
// "outline: 0;" : ud+9... from ui-select
// "margin-right: 10px" : 4If ... from ui-select
// The above won't work in Chrome due to: https://bugs.chromium.org/p/chromium/issues/detail?id=571234
// Safari throws the same error, but without suggesting an hash to whitelist.
// Firefox appears to just allow unsafe-inline CSS
"style-src 'self' 'uD+9kGdg1SXQagzGsu2+gAKYXqLRT/E07bh4OhgXN8Y=' '4IfJmohiqxpxzt6KnJiLmxBD72c3jkRoQ+8K5HT5K8o='",
`child-src ${ walletHelperFrameDomain } ${ iSignThisDomain} `,
`frame-src ${ walletHelperFrameDomain } ${ iSignThisDomain} `,
"script-src 'self'",
'connect-src ' + [
"'self'",
rootURL,
(webSocketURL || 'wss://ws.blockchain.info'),
(webSocketURL || 'wss://ws.blockchain.info/inv').replace('/inv', '/eth/inv'),
(webSocketURL || 'wss://ws.blockchain.info/inv').replace('/inv', '/bch/inv'),
(apiDomain || 'https://api.blockchain.info'),
'https://api.sfox.com',
'https://shapeshift.io',
`https://app-api.${!production ? 'sandbox.' : ''}coinify.com`,
`https://api.${sfoxProduction ? '' : 'staging.'}sfox.com`,
`https://quotes.${sfoxProduction ? '' : 'staging.'}sfox.com`,
`https://sfox-kyc${sfoxProduction ? '' : 'test'}.s3.amazonaws.com`,
`https://${unocoinProduction ? 'www.' : 'sandbox.'}unocoin.${unocoinProduction ? 'com' : 'co'}`
].join(' '),
"object-src 'none'",
"media-src 'self' https://storage.googleapis.com/bc_public_assets/ data: mediastream: blob:",
"font-src 'self'", ''
]).join('; ');
res.setHeader('content-security-policy', cspHeader);
res.setHeader('X-Frame-Options', 'SAMEORIGIN');
res.render(dist ? 'index.html' : 'build/index.pug', {pretty: true});
return;
} else if (req.url === '/landing.html') {
res.render(dist ? 'landing.html' : 'build/landing.pug');
return;
}
if (dist) {
res.setHeader('Cache-Control', 'public, max-age=31557600');
} else {
res.setHeader('Cache-Control', 'public, max-age=0, no-cache');
}
next();
});
if (runWalletHelper) {
helperApp.use(function (req, res, next) {
var cspHeader;
if (req.url === '/wallet-helper/plaid/') {
cspHeader = ([
"img-src 'none'",
"style-src 'self'",
'child-src https://cdn.plaid.com',
'frame-src https://cdn.plaid.com',
'frame-ancestors http://localhost:8080',
"script-src 'self' https://cdn.plaid.com https://ajax.googleapis.com",
"connect-src 'none'",
"object-src 'none'",
"media-src 'none'",
"font-src 'self'"
]).join('; ');
res.setHeader('content-security-policy', cspHeader);
// Not supported in Chrome, so using frame-ancestors instead
// res.setHeader('X-Frame-Options', 'ALLOW-FROM http://localhost:8080/');
res.render('plaid/index.html');
return;
} else if (req.url === '/wallet-helper/sift-science/') {
cspHeader = ([
'img-src https://hexagon-analytics.com',
"style-src 'none'",
"child-src 'none'",
"frame-src 'none'",
'frame-ancestors http://localhost:8080',
"script-src 'self' https://ajax.googleapis.com https://cdn.siftscience.com",
"connect-src 'none'",
"object-src 'none'",
"media-src 'none'",
"font-src 'none'"
]).join('; ');
res.setHeader('content-security-policy', cspHeader);
// Not supported in Chrome, so using frame-ancestors instead
// res.setHeader('X-Frame-Options', 'ALLOW-FROM http://localhost:8080/');
res.render('sift-science/index.html');
return;
}
if (dist) {
res.setHeader('Cache-Control', 'public, max-age=31557600');
} else {
res.setHeader('Cache-Control', 'public, max-age=0, no-cache');
}
next();
});
}
rootApp.use(function (req, res, next) {
if (req.url === '/') {
res.redirect('wallet/');
} else if (req.url === '/wallet') {
res.redirect('wallet/');
} else if (req.url === '/Resources/wallet-options.json') {
var parsedJSON = require('./rootApp/Resources/wallet-options.json');
parsedJSON.domains = {
root: process.env.ROOT_URL,
webSocket: process.env.WEB_SOCKET_URL,
api: process.env.API_DOMAIN,
walletHelperUrl: walletHelperFrameDomain
};
parsedJSON.network = process.env.NETWORK || 'bitcoin';
parsedJSON.partners.sfox.production = sfoxProduction;
parsedJSON.partners.sfox.apiKey = process.env.SFOX_API_KEY || parsedJSON.partners.sfox.apiKey;
parsedJSON.partners.sfox.plaidEnv = process.env.SFOX_PLAID_ENV || parsedJSON.partners.sfox.plaidEnv;
parsedJSON.partners.sfox.siftScience = process.env.SFOX_SIFT_SCIENCE_KEY || parsedJSON.partners.sfox.siftScience;
parsedJSON.partners.unocoin.production = unocoinProduction;
res.json(parsedJSON);
} else {
next();
}
});
if (runWalletHelper) {
helperApp.engine('html', ejs.renderFile);
}
if (dist) {
console.log('Production mode: single javascript file, cached');
app.engine('html', ejs.renderFile);
app.use(express.static('dist'));
app.set('views', path.join(__dirname, 'dist'));
if (runWalletHelper) {
helperApp.use('/wallet-helper', express.static('dist/wallet-helper'));
helperApp.set('views', path.join(__dirname, 'dist/wallet-helper'));
}
} else {
console.log('Development mode: multiple javascript files, not cached');
app.use(express.static(__dirname));
app.set('view engine', 'pug');
app.set('views', __dirname);
if (runWalletHelper) {
helperApp.use('/wallet-helper', express.static(path.join(__dirname, 'helperApp/build')));
helperApp.set('views', path.join(__dirname, 'helperApp/build'));
}
}
rootApp.use(express.static(__dirname + '/rootApp'));
rootApp.use(function (req, res) {
res.status(404).send('<center><h1>404 Not Found</h1></center>');
res.setHeader('Access-Control-Allow-Origin', '*');
});
app.use(function (req, res) {
res.status(404).send('<center><h1>404 Not Found</h1></center>');
});
if (runWalletHelper) {
helperApp.use(function (req, res) {
res.status(404).send('<center><h1>404 Not Found</h1></center>');
});
}
rootApp.listen(port, function () {
console.log('Visit http://localhost:%d/', port);
});
if (runWalletHelper) {
helperApp.listen(walletHelperPort, function () {
console.log('Helper App running on http://localhost:%d/wallet-helper/', walletHelperPort);
});
} else {
console.log('Connect to Helper App on %s/wallet-helper/', walletHelperFrameDomain);
}
// Helper functions
function loadEnv (envFile) {
try {
require('node-env-file')(envFile);
} catch (e) {
console.log('You may optionally create a .env file to configure the server.');
}
}