Skip to content

Commit c969f60

Browse files
committed
LiveStats, fix reverse DNS check logic
1 parent 6626b1e commit c969f60

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

Protest/Tasks/Watchdog.cs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
namespace Protest.Tasks;
1717

1818
internal static class Watchdog {
19-
private const long WEEK_IN_TICKS = 6_048_000_000_000L;
19+
private const long WEEK_IN_TICKS = 6_048_000_000_000L;
2020
private const long FIVE_MINUTE_IN_TICKS = 3_000_000_000L;
21-
private const long MINUTE_IN_TICKS = 600_000_000L;
22-
private const int FIVE_MINUTE_IN_MILLI = 300_000;
21+
private const long MINUTE_IN_TICKS = 600_000_000L;
22+
private const int FIVE_MINUTE_IN_MILLI = 300_000;
2323

2424
public enum WatcherType : byte {
2525
icmp,
@@ -296,8 +296,7 @@ private static short CheckHttp(Watcher watcher) {
296296
int statusCode = (int)response.StatusCode;
297297
int category = statusCode / 100 - 1;
298298

299-
if (watcher.httpstatus.Length < category)
300-
continue;
299+
if (watcher.httpstatus.Length < category) continue;
301300

302301
if (watcher.httpstatus[category]) {
303302
result = 0;
@@ -362,7 +361,7 @@ private static short CheckHttpKeyword(Watcher watcher) {
362361
}
363362

364363
private static short CheckTls(Watcher watcher) {
365-
short result = -1;
364+
short result = short.MaxValue;
366365

367366
using HttpClientHandler handler = new HttpClientHandler();
368367
handler.ServerCertificateCustomValidationCallback = (request, cert, chain, errors) => {
@@ -390,8 +389,7 @@ private static short CheckTls(Watcher watcher) {
390389
}
391390
catch { }
392391

393-
if (result == short.MaxValue)
394-
continue;
392+
if (result == short.MaxValue) continue;
395393
break;
396394
}
397395

@@ -642,7 +640,7 @@ public static void SendSmtpNotification(Watcher watcher, Notification notificati
642640
switch (watcher.type) {
643641
case WatcherType.icmp:
644642
if (status < 0) {
645-
body.Append($"<b>{watcher.target}</b> stoped responding to ICMP requests.");
643+
body.Append($"<b>{watcher.target}</b> stopped responding to ICMP requests.");
646644
}
647645
else {
648646
body.Append($"<b>{watcher.target}</b> is now responding to ICMP requests.");
@@ -651,7 +649,7 @@ public static void SendSmtpNotification(Watcher watcher, Notification notificati
651649

652650
case WatcherType.tcp:
653651
if (status < 0) {
654-
body.Append($"<b>{watcher.target}</b> stoped listening on TCP port {watcher.port}.");
652+
body.Append($"<b>{watcher.target}</b> stopped listening on TCP port {watcher.port}.");
655653
}
656654
else {
657655
body.Append($"<b>{watcher.target}</b> is now listening on TCP port {watcher.port}.");

Protest/Tools/LiveStats.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using Lextm.SharpSnmpLib;
1717

1818
using static Protest.Protocols.Snmp.Polling;
19+
using System.Net.Sockets;
1920

2021
namespace Protest.Tools;
2122

@@ -273,7 +274,7 @@ public static async void DeviceStats(HttpListenerContext ctx) {
273274
catch { }
274275
}
275276

276-
if (String.IsNullOrEmpty(_hostname?.value) && String.IsNullOrEmpty(_ip?.value)) { //check reverse dns mismatch
277+
if (!String.IsNullOrEmpty(_hostname?.value) && !String.IsNullOrEmpty(_ip?.value)) { //check reverse dns mismatch
277278
string hostnameValue = _hostname?.value ?? string.Empty;
278279
string[] hostnames = hostnameValue.Split(';').Select(o => o.Trim()).ToArray();
279280

@@ -286,6 +287,7 @@ public static async void DeviceStats(HttpListenerContext ctx) {
286287
try {
287288
IPAddress[] reversed = System.Net.Dns.GetHostAddresses(hostnames[i]);
288289
for (int j = 0; j < reversed.Length; j++) {
290+
if (reversed[i].AddressFamily != AddressFamily.InterNetwork) continue;
289291
if (!ips.Contains(reversed[j].ToString())) {
290292
WsWriteText(ws, $"{{\"warning\":\"Reverse DNS mismatch: {Data.EscapeJsonText(reversed[j].ToString())}\",\"source\":\"DNS\"}}", mutex);
291293
break;

0 commit comments

Comments
 (0)