-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsocket.js
287 lines (253 loc) · 8.8 KB
/
socket.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
const InitialFetch = require('./initial-fetch')
const Cheerio = require('cheerio')
const FileSystem = require('fs')
const Firehose = require('./firehose')
require('colors')
const WebSocket = require('ws')
const http = require('http')
const _uniq = require('lodash.uniq')
const twitter = require('twitter-text')
const normalizeURL = require('normalize-url')
const { URL } = require('url')
const franc = require('franc')
// const Hash = require('hash.js')
const polarity = require('polarity')
const natural = require('natural')
const mongoose = require('mongoose')
const Post = require('./models/posts')
const Account = require('./models/accounts')
mongoose.connect(process.env.FIREHOSE_MONGODB, { useNewUrlParser: true })
const DB = mongoose.connection
const server = http.createServer()
const SocketServer = new WebSocket.Server({
perMessageDeflate: false,
noServer: true
})
SocketServer.broadcast = (data) => {
SocketServer.clients.forEach((client) => {
if (client.readyState === WebSocket.OPEN) {
if (typeof data === 'string') client.send(data)
else client.send(JSON.stringify(data))
}
})
}
SocketServer.on('connection', (socket, req) => {
const ip = req.connection.remoteAddress
socket.isAlive = true
socket.send(JSON.stringify({ event: 'system:info', data: `Connected! Streaming to ${ip}` }))
socket.on('error', () => true)
socket.on('pong', () => {
socket.isAlive = true
})
})
setInterval(() => {
SocketServer.clients.forEach(socket => {
if (socket.isAlive === false) {
socket.terminate()
return
}
socket.isAlive = false
socket.ping('', false, true)
})
}, 30000)
server.on('upgrade', (request, socket, head) => {
SocketServer.handleUpgrade(request, socket, head, (ws) => {
SocketServer.emit('connection', ws, request)
})
})
server.on('error', () => true)
server.listen(process.env.PORT || 8080)
function FirehoseHandler (servers) {
// this connects to each server's local feed
const firehose = new Firehose(servers)
const superhose = new Firehose(servers, true)
firehose.attempts = {}
superhose.attempts = {}
const statusCheck = setInterval(() => {
const serverCount = firehose.connections
console.info(` STATUS `.black.bgWhite + `\t${serverCount[0]} SOCKETS, ${serverCount[1]} STREAMS`.magenta)
}, 10000)
setTimeout(() => {
const newServers = servers // to remove the old reference
firehose.disconnect()
.then(() => { clearInterval(statusCheck); superhose.disconnect(); FirehoseHandler(newServers) })
}, 3600000) // every hour
// errors happen, but they're unnecessary and useless here
firehose.events.on('error', () => true)
superhose.events.on('error', () => true)
// SUPERHOSE DISCOVERY
superhose.events.on('servers:posts:update', (post) => {
if (post.url && twitter.isValidUrl(post.url)) {
const host = (new URL(normalizeURL(post.url))).host.toLowerCase()
if (firehose.servers.indexOf(host) === -1 && firehose.blacklist.indexOf(host) === -1) {
firehose.addServer(host)
superhose.addServer(host)
console.info(' DISCOVERY '.black.bgYellow + `\t${host}`.yellow)
}
}
})
// FIREHOSE SERVER POST EVENTS
firehose.events.on('servers:posts:update', (post, mainHost) => {
const inHost = mainHost.toLowerCase()
post.origin = inHost
post.account.origin = inHost
// post.global_id = Hash.sha256().update(`${inHost}:${post.created_at}/${post.account.id}`).digest('hex')
const $ = Cheerio.load(
post.content
.replace('</p><p>', '\n\n')
.replace('<br />', '\n')
.replace('<br/>', '\n')
)
$('a').each((i, el) => {
const obj = $(el)
if (twitter.isValidUrl(obj.attr('href'))) {
const username = obj.text().replace('@', '')
if (!twitter.isValidUsername(username)) return
const host = (new URL(obj.attr('href'))).host
obj.html(`@${username}@${host}`)
}
})
let textStatus
if (post.sensitive && post.spoiler_text != null && post.spoiler_text !== '') {
let spoiler = { text: '', indices: [0, 0] }
let spoiled = post.spoiler_text
if (spoiled.charAt(0) === '[') {
spoiled = spoiled.split('')
spoiled.shift()
spoiled = spoiled.join('')
}
if (spoiled.charAt(spoiled.length - 1) === ']') {
spoiled = spoiled.split('')
spoiled.pop()
spoiled = spoiled.join('')
}
spoiler.text = spoiled
spoiler.indices[1] = spoiled.length - 1
post.spoiler = spoiler
post.sensitive = true
textStatus = `[${spoiled}]\n\n${$.text()}`
} else {
textStatus = $.text()
post.sensitive = false
post.spoiler = null
}
// TODO: Limit post sizes to 2,000 characters.
// Any more may exploit the system to store data/etc.
post.content = textStatus
post.hashtags = twitter.extractHashtagsWithIndices(textStatus).map(tag => {
const url = normalizeURL(post.url)
tag.url = normalizeURL((new URL(url).host), {
normalizeHttp: (new URL(url)).protocol === 'https:'
}) + '/tags/' + tag.hashtag.replace('#', '')
return tag
})
post.lang = franc(post.content)
if (post.lang === 'eng') {
const tokenizer = new natural.WordTokenizer()
post.polarity = polarity(tokenizer.tokenize(post.content)).polarity
} else {
post.polarity = null
}
const localID = post.id
post.local_id = localID
delete post.language
delete post.spoiler_text
delete post.tags
delete post.favourites_count
delete post.reblogs_count
delete post.favourited
delete post.visibility
delete post.uri
delete post.muted
delete post.pinned
delete post.reblogged
delete post.reblog
delete post.pinned
delete post.id
SocketServer.broadcast({ event: 'posts:update', data: post })
console.log(' UPDATE '.black.bgCyan + `\t${post.lang}\t${post.content}`.white + `\n\t\t${post.url}`.cyan)
post.account.local_id = post.account.id
delete post.account.id
Account.findOneAndUpdate({
'acct': post.account.acct,
'origin': post.account.origin
}, post.account, { new: true, upsert: true }, (err, doc) => {
if (err) console.error(' ERROR '.white.bgRed + `\t${err.message}`)
delete post.account
post.account = doc._id
new Post(post).save((err) => {
if (err) console.error(' ERROR '.white.bgRed + `\t${err.message}`)
})
})
})
firehose.events.on('servers:posts:delete', (data) => {
SocketServer.broadcast({ event: 'posts:delete', data: data })
console.log(' DELETE '.white.bgMagenta + `\t${data.id}`.white + `\t${data.host}`)
})
// FIREHOSE SERVER EVENTS
firehose.events.on('servers:connected', (host) => {
console.info(' CONNECTED '.white.bgGreen + `\t${host}`.green)
})
firehose.events.on('servers:disconnect', (host, code, reason, problematic) => {
if (problematic === false) {
if (!firehose.attempts[host]) {
firehose.attempts[host] = 0
} else if (firehose.attempts[host] <= 5) {
setTimeout(() => {
firehose.attempts[host]++
firehose.addServer(host, true)
}, 1000)
} else {
delete firehose.attempts[host]
firehose.connectToServerLegacy(host)
}
} else {
firehose.blacklist.push(host)
}
console.warn(' DISCONNECTED '.white.bgRed + `\t${host}`.white + `\tPROBLEMATIC? ${problematic.toString().toUpperCase()}`)
})
// SUPERHOSE SERVER RECONNECT
superhose.events.on('servers:disconnect', (host, code, reason, problematic) => {
if (problematic === false) {
if (!superhose.attempts[host]) {
superhose.attempts[host] = 0
} else if (superhose.attempts[host] <= 5) {
setTimeout(() => {
superhose.attempts[host]++
superhose.addServer(host, true)
}, 1000)
} else {
delete superhose.attempts[host]
superhose.connectToServerLegacy(host)
}
} else {
superhose.blacklist.push(host)
}
})
firehose.events.on('servers:error', (host, message) => {
console.error(' ERROR '.white.bgRed + `\t${host}`.grey + `\t${message}`.red)
})
// FIREHOSE SYSTEM EVENTS
firehose.events.on('system:cleanup', (host) => {
console.info(` CLEANUP `.white.bgBlue + host.white)
})
firehose.events.on('system:discovery', (host) => {
console.info(' DISCOVERY '.black.bgYellow + `\t${host}`.yellow)
})
}
DB.on('error', console.error.bind(console, 'connection error:'))
DB.once('open', () => {
if (FileSystem.existsSync('.servers.json')) {
FileSystem.readFile('.servers.json', (err, contents) => {
if (err) throw err
if (typeof contents !== 'string') contents = contents.toString('utf8')
const servers = _uniq(JSON.parse(contents))
FirehoseHandler(servers)
})
} else {
InitialFetch()
.then(servers => {
FirehoseHandler(_uniq(servers))
})
}
})