error handling on TLS handshake and reporting wisp errors to client

This commit is contained in:
ProgrammerIn-wonderland 2025-01-14 23:54:47 -05:00
parent 6261d7c90c
commit 98be5c1323
No known key found for this signature in database
GPG Key ID: 75BC4D281955C56E
3 changed files with 55 additions and 14 deletions

View File

@ -1,4 +1,5 @@
import EventListener from "../../lib/EventListener.js";
import { errors } from "./parsers.js";
const texten = new TextEncoder();
export let wispInfo = {
@ -10,13 +11,18 @@ export class PSocket extends EventListener {
_events = new Map();
_streamID;
constructor(host, port) {
super(["data", "drain", "open", "close", "tlsdata", "tlsopen"]);
super(["data", "drain", "open", "error", "close", "tlsdata", "tlsopen"]);
const callbacks = {
dataCallBack: (data) => {
this.emit("data", data);
},
closeCallBack: (reason) => {
this.emit("close", false); // TODO, report errors
if (reason !== 0x02) {
this.emit("error", new Error(errors[reason]));
this.emit("close", true);
return;
}
this.emit("close", false);
}
}

View File

@ -12,29 +12,51 @@ export class PTLSSocket extends PSocket {
(async() => {
if (!rustls) {
rustls = (await import( /* webpackIgnore: true */ "https://puter-net.b-cdn.net/rustls.js"))
// await rustls.default("https://puter-net.b-cdn.net/rustls.wasm")
await rustls.default("https://alicesworld.tech/fun/rustls.wasm")
await rustls.default("https://puter-net.b-cdn.net/rustls.wasm")
}
// const socket = new puter.net.Socket("google.com", 443)
let cancelled = false;
const readable = new ReadableStream({
/**
*
* @param {ReadableStreamDefaultController} controller
*/
start: (controller) => {
super.on("data", (data) => {
controller.enqueue(data.buffer)
})
super.on("close", () => {
controller.close()
if (!cancelled)
controller.close()
})
},
pull: (controller) => {
},
cancel: () => {
cancelled = true;
}
})
const writable = new WritableStream({
write: (chunk) => { super.write(chunk); },
abort: () => { super.close(); },
abort: () => { console.log("hello"); super.close(); },
close: () => { super.close(); },
})
const {read, write} = await rustls.connect_tls(readable, writable, args[0]);
let read, write;
try {
const TLSConnnection = await rustls.connect_tls(readable, writable, args[0])
read = TLSConnnection.read;
write = TLSConnnection.write;
} catch (e) {
this.emit("error", new Error("TLS Handshake failed: " + e));
return;
}
this.writer = write.getWriter();
// writer.write("GET / HTTP/1.1\r\nHost: google.com\r\n\r\n");
let reader = read.getReader();
@ -42,10 +64,14 @@ export class PTLSSocket extends PSocket {
this.emit("tlsopen", undefined);
while (!done) {
const {done: readerDone, value} = await reader.read();
done = readerDone;
if (!done) {
this.emit("tlsdata", value);
try {
const {done: readerDone, value} = await reader.read();
done = readerDone;
if (!done) {
this.emit("tlsdata", value);
}
} catch (e) {
this.emit("error", e)
}
}
})();

View File

@ -14,6 +14,15 @@ export const UDP = 0x02;
// Frequently used objects
export const textde = new TextDecoder();
const texten = new TextEncoder();
export const errors = {
0x41: "Stream creation failed due to invalid information. This could be sent if the destination was a reserved address or the port is invalid."
,0x42: "Stream creation failed due to an unreachable destination host. This could be sent if the destination is an domain which does not resolve to anything."
,0x43: "Stream creation timed out due to the destination server not responding."
,0x44: "Stream creation failed due to the destination server refusing the connection."
,0x47: "TCP data transfer timed out."
,0x48: "Stream destination address/domain is intentionally blocked by the proxy server."
,0x49: "Connection throttled by the server."
}
/**
* @typedef {{packetType: number, streamID: number, streamType?: number, port?: number, hostname?: string, payload?: Uint8Array, reason?: number, remainingBuffer?: number}} ParsedWispPacket