-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
237 lines (199 loc) · 5.93 KB
/
index.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
const fastify = require('fastify')();
const helmet = require('fastify-helmet');
const TSLibrary = require('./tsLib');
const path = require('path');
const _ = require('lodash');
const RSS = require('rss');
const url = require('url');
const ProgressBar = require('progress');
//const telegram = require('./telegram');
const telegraf = require('./telegraf');
const io = require('socket.io')(fastify.server);
fastify.register(helmet);
const ts = new TSLibrary({
ip: process.env.TSVSERVERIP || '127.0.0.1',
port: process.env.TSVSERVERPORT || 10011,
username: process.env.TSVSERVERUSERNAME || 'serveradmin',
password: process.env.TSVSERVERPASSWORD || '',
server: process.env.TSVSERVER || 1,
mongodb: process.env.TSVMONGO || "mongodb://localhost/teamspeak",
cache: process.env.TSVCACHE || 1000 * 120
});
const refreshTime = process.env.TSPOLLINGTIME ? parseInt(process.env.TSPOLLINGTIME) : 500;
let bar;
io.on('connection', socket => {
console.log("Socket connection ->", socket.conn.remoteAddress);
});
ts.on('indexProgress', ({count, current, lastClient}) => {
if (!bar) bar = new ProgressBar('Indexing clients [:bar] :percent :etas ETA Indexed: :last', {total: count});
bar.tick(1, {
"last": lastClient
});
});
ts.on('indexFinished', () => {
bar = null;
console.log("Finished indexing clients!");
});
ts.on('update', async () => {
let newData;
try {
newData = await ts.getChannelTree();
} catch(e) {
console.error(e);
setTimeout(refreshData, 800);
}
if (newData) {
io.emit('update', newData);
} else {
io.emit('updateYourself');
}
});
ts.on('left', data => {
io.emit('left', data);
});
ts.on('join', data => {
io.emit('join', data);
});
async function online() {
return await ts.getOnlineClients();
}
async function users() {
return await ts.User.find({}).exec();
}
async function channel(req, reply) {
if (req.params['id']) {
return await ts.getChannelAndUpdateIfRequired(req.params['id']);
} else {
reply.code(404).send('Sorry');
}
}
async function icon(req, reply) {
if (req.params['id']) {
try {
const {meta, stream} = await ts.streamIcon({id: req.params['id']});
if (meta) {
reply.type(meta['contentType']).send(stream);
} else {
reply.send(stream);
}
} catch (e) {
reply.code(500).send(e);
}
} else {
reply.code(404).send('Sorry');
}
}
async function avatar(req, reply) {
if ((req.params['type'] === 'uid' || req.params['type'] === 'dbid') || !req.params['id']) {
try {
const {meta, stream} = await ts.streamAvatarFrom(req.params['type'] === 'uid' ? {uid: req.params['id']} : {dbid: req.params['id']});
if (meta) {
reply.type(meta['contentType']).send(stream);
} else {
reply.send(stream);
}
} catch (e) {
reply.code(500).send(e);
}
} else {
reply.code(404).send('Sorry');
}
}
async function user(req, reply) {
if ((req.params['type'] === 'uid' || req.params['type'] === 'dbid') || !req.params['id']) {
const user = await ts.updateUser(req.params['type'] === 'uid' ? {uid: req.params['id']} : {dbid: req.params['id']});
reply.send(JSON.parse(JSON.stringify(user)));
} else {
reply.code(404).send('Sorry');
}
}
async function channelTree() {
return await ts.getChannelTree();
}
fastify.get('/avatar/:type/:id', avatar);
fastify.get('/user/:type/:id', user);
fastify.get('/user/', online);
fastify.get('/user', online);
fastify.get('/online', online);
fastify.get('/online/', online);
fastify.get('/users', users);
fastify.get('/users/', users);
fastify.get('/channel/:id', channel);
fastify.get('/icon/:id', icon);
if (process.env.FEEDURL && process.env.FEEDSITEURL && process.env.FEEDTITLE) {
async function feed(req, reply) {
const feed = new RSS({
ttl: 1,
pubDate: new Date(),
title: process.env.FEEDTITLE,
feed_url: process.env.FEEDURL,
site_url: process.env.FEEDSITEURL
});
const logs = await ts.Log.find({}).sort({date: 'asc'}).limit(500).exec();
const u = url.parse(process.env.FEEDURL);
for (let log of logs) {
const user = await ts.getUser({uid: log.meta.uid});
let meta;
if (user['hasAvatar']) {
meta = await new Promise((resolve, reject) => {
ts.gfs.files.find({filename: `/0/avatar_${user['avatarID']}`}).toArray((err, files) => {
if (err) return reject(err);
resolve(files[0]);
});
});
}
console.log(meta);
feed.item({
title: log.message,
description: `${user['nickname']} already joined ${user['connections']} times.`,
guid: log._id,
date: log.date,
url: `${u.protocol}//${u.hostname}:${u.port || (u.protocol === 'https:' ? 443 : 80)}/user/uid/${log.meta.uid}`,
enclosure: {
url: user['hasAvatar'] ? `${u.protocol}//${u.hostname}:${u.port || (u.protocol === 'https:' ? 443 : 80)}/avatar/dbid/${user['dbid']}` : undefined,
type: meta && meta.hasOwnProperty('contentType') ? meta['contentType'] : undefined,
size: meta && meta.hasOwnProperty('length') ? meta['length'] : undefined,
},
custom_elements: [
{'dbid': user['dbid']},
{'country': user['country']},
{'uid': user['uid']},
{'nickname': user['nickname']}
]
});
}
reply.type('application/rss+xml');
return feed.xml();
}
fastify.get('/feed', feed);
}
fastify.get('/channelTree', channelTree);
fastify.register(require('fastify-static'), {
root: path.join(__dirname, 'public'),
});
ts.login().then(ts.indexClients).then(() => {
let lastData;
async function refreshData() {
let newData;
try {
newData = await ts.getChannelTree();
} catch(e) {
console.error(e);
setTimeout(refreshData, refreshTime);
}
if (!_.isEqual(newData, lastData)) {
lastData = newData;
io.emit('update', newData);
}
setTimeout(refreshData, refreshTime);
}
setTimeout(refreshData, refreshTime);
telegraf(ts, fastify);
fastify.listen(process.env.TSVPORT || 5000, process.env.TSVHOST || "0.0.0.0", err => {
if (err) {
console.error(err);
process.exit(1);
}
console.log(`Listen on ${process.env.TSVHOST || "0.0.0.0"}:${process.env.TSVPORT || 5000}`);
});
}).catch(console.error);