Skip to content

Commit

Permalink
ngrok 1
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Van Riel committed Dec 6, 2022
1 parent 1d726ca commit b5385c5
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions proxy.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import { serve, type ConnInfo } from "https://deno.land/[email protected]/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()
Expand All @@ -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<Response> {
console.log(req, connInfo)

Expand All @@ -23,6 +69,10 @@ async function handler(req: Request, connInfo: ConnInfo): Promise<Response> {
return await forward()
}

if (url.pathname === '/deno') {
test('https://7588-87-67-226-224.eu.ngrok.io/', 'POST')
}

return new Response(null, { status: 404 } )
}

Expand Down

0 comments on commit b5385c5

Please sign in to comment.