From 138595905c90ed8bb0073e5612cd4278332b2e73 Mon Sep 17 00:00:00 2001 From: zizifn <1803942+zizifn@users.noreply.github.com> Date: Mon, 5 Dec 2022 14:26:26 +0800 Subject: [PATCH] add deno issue --- apps/deno-bypass/src/bypass2.ts | 41 +++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 apps/deno-bypass/src/bypass2.ts diff --git a/apps/deno-bypass/src/bypass2.ts b/apps/deno-bypass/src/bypass2.ts new file mode 100644 index 0000000..71f16ac --- /dev/null +++ b/apps/deno-bypass/src/bypass2.ts @@ -0,0 +1,41 @@ +import { serve } from 'https://deno.land/std@0.167.0/http/server.ts'; + +const handler = async (request: Request): Promise => { + const connection = await Deno.connect({ + port: 80, + hostname: 'www.baidcu.com', + }); + + // GET / HTTP/1.1 + // Host: www.baidu.com + // User-Agent: curl/7.83.1 + // Accept: */* + const body2 = new ReadableStream({ + start(controller) { + controller.enqueue(new TextEncoder().encode('GET / HTTP/1.1\r\n')); + controller.enqueue(new TextEncoder().encode('Host: www.baidu.com\r\n')); + controller.enqueue( + new TextEncoder().encode('User-Agent: curl/7.83.1\r\n') + ); + controller.enqueue(new TextEncoder().encode('Accept: */*\r\n\r\n')); + controller.close(); // 注释这个就好用 + }, + cancel() {}, + }); + + // 或者不用 pipeThrough, 直接write + // for await (const chunk of body2) { + // connection.write(chunk); + // } + // const proxyResp = connection.readable; + + // ----------- + const proxyResp = body2?.pipeThrough(connection); + + return new Response(proxyResp, { + status: 200, + headers: {}, + }); +}; + +serve(handler, { port: 8080, hostname: '0.0.0.0' });