-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- 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.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
|
@@ -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) | ||
|
||
|
@@ -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 } ) | ||
} | ||
|
||
|