private void AppendRule(string repositoryRule, string ruleFile) { Rule rule = new Rule(repositoryRule); rule.SetPath(ResourcePath + $"{repositoryRule}{System.IO.Path.DirectorySeparatorChar}"); foreach (string line in System.IO.File.ReadLines(ruleFile)) { if (string.IsNullOrEmpty(line) || !line.Contains(':') || line.StartsWith("--") || line.StartsWith('#')) { continue; } string[] parts = line.Split(':', 2, StringSplitOptions.RemoveEmptyEntries).Select(p => p.Trim()).ToArray(); if (parts.Length != 2) { if ((bool)Current.AllowOutput) { ErrorLogger.Error($"[Settings] : Property error in repository({repositoryRule}) rule file at line '{line}'.\nProperty ignored but it might causes bugs"); } continue; } if (parts[0].StartsWith("home")) { rule.SetHome(parts[1]); } else if (parts[0].StartsWith("connection-handler")) { rule.SetConnectionHandler(parts[1]); } else if (parts[0].StartsWith("upload-directory")) { rule.SetUploadDirectory(parts[1]); } else if (parts[0].StartsWith("shared-resources")) { rule.SetAsSharedResources("true".Equals(parts[1])); } else if (parts[0].StartsWith("available")) { rule.SetAvailable("true".Equals(parts[1])); } else if (parts[0].StartsWith("private-directories")) { rule.SetPrivateDirectories(parts[1].Split(',').Select(a => a.Trim()).ToArray()); } else if (parts[0].StartsWith("exclusive-for-server")) { rule.SetExclusiveForServer(parts[1]); } else if (parts[0].StartsWith("allow-websocket-protocol")) { rule.SetAllowWebsocket(parts[1].Equals("true")); } else if (parts[0].StartsWith("websocket-idle-timeout")) { rule.SetWebSocketIdelTimeout(int.Parse(parts[1])); } else if (parts[0].StartsWith("websocket-idle-chances")) { rule.SetWebSocketIdelChances(int.Parse(parts[1])); } else if (parts[0].StartsWith("allow-server-event")) { rule.SetAllowServerEvent(parts[1].Equals("true")); } else if (parts[0].StartsWith("server-event-method")) { rule.SetServerEventMethod(parts[1].ToLower().Equals("push") ? ServerEventMethod.PUSH : ServerEventMethod.LOOP); } else if (parts[0].StartsWith("startup")) { rule.SetStartupFile(parts[1]); } else if (parts[0].StartsWith("allow-cross-repo-requests")) { rule.SetAllowCrossRepositoriesRequests(parts[1].Equals("true")); } } try { rule.Verify(); if (rule.WebSocketIdelChances == -1) { rule.SetWebSocketIdelChances((int)Current.WebSocketIdelChances); } if (rule.WebSocketIdelTimeout == -1) { rule.SetWebSocketIdelTimeout((int)Current.WebSocketIdelChances); } RepositoriesRules.Add(repositoryRule, rule); } catch (Exception exp) { ErrorLogger.WithTrace(string.Format("[Warning][Server error => Verify()] : exception-message : {0}.\nstacktrace : {1}\n", exp.Message, exp.StackTrace), typeof(Settings)); } }
private void LoadSettings() { try { string lines = System.IO.File.ReadAllText(SettingsFilePath, System.Text.Encoding.UTF8); if (string.IsNullOrEmpty(lines)) { throw new System.IO.IOException("failed to read file:" + SettingsFilePath); } Current = Newtonsoft.Json.JsonConvert.DeserializeObject <dynamic>(lines); if (Current.MainPage == null) { throw new InvalidPropertyValue("[Settings] : settings.json must contains main-page property"); } if (Current.FileNotFoundPage == null) { throw new InvalidPropertyValue("[Settings] : settings.json must contains fnf-page property"); } if (Current.WebSocketIdelChances == null || Current.WebSocketIdelChances == null) { throw new InvalidPropertyValue("[Settings] : settings.json must contains WebSocketIdleTimeout and WebSocketIdelChances"); } Current.MainPage = BasePath + ((string)Current.MainPage).Replace('/', System.IO.Path.DirectorySeparatorChar); Current.FileNotFoundPage = BasePath + ((string)Current.FileNotFoundPage).Replace('/', System.IO.Path.DirectorySeparatorChar); RepositoriesRules?.Clear(); foreach (string repositoryRule in Current.Repos) { string ruleFile = ResourcePath + $"{repositoryRule}{System.IO.Path.DirectorySeparatorChar}.rules"; if (!System.IO.File.Exists(ruleFile)) { ErrorLogger.Warn("[Settings] : Repository '" + repositoryRule + "' has no .rules file which might cause issues, won't be available"); continue; } AppendRule(repositoryRule, ruleFile); } if ((bool)Current.AllowSocketControlFlow && Current.SocketControlFlow != null) { if (SocketControlFlow.Count > 0) { SocketControlFlow.Clear(); } foreach (dynamic item in Current.SocketControlFlow) { if (!(bool)item.Enabled) { continue; } SocketControlFlow controlFlow = new SocketControlFlow(); if (controlFlow.Setup((string)item.Filter, BasePath + ((string)item.Target).Replace('/', System.IO.Path.DirectorySeparatorChar))) { SocketControlFlow.Add(controlFlow); } } } if ((bool)Current.EnableVirtualLinks && Current.VirtualLinks != null) { if (VirtualLinks.Count > 0) { VirtualLinks.Clear(); } foreach (dynamic vlink in Current.VirtualLinks) { if ((bool)vlink.Dupricated) { continue; } VirtualLinks virtualLink = new(); virtualLink.Enabled = (bool)vlink.Enabled; virtualLink.Link = (string)vlink.Link; virtualLink.Target = (string)vlink.Target; VirtualLinks.Add(virtualLink.Link, virtualLink); } } ReceiveTimeout = (Current?.ReceiveTimeout != null) ? (int)Current?.ReceiveTimeout : System.Threading.Timeout.Infinite; SendTimeout = (Current.SendTimeout != null) ? (int)Current.SendTimeout : System.Threading.Timeout.Infinite; IsReady = true; } catch (Exception exp) { if (Current == null) { ErrorLogger.WithTrace(string.Format("[Warning][Server error => LoadSettings()] : exception-message : {0}.\nstacktrace : {1}\n", exp.Message, exp.StackTrace), GetType()); } else { ErrorLogger.WithTrace(this, string.Format("[Warning][Server error => LoadSettings()] : exception-message : {0}.\nstacktrace : {1}\n", exp.Message, exp.StackTrace), GetType()); } IsReady = false; } }