forked from jjxtra/UrlMonitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UrlMonitorService.cs
336 lines (298 loc) · 11.7 KB
/
UrlMonitorService.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
331
332
333
334
335
336
#region Imports
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.ServiceProcess;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Security.Cryptography;
using HtmlAgilityPack;
#endregion Imports
namespace UrlMonitor
{
public class UrlMonitorService : ServiceBase
{
private class UrlResponse
{
public HttpStatusCode StatusCode;
public string[] Headers;
public string Body;
public TimeSpan Duration;
public long MD51;
public long MD52;
}
private UrlMonitorConfig config;
private Thread serviceThread;
private bool run = true;
private int threadCount;
private UrlResponse GetResponseForUrl(MonitoredUrl url)
{
DateTime start = DateTime.UtcNow;
System.Net.ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072; //TLS 1.2
HttpWebRequest request = HttpWebRequest.Create(url.Path) as HttpWebRequest;
// Set the 'Timeout' property of the HttpWebRequest to default value 100,000 milliseconds (100 seconds).
request.Timeout = 100000;
request.KeepAlive = true;
request.ReadWriteTimeout = 100000;
if (!url.AllowRedirects)
request.AllowAutoRedirect = false;
if (string.IsNullOrWhiteSpace(url.Method))
{
url.Method = "GET";
}
request.Method = url.Method;
foreach (Header header in url.Headers)
{
switch (header.Name.ToUpperInvariant())
{
case "USER-AGENT":
request.UserAgent = header.Value;
break;
default:
request.Headers.Add(header.Name, header.Value);
break;
}
}
if (request.Method.Equals("GET", StringComparison.OrdinalIgnoreCase))
{
// do nothing, we'll just get a response right away
}
else if (request.Method.Equals("POST", StringComparison.OrdinalIgnoreCase) || request.Method.Equals("PUT", StringComparison.OrdinalIgnoreCase))
{
byte[] bytes = (url.PostBytes == null ? Encoding.UTF8.GetBytes(url.PostText) : url.PostBytes);
request.ContentLength = bytes.Length;
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(bytes, 0, bytes.Length);
}
}
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
Stream responseStream = response.GetResponseStream();
string data;
using (StreamReader reader = new StreamReader(responseStream))
{
responseStream.Flush();
data = reader.ReadToEnd();
}
var doc = new HtmlDocument();
doc.LoadHtml(data);
HtmlNode RootNode = doc.DocumentNode;
HtmlNodeCollection nodes = new HtmlNodeCollection(RootNode);
if (!string.IsNullOrEmpty(url.FilterElements))
{
string[] words = url.FilterElements.Split(',');
foreach (string word in words)
{
doc.DocumentNode.SelectNodes(word)?.ToList().ForEach(a => a.Remove());
}
}
// From string to byte array
byte[] buffer = Encoding.UTF8.GetBytes(RootNode.OuterHtml);
// From byte array to string
string responseBody = Encoding.UTF8.GetString(buffer, 0, buffer.Length);
TimeSpan duration = (DateTime.UtcNow - start);
string[] headers = new string[response.Headers.Count];
int i = 0;
foreach (string key in response.Headers.AllKeys)
{
headers[i++] = key + ":" + response.Headers.Get(key);
}
byte[] md5 = new MD5CryptoServiceProvider().ComputeHash(buffer, 0, buffer.Length);
return new UrlResponse
{
MD51 = md5[0] | md5[1] << 8 | md5[2] << 16 | md5[3] << 24,
MD52 = md5[4] | md5[5] << 8 | md5[6] << 16 | md5[7] << 24,
Body = responseBody,
Duration = duration,
Headers = headers,
StatusCode = response.StatusCode
};
}
private void SendEmail(MonitoredUrl url, string emailBody)
{
SmtpClient client = new SmtpClient();
client.Host = config.Email.Host;
client.Port = config.Email.Port;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential(config.Email.UserName, config.Email.Password);
client.EnableSsl = config.Email.Ssl;
MailMessage msg = new MailMessage
{
Body = emailBody,
BodyEncoding = Encoding.UTF8,
From = new MailAddress(config.Email.UserName, config.Email.From),
IsBodyHtml = true,
Sender = new MailAddress(config.Email.UserName, config.Email.From),
Subject = config.Email.Subject
};
foreach (string address in url.EmailAddresses.Split(',', ';', '|'))
{
string trimmedAddress = address.Trim();
if (trimmedAddress.Length != 0)
{
msg.To.Add(trimmedAddress);
}
}
client.Send(msg);
}
private string CheckUrlResponse(UrlResponse response, MonitoredUrl url)
{
string message = string.Empty;
if (!string.IsNullOrWhiteSpace(url.BodyRegex))
{
if (!Regex.IsMatch(response.Body, url.BodyRegex, RegexOptions.IgnoreCase | RegexOptions.Singleline))
{
message += "- Failed to match body regex<br/>";
}
}
if (!string.IsNullOrWhiteSpace(url.HeadersRegex))
{
bool foundOne = false;
foreach (string header in response.Headers)
{
if (Regex.IsMatch(header, url.HeadersRegex, RegexOptions.IgnoreCase | RegexOptions.Singleline))
{
foundOne = true;
break;
}
}
if (!foundOne)
{
message += "- Failed to match headers regex<br/>";
}
}
if (!string.IsNullOrWhiteSpace(url.StatusCodeRegex))
{
string statusCodeString = response.StatusCode.ToString("D");
if (!Regex.IsMatch(statusCodeString, url.StatusCodeRegex, RegexOptions.IgnoreCase))
{
message += "- Failed to match status code regex, got " + statusCodeString + "<br/>";
}
}
if (url.AlertIfChanged && (url.MD51 != 0 || url.MD52 != 0) && (url.MD51 != response.MD51 || url.MD52 != response.MD52))
{
message += "- MD5 Changed, body contents are new<br/>";
}
if (url.MaxTime.TotalSeconds > 0.0d && response.Duration > url.MaxTime)
{
message += "- URL took " + response.Duration.TotalSeconds.ToString("0.00") + " seconds, too long<br/>";
}
url.MD51 = response.MD51;
url.MD52 = response.MD52;
if (message.Length != 0)
{
if (!string.IsNullOrWhiteSpace(url.MismatchMessage))
{
message = url.MismatchMessage + "<br/>" + message;
}
message = "<a href='" + url.Path + "' title='Url Link'>" + url.Path + "</a><br/>" + message;
}
return message;
}
private void ProcessUrl(object state)
{
MonitoredUrl url = state as MonitoredUrl;
try
{
UrlResponse response = GetResponseForUrl(url);
string emailBody = CheckUrlResponse(response, url);
if (emailBody.Length != 0)
{
SendEmail(url, emailBody);
}
}
catch (Exception ex)
{
#if DEBUG
string msg = string.Format("Error accessing url: {0}\r\nError: {1}", url.Path, ex);
#else
string msg = string.Format("Error accessing url: {0}\r\nError: {1}\r\n", url.Path, ex.Message);
#endif
Log.Write(LogLevel.Error, msg);
WebException webException = ex as WebException;
if (webException != null)
{
HttpWebResponse webResponse = (HttpWebResponse)webException.Response;
msg += string.Format("Status code: {0}", webResponse.StatusCode.ToString("D"));
SendEmail(url, msg);
}
}
finally
{
url.InProcess = false;
Interlocked.Decrement(ref threadCount);
}
}
private void ServiceThread()
{
try
{
while (run)
{
MonitoredUrl[] urls = config.UrlSet.Where(u => u.Enabled && !u.InProcess && (DateTime.UtcNow - u.LastCheck) > u.Frequency).ToArray();
foreach (MonitoredUrl url in urls)
{
Interlocked.Increment(ref threadCount);
url.InProcess = true;
config.UrlSet.Remove(url);
url.LastCheck = DateTime.UtcNow;
config.UrlSet.Add(url);
ThreadPool.QueueUserWorkItem(new WaitCallback(ProcessUrl), url);
while (run && threadCount >= config.MaxThreads)
{
Thread.Sleep(20);
}
Thread.Sleep(config.SleepTimeUrl);
}
if (!run)
{
break;
}
Thread.Sleep(config.SleepTimeBatch);
}
while (threadCount != 0)
{
Thread.Sleep(20);
}
}
catch (Exception ex)
{
Log.Write(LogLevel.Error, "Fatal error: {0}", ex);
}
}
internal void Start(string[] args)
{
ReloadConfig();
run = true;
serviceThread = new Thread(new ThreadStart(ServiceThread));
serviceThread.Start();
}
protected override void OnStart(string[] args)
{
base.OnStart(args);
Start(args);
}
protected override void OnStop()
{
base.OnStop();
run = false;
serviceThread = null;
}
public void ReloadConfig()
{
UrlMonitorConfig _config = (UrlMonitorConfig)System.Configuration.ConfigurationManager.GetSection("UrlMonitorConfig");
_config.UrlSet = new SortedSet<MonitoredUrl>();
foreach (MonitoredUrl url in _config.Urls)
{
_config.UrlSet.Add(url);
}
config = _config;
ThreadPool.SetMaxThreads(config.MaxThreads, config.MaxThreads * 2);
}
}
}