From 5f532163897681d898ec7ca81ec9979dd6e94fa7 Mon Sep 17 00:00:00 2001 From: scart Date: Wed, 5 Nov 2014 14:30:30 +0300 Subject: [PATCH 1/3] Change protocol to TLS Apple has changed the server security and you have to change from SSL to TLS. http://stackoverflow.com/questions/18893347/a-call-to-sspi-failed-see-inner-exception --- Application/MoonApns/PushNotification.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Application/MoonApns/PushNotification.cs b/Application/MoonApns/PushNotification.cs index 4c6285a..e65e3e7 100644 --- a/Application/MoonApns/PushNotification.cs +++ b/Application/MoonApns/PushNotification.cs @@ -229,7 +229,7 @@ private bool OpenSslStream(string host, X509CertificateCollection certificates) try { - _apnsStream.AuthenticateAsClient(host, certificates, System.Security.Authentication.SslProtocols.Ssl3, false); + _apnsStream.AuthenticateAsClient(host, certificates, System.Security.Authentication.SslProtocols.Tls, false); } catch (System.Security.Authentication.AuthenticationException ex) { From 8861f26ea10408937d90a710f0cc74bfb9916419 Mon Sep 17 00:00:00 2001 From: scart Date: Thu, 6 Nov 2014 16:59:38 +0300 Subject: [PATCH 2/3] Switch to UTF8 encoding --- Application/MoonApns/PushNotification.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Application/MoonApns/PushNotification.cs b/Application/MoonApns/PushNotification.cs index e65e3e7..f9e1983 100644 --- a/Application/MoonApns/PushNotification.cs +++ b/Application/MoonApns/PushNotification.cs @@ -276,7 +276,7 @@ private static byte[] GeneratePayload(NotificationPayload payload) memoryStream.WriteByte(1); // Changed command Type //Adding ID to Payload - memoryStream.Write(Encoding.ASCII.GetBytes(payload.PayloadId.ToString()), 0, payload.PayloadId.ToString().Length); + memoryStream.Write(Encoding.UTF8.GetBytes(payload.PayloadId.ToString()), 0, payload.PayloadId.ToString().Length); //Adding ExpiryDate to Payload int epoch = (int) (DateTime.UtcNow.AddMinutes(300) - new DateTime(1970, 1, 1)).TotalSeconds; @@ -302,7 +302,7 @@ private static byte[] GeneratePayload(NotificationPayload payload) memoryStream.Write(apnMessageLength, 0, 2); // Write the message - memoryStream.Write(Encoding.ASCII.GetBytes(apnMessage), 0, apnMessage.Length); + memoryStream.Write(Encoding.UTF8.GetBytes(apnMessage), 0, apnMessage.Length); return memoryStream.ToArray(); } catch (Exception ex) From 7809a109153c356f5ef18adff2b48a3778bf1a9e Mon Sep 17 00:00:00 2001 From: scart Date: Fri, 7 Nov 2014 16:15:23 +0300 Subject: [PATCH 3/3] switch to utf8 --- Application/MoonApns/MoonAPNS.csproj | 13 ++------ Application/MoonApns/NLog.config | 14 --------- Application/MoonApns/PushNotification.cs | 38 ++++-------------------- 3 files changed, 8 insertions(+), 57 deletions(-) delete mode 100644 Application/MoonApns/NLog.config diff --git a/Application/MoonApns/MoonAPNS.csproj b/Application/MoonApns/MoonAPNS.csproj index 95bef19..d9041f6 100644 --- a/Application/MoonApns/MoonAPNS.csproj +++ b/Application/MoonApns/MoonAPNS.csproj @@ -40,7 +40,7 @@ 4 - x86 + AnyCPU pdbonly true bin\Release\ @@ -49,15 +49,13 @@ 4 - + + ..\..\Libraries\Newtonsoft.Json.dll - - ..\..\Libraries\NLog.dll - @@ -74,11 +72,6 @@ - - - Always - - False diff --git a/Application/MoonApns/NLog.config b/Application/MoonApns/NLog.config deleted file mode 100644 index 0359a6f..0000000 --- a/Application/MoonApns/NLog.config +++ /dev/null @@ -1,14 +0,0 @@ - - - - - - - - - - - - - - diff --git a/Application/MoonApns/PushNotification.cs b/Application/MoonApns/PushNotification.cs index f9e1983..9b38bb5 100644 --- a/Application/MoonApns/PushNotification.cs +++ b/Application/MoonApns/PushNotification.cs @@ -22,13 +22,11 @@ limitations under the License. using System.Security.Cryptography.X509Certificates; using System.Text; using System.Threading; -using NLog; namespace MoonAPNS { public class PushNotification { - private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); private TcpClient _apnsClient; private SslStream _apnsStream; private X509Certificate _certificate; @@ -92,7 +90,6 @@ public PushNotification(bool useSandbox, string p12File, string p12FilePassword) public List SendToApple(List queue) { - Logger.Info("Payload queue received."); _notifications = queue; if (queue.Count < 8999) { @@ -134,15 +131,11 @@ private void SendQueueToapple(IEnumerable queue) item.PayloadId = i; byte[] payload = GeneratePayload(item); _apnsStream.Write(payload); - Logger.Info("Notification successfully sent to APNS server for Device Toekn : " + item.DeviceToken); Thread.Sleep(1000); //Wait to get the response from apple. } - else - Logger.Error("Invalid device token length, possible simulator entry: " + item.DeviceToken); } catch (Exception ex) { - Logger.Error("An error occurred on sending payload for device token {0} - {1}", item.DeviceToken, ex.Message); _conected = false; } i++; @@ -168,22 +161,17 @@ private void ReadResponse(IAsyncResult ar) payLoadId = Encoding.Default.GetString(ID); payLoadIndex = ((int.Parse(payLoadId)) - 1000); - Logger.Error("Apple rejected palyload for device token : " + _notifications[payLoadIndex].DeviceToken); - Logger.Error("Apple Error code : " + _errorList[status]); - Logger.Error("Connection terminated by Apple."); _rejected.Add(_notifications[payLoadIndex].DeviceToken); _conected = false; } } catch (Exception ex) { - Logger.Error("An error occurred while reading Apple response for token {0} - {1}", _notifications[payLoadIndex].DeviceToken, ex.Message); } } private void Connect(string host, int port, X509CertificateCollection certificates) { - Logger.Info("Connecting to apple server."); try { _apnsClient = new TcpClient(); @@ -191,7 +179,6 @@ private void Connect(string host, int port, X509CertificateCollection certificat } catch (SocketException ex) { - Logger.Error("An error occurred while connecting to APNS servers - " + ex.Message); } var sslOpened = OpenSslStream(host, certificates); @@ -199,7 +186,6 @@ private void Connect(string host, int port, X509CertificateCollection certificat if (sslOpened) { _conected = true; - Logger.Info("Conected."); } } @@ -214,17 +200,14 @@ private void Disconnect() _apnsStream.Dispose(); _apnsStream = null; _conected = false; - Logger.Info("Disconnected."); } catch (Exception ex) { - Logger.Error("An error occurred while disconnecting. - " + ex.Message); } } private bool OpenSslStream(string host, X509CertificateCollection certificates) { - Logger.Info("Creating SSL connection."); _apnsStream = new SslStream(_apnsClient.GetStream(), false, validateServerCertificate, SelectLocalCertificate); try @@ -233,19 +216,16 @@ private bool OpenSslStream(string host, X509CertificateCollection certificates) } catch (System.Security.Authentication.AuthenticationException ex) { - Logger.Error(ex.Message); return false; } if (!_apnsStream.IsMutuallyAuthenticated) { - Logger.Error("SSL Stream Failed to Authenticate"); return false; } if (!_apnsStream.CanWrite) { - Logger.Error("SSL Stream is not Writable"); return false; } return true; @@ -275,8 +255,10 @@ private static byte[] GeneratePayload(NotificationPayload payload) // Command memoryStream.WriteByte(1); // Changed command Type + + var payloadId = Encoding.UTF8.GetBytes(payload.PayloadId.ToString()); //Adding ID to Payload - memoryStream.Write(Encoding.UTF8.GetBytes(payload.PayloadId.ToString()), 0, payload.PayloadId.ToString().Length); + memoryStream.Write(payloadId, 0, payloadId.Length); //Adding ExpiryDate to Payload int epoch = (int) (DateTime.UtcNow.AddMinutes(300) - new DateTime(1970, 1, 1)).TotalSeconds; @@ -293,7 +275,6 @@ private static byte[] GeneratePayload(NotificationPayload payload) // String length string apnMessage = payload.ToJson(); - Logger.Info("Payload generated for " + payload.DeviceToken + " : " + apnMessage); byte[] apnMessageLength = BitConverter.GetBytes((Int16) apnMessage.Length); Array.Reverse(apnMessageLength); @@ -301,13 +282,13 @@ private static byte[] GeneratePayload(NotificationPayload payload) // message length memoryStream.Write(apnMessageLength, 0, 2); + var encoded = Encoding.UTF8.GetBytes(apnMessage); // Write the message - memoryStream.Write(Encoding.UTF8.GetBytes(apnMessage), 0, apnMessage.Length); + memoryStream.Write(encoded, 0, encoded.Length); return memoryStream.ToArray(); } catch (Exception ex) { - Logger.Error("Unable to generate payload - " + ex.Message); return null; } } @@ -317,7 +298,6 @@ public List GetFeedBack() try { var feedbacks = new List(); - Logger.Info("Connecting to feedback service."); if (!_conected) Connect(_feedbackHost, FeedbackPort, _certificates); @@ -331,15 +311,10 @@ public List GetFeedBack() //Get the first feedback recd = _apnsStream.Read(buffer, 0, buffer.Length); - Logger.Info("Feedback response received."); - - if (recd == 0) - Logger.Info("Feedback response is empty."); //Continue while we have results and are not disposing while (recd > 0) { - Logger.Info("processing feedback response"); var fb = new Feedback(); //Get our seconds since 1970 ? @@ -379,14 +354,11 @@ public List GetFeedBack() } //clode the connection here ! Disconnect(); - if (feedbacks.Count > 0) - Logger.Info("Total {0} feedbacks received.", feedbacks.Count); return feedbacks; } } catch (Exception ex) { - Logger.Error("Error occurred on receiving feed back. - " + ex.Message); return null; } return null;