diff options
Diffstat (limited to 'src/server')
| -rw-r--r-- | src/server/ConsoleInput.hx | 15 | ||||
| -rw-r--r-- | src/server/HttpServer.hx | 20 | ||||
| -rw-r--r-- | src/server/Logger.hx | 9 | ||||
| -rw-r--r-- | src/server/Main.hx | 114 | ||||
| -rw-r--r-- | src/server/ServerState.hx | 3 | ||||
| -rw-r--r-- | src/server/Utils.hx | 15 | ||||
| -rw-r--r-- | src/server/VideoTimer.hx | 3 |
7 files changed, 100 insertions, 79 deletions
diff --git a/src/server/ConsoleInput.hx b/src/server/ConsoleInput.hx index 3c287af..9b8faf3 100644 --- a/src/server/ConsoleInput.hx +++ b/src/server/ConsoleInput.hx @@ -1,13 +1,14 @@ package server; +import haxe.Json; import haxe.extern.EitherType as Or; import haxe.io.Path; -import haxe.Json; -import sys.FileSystem; -import sys.io.File; +import js.Node.process; import js.html.Console; import js.node.Readline; -import js.Node.process; +import sys.FileSystem; +import sys.io.File; + using StringTools; private typedef CommandData = { @@ -23,7 +24,6 @@ private enum abstract Command(String) from String { } class ConsoleInput { - final main:Main; final commands:Map<Command, CommandData> = [ AddAdmin => { @@ -118,7 +118,9 @@ class ConsoleInput { Utils.ensureDir(main.logsDir); final names = FileSystem.readDirectory(main.logsDir) .filter(s -> s.endsWith(".json")); - for (name in names) trace(Path.withoutExtension(name)); + for (name in names) { + trace(Path.withoutExtension(name)); + } case Exit: main.exit(); @@ -150,5 +152,4 @@ class ConsoleInput { final desc = list.join("\n"); trace('Unknown command "$line". List:\n$desc'); } - } diff --git a/src/server/HttpServer.hx b/src/server/HttpServer.hx index 46540a3..64962dc 100644 --- a/src/server/HttpServer.hx +++ b/src/server/HttpServer.hx @@ -1,20 +1,20 @@ package server; -import sys.FileSystem; -import js.node.Buffer; import haxe.io.Path; +import js.node.Buffer; import js.node.Fs; -import js.node.Https; import js.node.Http; -import js.node.url.URL; +import js.node.Https; +import js.node.Path as JsPath; +import js.node.http.ClientRequest; import js.node.http.IncomingMessage; import js.node.http.ServerResponse; -import js.node.http.ClientRequest; -import js.node.Path as JsPath; +import js.node.url.URL; +import sys.FileSystem; + using StringTools; class HttpServer { - static final mimeTypes = [ "html" => "text/html", "js" => "text/javascript", @@ -58,7 +58,8 @@ class HttpServer { res.setHeader("Accept-Ranges", "bytes"); res.setHeader("Content-Type", getMimeType(ext)); - if (allowLocalRequests && req.connection.remoteAddress == req.connection.localAddress + if (allowLocalRequests + && req.connection.remoteAddress == req.connection.localAddress || allowedLocalFiles[url]) { if (isMediaExtension(ext)) { allowedLocalFiles[url] = true; @@ -176,7 +177,7 @@ class HttpServer { static function proxyRequest( url:String, req:IncomingMessage, res:ServerResponse, - fn:(req:IncomingMessage)->Bool + fn:(req:IncomingMessage) -> Bool ):Null<ClientRequest> { final url = try { new URL(js.Node.global.decodeURI(url)); @@ -211,5 +212,4 @@ class HttpServer { if (contentType == null) return "application/octet-stream"; return contentType; } - } diff --git a/src/server/Logger.hx b/src/server/Logger.hx index 9ff4c34..cd96d1b 100644 --- a/src/server/Logger.hx +++ b/src/server/Logger.hx @@ -1,14 +1,14 @@ package server; -import haxe.io.Path; -import sys.io.File; import haxe.Json; +import haxe.io.Path; import sys.FileSystem; -using StringTools; +import sys.io.File; + using Lambda; +using StringTools; class Logger { - final folder:String; final maxCount:Int; final verbose:Bool; @@ -65,5 +65,4 @@ class Logger { final s = '${d[0]}-${d[1]}-${d[2]} ${t[0]}:${t[1]}:${t[2]}'; return Date.fromString(s); } - } diff --git a/src/server/Main.hx b/src/server/Main.hx index d93c2e5..ea1e40b 100644 --- a/src/server/Main.hx +++ b/src/server/Main.hx @@ -1,41 +1,44 @@ 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 json2object.JsonParser; -import json2object.ErrorUtils; import Client.ClientData; import Types.Config; +import Types.Message; import Types.Permission; import Types.UserList; -import Types.Message; import Types.WsEvent; -using StringTools; +import haxe.Json; +import haxe.Timer; +import haxe.crypto.Sha256; +import js.Node.__dirname; +import js.Node.process; +import js.node.Http; +import js.node.http.IncomingMessage; +import js.npm.ws.Server as WSServer; +import js.npm.ws.WebSocket; +import json2object.ErrorUtils; +import json2object.JsonParser; +import sys.FileSystem; +import sys.io.File; + using ClientTools; using Lambda; +using StringTools; class Main { - static inline var VIDEO_START_MAX_DELAY = 3000; static inline var VIDEO_SKIP_DELAY = 1000; + final rootDir = '$__dirname/..'; + public final logsDir:String; + public final config:Config; + final verbose:Bool; final statePath:String; var wss:WSServer; final localIp:String; var globalIp:String; var port:Int; - public final config:Config; final userList:UserList; final clients:Array<Client> = []; final freeIds:Array<Int> = []; @@ -48,7 +51,9 @@ class Main { var isPlaylistOpen = true; var itemPos = 0; - static function main():Void new Main(); + static function main():Void { + new Main(); + } function new() { verbose = Sys.args().has("--verbose"); @@ -87,8 +92,8 @@ class Main { var attempts = 5; function preparePort():Void { - Utils.isPortFree(port, free -> { - if (!free && attempts > 0) { + Utils.isPortFree(port, isFree -> { + if (!isFree && attempts > 0) { trace('Warning: port $port is already in use. Changed to ${port + 1}'); attempts--; port++; @@ -138,8 +143,9 @@ class Main { } function generateConfigSalt():String { - if (userList.salt == null) + if (userList.salt == null) { userList.salt = Sha256.encode('${Math.random()}'); + } return userList.salt; } @@ -155,7 +161,9 @@ class Main { if (type == field) continue; if (group.indexOf(type) == -1) continue; group.remove(type); - for (item in getPermissions(type)) group.push(item); + for (item in getPermissions(type)) { + group.push(item); + } } } return config; @@ -167,7 +175,9 @@ class Main { 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'); + 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> = []; @@ -175,7 +185,9 @@ class Main { 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'); + if (emoteCopies[emote.image]) { + trace('Warning: emote url of name "${emote.name}" has copy'); + } emoteCopies[emote.image] = true; } return config; @@ -218,10 +230,14 @@ class Main { final data:ServerState = Json.parse(File.getContent(statePath)); videoList.resize(0); messages.resize(0); - for (item in data.videoList) videoList.push(item); + for (item in data.videoList) { + videoList.push(item); + } isPlaylistOpen = data.isPlaylistOpen; itemPos = data.itemPos; - for (message in data.messages) messages.push(message); + for (message in data.messages) { + messages.push(message); + } videoTimer.start(); videoTimer.setTime(data.timer.time); videoTimer.pause(); @@ -346,8 +362,9 @@ class Main { switch (data.type) { case Connected: if (!internal) return; - if (clients.length == 1 && videoList.length > 0) + if (clients.length == 1 && videoList.length > 0) { if (videoTimer.isPaused()) videoTimer.play(); + } send(client, { type: Connected, @@ -381,7 +398,8 @@ class Main { Timer.delay(() -> { if (clients.exists(i -> i.name == client.name)) return; broadcast({ - type: ServerMessage, serverMessage: { + type: ServerMessage, + serverMessage: { textId: '${client.name} has left' } }); @@ -405,8 +423,9 @@ class Main { } } else { if (userList.admins.exists( - a -> a.name.toLowerCase() == lcName && a.hash == hash - )) client.isAdmin = true; + a -> a.name.toLowerCase() == lcName && a.hash == hash)) { + client.isAdmin = true; + } else { serverMessage(client, "passwordMatchError"); send(client, {type: LoginError}); @@ -462,8 +481,7 @@ class Main { if (!isPlaylistOpen) { if (!checkPermission(client, LockPlaylistPerm)) return; } - if (config.totalVideoLimit != 0 - && videoList.length >= config.totalVideoLimit) { + if (config.totalVideoLimit != 0 && videoList.length >= config.totalVideoLimit) { serverMessage(client, "totalVideoLimitError"); return; } @@ -540,7 +558,8 @@ class Main { if (videoList.length != currentLength) return; if (itemPos != currentPos) return; skipVideo({ - type: SkipVideo, skipVideo: { + type: SkipVideo, + skipVideo: { url: videoList[itemPos].url } }); @@ -548,7 +567,8 @@ class Main { return; } final obj:WsEvent = { - type: GetTime, getTime: { + type: GetTime, + getTime: { time: videoTimer.getTime() } }; @@ -588,7 +608,8 @@ class Main { } clients.setLeader(clientName); broadcast({ - type: SetLeader, setLeader: { + type: SetLeader, + setLeader: { clientName: clientName } }); @@ -597,7 +618,8 @@ class Main { if (videoTimer.isPaused()) videoTimer.play(); videoTimer.setRate(1); broadcast({ - type: Play, play: { + type: Play, + play: { time: videoTimer.getTime() } }); @@ -643,15 +665,17 @@ class Main { broadcast({ type: UpdatePlaylist, updatePlaylist: { - videoList: videoList - }}); + videoList: videoList + } + }); case UpdatePlaylist: broadcast({ type: UpdatePlaylist, updatePlaylist: { - videoList: videoList - }}); + videoList: videoList + } + }); case TogglePlaylistLock: if (!checkPermission(client, LockPlaylistPerm)) return; @@ -691,7 +715,8 @@ class Main { function serverMessage(client:Client, textId:String):Void { send(client, { - type: ServerMessage, serverMessage: { + type: ServerMessage, + serverMessage: { textId: textId } }); @@ -703,7 +728,8 @@ class Main { function broadcast(data:WsEvent):Void { final json = Json.stringify(data); - for (client in clients) client.ws.send(json, null); + for (client in clients) + client.ws.send(json, null); } function broadcastExcept(skipped:Client, data:WsEvent):Void { @@ -726,7 +752,8 @@ class Main { function checkPermission(client:Client, perm:Permission):Bool { final state = client.hasPermission(perm, config.permissions); if (!state) send(client, { - type: ServerMessage, serverMessage: { + type: ServerMessage, + serverMessage: { textId: "accessError" } }); @@ -767,5 +794,4 @@ class Main { broadcast({type: VideoLoaded}); videoTimer.start(); } - } diff --git a/src/server/ServerState.hx b/src/server/ServerState.hx index 9c8d751..4ccdd44 100644 --- a/src/server/ServerState.hx +++ b/src/server/ServerState.hx @@ -9,7 +9,6 @@ typedef ServerState = { itemPos:Int, messages:Array<Message>, timer:{ - time:Float, - paused:Bool + time:Float, paused:Bool } } diff --git a/src/server/Utils.hx b/src/server/Utils.hx index bab6300..9aa0d66 100644 --- a/src/server/Utils.hx +++ b/src/server/Utils.hx @@ -1,18 +1,17 @@ package server; -import js.node.url.URL; -import js.node.Https; import js.node.Http; +import js.node.Https; import js.node.Os; +import js.node.url.URL; import sys.FileSystem; class Utils { - public static function ensureDir(path:String):Void { if (!FileSystem.exists(path)) FileSystem.createDirectory(path); } - public static function isPortFree(port:Int, callback:(free:Bool)->Void):Void { + public static function isPortFree(port:Int, callback:(isFree:Bool) -> Void):Void { final server = Http.createServer(); final timeout = 1000; var status = false; @@ -22,7 +21,7 @@ class Utils { status = false; server.close(); }); - server.once("timeout", function () { + server.once("timeout", function() { status = false; trace('Timeout (${timeout}ms) occurred waiting for port $port to be available'); server.close(); @@ -35,7 +34,7 @@ class Utils { server.listen(port); } - public static function getGlobalIp(callback:(ip:String)->Void):Void { + public static function getGlobalIp(callback:(ip:String) -> Void):Void { function onError(e):Void { trace("Warning: connection error, server is local."); callback("127.0.0.1"); @@ -51,8 +50,7 @@ class Utils { final data = new StringBuf(); r.on("data", chunk -> data.add(chunk)); r.on("end", _ -> callback(data.toString())); - }).on("error", onError) - .on("timeout", onError); + }).on("error", onError).on("timeout", onError); } public static function getLocalIp():String { @@ -89,5 +87,4 @@ class Utils { arr[n] = a; } } - } diff --git a/src/server/VideoTimer.hx b/src/server/VideoTimer.hx index 53d4e28..2dd6720 100644 --- a/src/server/VideoTimer.hx +++ b/src/server/VideoTimer.hx @@ -3,8 +3,8 @@ package server; import haxe.Timer.stamp; class VideoTimer { - public var isStarted(default, null) = false; + var startTime = 0.0; var pauseStartTime = 0.0; var rateStartTime = 0.0; @@ -75,5 +75,4 @@ class VideoTimer { if (rateStartTime == 0) return 0; return stamp() - rateStartTime - pauseTime(); } - } |
