aboutsummaryrefslogtreecommitdiffstats
path: root/src/server/Main.hx
blob: 0023c5fc5e1045beb5bc95877eed93b6ed02e029 (plain) (blame)
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
package server;

import haxe.crypto.Sha256;
import sys.FileSystem;
import sys.io.File;
import haxe.Timer;
import haxe.Json;
import js.Node.process;
import js.Node.__dirname;
import js.npm.ws.Server as WSServer;
import js.npm.ws.WebSocket;
import js.node.http.IncomingMessage;
import js.node.Http;
import Client.ClientData;
import Types.Config;
import Types.Permission;
import Types.UserList;
import Types.Message;
import Types.WsEvent;
using StringTools;
using ClientTools;
using Lambda;

class Main {

	static inline var VIDEO_START_MAX_DELAY = 3000;
	static inline var VIDEO_SKIP_DELAY = 1000;
	final rootDir = '$__dirname/..';
	final verbose:Bool;
	final statePath:String;
	final wss:WSServer;
	final localIp:String;
	var globalIp:String;
	final port:Int;
	public final config:Config;
	final userList:UserList;
	final clients:Array<Client> = [];
	final freeIds:Array<Int> = [];
	final consoleInput:ConsoleInput;
	final videoList = new VideoList();
	final videoTimer = new VideoTimer();
	final messages:Array<Message> = [];
	var isPlaylistOpen = true;
	var itemPos = 0;

	static function main():Void new Main();

	function new() {
		verbose = Sys.args().has("--verbose");
		statePath = '$rootDir/user/state.json';
		// process.on("exit", exit);
		process.on("SIGINT", exit); // ctrl+c
		process.on("SIGUSR1", exit); // kill pid
		process.on("SIGUSR2", exit);
		process.on("SIGTERM", exit);
		process.on("uncaughtException", err -> {
			logError("uncaughtException", {
				message: err.message,
				stack: err.stack
			});
			exit();
		});
		process.on("unhandledRejection", (reason, promise) -> {
			logError("unhandledRejection", reason);
			exit();
		});
		consoleInput = new ConsoleInput(this);
		consoleInput.initConsoleInput();
		initIntergationHandlers();
		loadState();
		config = loadUserConfig();
		userList = loadUsers();
		config.salt = generateConfigSalt();
		localIp = Utils.getLocalIp();
		globalIp = localIp;
		port = config.port;
		final envPort = (process.env : Dynamic).PORT;
		if (envPort != null) port = envPort;

		Utils.getGlobalIp(ip -> {
			globalIp = ip;
			trace('Local: http://$localIp:$port');
			trace('Global: http://$globalIp:$port');
		});

		final dir = '$rootDir/res';
		HttpServer.init(dir, '$rootDir/user/res');
		Lang.init('$dir/langs');

		final server = Http.createServer((req, res) -> {
			HttpServer.serveFiles(req, res);
		});
		server.listen(port);
		wss = new WSServer({server: server});
		wss.on("connection", onConnect);
	}

	public function exit():Void {
		saveState();
		if (wss == null) {
			process.exit();
			return;
		}
		wss.close(() -> process.exit());
	}

	function generateConfigSalt():String {
		if (userList.salt == null)
			userList.salt = Sha256.encode('${Math.random()}');
		return userList.salt;
	}

	function loadUserConfig():Config {
		final config = getUserConfig();
		inline function getPermissions(type:Permission):Array<Permission> {
			return Reflect.field(config.permissions, cast type);
		}
		final groups = [GuestPerm, UserPerm, LeaderPerm, AdminPerm];
		for (field in groups) {
			final group = getPermissions(field);
			for (type in groups) {
				if (type == field) continue;
				if (group.indexOf(type) == -1) continue;
				group.remove(type);
				for (item in getPermissions(type)) group.push(item);
			}
		}
		return config;
	}

