forked from steemit/condenser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
334 lines (302 loc) · 10.3 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
import path from 'path';
import Koa from 'koa';
import mount from 'koa-mount';
import helmet from 'koa-helmet';
import koa_logger from 'koa-logger';
import requestTime from './requesttimings';
import StatsLoggerClient from './utils/StatsLoggerClient';
import hardwareStats from './hardwarestats';
import cluster from 'cluster';
import os from 'os';
import prod_logger from './prod_logger';
import favicon from 'koa-favicon';
import staticCache from 'koa-static-cache';
import useRedirects from './redirects';
import useGeneralApi from './api/general';
import useAccountRecoveryApi from './api/account_recovery';
import useEnterAndConfirmEmailPages from './sign_up_pages/enter_confirm_email';
import useEnterAndConfirmMobilePages from './sign_up_pages/enter_confirm_mobile';
import useUserJson from './json/user_json';
import usePostJson from './json/post_json';
import isBot from 'koa-isbot';
import session from '@steem/crypto-session';
import csrf from 'koa-csrf';
import minimist from 'minimist';
import config from 'config';
import { routeRegex } from 'app/ResolveRoute';
import secureRandom from 'secure-random';
import userIllegalContent from 'app/utils/userIllegalContent';
import koaLocale from 'koa-locale';
import { getSupportedLocales } from './utils/misc';
import { pinnedPosts } from './utils/PinnedPosts';
if (cluster.isMaster) console.log('application server starting, please wait.');
// import uploadImage from 'server/upload-image' //medium-editor
const app = new Koa();
app.name = 'Steemit app';
const env = process.env.NODE_ENV || 'development';
// cache of a thousand days
const cacheOpts = { maxAge: 86400000, gzip: true, buffer: true };
// Serve static assets without fanfare
app.use(
favicon(path.join(__dirname, '../app/assets/images/favicons/favicon.ico'))
);
app.use(
mount(
'/favicons',
staticCache(
path.join(__dirname, '../app/assets/images/favicons'),
cacheOpts
)
)
);
app.use(
mount(
'/images',
staticCache(path.join(__dirname, '../app/assets/images'), cacheOpts)
)
);
// Proxy asset folder to webpack development server in development mode
if (env === 'development') {
const webpack_dev_port = process.env.PORT
? parseInt(process.env.PORT) + 1
: 8081;
const proxyhost = 'http://0.0.0.0:' + webpack_dev_port;
console.log('proxying to webpack dev server at ' + proxyhost);
const proxy = require('koa-proxy')({
host: proxyhost,
map: filePath => 'assets/' + filePath,
});
app.use(mount('/assets', proxy));
} else {
app.use(
mount(
'/assets',
staticCache(path.join(__dirname, '../../dist'), cacheOpts)
)
);
}
let resolvedAssets = false;
let supportedLocales = false;
if (process.env.NODE_ENV === 'production') {
resolvedAssets = require(path.join(
__dirname,
'../..',
'/tmp/webpack-stats-prod.json'
));
supportedLocales = getSupportedLocales();
}
app.use(isBot());
// set number of processes equal to number of cores
// (unless passed in as an env var)
const numProcesses = process.env.NUM_PROCESSES || os.cpus().length;
const statsLoggerClient = new StatsLoggerClient(process.env.STATSD_IP);
app.use(requestTime(statsLoggerClient));
app.keys = [config.get('session_key')];
const crypto_key = config.get('server_session_secret');
session(app, {
maxAge: 1000 * 3600 * 24 * 60,
crypto_key,
key: config.get('session_cookie_key'),
});
csrf(app);
koaLocale(app);
function convertEntriesToArrays(obj) {
return Object.keys(obj).reduce((result, key) => {
result[key] = obj[key].split(/\s+/);
return result;
}, {});
}
// some redirects and health status
app.use(function*(next) {
if (this.method === 'GET' && this.url === '/.well-known/healthcheck.json') {
this.status = 200;
this.body = {
status: 'ok',
docker_tag: process.env.DOCKER_TAG ? process.env.DOCKER_TAG : false,
source_commit: process.env.SOURCE_COMMIT
? process.env.SOURCE_COMMIT
: false,
};
return;
}
// redirect to home page/feed if known account
if (this.method === 'GET' && this.url === '/' && this.session.a) {
this.status = 302;
this.redirect(`/@${this.session.a}/feed`);
return;
}
// normalize user name url from cased params
if (
this.method === 'GET' &&
(routeRegex.UserProfile1.test(this.url) ||
routeRegex.PostNoCategory.test(this.url) ||
routeRegex.Post.test(this.url))
) {
const p = this.originalUrl.toLowerCase();
let userCheck = '';
if (routeRegex.Post.test(this.url)) {
userCheck = p.split('/')[2].slice(1);
} else {
userCheck = p.split('/')[1].slice(1);
}
if (userIllegalContent.includes(userCheck)) {
console.log('Illegal content user found blocked', userCheck);
this.status = 451;
return;
}
if (p !== this.originalUrl) {
this.status = 301;
this.redirect(p);
return;
}
}
// normalize top category filtering from cased params
if (this.method === 'GET' && routeRegex.CategoryFilters.test(this.url)) {
const p = this.originalUrl.toLowerCase();
if (p !== this.originalUrl) {
this.status = 301;
this.redirect(p);
return;
}
}
// // do not enter unless session uid & verified phone
// if (this.url === '/create_account' && !this.session.uid) {
// this.status = 302;
// this.redirect('/enter_email');
// return;
// }
// remember ch, cn, r url params in the session and remove them from url
if (this.method === 'GET' && /\?[^\w]*(ch=|cn=|r=)/.test(this.url)) {
let redir = this.url.replace(/((ch|cn|r)=[^&]+)/gi, r => {
const p = r.split('=');
if (p.length === 2) this.session[p[0]] = p[1];
return '';
});
redir = redir.replace(/&&&?/, '');
redir = redir.replace(/\?&?$/, '');
console.log(`server redirect ${this.url} -> ${redir}`);
this.status = 302;
this.redirect(redir);
} else {
yield next;
}
});
// load production middleware
if (env === 'production') {
app.use(require('koa-conditional-get')());
app.use(require('koa-etag')());
app.use(require('koa-compressor')());
}
// Logging
if (env === 'production') {
app.use(prod_logger());
} else {
app.use(koa_logger());
}
app.use(
helmet({
hsts: false,
})
);
app.use(
mount(
'/static',
staticCache(path.join(__dirname, '../app/assets/static'), cacheOpts)
)
);
app.use(
mount('/robots.txt', function*() {
this.set('Cache-Control', 'public, max-age=86400000');
this.type = 'text/plain';
this.body = 'User-agent: *\nAllow: /';
})
);
// set user's uid - used to identify users in logs and some other places
// FIXME SECURITY PRIVACY cycle this uid after a period of time
app.use(function*(next) {
const last_visit = this.session.last_visit;
this.session.last_visit = (new Date().getTime() / 1000) | 0;
const from_link = this.request.headers.referer;
if (!this.session.uid) {
this.session.uid = secureRandom.randomBuffer(13).toString('hex');
this.session.new_visit = true;
if (from_link) this.session.r = from_link;
} else {
this.session.new_visit = this.session.last_visit - last_visit > 1800;
if (!this.session.r && from_link) {
this.session.r = from_link;
}
}
yield next;
});
useRedirects(app);
useEnterAndConfirmEmailPages(app);
useEnterAndConfirmMobilePages(app);
useUserJson(app);
usePostJson(app);
useAccountRecoveryApi(app);
useGeneralApi(app);
// helmet wants some things as bools and some as lists, makes config difficult.
// our config uses strings, this splits them to lists on whitespace.
if (env === 'production') {
const helmetConfig = {
directives: convertEntriesToArrays(config.get('helmet.directives')),
reportOnly: config.get('helmet.reportOnly'),
setAllHeaders: config.get('helmet.setAllHeaders'),
};
helmetConfig.directives.reportUri = helmetConfig.directives.reportUri[0];
if (helmetConfig.directives.reportUri === '-') {
delete helmetConfig.directives.reportUri;
}
app.use(helmet.contentSecurityPolicy(helmetConfig));
}
if (env !== 'test') {
const appRender = require('./app_render');
app.use(function*() {
// Load the pinned posts and store them on the ctx for later use. Since
// we're inside a generator, we can't `await` here, so we pass a promise
// so `src/server/app_render.jsx` can `await` on it.
this.pinnedPostsPromise = pinnedPosts();
yield appRender(this, supportedLocales, resolvedAssets);
// if (app_router.dbStatus.ok) recordWebEvent(this, 'page_load');
const bot = this.state.isBot;
if (bot) {
console.log(
` --> ${this.method} ${this.originalUrl} ${
this.status
} (BOT '${bot}')`
);
}
});
const argv = minimist(process.argv.slice(2));
const port = process.env.PORT ? parseInt(process.env.PORT) : 8080;
if (env === 'production') {
if (cluster.isMaster) {
for (var i = 0; i < numProcesses; i++) {
cluster.fork();
}
// if a worker dies replace it so application keeps running
cluster.on('exit', function(worker) {
console.log(
'error: worker %d died, starting a new one',
worker.id
);
cluster.fork();
});
} else {
app.listen(port);
if (process.send) process.send('online');
console.log(`Worker process started for port ${port}`);
}
} else {
// spawn a single thread if not running in production mode
app.listen(port);
if (process.send) process.send('online');
console.log(`Application started on port ${port}`);
}
}
// set PERFORMANCE_TRACING to the number of seconds desired for
// logging hardware stats to the console
if (process.env.PERFORMANCE_TRACING)
setInterval(hardwareStats, 1000 * process.env.PERFORMANCE_TRACING);
module.exports = app;