diff --git a/Protest/Http/KeepAlive.cs b/Protest/Http/KeepAlive.cs index 83de57de..2af1b913 100644 --- a/Protest/Http/KeepAlive.cs +++ b/Protest/Http/KeepAlive.cs @@ -33,7 +33,6 @@ static KeepAlive() { public static async void WebSocketHandler(HttpListenerContext ctx) { WebSocket ws; - try { WebSocketContext wsc = await ctx.AcceptWebSocketAsync(null); ws = wsc.WebSocket; @@ -101,12 +100,10 @@ public static async void WebSocketHandler(HttpListenerContext ctx) { } } catch (WebSocketException ex) when (ex.WebSocketErrorCode == WebSocketError.ConnectionClosedPrematurely) { - Logger.Error(ex); return; } catch (WebSocketException ex) when (ex.WebSocketErrorCode != WebSocketError.ConnectionClosedPrematurely) { //do nothing - //return; } catch (Exception ex) { Logger.Error(ex); @@ -115,7 +112,7 @@ public static async void WebSocketHandler(HttpListenerContext ctx) { connections.Remove(ws, out _); } - if (ws.State == WebSocketState.Open) { + if (ws?.State == WebSocketState.Open) { try { await ws.CloseAsync(WebSocketCloseStatus.NormalClosure, String.Empty, CancellationToken.None); } diff --git a/Protest/Protocols/Dhcp.cs b/Protest/Protocols/Dhcp.cs index 8c00e86f..dc1579e2 100644 --- a/Protest/Protocols/Dhcp.cs +++ b/Protest/Protocols/Dhcp.cs @@ -13,11 +13,9 @@ internal static class Dhcp { private static readonly byte[] NULL_IP = new byte[] {0,0,0,0}; public static async void WebSocketHandler(HttpListenerContext ctx) { - WebSocketContext wsc; WebSocket ws; - try { - wsc = await ctx.AcceptWebSocketAsync(null); + WebSocketContext wsc = await ctx.AcceptWebSocketAsync(null); ws = wsc.WebSocket; } catch (WebSocketException ex) { diff --git a/Protest/Protocols/Icmp.cs b/Protest/Protocols/Icmp.cs index 462e247e..db546d54 100644 --- a/Protest/Protocols/Icmp.cs +++ b/Protest/Protocols/Icmp.cs @@ -13,7 +13,7 @@ namespace Protest.Protocols; internal static class Icmp { - private static readonly byte[] ICMP_PAYLOAD = "0000000000000000"u8.ToArray(); + private static readonly byte[] ICMP_PAYLOAD = "0123456789abcdef"u8.ToArray(); public static byte[] BulkPing(Dictionary parameters) { if (parameters is null) { return null; } @@ -44,10 +44,9 @@ public static byte[] BulkPing(Dictionary parameters) { } public static async void WebSocketHandler(HttpListenerContext ctx) { - WebSocketContext wsc; WebSocket ws; try { - wsc = await ctx.AcceptWebSocketAsync(null); + WebSocketContext wsc = await ctx.AcceptWebSocketAsync(null); ws = wsc.WebSocket; } catch (WebSocketException ex) { @@ -156,11 +155,18 @@ public static async void WebSocketHandler(HttpListenerContext ctx) { return; } catch (WebSocketException ex) when (ex.WebSocketErrorCode != WebSocketError.ConnectionClosedPrematurely) { - Logger.Error(ex); + //do nothing } catch (Exception ex) { Logger.Error(ex); } + + if (ws?.State == WebSocketState.Open) { + try { + await ws.CloseAsync(WebSocketCloseStatus.NormalClosure, String.Empty, CancellationToken.None); + } + catch { } + } } private static async Task PingArrayAsync(string[] name, string[] id, int timeout) { List> tasks = new List>(); diff --git a/Protest/Protocols/Telnet.cs b/Protest/Protocols/Telnet.cs index e1979c3f..b31b9236 100644 --- a/Protest/Protocols/Telnet.cs +++ b/Protest/Protocols/Telnet.cs @@ -23,10 +23,9 @@ private static async Task WsWriteText(WebSocket ws, byte[] data) { } public static async void WebSocketHandler(HttpListenerContext ctx) { - WebSocketContext wsc; WebSocket ws; try { - wsc = await ctx.AcceptWebSocketAsync(null); + WebSocketContext wsc = await ctx.AcceptWebSocketAsync(null); ws = wsc.WebSocket; } catch (WebSocketException ex) { diff --git a/Protest/Tools/LiveStats.cs b/Protest/Tools/LiveStats.cs index b4e23d80..a455cc2d 100644 --- a/Protest/Tools/LiveStats.cs +++ b/Protest/Tools/LiveStats.cs @@ -21,11 +21,9 @@ private static async Task WsWriteText(WebSocket ws, byte[] bytes) { } public static async void DeviceStats(HttpListenerContext ctx) { - WebSocketContext wsc; WebSocket ws; - try { - wsc = await ctx.AcceptWebSocketAsync(null); + WebSocketContext wsc = await ctx.AcceptWebSocketAsync(null); ws = wsc.WebSocket; } catch (WebSocketException ex) { @@ -261,13 +259,13 @@ firstAlive is not null && return; } catch (WebSocketException ex) when (ex.WebSocketErrorCode != WebSocketError.ConnectionClosedPrematurely) { - Logger.Error(ex); + //do nothing } catch (Exception ex) { Logger.Error(ex); } - if (ws.State == WebSocketState.Open) { + if (ws?.State == WebSocketState.Open) { try { await ws.CloseAsync(WebSocketCloseStatus.NormalClosure, String.Empty, CancellationToken.None); } @@ -276,11 +274,9 @@ firstAlive is not null && } public static async void UserStats(HttpListenerContext ctx) { - WebSocketContext wsc; WebSocket ws; - try { - wsc = await ctx.AcceptWebSocketAsync(null); + WebSocketContext wsc = await ctx.AcceptWebSocketAsync(null); ws = wsc.WebSocket; } catch (WebSocketException ex) { @@ -339,13 +335,13 @@ public static async void UserStats(HttpListenerContext ctx) { return; } catch (WebSocketException ex) when (ex.WebSocketErrorCode != WebSocketError.ConnectionClosedPrematurely) { - Logger.Error(ex); + //do nothing } catch (Exception ex) { Logger.Error(ex); } - if (ws.State == WebSocketState.Open) { + if (ws?.State == WebSocketState.Open) { try { await ws.CloseAsync(WebSocketCloseStatus.NormalClosure, String.Empty, CancellationToken.None); } diff --git a/Protest/Tools/Monitor.cs b/Protest/Tools/Monitor.cs index e835a5ee..309b6be7 100644 --- a/Protest/Tools/Monitor.cs +++ b/Protest/Tools/Monitor.cs @@ -72,11 +72,9 @@ private static void WsWriteText(WebSocket ws, byte[] bytes) { } public static async void WebSocketHandler(HttpListenerContext ctx) { - WebSocketContext wsc; WebSocket ws; - try { - wsc = await ctx.AcceptWebSocketAsync(null); + WebSocketContext wsc = await ctx.AcceptWebSocketAsync(null); ws = wsc.WebSocket; } catch (WebSocketException ex) { @@ -293,21 +291,22 @@ public static async void WebSocketHandler(HttpListenerContext ctx) { catch (JsonException) { return; } + catch (ManagementException ex) { + Logger.Error(ex); + } catch (WebSocketException ex) when (ex.WebSocketErrorCode == WebSocketError.ConnectionClosedPrematurely) { return; } catch (WebSocketException ex) when (ex.WebSocketErrorCode != WebSocketError.ConnectionClosedPrematurely) { - Logger.Error(ex); - } - catch (ManagementException ex) { - Logger.Error(ex); + //do nothing } - try { - if (ws.State == WebSocketState.Open) { + if (ws?.State == WebSocketState.Open) { + try { await ws.CloseAsync(WebSocketCloseStatus.NormalClosure, String.Empty, CancellationToken.None); } - } catch { } + catch { } + } } private static long HandlePing(string host, int timeout) { diff --git a/Protest/Tools/PortScan.cs b/Protest/Tools/PortScan.cs index 076448d3..a4660033 100644 --- a/Protest/Tools/PortScan.cs +++ b/Protest/Tools/PortScan.cs @@ -151,11 +151,9 @@ internal static class PortScan { }; public static async void WebSocketHandler(HttpListenerContext ctx) { - WebSocketContext wsc; WebSocket ws; - try { - wsc = await ctx.AcceptWebSocketAsync(null); + WebSocketContext wsc = await ctx.AcceptWebSocketAsync(null); ws = wsc.WebSocket; } catch (WebSocketException ex) { @@ -166,9 +164,7 @@ public static async void WebSocketHandler(HttpListenerContext ctx) { object mutex = new object(); -#if !DEBUG try { -#endif while (ws.State == WebSocketState.Open) { byte[] buff = new byte[2048]; WebSocketReceiveResult receiveResult = await ws.ReceiveAsync(new ArraySegment(buff), CancellationToken.None); @@ -232,21 +228,23 @@ public static async void WebSocketHandler(HttpListenerContext ctx) { }).Start(); } -#if !DEBUG } catch (WebSocketException ex) when (ex.WebSocketErrorCode == WebSocketError.ConnectionClosedPrematurely) { return; } catch (WebSocketException ex) when (ex.WebSocketErrorCode != WebSocketError.ConnectionClosedPrematurely) { - Logger.Error(ex); + //do nothing } catch (Exception ex) { Logger.Error(ex); + } - } /*finally { - ctx.Response.Close(); - }*/ -#endif + if (ws?.State == WebSocketState.Open) { + try { + await ws.CloseAsync(WebSocketCloseStatus.NormalClosure, String.Empty, CancellationToken.None); + } + catch { } + } } public static async Task PortsScanAsync(string host, short[] ports) { diff --git a/Protest/Tools/SiteCheck.cs b/Protest/Tools/SiteCheck.cs index e9b2d3b4..8a9859e0 100644 --- a/Protest/Tools/SiteCheck.cs +++ b/Protest/Tools/SiteCheck.cs @@ -13,11 +13,9 @@ namespace Protest.Tools; internal static class SiteCheck { public static async void WebSocketHandler(HttpListenerContext ctx) { - WebSocketContext wsc; WebSocket ws; - try { - wsc = await ctx.AcceptWebSocketAsync(null); + WebSocketContext wsc = await ctx.AcceptWebSocketAsync(null); ws = wsc.WebSocket; } catch (WebSocketException ex) { @@ -220,13 +218,13 @@ public static async void WebSocketHandler(HttpListenerContext ctx) { return; } catch (WebSocketException ex) when (ex.WebSocketErrorCode != WebSocketError.ConnectionClosedPrematurely) { - Logger.Error(ex); + //do nothing } catch (Exception ex) { Logger.Error(ex); } - if (ws.State == WebSocketState.Open) { + if (ws?.State == WebSocketState.Open) { try { await ws.CloseAsync(WebSocketCloseStatus.NormalClosure, String.Empty, CancellationToken.None); } diff --git a/Protest/Tools/TraceRoute.cs b/Protest/Tools/TraceRoute.cs index ee9ae4aa..c613929d 100644 --- a/Protest/Tools/TraceRoute.cs +++ b/Protest/Tools/TraceRoute.cs @@ -13,10 +13,9 @@ internal static class TraceRoute { static readonly byte[] ICMP_PAYLOAD = "0000000000000000"u8.ToArray(); public static async void WebSocketHandler(HttpListenerContext ctx) { - WebSocketContext wsc; WebSocket ws; try { - wsc = await ctx.AcceptWebSocketAsync(null); + WebSocketContext wsc = await ctx.AcceptWebSocketAsync(null); ws = wsc.WebSocket; } catch (WebSocketException ex) { @@ -122,11 +121,18 @@ public static async void WebSocketHandler(HttpListenerContext ctx) { return; } catch (WebSocketException ex) when (ex.WebSocketErrorCode != WebSocketError.ConnectionClosedPrematurely) { - Logger.Error(ex); + //do nothing } catch (Exception ex) { Logger.Error(ex); } + + if (ws?.State == WebSocketState.Open) { + try { + await ws.CloseAsync(WebSocketCloseStatus.NormalClosure, String.Empty, CancellationToken.None); + } + catch { } + } } }