	function getUserConfig():Config {
		final config:Config = Json.parse(File.getContent('$rootDir/default-config.json'));
		final customPath = '$rootDir/user/config.json';
		if (!FileSystem.exists(customPath)) return config;
		final customConfig:Config = Json.parse(File.getContent(customPath));
		for (field in Reflect.fields(customConfig)) {
			if (Reflect.field(config, field) == null) trace('Warning: config field "$field" is unknown');
			Reflect.setField(config, field, Reflect.field(customConfig, field));
		}
		final emoteCopies:Map<String, Bool> = [];
		for (emote in config.emotes) {
			if (emoteCopies[emote.name]) trace('Warning: emote name "${emote.name}" has copy');
			emoteCopies[emote.name] = true;
			if (!verbose) continue;
			if (emoteCopies[emote.image]) trace('Warning: emote url of name "${emote.name}" has copy');
			emoteCopies[emote.image] = true;
		}
		return config;
	}

	function loadUsers():UserList {
		final customPath = '$rootDir/user/users.json';
		if (!FileSystem.exists(customPath)) return {
			admins: []
		};
		return Json.parse(File.getContent(customPath));
	}

	function writeUsers(users:UserList):Void {
		final folder = '$rootDir/user';
		if (!FileSystem.exists(folder)) {
			FileSystem.createDirectory(folder);
		}
		final data = Json.stringify(users, "\t");
		File.saveContent('$folder/users.json', data);
	}

	function saveState():Void {
		trace("Saving state...");
		final data:ServerState = {
			videoList: videoList,
			isPlaylistOpen: isPlaylistOpen,
			itemPos: itemPos,
			messages: messages,
			timer: {
				time: videoTimer.getTime(),
				paused: videoTimer.isPaused()
			}
		}
		final json = Json.stringify(data, "\t");
		File.saveContent(statePath, json);
	}

	function loadState():Void {
		if (!FileSystem.exists(statePath)) return;
		trace("Loading state...");
		final data:ServerState = Json.parse(File.getContent(statePath));
		videoList.resize(0);
		messages.resize(0);
		for (item in data.videoList) videoList.push(item);
		isPlaylistOpen = data.isPlaylistOpen;
		itemPos = data.itemPos;
		for (message in data.messages) messages.push(message);
		videoTimer.start();
		videoTimer.setTime(data.timer.time);
		videoTimer.pause();
	}

	function logError(type:String, data:Dynamic):Void {
		trace(type, data);
		final crashesFolder = '$rootDir/user/crashes';
		if (!FileSystem.exists(crashesFolder)) FileSystem.createDirectory(crashesFolder);
		final name = DateTools.format(Date.now(), "%Y-%m-%d_%H_%M_%S") + "-" + type;
		File.saveContent('$crashesFolder/$name.json', Json.stringify(data, "\t"));
	}

	function initIntergationHandlers():Void {
		// Prevent heroku idle when clients online (needs APP_URL env var)
		if (process.env["_"] != null && process.env["_"].contains("heroku")
			&& process.env["APP_URL"] != null) {
			var url = process.env["APP_URL"];
			if (!url.startsWith("http")) url = 'http://$url';
			new Timer(10 * 60 * 1000).run = function() {
				if (clients.length == 0) return;
				trace('Ping $url');
				Http.get(url, r -> {});
			}
		}
	}

	public function addAdmin(name:String, password:String):Void {
		password += config.salt;
		final hash = Sha256.encode(password);
		if (userList.admins == null) userList.admins = [];
		userList.admins.push({
			name: name,
			hash: hash
		});
		writeUsers(userList);
		trace('Admin $name added.');
	}

