-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.coffee
291 lines (263 loc) · 9 KB
/
server.coffee
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
express = require 'express'
_every = require 'lodash/every'
_values = require 'lodash/values'
_defaults = require 'lodash/defaults'
_mapValues = require 'lodash/mapValues'
_map = require 'lodash/map'
compress = require 'compression'
log = require 'loga'
helmet = require 'helmet'
z = require 'zorium'
Promise = require 'bluebird'
request = require 'clay-request'
cookieParser = require 'cookie-parser'
fs = require 'fs'
socketIO = require 'socket.io-client'
HttpHash = require 'http-hash'
RxBehaviorSubject = require('rxjs/BehaviorSubject').BehaviorSubject
require 'rxjs/add/operator/do'
require 'rxjs/add/operator/take'
require 'rxjs/add/operator/toPromise'
require 'rxjs/add/operator/publishReplay'
require 'rxjs/add/operator/concat'
config = require './src/config'
gulpPaths = require './gulp_paths'
App = require './src/app'
Model = require './src/models'
RouterService = require './src/services/router'
MIN_TIME_REQUIRED_FOR_HSTS_GOOGLE_PRELOAD_MS = 10886400000 # 18 weeks
HEALTHCHECK_TIMEOUT = 200
RENDER_TO_STRING_TIMEOUT_MS = 1200
BOT_RENDER_TO_STRING_TIMEOUT_MS = 4500
styles = if config.ENV is config.ENVS.PROD
fs.readFileSync gulpPaths.dist + '/bundle.css', 'utf-8'
else
null
# memwatch = require 'memwatch-next'
#
# hd = undefined
# snapshotTaken = false
# # memwatch.on 'stats', (stats) ->
# console.log 'stats:', stats
# if snapshotTaken is false
# hd = new (memwatch.HeapDiff)
# snapshotTaken = true
# setTimeout ->
# diff = hd.end()
# console.log(JSON.stringify(diff, null, '\t'))
# , 15000
# else
# # diff = hd.end()
# snapshotTaken = false
# # console.log(JSON.stringify(diff, null, '\t'))
app = express()
app.use compress()
# CSP is disabled because kik lacks support
# frameguard header is disabled because Native app frames page
app.disable 'x-powered-by'
app.use helmet.xssFilter()
app.use helmet.hsts
# https://hstspreload.appspot.com/
maxAge: MIN_TIME_REQUIRED_FOR_HSTS_GOOGLE_PRELOAD_MS
includeSubDomains: true # include in Google Chrome
preload: true # include in Google Chrome
force: true
app.use helmet.noSniff()
app.use cookieParser()
app.use '/healthcheck', (req, res, next) ->
Promise.all [
Promise.cast(request(config.API_URL + '/ping'))
.timeout HEALTHCHECK_TIMEOUT
.reflect()
]
.spread (api) ->
result =
api: api.isFulfilled()
isHealthy = _every _values result
if isHealthy
res.json {healthy: isHealthy}
else
res.status(500).json _defaults {healthy: isHealthy}, result
.catch next
app.use '/ping', (req, res) ->
res.send 'pong'
app.use '/setCookie', (req, res) ->
res.statusCode = 302
res.cookie 'first_cookie', '1', {maxAge: 3600 * 24 * 365 * 10}
res.setHeader 'Location', decodeURIComponent req.query?.redirect_url
res.end()
# app.get '/manifest.json', (req, res, next) ->
# try
# res.setHeader 'Content-Type', 'application/json'
# res.send new Buffer(req.query.data, 'base64').toString()
# catch err
# next err
# legacy 301s. can remove in may 2018
redirects =
'/clash-royale/mod/:key': '/tool/:key'
'/:lang/clash-royale/mod/:key': '/tool/:key'
'/pt/clash-royale/jogador/:playerId/embutir': '/tool/clash-royale-player/:playerId'
'/es/clash-royale/jugador/:playerId/emportar': '/tool/clash-royale-player/:playerId'
'/clash-royale/player/:playerId/embed': '/tool/clash-royale-player/:playerId'
'/clash-royale/user/id/:userId': '/user/id/:userId'
'/clash-royale/user/:username': '/user/:username'
'/:lang/clash-royale/user/id/:userId': '/user/id/:userId'
'/:lang/clash-royale/user/:username': '/user/:username'
'/:lang/clash-royale/*': '/g/clashroyale/*'
'/clash-royale/*': '/g/clashroyale/*'
'/:lang/clash-royale': '/g/clashroyale'
'/g/*': '/*'
# '/addons': '/clash-royale/mods'
# '/addon': '/clash-royale/mod'
# '/clan': '/clash-royale/clan'
# '/profile': '/clash-royale/profile'
# '/recruiting': '/clash-royale/recruit'
# '/forum': '/clash-royale/forum'
# '/clay-royale-player/:playerId': '/clash-royale/:playerId'
# '/players': '/clash-royale/players'
# '/player/:playerId': '/clash-royale/player/:playerId'
# '/players/search': '/clash-royale/players/search'
# '/user/:id': '/clash-royale/user/:id'
# '/conversation/:id': '/clash-royale/conversation/:id'
# '/thread/*': '/clash-royale/thread/*'
# # '/group/*': '/clash-royale/group/*'
# # '/user/id/:id/chests': '/clash-royale/chest-cycle/:id' # userId != playerId
# '/player/:playerId/chests': '/clash-royale/chest-cycle/:playerId'
_map redirects, (newPath, oldPath) ->
app.use oldPath, (req, res) ->
goPath = newPath
_map req.params, (value, key) ->
goPath = goPath.replace ":#{key}", value
oldPath = oldPath.replace ":#{key}", value
if oldPath.indexOf('*') isnt -1
oldPathRegex = new RegExp oldPath.replace('*', '(.*?)$')
matches = oldPathRegex.exec req.originalUrl
if matches
goPath = goPath.replace '*', matches[1]
else
goPath = '/'
res.redirect 301, goPath
# end legacy
if config.ENV is config.ENVS.PROD
then app.use express.static(gulpPaths.dist, {maxAge: '4h'})
else app.use express.static(gulpPaths.build, {maxAge: '4h'})
stats = JSON.parse \
fs.readFileSync gulpPaths.dist + '/stats.json', 'utf-8'
app.use (req, res, next) ->
# migrate to fam.gg
# check if native app
userAgent = req.headers['user-agent']
host = req.headers.host
accessToken = req.query.accessToken
isNativeApp = userAgent?.indexOf('starfire') isnt -1 or
userAgent?.indexOf('openfam') isnt -1
isiOS = /(iPad|iPhone|iPod)/g.test(userAgent)
isiOSApp = isNativeApp and isiOS
isBot = /bot|crawler|spider|crawling/i.test(userAgent)
isLegacyHost = host.indexOf('starfi.re') isnt -1 or
host.indexOf('redtritium.com') isnt -1 or
host.indexOf('starfire.games') isnt -1 or
host.indexOf('openfam.com') isnt -1
if isLegacyHost and req.cookies?.accessToken and not isBot and not isiOSApp
return res.redirect(
301
'https://fam.gg' + req.path +
'?accessToken=' + req.cookies?.accessToken
)
else if isLegacyHost and not isiOSApp
return res.redirect(301, 'https://fam.gg' + req.path)
else if accessToken and not isLegacyHost and not isiOSApp
res.cookie(
'accessToken'
accessToken
{
path: '/'
expires: new Date(Date.now() + 3600 * 24 * 365)
domain: '.' + host.split(':')[0]
}
)
return res.redirect(301, 'https://fam.gg' + req.path)
# end migrate
hasSent = false
io = socketIO config.API_HOST, {
path: (config.API_PATH or '') + '/socket.io'
timeout: 5000
transports: ['websocket']
}
fullLanguage = req.headers?['accept-language']
language = req.query?.lang or
req.cookies?['language'] or
fullLanguage?.substr(0, 2)
unless language in config.LANGUAGES
language = 'en'
model = new Model {
io, language, host
initialCookies: req.cookies
serverHeaders: req.headers
setCookie: (key, value, options) ->
res.cookie key, value, options
}
router = new RouterService {
router: null
model: model
host: host
}
requests = new RxBehaviorSubject(req)
# redirect /game/gameKey to correct group
hash = new HttpHash()
hash.set '/game/:gameKey', (params) ->
model.group.getByGameKeyAndLanguage(params.gameKey, language)
.take(1).subscribe (group) ->
res.redirect 302, "/g/#{group?.key or group?.id or 'fortnitees'}"
redirectToGameRoute = hash.get req.path
if redirectToGameRoute?.handler
return redirectToGameRoute.handler redirectToGameRoute.params
# for client to access
model.cookie.set(
'ip'
req.headers?['x-forwarded-for'] or req.connection.remoteAddress
)
if config.ENV is config.ENVS.PROD
scriptsCdnUrl = config.SCRIPTS_CDN_URL
bundlePath = "#{scriptsCdnUrl}/bundle_#{stats.hash}_#{language}.js"
bundleCssPath = "/bundle.css?#{stats.time}"
else
bundlePath = null
bundleCssPath = null
serverData = {req, res, bundlePath, bundleCssPath, styles}
userAgent = req.headers?['user-agent']
isFacebookCrawler = userAgent?.indexOf('facebookexternalhit') isnt -1 or
userAgent?.indexOf('Facebot') isnt -1
isOtherBot = userAgent?.indexOf('bot') isnt -1
isCrawler = isFacebookCrawler or isOtherBot
start = Date.now()
z.renderToString new App({requests, model, serverData, router, isCrawler}), {
timeout: if isCrawler \
then BOT_RENDER_TO_STRING_TIMEOUT_MS
else RENDER_TO_STRING_TIMEOUT_MS
}
.then (html) ->
io.disconnect()
model.dispose()
disposable = null
hasSent = true
# TODO: not sure why, but some paths (eg /g/clashroyale/somerandompage)
# send back before head exists
if html.indexOf('<head>') is -1
res.redirect 302, '/'
else
res.send '<!DOCTYPE html>' + html
.catch (err) ->
io.disconnect()
model.dispose()
disposable?.unsubscribe()
log.error err
if err.html
hasSent = true
if err.html.indexOf('<head>') is -1
res.redirect 302, '/'
else
res.send '<!DOCTYPE html>' + err.html
else
next err
module.exports = app