Skip to content

Commit

Permalink
WebSockets error handing
Browse files Browse the repository at this point in the history
  • Loading branch information
veniware committed May 17, 2024
1 parent 692fcc0 commit 4c3cf48
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 52 deletions.
5 changes: 1 addition & 4 deletions Protest/Http/KeepAlive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
Expand Down
4 changes: 1 addition & 3 deletions Protest/Protocols/Dhcp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
14 changes: 10 additions & 4 deletions Protest/Protocols/Icmp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string> parameters) {
if (parameters is null) { return null; }
Expand Down Expand Up @@ -44,10 +44,9 @@ public static byte[] BulkPing(Dictionary<string, string> 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) {
Expand Down Expand Up @@ -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<string> PingArrayAsync(string[] name, string[] id, int timeout) {
List<Task<string>> tasks = new List<Task<string>>();
Expand Down
3 changes: 1 addition & 2 deletions Protest/Protocols/Telnet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
16 changes: 6 additions & 10 deletions Protest/Tools/LiveStats.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
}
Expand Down
19 changes: 9 additions & 10 deletions Protest/Tools/Monitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
20 changes: 9 additions & 11 deletions Protest/Tools/PortScan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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<byte>(buff), CancellationToken.None);
Expand Down Expand Up @@ -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<bool[]> PortsScanAsync(string host, short[] ports) {
Expand Down
8 changes: 3 additions & 5 deletions Protest/Tools/SiteCheck.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
Expand Down
12 changes: 9 additions & 3 deletions Protest/Tools/TraceRoute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 { }
}
}

}

0 comments on commit 4c3cf48

Please sign in to comment.