	function onConnect(ws:WebSocket, req:IncomingMessage):Void {
		final ip = req.connection.remoteAddress;
		final id = freeIds.length > 0 ? freeIds.shift() : clients.length;
		final name = 'Guest ${id + 1}';
		trace('$name connected ($ip)');
		final isAdmin = req.connection.localAddress == ip;
		final client = new Client(ws, req, id, name, 0);
		client.isAdmin = isAdmin;
		clients.push(client);
		if (clients.length == 1 && videoList.length > 0)
			if (videoTimer.isPaused()) videoTimer.play();

		send(client, {
			type: Connected,
			connected: {
				config: config,
				history: messages,
				isUnknownClient: true,
				clientName: client.name,
				clients: [
					for (client in clients) client.getData()
				],
				videoList: videoList,
				isPlaylistOpen: isPlaylistOpen,
				itemPos: itemPos,
				globalIp: globalIp
			}
		});
		sendClientList();

		ws.on("message", data -> {
			onMessage(client, Json.parse(data));
		});
		ws.on("close", err -> {
			trace('Client ${client.name} disconnected');
			Utils.sortedPush(freeIds, client.id);
			clients.remove(client);
			sendClientList();
			if (client.isLeader) {
				if (videoTimer.isPaused()) videoTimer.play();
			}
			if (clients.length == 0) {
				if (waitVideoStart != null) waitVideoStart.stop();
				videoTimer.pause();
			}
		});
	}

