我是初学者,刚高考完想做一个HTTP Tunnel。下面的代码能运行,但运行几秒就崩了qwq,而且这个错误死活捕获不到
PS C:\Users\hcy\Desktop\test> deno run -A .\test.ts
error: Uncaught (in promise) Interrupted: operation canceled
at async Object.pull (ext:deno_web/06_streams.js:798:27)
const decoder = new TextDecoder();
const encoder = new TextEncoder();
const listener = Deno.listen({ port: 8100 });
for await (const conn of listener) {
const a = new Uint8Array(256);
await conn.read(a);
const b = decoder.decode(a);
if (b.startsWith("CONNECT")) {
const host = (b.match(/Host:\s*([^\s]+)/) as RegExpMatchArray)[1].split(
":",
);
const hostname = host[0];
const port = parseInt(host[1]);
const connect = await Deno.connect({ hostname, port });
connect.setKeepAlive(true);
await conn.write(encoder.encode("HTTP/1.1 200\r\n\r\n"));
conn.readable.pipeTo(connect.writable).catch(console.log);
connect.readable.pipeTo(conn.writable).catch(console.log);
}
}
还有一个问题:
如何在同一个端口上监听connect和http请求?
我注意到用Deno.serveHttp(conn)
后原始连接就不能用了,不会要手搓http吧qwq