private string ExecuteCommand(string command, SecurityIP ip) { if (command == null || command.Length <= 0) return _unknown; else { string cmd = command[0].ToString(); string parameters = command.Substring(1); switch (cmd) { //Get list of SubSystems case "0": return Common.GetListOfSubSystems(); //Get content of SubSystem case "1": return Common.GetContentOfSubSystem(parameters); //Get properties case "2": return Common.GetProperties(parameters.Split(new string[] { ";" }, StringSplitOptions.None), ip.IsLocal); //Set properties case "3": LogMessage(command, new LogMessageEventArgs(string.Format("{0} {1}", AddinManager.CurrentLocalizer.GetString("SettingProperties"), parameters), null, LogLevel.Info)); return Common.SetProperties(parameters.Split(new string[] { ";" }, StringSplitOptions.None)).ToString(); //Execute SubSystem commands case "4": LogMessage(command, new LogMessageEventArgs(string.Format("{0} {1}", AddinManager.CurrentLocalizer.GetString("ExecutingCommands"), parameters), null, LogLevel.Info)); return Common.ExecuteSubSystemCommand(parameters.Split(new string[] { ";" }, StringSplitOptions.None)).ToString(); //Get the numbers of red and yellow properties case "5": return Common.GetNumbersOfRedYellow(); //Get the list of red or yellow properties case "6": return Common.GetListOfRedYellow(); //Get history values case "7": return Common.GetHistoryValues(parameters); default: return _unknown; } } }
private void SendData(HttpListenerContext context, SecurityIP ip) { try { string data = ""; if (context.Request.HttpMethod == "POST" && context.Request.HasEntityBody) { using (Stream body = context.Request.InputStream) { using (StreamReader reader = new StreamReader(body, context.Request.ContentEncoding)) { data = reader.ReadToEnd(); } } } string[] array = Common.ParseCommand(data); Answer(context, ExecuteCommand(array[1], ip)); } catch (Exception ex) { LogMessage(context, new LogMessageEventArgs(ex.Message, ex, LogLevel.Warning)); } }
private bool CheckAccess(SecurityIP ip, HttpListenerContext context, bool isUpdateTime) { bool answer = false; switch (ip.AccessLevel) { case AccessLevel.White: ip.Access = Access.IsAllowed; if (isUpdateTime) ip.LastRequest = DateTime.Now; answer = true; break; case AccessLevel.Light: bool checkPassword = true; if ((ip.Access == Access.IsAllowed && ip.LastRequest >= DateTime.Now.AddMinutes(_configuration.SessionMinutes * (-1)))) { checkPassword = false; if (isUpdateTime) ip.LastRequest = DateTime.Now; answer = true; } if (checkPassword) { ip.LastRequest = DateTime.Now; Dictionary<string, string> parameters = GetPOSTParameters(context.Request); if (ip.Access == Access.NeedPassword && CheckPassword(parameters.ContainsKey("pass") ? parameters["pass"] : "")) { ip.Access = Access.IsAllowed; answer = true; } else { ip.Access = Access.NeedPassword; SendLogin1WebPage(context); } } break; case AccessLevel.Gray: bool checkPwd = true; if (ip.Access == Access.IsAllowed && ip.LastRequest >= DateTime.Now.AddMinutes(_configuration.SessionMinutes * (-1))) { checkPwd = false; if (isUpdateTime) ip.LastRequest = DateTime.Now; answer = true; } if (checkPwd) { ip.LastRequest = DateTime.Now; Dictionary<string, string> parameters = GetPOSTParameters(context.Request); if (ip.Access == Access.NeedPassword && CheckPassword(parameters.ContainsKey("pass") ? parameters["pass"] : "")) { ip.Access = Access.NeedSMSPassword; Common.GenerateSMSPassword(); SendLogin2WebPage(context); } else if (ip.Access == Access.NeedSMSPassword && Common.CheckSMSPassword(parameters.ContainsKey("pass") ? parameters["pass"] : "")) { ip.Access = Access.IsAllowed; answer = true; } else { ip.Access = Access.NeedPassword; SendLogin1WebPage(context); } } break; default: case AccessLevel.Black: ip.Access = Access.Denied; context.Response.Abort(); break; } return answer; }