	function onMessage(client:Client, data:WsEvent):Void {
		switch (data.type) {
			case Connected:
			case UpdateClients:
				sendClientList();
			case Login:
				final name = data.login.clientName;
				if (badNickName(name) || name.length > config.maxLoginLength
					|| clients.getByName(name) != null) {
					serverMessage(client, "usernameError");
					send(client, {type: LoginError});
					return;
				}
				final hash = data.login.passHash;
				if (hash == null) {
					if (userList.admins.exists(a -> a.name == name)) {
						send(client, {type: PasswordRequest});
						return;
					}
				} else {
					if (userList.admins.exists(
						a -> a.name == name && a.hash == hash
					)) client.isAdmin = true;
					else {
						serverMessage(client, "passwordMatchError");
						send(client, {type: LoginError});
						return;
					}
				}
				client.name = name;
				client.isUser = true;
				send(client, {
					type: data.type,
					login: {
						isUnknownClient: true,
						clientName: client.name,
						clients: clientList()
					}
				});
				sendClientList();

			case PasswordRequest:
			case LoginError:
			case Logout:
				final oldName = client.name;
				final id = clients.indexOf(client) + 1;
				client.name = 'Guest $id';
				client.isUser = false;
				send(client, {
					type: data.type,
					logout: {
						oldClientName: oldName,
						clientName: client.name,
						clients: clientList()
					}
				});
				sendClientList();

			case Message:
				if (!checkPermission(client, WriteChatPerm)) return;
				var text = data.message.text;
				if (text.length == 0) return;
				if (text.length > config.maxMessageLength) {
					text = text.substr(0, config.maxMessageLength);
				}
				data.message.text = text;
				data.message.clientName = client.name;
				final time = "[" + Date.now().toString().split(" ")[1] + "] ";
				messages.push({text: text, name: client.name, time: time});
				if (messages.length > config.serverChatHistory) messages.shift();
				broadcast(data);

			case ServerMessage:
			case AddVideo:
				if (!checkPermission(client, AddVideoPerm)) return;
				if (!isPlaylistOpen) {
					if (!checkPermission(client, LockPlaylistPerm)) return;
				}
				if (config.totalVideoLimit != 0
					&& videoList.length >= config.totalVideoLimit) {
					serverMessage(client, "totalVideoLimitError");
					return;
				}
				if (config.userVideoLimit != 0
					&& videoList.itemsByUser(client) >= config.userVideoLimit) {
					serverMessage(client, "videoLimitPerUserError");
					return;
				}
				final item = data.addVideo.item;
				item.author = client.name;
				final local = '$localIp:$port';
				if (item.url.contains(local)) {
					item.url = item.url.replace(local, '$globalIp:$port');
				}
				if (videoList.exists(i -> i.url == item.url)) {
					serverMessage(client, "videoAlreadyExistsError");
					return;
				}
				videoList.addItem(item, data.addVideo.atEnd, itemPos);
				broadcast(data);
				// Initial timer start if VideoLoaded is not happen
				if (videoList.length == 1) restartWaitTimer();

			case VideoLoaded:
				// Called if client loads next video and can play it
				prepareVideoPlayback();

			case RemoveVideo:
				if (!checkPermission(client, RemoveVideoPerm)) return;
				if (videoList.length == 0) return;
				final url = data.removeVideo.url;
				var index = videoList.findIndex(item -> item.url == url);
				if (index == -1) return;

				final isCurrent = videoList[itemPos].url == url;
				itemPos = videoList.removeItem(index, itemPos);
				if (isCurrent && videoList.length > 0) {
					broadcast(data);
					restartWaitTimer();
				} else {
					broadcast(data);
				}

			case SkipVideo:
				if (!checkPermission(client, RemoveVideoPerm)) return;
				if (videoList.length == 0) return;
				final item = videoList[itemPos];
				if (item.url != data.skipVideo.url) return;
				itemPos = videoList.skipItem(itemPos);
				if (videoList.length > 0) restartWaitTimer();
				broadcast(data);

			case Pause:
				if (videoList.length == 0) return;
				if (!client.isLeader) return;
				videoTimer.setTime(data.pause.time);
				videoTimer.pause();
				broadcast(data);

			case Play:
				if (videoList.length == 0) return;
				if (!client.isLeader) return;
				videoTimer.setTime(data.play.time);
				videoTimer.play();
				broadcast(data);

			case GetTime:
				if (videoList.length == 0) return;
				final maxTime = videoList[itemPos].duration - 0.01;
				if (videoTimer.getTime() > maxTime) {
					videoTimer.pause();
					videoTimer.setTime(maxTime);
					final currentLength = videoList.length;
					Timer.delay(() -> {
						if (videoList.length != currentLength) return;
						onMessage(client, {
							type: SkipVideo, skipVideo: {
								url: videoList[itemPos].url
							}
						});
					}, VIDEO_SKIP_DELAY);
					return;
				}
				final obj:WsEvent = {
					type: GetTime, getTime: {
						time: videoTimer.getTime()
					}
				};
				if (videoTimer.isPaused()) obj.getTime.paused = true;
				if (videoTimer.getRate() != 1) {
					if (!clients.hasLeader()) videoTimer.setRate(1);
					else obj.getTime.rate = videoTimer.getRate();
				}
				send(client, obj);

			case SetTime:
				if (videoList.length == 0) return;
				if (!client.isLeader) return;
				videoTimer.setTime(data.setTime.time);
				broadcastExcept(client, data);

			case SetRate:
				if (videoList.length == 0) return;
				if (!client.isLeader) return;
				videoTimer.setRate(data.setRate.rate);
				broadcastExcept(client, data);

			case Rewind:
				if (!checkPermission(client, RewindPerm)) return;
				if (videoList.length == 0) return;
				data.rewind.time += videoTimer.getTime();
				if (data.rewind.time < 0) data.rewind.time = 0;
				videoTimer.setTime(data.rewind.time);
				broadcast(data);

			case SetLeader:
				final clientName = data.setLeader.clientName;
				if (client.name == clientName) {
					if (!checkPermission(client, RequestLeaderPerm)) return;
				} else if (!client.isLeader && clientName != "") {
					if (!checkPermission(client, SetLeaderPerm)) return;
				}
				clients.setLeader(clientName);
				broadcast({
					type: SetLeader, setLeader: {
						clientName: clientName
					}
				});
				if (videoList.length == 0) return;
				if (!clients.hasLeader()) {
					if (videoTimer.isPaused()) videoTimer.play();
					videoTimer.setRate(1);
					broadcast({
						type: Play, play: {
							time: videoTimer.getTime()
						}
					});
				}

			case PlayItem:
				if (!checkPermission(client, ChangeOrderPerm)) return;
				itemPos = data.playItem.pos;
				restartWaitTimer();
				broadcast(data);

			case SetNextItem:
				if (!checkPermission(client, ChangeOrderPerm)) return;
				final pos = data.setNextItem.pos;
				if (pos == itemPos || pos == itemPos + 1) return;
				videoList.setNextItem(pos, itemPos);
				broadcast(data);

			case ToggleItemType:
				final pos = data.toggleItemType.pos;
				videoList.toggleItemType(pos);
				broadcast(data);

			case ClearChat:
				if (!checkPermission(client, ClearChatPerm)) return;
				messages.resize(0);
				broadcast(data);

			case ClearPlaylist:
				if (!checkPermission(client, RemoveVideoPerm)) return;
				videoTimer.stop();
				videoList.resize(0);
				itemPos = 0;
				broadcast(data);

			case ShufflePlaylist:
				if (!checkPermission(client, ChangeOrderPerm)) return;
				if (videoList.length == 0) return;
				final current = videoList[itemPos];
				videoList.remove(current);
				Utils.shuffle(videoList);
				videoList.insert(itemPos, current);
				broadcast({
					type: UpdatePlaylist,
					updatePlaylist: {
					videoList: videoList
				}});

			case UpdatePlaylist:
				broadcast({
					type: UpdatePlaylist,
					updatePlaylist: {
					videoList: videoList
				}});

			case TogglePlaylistLock:
				if (!checkPermission(client, LockPlaylistPerm)) return;
				isPlaylistOpen = !isPlaylistOpen;
				broadcast({
					type: TogglePlaylistLock,
					togglePlaylistLock: {
						isOpen: isPlaylistOpen
					}
				});
		}
	}

