mirror of
https://github.com/HeyPuter/puter.git
synced 2025-02-02 23:28:39 +08:00
error handling on TLS handshake and reporting wisp errors to client
This commit is contained in:
parent
6261d7c90c
commit
98be5c1323
@ -1,4 +1,5 @@
|
|||||||
import EventListener from "../../lib/EventListener.js";
|
import EventListener from "../../lib/EventListener.js";
|
||||||
|
import { errors } from "./parsers.js";
|
||||||
const texten = new TextEncoder();
|
const texten = new TextEncoder();
|
||||||
|
|
||||||
export let wispInfo = {
|
export let wispInfo = {
|
||||||
@ -10,13 +11,18 @@ export class PSocket extends EventListener {
|
|||||||
_events = new Map();
|
_events = new Map();
|
||||||
_streamID;
|
_streamID;
|
||||||
constructor(host, port) {
|
constructor(host, port) {
|
||||||
super(["data", "drain", "open", "close", "tlsdata", "tlsopen"]);
|
super(["data", "drain", "open", "error", "close", "tlsdata", "tlsopen"]);
|
||||||
const callbacks = {
|
const callbacks = {
|
||||||
dataCallBack: (data) => {
|
dataCallBack: (data) => {
|
||||||
this.emit("data", data);
|
this.emit("data", data);
|
||||||
},
|
},
|
||||||
closeCallBack: (reason) => {
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -12,29 +12,51 @@ export class PTLSSocket extends PSocket {
|
|||||||
(async() => {
|
(async() => {
|
||||||
if (!rustls) {
|
if (!rustls) {
|
||||||
rustls = (await import( /* webpackIgnore: true */ "https://puter-net.b-cdn.net/rustls.js"))
|
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://puter-net.b-cdn.net/rustls.wasm")
|
||||||
await rustls.default("https://alicesworld.tech/fun/rustls.wasm")
|
|
||||||
}
|
}
|
||||||
// const socket = new puter.net.Socket("google.com", 443)
|
|
||||||
|
let cancelled = false;
|
||||||
const readable = new ReadableStream({
|
const readable = new ReadableStream({
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param {ReadableStreamDefaultController} controller
|
||||||
|
*/
|
||||||
start: (controller) => {
|
start: (controller) => {
|
||||||
super.on("data", (data) => {
|
super.on("data", (data) => {
|
||||||
controller.enqueue(data.buffer)
|
controller.enqueue(data.buffer)
|
||||||
})
|
})
|
||||||
super.on("close", () => {
|
super.on("close", () => {
|
||||||
controller.close()
|
if (!cancelled)
|
||||||
|
controller.close()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
},
|
||||||
|
pull: (controller) => {
|
||||||
|
|
||||||
|
},
|
||||||
|
cancel: () => {
|
||||||
|
cancelled = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
const writable = new WritableStream({
|
const writable = new WritableStream({
|
||||||
write: (chunk) => { super.write(chunk); },
|
write: (chunk) => { super.write(chunk); },
|
||||||
abort: () => { super.close(); },
|
abort: () => { console.log("hello"); super.close(); },
|
||||||
close: () => { 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();
|
this.writer = write.getWriter();
|
||||||
// writer.write("GET / HTTP/1.1\r\nHost: google.com\r\n\r\n");
|
// writer.write("GET / HTTP/1.1\r\nHost: google.com\r\n\r\n");
|
||||||
let reader = read.getReader();
|
let reader = read.getReader();
|
||||||
@ -42,10 +64,14 @@ export class PTLSSocket extends PSocket {
|
|||||||
this.emit("tlsopen", undefined);
|
this.emit("tlsopen", undefined);
|
||||||
|
|
||||||
while (!done) {
|
while (!done) {
|
||||||
const {done: readerDone, value} = await reader.read();
|
try {
|
||||||
done = readerDone;
|
const {done: readerDone, value} = await reader.read();
|
||||||
if (!done) {
|
done = readerDone;
|
||||||
this.emit("tlsdata", value);
|
if (!done) {
|
||||||
|
this.emit("tlsdata", value);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
this.emit("error", e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})();
|
})();
|
||||||
|
@ -14,6 +14,15 @@ export const UDP = 0x02;
|
|||||||
// Frequently used objects
|
// Frequently used objects
|
||||||
export const textde = new TextDecoder();
|
export const textde = new TextDecoder();
|
||||||
const texten = new TextEncoder();
|
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
|
* @typedef {{packetType: number, streamID: number, streamType?: number, port?: number, hostname?: string, payload?: Uint8Array, reason?: number, remainingBuffer?: number}} ParsedWispPacket
|
||||||
|
Loading…
Reference in New Issue
Block a user