Skip to content

Commit

Permalink
Telnet, graphics modes, some progress
Browse files Browse the repository at this point in the history
  • Loading branch information
veniware committed May 13, 2024
1 parent f600e74 commit 425fb35
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 49 deletions.
26 changes: 23 additions & 3 deletions Protest/Front/telnet.js
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,12 @@ class Telnet extends Window {

HandleEscSequence(data, index) { //Control Sequence Introducer
if (data[index+1] === "[" || data[index+1] === "\x9b") {
return this.HandleCSI(data, index);
let consumed = this.HandleCSI(data, index);
console.log(data.substring(index, index+consumed));
//console.log("consumed: " + consumed);
console.log(" - - - - - - - - ");
return consumed;
//return this.HandleCSI(data, index);
}

if (data[index+1] === "P" || data[index+1] === "\x90") {
Expand Down Expand Up @@ -540,6 +545,9 @@ class Telnet extends Window {
console.warn("Unknown token: " + data[i]);
break;
}

console.warn("Token: " + data[i]);

i++;
}

Expand Down Expand Up @@ -653,7 +661,19 @@ class Telnet extends Window {
break;

case "m": //graphics modes
if (values.length === 0) break;
if (values.length === 0) { //same as reset?
this.foreColor = null;
this.backColor = null;
this.isBold = false;
this.isDim = false;
this.isItalic = false;
this.isUnderline = false;
this.isBlinking = false;
this.isInverse = false;
this.isHidden = false;
this.isStrikethrough = false;
return 3;
}

for (let p=0; p<values.length; p++) {
switch (values[p]) {
Expand Down Expand Up @@ -772,7 +792,7 @@ class Telnet extends Window {
break;
}

return i - index + 1;
return i - index;
}

HandleDCS(data, index) { //Device Control String
Expand Down
1 change: 0 additions & 1 deletion Protest/Protocols/Icmp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@ public static async void WebSocketHandler(HttpListenerContext ctx) {
break;
}
}

}
catch (WebSocketException ex) when (ex.WebSocketErrorCode == WebSocketError.ConnectionClosedPrematurely) {
return;
Expand Down
100 changes: 55 additions & 45 deletions Protest/Protocols/Telnet.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using System.Net;
using Protest.Http;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Net.WebSockets;
using System.Runtime.Intrinsics.X86;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Protest.Http;

namespace Protest.Protocols;

Expand Down Expand Up @@ -62,51 +64,13 @@ public static async void WebSocketHandler(HttpListenerContext ctx) {

await WsWriteText(ws, "{\"connected\":true}"u8.ToArray());

Task fork = new Task(async ()=>{
while (ws.State == WebSocketState.Open && telnet.Connected) { //host read loop
byte[] data = new byte[2048];

try {
int count = stream.Read(data, 0, data.Length);

for (int i=0; i<count; i++) {
if (data[i] < 128) continue;
data[i] = 63; //?
}

await ws.SendAsync(new ArraySegment<byte>(data, 0, count), WebSocketMessageType.Text, true, CancellationToken.None);

if (count == 0) { // Remote host has closed the connection
if (ws.State == WebSocketState.Open) {
try {
await ws?.CloseAsync(WebSocketCloseStatus.NormalClosure, String.Empty, CancellationToken.None);
}
catch { }
}
return;
}

//Console.Write(dataString);
}
catch (System.IO.IOException) {
return;
}
catch {
return;
}

if (!Auth.IsAuthenticatedAndAuthorized(ctx, "/ws/telnet")) { //check session
ctx.Response.Close();
telnet.Close();
return;
}
}
Thread fork = new Thread(()=>{
HandleDownstream(ctx, ws, telnet, stream);
});
fork.Start();

while (ws.State == WebSocketState.Open && telnet.Connected) { //host write loop
byte[] buff = new byte[2048];

byte[] buff = new byte[2048];
while (ws.State == WebSocketState.Open && telnet.Connected) { //handle upstream
WebSocketReceiveResult receiveResult = await ws.ReceiveAsync(new ArraySegment<byte>(buff), CancellationToken.None);

if (receiveResult.MessageType == WebSocketMessageType.Close) {
Expand All @@ -131,7 +95,6 @@ public static async void WebSocketHandler(HttpListenerContext ctx) {
return;
}
catch (Exception ex) {
//await WsWriteText(ws, $"{{\"error\":\"{ex.Message}\"}}");
Logger.Error(ex);
}
finally {
Expand All @@ -145,4 +108,51 @@ public static async void WebSocketHandler(HttpListenerContext ctx) {
catch { }
}
}

private static async void HandleDownstream(HttpListenerContext ctx, WebSocket ws, TcpClient telnet, Stream stream) {
byte[] data = new byte[2048];

while (ws.State == WebSocketState.Open && telnet.Connected) {
try {
int count = stream.Read(data, 0, data.Length);

if (count == 0) { // Remote host has closed the connection
if (ws.State == WebSocketState.Open) {
try {
await ws?.CloseAsync(WebSocketCloseStatus.NormalClosure, String.Empty, CancellationToken.None);
}
catch { }
}
return;
}

if (count == 1 && data[0] == 0) { //keep alive
continue;
}

for (int i = 0; i < count; i++) {
if (data[i] < 128)
continue;
data[i] = 63; //?
}

await ws.SendAsync(new ArraySegment<byte>(data, 0, count), WebSocketMessageType.Text, true, CancellationToken.None);

string dataString = Encoding.ASCII.GetString(data, 0, count);
Console.Write(dataString);
}
catch (System.IO.IOException) {
return;
}
catch {
return;
}

if (!Auth.IsAuthenticatedAndAuthorized(ctx, "/ws/telnet")) { //check session
ctx.Response.Close();
telnet.Close();
return;
}
}
}
}

0 comments on commit 425fb35

Please sign in to comment.