public Json GetJson() { Json j = new Json(); j.EnsureArray(); lock (Entries) { foreach (LogEntry entry in Entries) { j.Append(entry.GetJson()); } } return(j); }
public virtual Json GetRealtimeNetworkStats() { Json result = new Json(); result.EnsureArray(); NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces(); foreach (NetworkInterface adapter in interfaces) { string id = adapter.Id; IPv4InterfaceStatistics istats = adapter.GetIPv4Statistics(); Int64 bytesRcv = istats.BytesReceived; Int64 bytesSnd = istats.BytesSent; Json jInterface = new Json(); jInterface["id"].Value = id; //jInterface["ts"].Value = Utils.UnixTimeStamp(); jInterface["rcv"].Value = bytesRcv; jInterface["snd"].Value = bytesSnd; result.Append(jInterface); } return(result); }
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); }
private static bool FromJson(string s, bool canThrowException, out object result, out string remain) { try { s = s.TrimStart(); if (s == "") { throw new Exception("Empty value"); } if (s[0] == '[') { s = s.Substring(1).TrimStart(); Json a = new Json(); a.InitAsArray(); for (;;) { if (s[0] == ']') { break; } object v; FromJson(s, canThrowException, out v, out s); a.Append(v); s = s.TrimStart(); if (s[0] == ',') { s = s.Substring(1).TrimStart(); } } result = a; remain = s.Substring(1).TrimStart(); return(true); } else if (s[0] == '{') { s = s.Substring(1).TrimStart(); Json a = new Json(); a.InitAsDictionary(); for (;;) { if (s[0] == '}') { break; } object k; FromJson(s, canThrowException, out k, out s); s = s.TrimStart(); if (s[0] == ':') { s = s.Substring(1).TrimStart(); object v; FromJson(s, canThrowException, out v, out s); a.SetKey(k as string, v); s = s.TrimStart(); if (s[0] == ',') { s = s.Substring(1).TrimStart(); } } else { throw new Exception("Syntax error"); } } result = a; remain = s.Substring(1).TrimStart(); return(true); } else { // Direct value bool inQuote = false; int i = 0; for (i = 0; i < s.Length + 1; i++) { char ch = (char)0; if (i < s.Length) { ch = s[i]; } if (inQuote) { if ((ch == '\"') && ((i == 0) || (s[i - 1] != '\\'))) { inQuote = false; } } else { if ((ch == '\"') && ((i == 0) || (s[i - 1] != '\\'))) { inQuote = true; } else if ((ch == (char)0) || (ch == ',') || (ch == ':') || (ch == '}') || (ch == ']')) { // Valore singolo string value = s.Substring(0, i).Trim(); if ((value.StartsWith("\"")) && (value.EndsWith("\""))) { result = DecodeString(value.Substring(1, value.Length - 2)); remain = s.Substring(i); return(true); } if (value == "null") { result = null; remain = s.Substring(i); return(true); } if (value == "true") { result = true; remain = s.Substring(i); return(true); } if (value == "false") { result = false; remain = s.Substring(i); return(true); } int dI = 0; if (int.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out dI)) { result = dI; remain = s.Substring(i); return(true); } double dD = 0; if (double.TryParse(value, NumberStyles.Float, CultureInfo.InvariantCulture, out dD)) { result = dD; remain = s.Substring(i); return(true); } throw new Exception("Cannot detect type of value '" + value + "'"); } } } throw new Exception("Syntax error"); } } catch (Exception e) { if (canThrowException) { throw new Exception("JsonParser:" + e.Message); } } result = null; remain = s; return(false); }
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); }