	function clientList():Array<ClientData> {
		return [
			for (client in clients) client.getData()
		];
	}

	function sendClientList():Void {
		broadcast({
			type: UpdateClients,
			updateClients: {
				clients: clientList()
			}
		});
	}

	function serverMessage(client:Client, textId:String):Void {
		send(client, {
			type: ServerMessage, serverMessage: {
				textId: textId
			}
		});
	}

	function send(client:Client, data:WsEvent):Void {
		client.ws.send(Json.stringify(data), null);
	}

	function broadcast(data:WsEvent):Void {
		final json = Json.stringify(data);
		for (client in clients) client.ws.send(json, null);
	}

	function broadcastExcept(skipped:Client, data:WsEvent):Void {
		final json = Json.stringify(data);
		for (client in clients) {
			if (client == skipped) continue;
			client.ws.send(json, null);
		}
	}

	function checkPermission(client:Client, perm:Permission):Bool {
		final state = hasPermission(client, perm);
		if (!state) send(client, {
			type: ServerMessage, serverMessage: {
				textId: "accessError"
			}
		});
		return state;
	}

	function hasPermission(client:Client, perm:Permission):Bool {
		final p = config.permissions;
		if (client.isAdmin) return p.admin.indexOf(cast perm) != -1;
		if (client.isLeader) return p.leader.indexOf(cast perm) != -1;
		if (client.isUser) return p.user.indexOf(cast perm) != -1;
		return p.guest.indexOf(cast perm) != -1;
	}

	final htmlChars = ~/[&^<>'"]/;

	public function badNickName(name:String):Bool {
		if (name.length == 0) return true;
		if (htmlChars.match(name)) return true;
		return false;
	}

	var waitVideoStart:Timer;
	var loadedClientsCount = 0;

	function restartWaitTimer():Void {
		videoTimer.stop();
		if (waitVideoStart != null) waitVideoStart.stop();
		waitVideoStart = Timer.delay(startVideoPlayback, VIDEO_START_MAX_DELAY);
	}

	function prepareVideoPlayback():Void {
		if (videoTimer.isStarted) return;
		loadedClientsCount++;
		if (loadedClientsCount == 1) restartWaitTimer();
		if (loadedClientsCount >= clients.length) startVideoPlayback();
	}

	function startVideoPlayback():Void {
		if (waitVideoStart != null) waitVideoStart.stop();
		loadedClientsCount = 0;
		broadcast({type: VideoLoaded});
		videoTimer.start();
	}

}
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage