-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathserver.js
393 lines (359 loc) · 14.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
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
const fs = require('fs')
const express = require('express')
const cors = require('cors')
const path = require('path')
const { createClient } = require('lightrpc');
const htmlEncode = require('htmlencode').htmlEncode;
const app = express()
app.use(cors())
const port = process.env.PORT || 3000
const jsonfile = require('jsonfile')
const file = 'robots.json'
const crawlers = jsonfile.readFileSync(file)
const rootDomain = 'https://d.tube'
const lightrpc = createClient('https://api.steemit.com');
const javalon = require('javalon')
let layouts = {}
const puppeteer = require('puppeteer');
let browser = null
let page = null
let isOpeningTab = true
let isPuppeteering = false
let puppetReloadTime = 1000*60*15
let cacheCleanInterval = 1000*30
let cacheTimeout = 1000*60*30
let cache = {}
var date = new Date().getTime()
initBrowser(function() {
resetBrowserTab(function() {
initHttp(function() {
// resetting the browser tab every 10 minutes
setInterval(function() {
resetBrowserTab(function() {})
}, puppetReloadTime)
setInterval(function() {
cleanCache()
}, cacheCleanInterval)
var took = (new Date().getTime() - date)/1000
console.log('Startup took '+took+' sec')
})
})
})
function initHttp(cb) {
app.use('/DTube_files', express.static(path.join(__dirname, 'static/production/DTube_files')))
app.use('/favicon.ico', express.static(path.join(__dirname, 'static/production/DTube_files/images/dtubefavicon.png')))
app.get('*', function(req, res, next) {
// this is based on this list: https://github.com/monperrus/crawler-user-agents/blob/master/crawler-user-agents.json
var isRobot = getRobotName(req.headers['user-agent'])
// parsing the query
var reqPath = null
if (req.query._escaped_fragment_ && req.query._escaped_fragment_.length > 0)
reqPath = req.query._escaped_fragment_
else
reqPath = req.path
if (reqPath.startsWith('/sockjs/info')) {
res.send('{}')
return;
}
if (isRobot)
console.log(isRobot, 'GET', req.path, req.query)
if (!isRobot) return human(reqPath, res, next)
// Robots
// Served data for robots
if (reqPath == '/robots.txt') {
res.set('Content-Type', 'text/plain')
res.send(`User-agent: *\nDisallow:`)
} else if (reqPath.startsWith('/v/')) {
var date = new Date().getTime()
// video page
getVideoHTML(
reqPath.split('/')[2],
reqPath.split('/')[3],
function(err, contentHTML, pageTitle, description, url, snap, urlvideo, duration, embedUrl, author, permlink, snapDimensions) {
if (error(err, next)) return
getRobotHTML(function(err, baseHTML) {
if (error(err, next)) return
baseHTML = baseHTML.replace(/@@CONTENT@@/g, contentHTML)
baseHTML = baseHTML.replace(/@@TITLE@@/g, htmlEncode(pageTitle))
baseHTML = baseHTML.replace(/@@DESCRIPTION@@/g, htmlEncode(description))
baseHTML = baseHTML.replace(/@@URL@@/g, htmlEncode(url))
baseHTML = baseHTML.replace(/@@URLNOHASH@@/g, htmlEncode(url).replace('/#!',''))
// facebook minimum snap is 200x200 otherwise useless
baseHTML = baseHTML.replace(/@@SNAP@@/g, htmlEncode(snap))
baseHTML = baseHTML.replace(/@@VIDEO@@/g, htmlEncode(urlvideo))
baseHTML = baseHTML.replace(/@@EMBEDURL@@/g, htmlEncode(embedUrl))
baseHTML = baseHTML.replace(/@@AUTHOR@@/g, htmlEncode(author))
baseHTML = baseHTML.replace(/@@PERMLINK@@/g, htmlEncode(permlink))
snapDimensions = snapDimensions.split('x')
baseHTML = baseHTML.replace(/@@SNAPW@@/g, htmlEncode(snapDimensions[0]))
baseHTML = baseHTML.replace(/@@SNAPH@@/g, htmlEncode(snapDimensions[1]))
if (duration) {
var durationHTML = '<meta property="og:video:duration" content="@@VIDEODURATION@@" />'
durationHTML = durationHTML.replace(/@@VIDEODURATION@@/g, htmlEncode(""+Math.round(duration)))
baseHTML = baseHTML.replace(/@@METAVIDEODURATION@@/g, durationHTML)
} else {
baseHTML = baseHTML.replace(/@@METAVIDEODURATION@@/g, '')
}
var took = (new Date().getTime() - date)
console.log(took/1000+' sec for generating '+reqPath)
res.send(baseHTML)
})
})
} else {
// beta pre-rendering for all other pages
if (cache[reqPath] && new Date().getTime() - cache[reqPath].ts < cacheTimeout)
res.send(cache[reqPath].data)
else {
// console.log(isPuppeteering, isOpeningTab)
if (isPuppeteering || isOpeningTab)
human(reqPath, res, next)
else {
isPuppeteering = true;
(async () => {
try {
let url = 'http://localhost:'+port+'/#!'+reqPath
var date = new Date().getTime()
await page.setUserAgent('Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36');
await page.goto(url, {waitUntil: 'networkidle0'});
try {
await page.waitForSelector('#snapload', {
timeout: 1000
});
} catch (error) {
}
let content = await page.content()
content = content.replace(/\.\/DTube_files\//g, "/DTube_files/")
content = content.replace(/\/DTube_files\/[0-9a-fA-F]+.js/, "https://google.com/notforrobots.js")
var took = (new Date().getTime() - date)
console.log(took/1000+' sec for rendering '+reqPath)
// console.log(page.target()._targetInfo)
cache[reqPath] = {
data: content,
ts: new Date().getTime()
}
res.send(content)
isPuppeteering = false;
} catch (error) {
console.log(error)
isPuppeteering = false;
human(reqPath, res, next)
}
})();
}
}
}
})
var date = new Date().getTime()
app.listen(port, () => console.log('minidtube listening on port '+port), null, function() {
var took = (new Date().getTime() - date)/1000
console.log('Express.js port '+port+' started in '+took+' sec')
cb()
})
}
function human(reqPath, res, next) {
// Human User
// Serving static app and letting the real browser handle the rest
if (reqPath == '/') {
getHumanHTML(function(err, humanHTML) {
if (error(err, next)) return
res.send(humanHTML)
})
} else {
res.redirect('/#!'+reqPath);
}
}
function error(err, next) {
if (err) {
console.log(err)
next()
return true
}
return false
}
function getRobotHTML(cb) {
if (layouts.robot) {
cb(null, layouts.robot)
return
}
else {
fs.readFile(path.join(__dirname,"static","robots.html"), 'utf8', function (err,data) {
if (err) {
cb(err)
return
} else {
layouts.robot = data
cb(null, data)
return
}
});
}
}
function getHumanHTML(cb) {
if (layouts.human) {
cb(null, layouts.human)
return
} else {
fs.readFile(path.join(__dirname,"static","production","index.html"), 'utf8', function (err,data) {
if (err) {
cb(err)
return
} else {
layouts.human = data
cb(null, data)
return
}
});
}
}
function handleChainData(author, permlink, video, cb) {
if (video.json.ipfs) {
var hashVideo = video.json.ipfs.videohash
if (video.json.ipfs.video240hash) hashVideo = video.json.ipfs.video240hash
if (video.json.ipfs.video480hash) hashVideo = video.json.ipfs.video480hash
}
var html = ''
html += '<div class="fluidMedia"><iframe src="https://emb.d.tube/#!/'+author+'/'+permlink+'/true" frameborder="0" scrolling="no" webkitallowfullscreen="" mozallowfullscreen="" allowfullscreen=""></iframe></div>'
var title = video.json.title || video.json.info.title
html += '<h1>'+title+'</h1>'
html += '<h2>Author: <a href="/c/'+video.author+'">'+video.author+'</a></h2>'
var description = null
if (video.json.description)
description = video.json.description
else if (video.json.desc)
description = video.json.desc
else if (video.json.content && video.json.content.description)
description = video.json.content.description
if (description)
html += '<p><strong>Description: </strong>'+description.replace(/(?:\r\n|\r|\n)/g, '<br />')+'</p>'
var url = rootDomain+'/#!/v/'+author+'/'+permlink
var snap = null
var snapDimensions = "210x118"
if (video.json.ipfs && video.json.ipfs.snaphash)
snap = 'https://snap1.d.tube/ipfs/'+video.json.ipfs.snaphash
if (video.json.thumbnailUrl)
snap = video.json.thumbnailUrl
if (video.json.files && video.json.files.ipfs && video.json.files.ipfs.img['360']) {
snap = snap = 'https://snap1.d.tube/ipfs/'+video.json.files.ipfs.img['360']
snapDimensions = "640x360"
} else if (video.json.files && video.json.files.ipfs && video.json.files.ipfs.img['118'])
snap = snap = 'https://snap1.d.tube/ipfs/'+video.json.files.ipfs.img['118']
if (!snap && video.json.files && video.json.files.youtube) {
snap = 'https://i.ytimg.com/vi/'+video.json.files.youtube+'/hqdefault.jpg'
snapDimensions = "640x360"
}
var urlVideo = null
if (hashVideo)
urlVideo = 'https://player.d.tube/btfs/'+hashVideo
var embedUrl = 'https://emb.d.tube/#!/'+author+'/'+permlink+'/true'
var duration = video.json.duration || null
var author = video.author
var link = video.link
if (!link && video.permlink)
link = video.link
cb(null, html, title, description, url, snap, urlVideo, duration, embedUrl, author, link, snapDimensions)
}
function getVideoHTML(author, permlink, cb) {
var hasReplied = false
var steemDone = false
var avalonDone = false
javalon.getContent(author, permlink, function(err, video) {
if (hasReplied) return
avalonDone = true
if (err) {
if (steemDone && cb)
cb(err)
return
}
handleChainData(author, permlink, video, cb)
hasReplied = true
})
lightrpc.send('get_state', [`/dtube/@${author}/${permlink}`], function(err, result) {
if (hasReplied) return
steemDone = true
if (err) {
if (avalonDone && cb)
cb(err)
return
}
if (!result.content[author+'/'+permlink]) {
if (avalonDone && cb)
cb('Not found')
return
}
var video = parseVideo(result.content[author+'/'+permlink])
handleChainData(author, permlink, video, cb)
hasReplied = true
})
}
function parseVideo(video, isComment) {
try {
var newVideo = {}
newVideo.json = JSON.parse(video.json_metadata).video
} catch(e) {
console.log(e)
}
if (!newVideo) newVideo = {}
// newVideo.active_votes = video.active_votes
newVideo.author = video.author
newVideo.body = video.body
// newVideo.total_payout_value = video.total_payout_value
// newVideo.curator_payout_value = video.curator_payout_value
// newVideo.pending_payout_value = video.pending_payout_value
newVideo.permlink = video.permlink
newVideo.created = video.created
// newVideo.net_rshares = video.net_rshares
// newVideo.reblogged_by = video.reblogged_by
return newVideo;
}
function getRobotName(userAgent) {
for (let i = 0; i < crawlers.length; i++) {
var re = new RegExp(crawlers[i].pattern);
var isRobot = re.test(userAgent)
if (isRobot) return crawlers[i].pattern;
}
return;
}
function initBrowser(cb) {
var date = new Date().getTime()
puppeteer.launch({
headerless: 'true',
args: ['--no-sandbox', '--disable-setuid-sandbox']
}).then((newBrowser) => {
browser = newBrowser
var took = (new Date().getTime() - date)/1000
console.log('Browser started in '+took+' sec')
cb()
});
}
function resetBrowserTab(cb) {
var date = new Date().getTime()
isOpeningTab = true
browser.newPage().then((newPage) => {
if (page) {
page.close().then(function() {
page = newPage
isOpeningTab = false
var took = (new Date().getTime() - date)/1000
console.log('Browser tab reset in '+took+' sec')
cb()
})
} else {
page = newPage
isOpeningTab = false
var took = (new Date().getTime() - date)/1000
console.log('Browser tab reset in '+took+' sec')
cb()
}
})
}
function cleanCache() {
var date = new Date().getTime()
var before = Object.keys(cache).length
for (const path in cache) {
var date = new Date().getTime()
if (date - cache[path].ts > cacheTimeout)
delete cache[path]
}
var took = (new Date().getTime() - date)/1000
console.log('Cache '+before+' -> '+Object.keys(cache).length+' in '+took+' sec')
}