@@ -24,23 +24,42 @@ const cors = require('micro-cors')({allowHeaders})
2424const fetch = require ( 'node-fetch' )
2525
2626async function service ( req , res ) {
27- let q = url . parse ( req . url , true ) . query
28- if ( ! q . href ) {
27+ let p = url . parse ( req . url , true ) . path
28+ let parts = p . match ( / \/ ( [ ^ \/ ] * ) \/ ( .* ) / )
29+ if ( parts === null ) {
2930 res . setHeader ( 'content-type' , 'text/html' )
3031 let html = `<!DOCTYPE html>
3132 <html>
32- <title>400 Error </title>
33- <h1>Missing 'href' parameter. </h1>
33+ <title>cors-buster </title>
34+ <h1>CORS Buster! 👻⃠ </h1>
3435 <h2>See docs: <a href="https://npmjs.org/package/${ pkg . name } ">https://npmjs.org/package/${ pkg . name } </a></h2>
36+ <h2>Authenticity</h2>
37+ This is a publicly available service. As such you may wonder if it is safe to trust.
38+ You can inspect the source code that this server is running by visiting this page: <a href="/_src">/_src</a>.
39+ The deploys are immutable, so you can be sure that the code will never change.
40+ <h2>Logging</h2>
41+ The cloud hosting provider keeps log of all requests. That log is public and available on this page: <a href="/_logs">/_logs</a>.
42+ It records the URL, origin IP, referer, and user-agent. None of the sensitive HTTP headers (including those used for
43+ HTTP Basic Auth and HTTP Token auth) are ever logged.
44+ <h2>Request API</h2>
45+ https://${ process . env . NOW_URL } /domain/path?query
46+ <ul>
47+ <li>domain - the destination host</li>
48+ <li>path - the rest of the URL</li>
49+ <li>query - optional query parameters</li>
50+ </ul>
51+ Example: https://${ process . env . NOW_URL } /github.com/wmhilton/cors-buster?service=git-upload-pack
52+ <h2>Supported Protocols</h2>
53+ In order to protect users who might send their usernames and passwords through the proxy,
54+ all requests must be made using HTTPS. Plain old HTTP is insecure and therefore not allowed.
55+ This proxy cannot be used to make requests to HTTP-only sites.
3556 <h2>Supported HTTP Methods</h2>
3657 <ul>
3758 <li>All - OPTIONS, GET, POST, PUT, DELETE, etc</li>
3859 </ul>
3960 <h2>Supported Query Parameters</h2>
4061 <ul>
41- <li>?href=<href> - <i>required</i>, the URL you are trying to reach</li>
42- <li>&method=<method> - <i>optional</i>, the HTTP method to use</li>
43- <li>&<HTTP Header>=<value> - <i>optional</i>, set any supported HTTP headers</li>
62+ <li>All URL query parameters are passed on as-is to the destination address.</li>
4463 </ul>
4564 <h2>Supported Headers</h2>
4665 <ul>
@@ -50,17 +69,25 @@ async function service (req, res) {
5069 `
5170 return send ( res , 400 , html )
5271 }
72+
5373 let headers = { }
5474 for ( let h of allowHeaders ) {
5575 if ( req . headers [ h ] ) {
56- headers [ h ] = q [ h ] || req . headers [ h ]
76+ headers [ h ] = req . headers [ h ]
5777 }
5878 }
59- let f = await fetch ( q . href , {
60- method : q . method || req . method ,
61- headers,
62- body : req
63- } )
79+
80+ let pathdomain = parts [ 1 ]
81+ let remainingpath = parts [ 2 ]
82+ console . log ( `https://${ pathdomain } /${ remainingpath } ` )
83+ let f = await fetch (
84+ `https://${ pathdomain } /${ remainingpath } ` ,
85+ {
86+ method : req . method ,
87+ headers,
88+ body : req
89+ }
90+ )
6491 f . body . pipe ( res )
6592}
6693
0 commit comments