public void FromJson(Json jResponse) { if (jResponse.HasKey("headers")) { string[] lines = jResponse["headers"].ValueString.Split('\n'); Status = ""; foreach (string line in lines) { string l = line.Trim(); if (l == "") { continue; } if (Status == "") { Status = l.Trim(); } else { int posSep = l.IndexOfInv(":"); if (posSep != -1) { string k = ""; string v = ""; k = l.Substring(0, posSep).ToLowerInvariant().Trim(); v = l.Substring(posSep + 1).Trim(); if (k != "") { Headers.Add(new KeyValuePair <string, string>(k, v)); } } } } } if (jResponse.HasKey("body")) { BufferData = ExtensionsString.HexToBytes(jResponse["body"].ValueString); } if (jResponse.HasKey("response_code")) { ResponseCode = jResponse["response_code"].ValueInt; } }
private static bool AllowPath(string path) { string sha256 = ""; string signId = ""; if (Platform.Instance.FileExists(path) == false) { return(false); } List <string> trustedPaths = Platform.Instance.GetTrustedPaths(); foreach (string trustedPath in trustedPaths) { if (path.StartsWith(trustedPath, StringComparison.InvariantCulture)) { return(true); } } // Avoid if possible any shell before the storage init. if (Engine.Instance.Storage == null) { return(false); } Json rulesCustom = Engine.Instance.Storage.GetJson("external.rules"); for (int r = 0; r < 2; r++) { Json rules = null; if (r == 0) { if (Engine.Instance.Storage.GetBool("external.rules.recommended")) { rules = Engine.Instance.Manifest["external-rules-recommended"].Value as Json; } else { continue; } } else if (r == 1) { rules = rulesCustom; } foreach (Json rule in rules.GetArray()) { string type = rule["type"].Value as string; if (type == "all") { return(true); } if (type == "sign") { if (signId == "") { signId = Platform.Instance.FileGetSignedId(path); } if (rule["id"].Value as string == signId) { return(true); } } if (type == "sha256") { if (sha256 == "") { sha256 = UtilsCore.HashSHA256File(path); } if (rule["hash"].Value as string == sha256) { return(true); } } if (type == "path") { if (rule["path"].Value as string == path) { return(true); } } } } // Ensure compute, Report and result if (signId == "") { signId = Platform.Instance.FileGetSignedId(path); } if (sha256 == "") { sha256 = UtilsCore.HashSHA256File(path); } Json askToUi = new Json(); askToUi["sha256"].Value = sha256; askToUi["sign-id"].Value = signId; askToUi["path"].Value = path; // Propose to add rule to UI Json replyUi = Engine.Instance.OnAskShellExternalPermission(askToUi); if (replyUi.HasKey("allow")) { if (Convert.ToBoolean(replyUi["allow"].Value) == false) { return(false); } } if (replyUi.HasKey("type")) { replyUi.RemoveKey("allow"); rulesCustom.Append(replyUi); Engine.Instance.Storage.SetJson("external.rules", rulesCustom); return(AllowPath(path)); } if (replyUi.HasKey("allow")) { if (Convert.ToBoolean(replyUi["allow"].Value) == true) { return(true); } } //Engine.Instance.Storage.SetJson("external.rules", rules); return(false); }
public Json OnCommand(Json data, UiClient sender) { string cmd = data["command"].Value as string; if (cmd == "test-json") { Json j = new Json(); j["result"].Value = "works"; return(j); } else if (cmd == "exit") { Engine.Instance.OnExit(); } else if (cmd == "system.report.start") { Report report = new Report(); report.Start(sender); } else if (cmd == "openvpn_management") { if (Engine.Instance.SendManagementCommand(data["management_command"].Value as string) == false) { Engine.Instance.Logs.Log(LogType.Warning, Messages.OpenVpnManagementCommandFail); } } else if (cmd == "tor_control") { string resultC = TorControl.SendCommand(data["control_command"].Value as string); foreach (string line in resultC.Split('\n')) { string l = line.Trim(); if (l != "") { Engine.Instance.Logs.Log(LogType.Verbose, l); } } } else if (cmd == "set_option") { string name = data["name"].Value as string; string value = data["value"].Value as string; if (Engine.Instance.Storage.Set(name, data["value"].Value)) { if (name == "tools.openvpn.path") { Software.Checking(); } } } else if (cmd == "ui.stats.pathprofile") { Platform.Instance.OpenDirectoryInFileManager(Engine.Instance.Stats.Get("PathProfile").Value); } else if (cmd == "ui.stats.pathdata") { Platform.Instance.OpenDirectoryInFileManager(Engine.Instance.Stats.Get("PathData").Value); } else if (cmd == "ui.stats.pathapp") { Platform.Instance.OpenDirectoryInFileManager(Engine.Instance.Stats.Get("PathApp").Value); } else if (cmd == "man") { string format = "text"; if (data.HasKey("format")) { format = data["format"].Value as string; } string body = Engine.Instance.Storage.GetMan(format); Engine.Instance.OnShowText("man", body); } else if (cmd == "ui.show.os.info") { Engine.Instance.OnShowText("ui.show.os.info", Engine.Instance.GenerateOsInfo().ToJsonPretty()); } else if (cmd == "tor.guard") { Engine.Instance.Logs.LogVerbose("Tor Guard IPs:" + TorControl.GetGuardIps(true).ToString()); } else if (cmd == "tor.NEWNYM") { TorControl.SendNEWNYM(); } else if (cmd == "ip.exit") { Engine.Instance.Logs.LogVerbose(Engine.Instance.DiscoverExit().ToString()); } else if (cmd == "test.log.info") { Engine.Instance.Logs.Log(LogType.InfoImportant, "Test log\nInfo"); } else if (cmd == "test.log.infoimportant") { Engine.Instance.Logs.Log(LogType.InfoImportant, "Test log\nInfo Important"); } else if (cmd == "test.log.warning") { Engine.Instance.Logs.Log(LogType.Warning, "Test log\nWarning"); } else if (cmd == "test.log.error") { Engine.Instance.Logs.Log(LogType.Error, "Test log\nError"); } else if (cmd == "test.log.fatal") { Engine.Instance.Logs.Log(LogType.Fatal, "Test log\nFatal"); } else if (cmd == "test.netlock.update") { if (Engine.Instance.NetworkLockManager != null) { Engine.Instance.NetworkLockManager.OnUpdateIps(); } } return(null); }
public Json ProcessCommand(Json data, UiClient sender) { string cmd = data["command"].Value as string; if (cmd == "exit") { Engine.Instance.Exit(); } else if (cmd == "mainaction.connect") { Engine.Instance.Connect(); } else if (cmd == "mainaction.disconnect") { Engine.Instance.Disconnect(); } else if (cmd == "system.report.start") { Report report = new Report(); report.Start(sender); } // TOCLEAN_OPENVPNMANAGEMENT /* * else if (cmd == "openvpn_management") * { * if (Engine.Instance.SendManagementCommand(data["management_command"].Value as string) == false) * Engine.Instance.Logs.Log(LogType.Warning, LanguageManager.GetText("OpenVpnManagementCommandFail")); * } */ else if (cmd == "tor_control") { string resultC = TorControl.SendCommand(data["control_command"].Value as string); foreach (string line in resultC.Split('\n')) { string l = line.Trim(); if (l != "") { Engine.Instance.Logs.Log(LogType.Verbose, l); } } } else if (cmd == "set_option") { string name = data["name"].Value as string; string value = data["value"].Value as string; if (Engine.Instance.Storage.Set(name, data["value"].Value)) { if (name == "tools.openvpn.path") { Software.Checking(); } } } else if (cmd == "ui.stats.pathprofile") { Platform.Instance.OpenDirectoryInFileManager(Engine.Instance.Stats.Get("PathProfile").Value); } else if (cmd == "ui.stats.pathdata") { Platform.Instance.OpenDirectoryInFileManager(Engine.Instance.Stats.Get("PathData").Value); } else if (cmd == "ui.stats.pathapp") { Platform.Instance.OpenDirectoryInFileManager(Engine.Instance.Stats.Get("PathApp").Value); } else if (cmd == "ui.start") { Json result = new Json(); result["manifest"].Value = Engine.Instance.Manifest; result["logs"].Value = Engine.Instance.Logs.GetJson(); return(result); } else if (cmd == "man") { string format = "text"; if (data.HasKey("format")) { format = data["format"].Value as string; } Json result = new Json(); result["layout"].Value = "text"; result["title"].Value = "MAN"; result["body"].Value = Engine.Instance.Storage.GetMan(format); return(result); } else if (cmd == "ui.show.os.info") { return(Engine.Instance.GenerateOsInfo().Clone()); } else if (cmd == "tor.guard") { Engine.Instance.Logs.LogVerbose("Tor Guard IPs:" + TorControl.GetGuardIps(true).ToString()); } else if (cmd == "tor.NEWNYM") { TorControl.SendNEWNYM(); } else if (cmd == "ip.exit") { Engine.Instance.Logs.LogVerbose(Engine.Instance.DiscoverExit().ToString()); } else if (cmd == "test.query") { Json result = new Json(); result["result"].Value = cmd; return(result); } else if (cmd == "test.logs") { Engine.Instance.Logs.Log(LogType.InfoImportant, "Test log\nInfo"); Engine.Instance.Logs.Log(LogType.InfoImportant, "Test log\nInfo Important"); Engine.Instance.Logs.Log(LogType.Warning, "Test log\nWarning\n" + DateTime.Now.ToString()); Engine.Instance.Logs.Log(LogType.Error, "Test log\nError"); //Engine.Instance.Logs.Log(LogType.Fatal, "Test log\nFatal"); } return(null); }
public static Json ToJson(XmlElement xml) { /* * Comments are ignored. * Node renames in json conversion can be forced with attribute json-convert-name. * Key name are lowered-case. * * Can fail if: * 1- There are duplicate child names. Example: * <test> * <alfa/> * <beta/> * <alfa/> * </test> * If all childs have the same name, works (detected as Array) * * 2- There are nested texts. Example: * <test> * mytext1 * <alfa/> * mytext2 * </test> * */ Json result = new Json(); foreach (XmlAttribute attr in xml.Attributes) { string keyName = attr.Name.ToLowerInvariant(); if (keyName.StartsWith("json-convert-")) { continue; } // Try Cast? if (result.HasKey(keyName)) { throw new Exception("Cannot convert."); } object value = attr.Value; if (attr.Value.ToLowerInvariant() == "true") { value = true; } else if (attr.Value.ToLowerInvariant() == "true") { value = false; } result.SetKey(keyName, value); } // Exception: if not have attributes, and childs are XmlElement with all the same name, use Json Array. bool isArray = false; if (xml.Attributes.Count == 0) { isArray = true; string commonName = ""; int nChildsWithName = 0; // Not really used yet foreach (XmlNode child in xml.ChildNodes) { if (child is XmlComment) { // Ignore } else if (child is XmlText) { // No Array isArray = false; break; } else if (child is XmlElement) { XmlElement xmlElement = child as XmlElement; string keyName = child.Name.ToLowerInvariant(); if (xmlElement.HasAttribute("json-convert-name")) { keyName = xmlElement.GetAttribute("json-convert-name"); } if (commonName == "") { commonName = keyName; } else if (commonName != keyName) { // No Array isArray = false; break; } nChildsWithName++; } else { throw new Exception("Xml node unknown type"); } } } foreach (XmlNode child in xml.ChildNodes) { if (child is XmlComment) { // Ignore } else if (child is XmlText) { /* * if (result.HasKey(child.ParentNode.Name)) * throw new Exception("Cannot convert."); * result.SetKey(child.ParentNode.Name, child.InnerText); */ } else if (child is XmlElement) { XmlElement xmlChild = child as XmlElement; // Exception: if contain text bool textFound = false; foreach (XmlNode xmlChild2 in xmlChild.ChildNodes) { if (xmlChild2 is XmlText) { result.SetKey(xmlChild.Name.ToLowerInvariant(), xmlChild.InnerText); textFound = true; break; } } if (textFound) { continue; } // Exception: if have only two attribute 'name' and 'value' if ((xmlChild.Attributes.Count == 2) && (xmlChild.HasAttribute("name")) && (xmlChild.HasAttribute("value"))) { if (result.HasKey(xmlChild.GetAttribute("name"))) { throw new Exception("Cannot convert."); } result.SetKey(xmlChild.GetAttribute("name").ToLowerInvariant(), xmlChild.GetAttribute("value")); } else { XmlElement xmlElement = child as XmlElement; string keyName = child.Name.ToLowerInvariant(); if (xmlElement.HasAttribute("json-convert-name")) { keyName = xmlElement.GetAttribute("json-convert-name"); } if ((isArray == false) && (result.HasKey(keyName))) { throw new Exception("Cannot convert."); } Json jChild = ToJson(xmlElement); if (isArray) { result.Append(jChild); } else { result.SetKey(keyName, jChild); } } } else { throw new Exception("Xml node unknown type"); } } return(result); }