Skip to content

Commit be6bca5

Browse files
committed
Cleaning null checks
1 parent 3a37101 commit be6bca5

File tree

17 files changed

+61
-44
lines changed

17 files changed

+61
-44
lines changed

Protest/Http/KeepAlive.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,19 @@ public static async void WebSocketHandler(HttpListenerContext ctx) {
3636
try {
3737
WebSocketContext wsc = await ctx.AcceptWebSocketAsync(null);
3838
ws = wsc.WebSocket;
39-
if (ws is null) return;
4039
}
4140
catch (WebSocketException ex) {
4241
ctx.Response.Close();
4342
Logger.Error(ex);
4443
return;
4544
}
4645

46+
if (ws is null) return;
47+
4748
string sessionId = ctx.Request.Cookies["sessionid"]?.Value ?? null;
4849
string username = IPAddress.IsLoopback(ctx.Request.RemoteEndPoint.Address) ? "loopback" : Auth.GetUsername(sessionId);
4950

50-
string[] accessArray = Auth.rbac.TryGetValue(username, out Auth.AccessControl accessControl) ? accessControl.authorization : new string[] { "*" };
51+
string[] accessArray = Auth.rbac.TryGetValue(username, out Auth.AccessControl accessControl) && accessControl is not null ? accessControl.authorization : new string[] { "*" };
5152

5253
Entry keepAliveEntry = new Entry{
5354
ws = ws,
@@ -111,7 +112,7 @@ public static async void WebSocketHandler(HttpListenerContext ctx) {
111112
connections.Remove(ws, out _);
112113
}
113114

114-
if (ws?.State == WebSocketState.Open) {
115+
if (ws.State == WebSocketState.Open) {
115116
try {
116117
await ws.CloseAsync(WebSocketCloseStatus.NormalClosure, String.Empty, CancellationToken.None);
117118
}

Protest/Protocols/Dhcp.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@ public static async void WebSocketHandler(HttpListenerContext ctx) {
1616
try {
1717
WebSocketContext wsc = await ctx.AcceptWebSocketAsync(null);
1818
ws = wsc.WebSocket;
19-
if (ws is null) return;
2019
}
2120
catch (WebSocketException ex) {
2221
ctx.Response.Close();
2322
Logger.Error(ex);
2423
return;
2524
}
2625

26+
if (ws is null) return;
27+
2728
byte[] buff = new byte[1024];
2829
WebSocketReceiveResult receiveResult = await ws.ReceiveAsync(new ArraySegment<byte>(buff), CancellationToken.None);
2930

Protest/Protocols/Icmp.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,14 @@ public static async void WebSocketHandler(HttpListenerContext ctx) {
5858
try {
5959
WebSocketContext wsc = await ctx.AcceptWebSocketAsync(null);
6060
ws = wsc.WebSocket;
61-
if (ws is null) return;
6261
}
6362
catch (WebSocketException ex) {
6463
ctx.Response.Close();
6564
Logger.Error(ex);
6665
return;
6766
}
6867

69-
if (ws == null) return;
68+
if (ws is null) return;
7069

7170
Hashtable hostnames = new Hashtable();
7271
Lock mutex = new Lock();

Protest/Protocols/Ssh.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,15 @@ public static async void WebSocketHandler(HttpListenerContext ctx) {
2828
try {
2929
WebSocketContext wsc = await ctx.AcceptWebSocketAsync(null);
3030
ws = wsc.WebSocket;
31-
if (ws is null) return;
3231
}
3332
catch (WebSocketException ex) {
3433
ctx.Response.Close();
3534
Logger.Error(ex);
3635
return;
3736
}
3837

38+
if (ws is null) return;
39+
3940
if (!Auth.IsAuthenticatedAndAuthorized(ctx, ctx.Request.Url.AbsolutePath)) {
4041
await ws.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, null, CancellationToken.None);
4142
return;

Protest/Protocols/Telnet.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,15 @@ public static async void WebSocketHandler(HttpListenerContext ctx) {
2626
try {
2727
WebSocketContext wsc = await ctx.AcceptWebSocketAsync(null);
2828
ws = wsc.WebSocket;
29-
if (ws is null) return;
3029
}
3130
catch (WebSocketException ex) {
3231
ctx.Response.Close();
3332
Logger.Error(ex);
3433
return;
3534
}
3635

36+
if (ws is null) return;
37+
3738
if (!Auth.IsAuthenticatedAndAuthorized(ctx, ctx.Request.Url.AbsolutePath)) {
3839
await ws.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, null, CancellationToken.None);
3940
return;
@@ -119,7 +120,7 @@ public static async void WebSocketHandler(HttpListenerContext ctx) {
119120
private static async void HandleDownstream(HttpListenerContext ctx, WebSocket ws, TcpClient telnet, Stream stream) {
120121
byte[] data = new byte[2048];
121122

122-
while (ws.State == WebSocketState.Open && telnet.Connected) {
123+
while (ws!.State == WebSocketState.Open && telnet.Connected) {
123124
if (!Auth.IsAuthenticatedAndAuthorized(ctx, "/ws/telnet")) { //check session
124125
ctx.Response.Close();
125126
telnet.Close();

Protest/Proxy/ReverseProxy.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,15 @@ public static async void WebSocketHandler(HttpListenerContext ctx) {
8686
try {
8787
WebSocketContext wsc = await ctx.AcceptWebSocketAsync(null);
8888
ws = wsc.WebSocket;
89-
if (ws is null) return;
9089
}
9190
catch (WebSocketException ex) {
9291
ctx.Response.Close();
9392
Logger.Error(ex);
9493
return;
9594
}
9695

96+
if (ws is null) return;
97+
9798
if (!Auth.IsAuthenticatedAndAuthorized(ctx, ctx.Request.Url.AbsolutePath)) {
9899
await ws.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, null, CancellationToken.None);
99100
return;
@@ -160,7 +161,7 @@ public static async void WebSocketHandler(HttpListenerContext ctx) {
160161
}
161162
catch {}
162163

163-
if (ws?.State == WebSocketState.Open) {
164+
if (ws.State == WebSocketState.Open) {
164165
try {
165166
await ws.CloseAsync(WebSocketCloseStatus.NormalClosure, String.Empty, CancellationToken.None);
166167
}

Protest/Tasks/Fetch.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,7 @@ public static byte[] DevicesTask(string[] hosts, bool dns, bool wmi, bool ldap,
660660
List<string> queue = new List<string>(hosts);
661661
List<string> redo = new List<string>();
662662

663-
while (!task.cancellationToken.IsCancellationRequested) {
663+
while (task is not null && !task.cancellationToken.IsCancellationRequested) {
664664

665665
while (queue.Count > 0) {
666666
int size = Math.Min(WINDOW, queue.Count);

Protest/Tasks/Issues.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,15 @@ public static async void WebSocketHandler(HttpListenerContext ctx) {
108108
try {
109109
WebSocketContext wsc = await ctx.AcceptWebSocketAsync(null);
110110
ws = wsc.WebSocket;
111-
if (ws is null) return;
112111
}
113112
catch (WebSocketException ex) {
114113
ctx.Response.Close();
115114
Logger.Error(ex);
116115
return;
117116
}
118117

118+
if (ws is null) return;
119+
119120
if (!Auth.IsAuthenticatedAndAuthorized(ctx, ctx.Request.Url.AbsolutePath)) {
120121
await ws.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, null, CancellationToken.None);
121122
return;

Protest/Tasks/Lifeline.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -189,14 +189,16 @@ private static void LifelineLoop() {
189189
wmiThread?.Join();
190190
snmpThread?.Join();
191191

192-
task?.status = TaskWrapper.TaskStatus.Idle;
193-
task.Sleep(Math.Max((int)((TWO_HOURS_IN_TICKS - (DateTime.UtcNow.Ticks - startTimeStamp)) / 10_000), 0));
194-
195-
if (task.cancellationToken.IsCancellationRequested) {
196-
task.status = TaskWrapper.TaskStatus.Canceling;
197-
task?.Dispose();
198-
task = null;
199-
return;
192+
if (task is not null) {
193+
task.status = TaskWrapper.TaskStatus.Idle;
194+
task.Sleep(Math.Max((int)((TWO_HOURS_IN_TICKS - (DateTime.UtcNow.Ticks - startTimeStamp)) / 10_000), 0));
195+
196+
if (task.cancellationToken.IsCancellationRequested) {
197+
task.status = TaskWrapper.TaskStatus.Canceling;
198+
task.Dispose();
199+
task = null;
200+
return;
201+
}
200202
}
201203
}
202204
}

Protest/Tasks/Watchdog.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -159,14 +159,16 @@ private static void WatchLoop() {
159159
}
160160
}
161161

162-
task.status = TaskWrapper.TaskStatus.Idle;
163-
task.Sleep(Math.Max(nextSleep - (int)((DateTime.UtcNow.Ticks - loopStartTimeStamp) / 10_000), 0));
164-
165-
if (task.cancellationToken.IsCancellationRequested) {
166-
task.status = TaskWrapper.TaskStatus.Canceling;
167-
task?.Dispose();
168-
task = null;
169-
return;
162+
if (task is not null) {
163+
task.status = TaskWrapper.TaskStatus.Idle;
164+
task.Sleep(Math.Max(nextSleep - (int)((DateTime.UtcNow.Ticks - loopStartTimeStamp) / 10_000), 0));
165+
166+
if (task.cancellationToken.IsCancellationRequested) {
167+
task.status = TaskWrapper.TaskStatus.Canceling;
168+
task.Dispose();
169+
task = null;
170+
return;
171+
}
170172
}
171173
}
172174
}

0 commit comments

Comments
 (0)