From b5385c51b0dcfd1231f1f22214c95db1624b81e8 Mon Sep 17 00:00:00 2001 From: Jan Van Riel <=> Date: Tue, 6 Dec 2022 09:45:32 +0100 Subject: [PATCH] ngrok 1 --- proxy.ts | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/proxy.ts b/proxy.ts index d81cff5..374f51b 100644 --- a/proxy.ts +++ b/proxy.ts @@ -1,5 +1,18 @@ import { serve, type ConnInfo } from "https://deno.land/std@0.152.0/http/server.ts"; +const log = (line:string, text:string) => { + console.log(line) + return `${text}\n${line}` +} + +const logHeaders = (headers:Headers, text:string) => { + let lines = '' + for (const header of headers) { + lines = log(` ${header[0]}: ${header[1]}`, lines) + } + return `${text}${lines}` +} + const forward = async () => { try { const headers = new Headers() @@ -15,6 +28,39 @@ const forward = async () => { } } +const test = async (url:string, method:string) => { + try { + const u = new URL(url) + const headers = new Headers() + headers.set('host', u.host) + headers.set('x-twintag-cloudflare-trace', `${Math.floor(Date.now())}`) + + // log request + let text = log('\nChecking ...\n', '') + text = log(`${method} ${url}`, text) + text = logHeaders(headers, text) + + // execute fetch + const rsp = await fetch(url, { + method: method, + headers: headers, + }) + + // log response + text = log(`Status ${rsp.status} `, text) + text = logHeaders(rsp.headers, text) + + // log body length + const body = await rsp.text() + text = log(`${body.length} body bytes`, text) + + return text + } catch(err) { + console.error(err) + return `${err}` + } +} + async function handler(req: Request, connInfo: ConnInfo): Promise { console.log(req, connInfo) @@ -23,6 +69,10 @@ async function handler(req: Request, connInfo: ConnInfo): Promise { return await forward() } + if (url.pathname === '/deno') { + test('https://7588-87-67-226-224.eu.ngrok.io/', 'POST') + } + return new Response(null, { status: 404 } ) }