-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathApnsConfiguration.cs
More file actions
232 lines (172 loc) · 9.34 KB
/
ApnsConfiguration.cs
File metadata and controls
232 lines (172 loc) · 9.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
using System;
using System.Collections.Generic;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Text.RegularExpressions;
namespace PushSharp.Apple
{
public class ApnsConfiguration
{
#region Constants
const string APNS_SANDBOX_HOST = "gateway.sandbox.push.apple.com";
const string APNS_PRODUCTION_HOST = "gateway.push.apple.com";
const string APNS_SANDBOX_FEEDBACK_HOST = "feedback.sandbox.push.apple.com";
const string APNS_PRODUCTION_FEEDBACK_HOST = "feedback.push.apple.com";
const int APNS_SANDBOX_PORT = 2195;
const int APNS_PRODUCTION_PORT = 2195;
const int APNS_SANDBOX_FEEDBACK_PORT = 2196;
const int APNS_PRODUCTION_FEEDBACK_PORT = 2196;
#endregion
public ApnsConfiguration (ApnsServerEnvironment serverEnvironment, string certificateFile, string certificateFilePwd, bool validateIsApnsCertificate)
: this (serverEnvironment, System.IO.File.ReadAllBytes (certificateFile), certificateFilePwd, validateIsApnsCertificate)
{
}
public ApnsConfiguration (ApnsServerEnvironment serverEnvironment, string certificateFile, string certificateFilePwd)
: this (serverEnvironment, System.IO.File.ReadAllBytes (certificateFile), certificateFilePwd)
{
}
public ApnsConfiguration (ApnsServerEnvironment serverEnvironment, byte[] certificateData, string certificateFilePwd, bool validateIsApnsCertificate)
: this (serverEnvironment, new X509Certificate2 (certificateData, certificateFilePwd,
X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable), validateIsApnsCertificate)
{
}
public ApnsConfiguration (ApnsServerEnvironment serverEnvironment, byte[] certificateData, string certificateFilePwd)
: this (serverEnvironment, new X509Certificate2 (certificateData, certificateFilePwd,
X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable))
{
}
public ApnsConfiguration (string overrideHost, int overridePort, bool skipSsl = true)
{
SkipSsl = skipSsl;
Initialize (ApnsServerEnvironment.Sandbox, null, false);
OverrideServer (overrideHost, overridePort);
}
public ApnsConfiguration (ApnsServerEnvironment serverEnvironment, X509Certificate2 certificate)
{
Initialize (serverEnvironment, certificate, true);
}
public ApnsConfiguration (ApnsServerEnvironment serverEnvironment, X509Certificate2 certificate, bool validateIsApnsCertificate)
{
Initialize (serverEnvironment, certificate, validateIsApnsCertificate);
}
void Initialize (ApnsServerEnvironment serverEnvironment, X509Certificate2 certificate, bool validateIsApnsCertificate)
{
ServerEnvironment = serverEnvironment;
var production = serverEnvironment == ApnsServerEnvironment.Production;
Host = production ? APNS_PRODUCTION_HOST : APNS_SANDBOX_HOST;
FeedbackHost = production ? APNS_PRODUCTION_FEEDBACK_HOST : APNS_SANDBOX_FEEDBACK_HOST;
Port = production ? APNS_PRODUCTION_PORT : APNS_SANDBOX_PORT;
FeedbackPort = production ? APNS_PRODUCTION_FEEDBACK_PORT : APNS_SANDBOX_FEEDBACK_PORT;
Certificate = certificate;
MillisecondsToWaitBeforeMessageDeclaredSuccess = 3000;
ConnectionTimeout = 10000;
MaxConnectionAttempts = 3;
FeedbackIntervalMinutes = 10;
FeedbackTimeIsUTC = false;
AdditionalCertificates = new List<X509Certificate2> ();
AddLocalAndMachineCertificateStores = false;
if (validateIsApnsCertificate)
CheckIsApnsCertificate ();
ValidateServerCertificate = false;
KeepAlivePeriod = TimeSpan.FromMinutes (20);
KeepAliveRetryPeriod = TimeSpan.FromSeconds (30);
InternalBatchSize = 1000;
InternalBatchingWaitPeriod = TimeSpan.FromMilliseconds (750);
InternalBatchFailureRetryCount = 1;
}
void CheckIsApnsCertificate ()
{
if (Certificate != null) {
var issuerName = Certificate.IssuerName.Name;
var commonName = Certificate.SubjectName.Name;
if (!issuerName.Contains ("Apple"))
throw new ArgumentOutOfRangeException ("Your Certificate does not appear to be issued by Apple! Please check to ensure you have the correct certificate!");
if (!Regex.IsMatch (commonName, "Apple.*?Push Services")
&& !commonName.Contains ("Website Push ID:"))
throw new ArgumentOutOfRangeException ("Your Certificate is not a valid certificate for connecting to Apple's APNS servers");
if (commonName.Contains ("Development") && ServerEnvironment != ApnsServerEnvironment.Sandbox)
throw new ArgumentOutOfRangeException ("You are using a certificate created for connecting only to the Sandbox APNS server but have selected a different server environment to connect to.");
if (commonName.Contains ("Production") && ServerEnvironment != ApnsServerEnvironment.Production)
throw new ArgumentOutOfRangeException ("You are using a certificate created for connecting only to the Production APNS server but have selected a different server environment to connect to.");
} else {
throw new ArgumentOutOfRangeException ("You must provide a Certificate to connect to APNS with!");
}
}
public void OverrideServer (string host, int port)
{
Host = host;
Port = port;
}
public void OverrideFeedbackServer (string host, int port)
{
FeedbackHost = host;
FeedbackPort = port;
}
public void SetProxy(string proxyHost, int proxyPort)
{
UseProxy = true;
ProxyHost = proxyHost;
ProxyPort = proxyPort;
ProxyCredentials = CredentialCache.DefaultNetworkCredentials;
}
public void SetProxy(string proxyHost, int proxyPort, string userName, string password, string domain)
{
UseProxy = true;
ProxyHost = proxyHost;
ProxyPort = proxyPort;
ProxyCredentials = new NetworkCredential(userName, password, domain);
}
public string Host { get; private set; }
public int Port { get; private set; }
public string FeedbackHost { get; private set; }
public int FeedbackPort { get; private set; }
public bool UseProxy { get; private set; }
public string ProxyHost { get; private set; }
public int ProxyPort { get; private set; }
public NetworkCredential ProxyCredentials { get; private set; }
public X509Certificate2 Certificate { get; private set; }
public List<X509Certificate2> AdditionalCertificates { get; private set; }
public bool AddLocalAndMachineCertificateStores { get; set; }
public bool SkipSsl { get; set; }
public int MillisecondsToWaitBeforeMessageDeclaredSuccess { get; set; }
public int FeedbackIntervalMinutes { get; set; }
public bool FeedbackTimeIsUTC { get; set; }
public bool ValidateServerCertificate { get; set; }
public int ConnectionTimeout { get; set; }
public int MaxConnectionAttempts { get; set; }
/// <summary>
/// The internal connection to APNS servers batches notifications to send before waiting for errors for a short time.
/// This value will set a maximum size per batch. The default value is 1000. You probably do not want this higher than 7500.
/// </summary>
/// <value>The size of the internal batch.</value>
public int InternalBatchSize { get; set; }
/// <summary>
/// How long the internal connection to APNS servers should idle while collecting notifications in a batch to send.
/// Setting this value too low might result in many smaller batches being used.
/// </summary>
/// <value>The internal batching wait period.</value>
public TimeSpan InternalBatchingWaitPeriod { get; set; }
/// <summary>
/// How many times the internal batch will retry to send in case of network failure. The default value is 1.
/// </summary>
/// <value>The internal batch failure retry count.</value>
public int InternalBatchFailureRetryCount { get; set; }
/// <summary>
/// Gets or sets the keep alive period to set on the APNS socket
/// </summary>
public TimeSpan KeepAlivePeriod { get; set; }
/// <summary>
/// Gets or sets the keep alive retry period to set on the APNS socket
/// </summary>
public TimeSpan KeepAliveRetryPeriod { get; set; }
/// <summary>
/// Gets the configured APNS server environment
/// </summary>
/// <value>The server environment.</value>
public ApnsServerEnvironment ServerEnvironment { get; private set; }
public enum ApnsServerEnvironment {
Sandbox,
Production
}
}
}