-
Notifications
You must be signed in to change notification settings - Fork 126
/
server.js
97 lines (79 loc) · 4.04 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
require('dotenv').config();
const express = require('express');
const { createServer } = require('http');
const { Server } = require('socket.io');
const { TikTokConnectionWrapper, getGlobalConnectionCount } = require('./connectionWrapper');
const { clientBlocked } = require('./limiter');
const app = express();
const httpServer = createServer(app);
// Enable cross origin resource sharing
const io = new Server(httpServer, {
cors: {
origin: '*'
}
});
io.on('connection', (socket) => {
let tiktokConnectionWrapper;
console.info('New connection from origin', socket.handshake.headers['origin'] || socket.handshake.headers['referer']);
socket.on('setUniqueId', (uniqueId, options) => {
// Prohibit the client from specifying these options (for security reasons)
if (typeof options === 'object' && options) {
delete options.requestOptions;
delete options.websocketOptions;
} else {
options = {};
}
// Session ID in .env file is optional
if (process.env.SESSIONID) {
options.sessionId = process.env.SESSIONID;
console.info('Using SessionId');
}
// Check if rate limit exceeded
if (process.env.ENABLE_RATE_LIMIT && clientBlocked(io, socket)) {
socket.emit('tiktokDisconnected', 'You have opened too many connections or made too many connection requests. Please reduce the number of connections/requests or host your own server instance. The connections are limited to avoid that the server IP gets blocked by TokTok.');
return;
}
// Connect to the given username (uniqueId)
try {
tiktokConnectionWrapper = new TikTokConnectionWrapper(uniqueId, options, true);
tiktokConnectionWrapper.connect();
} catch (err) {
socket.emit('tiktokDisconnected', err.toString());
return;
}
// Redirect wrapper control events once
tiktokConnectionWrapper.once('connected', state => socket.emit('tiktokConnected', state));
tiktokConnectionWrapper.once('disconnected', reason => socket.emit('tiktokDisconnected', reason));
// Notify client when stream ends
tiktokConnectionWrapper.connection.on('streamEnd', () => socket.emit('streamEnd'));
// Redirect message events
tiktokConnectionWrapper.connection.on('roomUser', msg => socket.emit('roomUser', msg));
tiktokConnectionWrapper.connection.on('member', msg => socket.emit('member', msg));
tiktokConnectionWrapper.connection.on('chat', msg => socket.emit('chat', msg));
tiktokConnectionWrapper.connection.on('gift', msg => socket.emit('gift', msg));
tiktokConnectionWrapper.connection.on('social', msg => socket.emit('social', msg));
tiktokConnectionWrapper.connection.on('like', msg => socket.emit('like', msg));
tiktokConnectionWrapper.connection.on('questionNew', msg => socket.emit('questionNew', msg));
tiktokConnectionWrapper.connection.on('linkMicBattle', msg => socket.emit('linkMicBattle', msg));
tiktokConnectionWrapper.connection.on('linkMicArmies', msg => socket.emit('linkMicArmies', msg));
tiktokConnectionWrapper.connection.on('liveIntro', msg => socket.emit('liveIntro', msg));
tiktokConnectionWrapper.connection.on('emote', msg => socket.emit('emote', msg));
tiktokConnectionWrapper.connection.on('envelope', msg => socket.emit('envelope', msg));
tiktokConnectionWrapper.connection.on('subscribe', msg => socket.emit('subscribe', msg));
});
socket.on('disconnect', () => {
if (tiktokConnectionWrapper) {
tiktokConnectionWrapper.disconnect();
}
});
});
// Emit global connection statistics
setInterval(() => {
io.emit('statistic', { globalConnectionCount: getGlobalConnectionCount() });
}, 5000)
// Serve frontend files
app.use(express.static('public'));
// Start http listener
const port = process.env.PORT || 8081;
httpServer.listen(port);
console.info(`Server running! Please visit http://localhost:${port}`);