public static bool fetchPushMessages(bool force = false) { if (force == false && lastUpdate + cooldownPeriod > Clock.getTimestamp()) { return(false); } lastUpdate = Clock.getTimestamp(); if (pushNotificationAuthKey == null) { if (!registerWithPushNotificationServer()) { return(false); } } try { string receiver = Base58Check.Base58CheckEncoding.EncodePlain(Node.walletStorage.getPrimaryAddress()); nonce++; byte[] sig = Crypto.sha512(UTF8Encoding.UTF8.GetBytes(nonce + pushNotificationAuthKey)); using (WebClient client = new WebClient()) { string url = String.Format("{0}/fetch.php?tag={1}&nonce={2}&sig={3}", Config.pushServiceUrl, receiver, nonce, Crypto.hashToString(sig)); string htmlCode = client.DownloadString(url); if (htmlCode.StartsWith("ERROR")) { if (htmlCode.StartsWith("ERROR: Nonce too low ")) { nonce = Int32.Parse(htmlCode.Substring("ERROR: Nonce too low ".Length)); } return(false); } if (htmlCode == "UNREGISTERED") { pushNotificationAuthKey = null; registerWithPushNotificationServer(); return(false); } List <string[]> jsonResponse = JsonConvert.DeserializeObject <List <string[]> >(htmlCode); if (jsonResponse != null && jsonResponse.Count > 0) { lastUpdate = 0; // If data was available, fetch it again without cooldown } foreach (string[] str in jsonResponse) { try { byte[] data = Convert.FromBase64String(str[1]); if (str[2] != "") { byte[] pk = Convert.FromBase64String(str[2]); Friend f = FriendList.getFriend(new Address(pk).address); if (f != null && pk != null) { f.setPublicKey(pk); } } StreamProcessor.receiveData(data, null); } catch (Exception e) { Logging.error("Exception occured in fetchPushMessages while parsing the json response: {0}", e); } try { nonce++; sig = Crypto.sha512(UTF8Encoding.UTF8.GetBytes(nonce + pushNotificationAuthKey)); string id = str[0]; url = String.Format("{0}/remove.php?id={1}&nonce={2}&sig={3}", Config.pushServiceUrl, id, nonce, Crypto.hashToString(sig)); htmlCode = client.DownloadString(url); } catch (Exception e) { Logging.error("Exception occured in fetchPushMessages while removing the message from server: {0}", e); } } } } catch (Exception e) { Logging.error("Exception occured in fetchPushMessages: {0}", e); return(false); } return(true); }
public static bool fetchPushMessages(bool force = false) { if (force == false && lastUpdate + cooldownPeriod > Clock.getTimestamp()) { return(false); } lastUpdate = Clock.getTimestamp(); try { string URI = String.Format("{0}/fetch.php", Config.pushServiceUrl); string unique_uri = String.Format("{0}/uniqueid.php", Config.pushServiceUrl); string receiver = Base58Check.Base58CheckEncoding.EncodePlain(Node.walletStorage.getPrimaryAddress()); WebClient uclient = new WebClient(); byte[] checksum = Convert.FromBase64String(uclient.DownloadString(unique_uri)); byte[] sig = CryptoManager.lib.getSignature(checksum, Node.walletStorage.getPrimaryPrivateKey()); using (WebClient client = new WebClient()) { string url = String.Format("{0}?tag={1}&sig={2}", URI, receiver, HttpUtility.UrlEncode(Convert.ToBase64String(sig))); string htmlCode = client.DownloadString(url); Logging.info("fetchPushMessages: {0}", htmlCode); if (htmlCode == "FALSE") { return(false); } lastUpdate = 0; // If data was available, fetch it again without cooldown List <string[]> jsonResponse = JsonConvert.DeserializeObject <List <string[]> >(htmlCode); foreach (string[] str in jsonResponse) { try { byte[] data = Convert.FromBase64String(str[0]); if (str[1] != "") { byte[] pk = Convert.FromBase64String(str[1]); Friend f = FriendList.getFriend(new Address(pk).address); if (f != null) { f.publicKey = pk; } FriendList.saveToStorage(); } StreamProcessor.receiveData(data, null); } catch (Exception e) { Logging.error(string.Format("Exception occured in fetchPushMessages while parsing the json response. {0}", e)); } } } } catch (Exception e) { Logging.error(string.Format("Exception occured in fetchPushMessages. {0}", e)); return(false); } return(true); }