Skip to content

Commit

Permalink
Fix telnet bug
Browse files Browse the repository at this point in the history
  • Loading branch information
veniware committed Jul 15, 2024
1 parent 2d33b18 commit 7d66c3b
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions Protest/Protocols/Telnet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,16 @@ public static async void WebSocketHandler(HttpListenerContext ctx) {
_ = int.TryParse(split[1], out port);
}

TcpClient telnet = new TcpClient(host, port);
TcpClient telnet;

if (IPAddress.TryParse(host, out IPAddress ip)) {
telnet = new TcpClient();
telnet.Connect(ip, port);
}
else {
telnet = new TcpClient(host, port);
}

NetworkStream stream = telnet.GetStream();

Logger.Action(username, $"Establish telnet connection to {host}:{port}");
Expand Down Expand Up @@ -84,8 +93,13 @@ public static async void WebSocketHandler(HttpListenerContext ctx) {
}
}
catch (SocketException ex) {
await WsWriteText(ws, $"{{\"error\":\"{ex.Message}\"}}");
await ws.CloseAsync(WebSocketCloseStatus.NormalClosure, String.Empty, CancellationToken.None);
if (ws.State == WebSocketState.Open) {
try {
await WsWriteText(ws, $"{{\"error\":\"{ex.Message}\"}}");
await ws.CloseAsync(WebSocketCloseStatus.NormalClosure, String.Empty, CancellationToken.None);
}
catch {}
}
return;
}
catch (Exception ex) {
Expand Down

0 comments on commit 7d66c3b

Please sign in to comment.