-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
MailDemonExtensionMethods.cs
330 lines (304 loc) · 12.8 KB
/
MailDemonExtensionMethods.cs
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using DnsClient.Internal;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewEngines;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using MimeKit;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.OpenSsl;
using Org.BouncyCastle.Security;
namespace MailDemon
{
public static class MailDemonExtensionMethods
{
public static Encoding Utf8EncodingNoByteMarker { get; } = new UTF8Encoding(false);
/// <summary>
/// Taken from https://www.regular-expressions.info/email.html
/// </summary>
private static readonly Regex validEmailRegex = new Regex(@"\A(?=[a-z0-9@.!#$%&'*+/=?^_`{|}~-]{6,254}\z)(?=[a-z0-9.!#$%&'*+/=?^_`{|}~-]{1,64}@)[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:(?=[a-z0-9-]{1,63}\.)[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?=[a-z0-9-]{1,63}\z)[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\z", RegexOptions.CultureInvariant | RegexOptions.Compiled | RegexOptions.IgnoreCase);
public static string ToUnsecureString(this SecureString s)
{
if (s == null)
{
return null;
}
IntPtr valuePtr = IntPtr.Zero;
try
{
valuePtr = Marshal.SecureStringToGlobalAllocUnicode(s);
char[] buffer = new char[s.Length];
Marshal.Copy(valuePtr, buffer, 0, s.Length);
return new string(buffer);
}
finally
{
Marshal.ZeroFreeGlobalAllocUnicode(valuePtr);
}
}
public static SecureString ToSecureString(this string s)
{
SecureString ss = new SecureString();
foreach (char c in s)
{
ss.AppendChar(c);
}
return ss;
}
public static async Task TimeoutAfter(this Task task, int milliseconds)
{
using CancellationTokenSource timeoutCancellationTokenSource = new CancellationTokenSource();
var completedTask = await Task.WhenAny(task, Task.Delay(milliseconds, timeoutCancellationTokenSource.Token));
if (completedTask == task)
{
timeoutCancellationTokenSource.Cancel();
await task; // Very important in order to propagate exceptions
}
else
{
throw new TimeoutException("The operation has timed out.");
}
}
public static async Task<TResult> TimeoutAfter<TResult>(this Task<TResult> task, int milliseconds)
{
using CancellationTokenSource timeoutCancellationTokenSource = new CancellationTokenSource();
var completedTask = await Task.WhenAny(task, Task.Delay(milliseconds, timeoutCancellationTokenSource.Token));
if (completedTask == task)
{
timeoutCancellationTokenSource.Cancel();
return await task; // Very important in order to propagate exceptions
}
else
{
throw new TimeoutException("The operation has timed out.");
}
}
public static T GetValue<T>(this IConfiguration config, string key, T defaultValue)
{
string value = config[key];
if (string.IsNullOrWhiteSpace(value))
{
return defaultValue;
}
return (T)Convert.ChangeType(value, typeof(T), CultureInfo.InvariantCulture);
}
/// <summary>
/// Get remote ip address, optionally allowing for x-forwarded-for header check
/// </summary>
/// <param name="context">Http context</param>
/// <param name="allowForwarded">Whether to allow x-forwarded-for header check</param>
/// <returns>IPAddress</returns>
public static System.Net.IPAddress GetRemoteIPAddress(this HttpContext context, bool allowForwarded = true)
{
if (allowForwarded)
{
string header = (context.Request.Headers["CF-Connecting-IP"].FirstOrDefault() ?? context.Request.Headers["X-Forwarded-For"].FirstOrDefault());
if (System.Net.IPAddress.TryParse(header, out System.Net.IPAddress ip))
{
return ip;
}
}
return context.Connection.RemoteIpAddress;
}
/// <summary>
/// Attempt to parse an email address. Email must have a @ in it to be valid. MailboxAddress.TryParse does not require the @.
/// </summary>
/// <param name="emailAddress">Email address</param>
/// <param name="address">Receives the parsed email address</param>
/// <returns>True if email is valid, false if not</returns>
public static bool TryParseEmailAddress(this string emailAddress, out MailboxAddress address)
{
if (string.IsNullOrWhiteSpace(emailAddress) || !emailAddress.Contains('@') || !MailboxAddress.TryParse(emailAddress, out address))
{
address = default;
return false;
}
return true;
}
/// <summary>
/// Get domain from an email address
/// </summary>
/// <param name="emailAddress">Email address</param>
/// <returns>Domain</returns>
public static string GetDomainFromEmailAddress(this string emailAddress)
{
int pos = emailAddress.IndexOf('@');
return emailAddress.Substring(++pos);
}
/// <summary>
/// Get domain from an email address
/// </summary>
/// <param name="emailAddress">Email address</param>
/// <returns>Domain</returns>
public static string GetDomain(this InternetAddress emailAddress)
{
return emailAddress.ToString().GetDomainFromEmailAddress();
}
/// <summary>
/// Get utf8 bytes from string
/// </summary>
/// <param name="text">String</param>
/// <returns>Utf8 bytes</returns>
public static byte[] ToUtf8Bytes(this string text)
{
return System.Text.Encoding.UTF8.GetBytes(text);
}
/// <summary>
/// Get string from utf8 bytes
/// </summary>
/// <param name="bytes">Utf8 bytes</param>
/// <returns>String</returns>
public static string ToUtf8String(this byte[] bytes)
{
return System.Text.Encoding.UTF8.GetString(bytes);
}
/// <summary>
/// Format text for html
/// </summary>
/// <param name="text">Text</param>
/// <param name="format">Format args</param>
/// <returns>Formatted text</returns>
public static string FormatHtml(this string text, params object[] format)
{
return string.Format((text ?? string.Empty).Replace("\n", "<br/>"), format);
}
/// <summary>
/// Check if a view exists
/// </summary>
/// <param name="helper">Html helper</param>
/// <param name="viewName">View name</param>
/// <returns>True if exists, false otherwise</returns>
public static bool ViewExists(this IHtmlHelper helper, string viewName)
{
if (string.IsNullOrWhiteSpace(viewName))
{
return false;
}
var viewEngine = helper.ViewContext.HttpContext.RequestServices.GetService(typeof(ICompositeViewEngine)) as ICompositeViewEngine;
var view = viewEngine.FindView(helper.ViewContext, viewName, false);
return view.View != null;
}
private static RSACryptoServiceProvider GetRSAProviderForPrivateKey(string pemPrivateKey)
{
RSACryptoServiceProvider rsaKey = new RSACryptoServiceProvider();
PemReader reader = new PemReader(new StringReader(pemPrivateKey));
RsaPrivateCrtKeyParameters rkp = reader.ReadObject() as RsaPrivateCrtKeyParameters;
RSAParameters rsaParameters = DotNetUtilities.ToRSAParameters(rkp);
rsaKey.ImportParameters(rsaParameters);
return rsaKey;
}
/// <summary>
/// Load an ssl certificate from .pem files
/// </summary>
/// <param name="publicKeyFile">Public key file</param>
/// <param name="privateKeyFile">Private key file</param>
/// <param name="password">Password</param>
/// <param name="logger">Logger</param>
/// <returns>X509Certificate2 or null if error</returns>
public static async Task<X509Certificate2> LoadSslCertificateAsync(string publicKeyFile, string privateKeyFile,
SecureString password, Microsoft.Extensions.Logging.ILogger logger)
{
// if missing files, no certificate possible
if (!File.Exists(publicKeyFile) || (!string.IsNullOrWhiteSpace(privateKeyFile) && !File.Exists(privateKeyFile)))
{
return null;
}
Exception error = null;
if (!string.IsNullOrWhiteSpace(publicKeyFile))
{
for (int i = 0; i < 2; i++)
{
try
{
byte[] bytes = await File.ReadAllBytesAsync(publicKeyFile);
var newSslCertificate = (password == null ? X509CertificateLoader.LoadCertificate(bytes) : X509CertificateLoader.LoadPkcs12(bytes, password.ToUnsecureString()));
if (!newSslCertificate.HasPrivateKey && !string.IsNullOrWhiteSpace(privateKeyFile))
{
var copiedCert = newSslCertificate.CopyWithPrivateKey(GetRSAProviderForPrivateKey(await File.ReadAllTextAsync(privateKeyFile)));
newSslCertificate.Dispose();
newSslCertificate = copiedCert;
}
logger.LogDebug("Loaded ssl certificate {cert}", publicKeyFile);
return newSslCertificate;
}
catch (Exception ex)
{
error = ex;
// in case something is copying a new certificate, give it a second and try one more time
Thread.Sleep(1000);
}
}
}
if (error != null)
{
logger.LogError(error, "Error loading ssl certificate {cert}", publicKeyFile);
}
return null;
}
/// <summary>
/// Async wait
/// </summary>
/// <param name="handle">Wait handle</param>
/// <param name="timeout">Timeout</param>
/// <param name="cancellationToken">Cancel token</param>
/// <returns>True if cancel/signalled, false otherwise</returns>
public static async Task<bool> WaitOneAsync(this WaitHandle handle, TimeSpan timeout, CancellationToken cancellationToken)
{
RegisteredWaitHandle registeredHandle = null;
CancellationTokenRegistration tokenRegistration = default;
try
{
var tcs = new TaskCompletionSource<bool>();
registeredHandle = ThreadPool.RegisterWaitForSingleObject(
handle,
(state, timedOut) => ((TaskCompletionSource<bool>)state).TrySetResult(!timedOut),
tcs,
timeout,
true);
tokenRegistration = cancellationToken.Register(
state => ((TaskCompletionSource<bool>)state).TrySetCanceled(),
tcs);
return await tcs.Task;
}
finally
{
if (registeredHandle != null)
{
registeredHandle.Unregister(null);
}
tokenRegistration.Dispose();
}
}
/// <summary>
/// Make a task execute synchronously
/// </summary>
/// <param name="task">Task</param>
public static void Sync(this Task task)
{
task.ConfigureAwait(false).GetAwaiter().GetResult();
}
/// <summary>
/// Make a task execute synchronously
/// </summary>
/// <param name="task">Task</param>
/// <returns>Result</returns>
public static T Sync<T>(this Task<T> task)
{
return task.ConfigureAwait(false).GetAwaiter().GetResult();
}
}
}