forked from wistingcn/WiLearning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.ts
executable file
·315 lines (254 loc) · 7.72 KB
/
server.ts
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
/*
* Copyright (c) 2020 Wisting Team. <[email protected]>
*
* This program is free software: you can use, redistribute, and/or modify
* it under the terms of the GNU Affero General Public License, version 3
* or later ("AGPL"), as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
process.title = 'WiLearning';
import * as fs from 'fs';
import * as os from 'os';
import * as https from 'https';
import * as http from 'http';
import express from 'express';
import {types as mediasoupTypes} from 'mediasoup';
import { Room } from './lib/Room';
import { Peer } from './lib/Peer';
import { createConnection } from 'typeorm';
import * as socketio from 'socket.io';
import yargs from 'yargs';
import { getLogger, configure } from 'log4js';
configure('./log4js.json');
const logger = getLogger('Server');
import { docRouter } from './route/document';
import { avatarRouter } from './route/avatar';
import { roomRouter } from './route/room';
import { ClaDocPages, ClaDocs, ClaRoom} from './model/model';
import * as path from 'path';
import {lConfig} from './config/config';
import got from 'got';
const helmet = require('helmet');
const cors = require('cors');
const mediasoup = require("mediasoup");
const bodyParser = require('body-parser');
const compression = require('compression');
const morgan = require('morgan');
yargs.usage('Usage: $0 --cert [file] --key [file] --eth [ethname] --publicIp [ipAdress]')
.version('Wisting-meeting v1.0')
.demandOption(['cert', 'key'])
.option('cert', {describe : 'ssl certificate file'})
.option('key', {describe: 'ssl certificate key file'})
.option('eth', {describe: 'local network interface, default "eth0"'})
.option('publicIp', {describe: 'public ip address, default get from network'});
const certfile = yargs.argv.cert as string;
const keyfile = yargs.argv.key as string;
const localEth = yargs.argv.eth as string || 'eth0';
const publicIp = yargs.argv.publicIp;
[certfile, keyfile].forEach(file => {
if (!fs.existsSync(file)){
logger.error('%s do not exist!', file);
process.exit(-1);
}
});
const tls = {
cert: fs.readFileSync(certfile),
key: fs.readFileSync(keyfile),
};
const app = express();
app.use(compression());
app.use(morgan('dev'));
app.use(helmet.hsts());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cors());
const mediasoupWorkers = new Array<mediasoupTypes.Worker>();
let nextMediasoupWorkerIdx = 0;
const rooms = new Map<string, Room>();
app.locals.rooms = rooms;
let httpsServer: https.Server;
let io: socketio.Server;
async function run() {
await getIps();
// Run a mediasoup Worker.
await runMediasoupWorkers();
// Run HTTPS server.
await runHttpsServer();
// Run WebSocketServer.
await runWebSocketServer();
// connect database
try {
await createConnection({
type: 'sqlite',
"database": "database.sqlite",
"synchronize": true,
entities:[
ClaDocPages,
ClaDocs,
ClaRoom
],
});
} catch(error) {
logger.error(error);
}
// Log rooms status every 300 seconds.
setInterval(() => {
let all = 0;
let closed = 0;
rooms.forEach(room => {
all++;
if ( room.closed ) {
closed++;
}
logger.debug(JSON.stringify(room.statusReport()));
});
logger.info('room total: %s, closed: %s', all, closed);
}, 300000);
// check for deserted rooms
setInterval(() => {
rooms.forEach(room => room.checkDeserted());
}, 10000);
}
const runHttpsServer = async () => {
app.use('/public', express.static('public', {
maxAge: '30d'
}));
app.use('/room', roomRouter);
app.use('/docs', docRouter);
app.use('/avatar', avatarRouter);
app.use('/admin', express.static('admin'));
app.get('/admin/*', (req, res) => {
const indexFile = path.resolve(__dirname + '/admin/index.html');
res.sendFile(indexFile);
});
app.use('/web', express.static('web'));
app.get('/web/*', (req, res) => {
const indexFile = path.resolve(__dirname + '/web/index.html');
res.sendFile(indexFile);
});
app.use('/app', express.static('app'));
app.get('/app/*', (req, res) => {
const indexFile = path.resolve(__dirname + '/app/index.html');
res.sendFile(indexFile);
});
app.get('*', (req,res,next) => {
res.status(404).send({res: '404'});
});
httpsServer = https.createServer(tls, app);
httpsServer.listen(lConfig.listeningPort);
const httpServer = http.createServer(app);
httpServer.listen(lConfig.listeningRedirectPort);
}
const runWebSocketServer = async () => {
io = socketio.listen(httpsServer, {
pingTimeout: 3000,
pingInterval: 5000,
transports: ['websocket'],
allowUpgrades: false,
});
logger.info("run websocket server....");
io.on('connection', async (socket) => {
const { roomId, peerId } = socket.handshake.query;
if (!roomId || !peerId) {
logger.warn('connection request without roomId and/or peerId');
socket.disconnect(true);
return;
}
logger.info('connection request [roomId:"%s", peerId:"%s"]', roomId, peerId);
try {
const room = await getOrCreateRoom(roomId);
let peer = room.getPeer(peerId);
if (!peer) {
peer = new Peer(peerId, socket, room);
room.handlePeer(peer);
logger.info('new peer, %s, %s', peerId, socket.id);
} else {
peer.handlePeerReconnect(socket);
logger.info('peer reconnect, %s, %s', peerId, socket.id);
}
} catch(error) {
logger.error('room creation or room joining failed [error:"%o"]', error);
socket.disconnect(true);
return;
};
});
}
const runMediasoupWorkers = async () => {
const numWorkers = os.cpus().length;
logger.info('mediasoup version: %s, running %d mediasoup Workers...', mediasoup.version, numWorkers);
for (let i = 0; i < numWorkers; ++i) {
const worker = await mediasoup.createWorker( {
logLevel : lConfig.worker.logLevel,
rtcMinPort : lConfig.worker.rtcMinPort,
rtcMaxPort : lConfig.worker.rtcMaxPort,
dtlsCertificateFile: certfile,
dtlsPrivateKeyFile: keyfile
}) as mediasoupTypes.Worker;
worker.on('died', () => {
logger.error(
'mediasoup Worker died, exiting in 2 seconds... [pid:%d]', worker.pid);
setTimeout(() => process.exit(1), 2000);
});
mediasoupWorkers.push(worker);
}
}
/**
* Get next mediasoup Worker.
*/
const getMediasoupWorker = () => {
const worker = mediasoupWorkers[nextMediasoupWorkerIdx];
if (++nextMediasoupWorkerIdx === mediasoupWorkers.length) {
nextMediasoupWorkerIdx = 0;
}
return worker;
}
const getOrCreateRoom = async (roomId: string) => {
let room = rooms.get(roomId);
if (!room) {
logger.info('creating a new Room [roomId:"%s"]', roomId);
const mediasoupWorker = getMediasoupWorker();
room = await Room.create(mediasoupWorker, roomId );
rooms.set(roomId, room);
room.on('close', () => rooms.delete(roomId));
}
return room;
}
const getIps = async () => {
const localIp = getLocalIp(localEth);
let announcedIp = publicIp;
if ( !announcedIp ) {
const url = 'https://api.ipify.org?format=json';
try {
const resp = await got(url).json() as any;
announcedIp = resp.ip;
} catch(e) {
logger.error('get public ip error!', e.message);
}
}
if ( !announcedIp ) {
logger.error('Got public ip error! exit now!');
process.exit(-1);
}
logger.info('localIp: %s, publicIp: %s', localIp, announcedIp);
lConfig.webRtcTransport.listenIps = [{
ip: localIp,
announcedIp
}] as any;
}
const getLocalIp = (eth: string) => {
const eths= os.networkInterfaces()[eth];
let localIp = '';
eths && eths.forEach(e => {
if(e.family === 'IPv4') {
localIp = e.address;
}
});
return localIp;
}
run();