22const url = require ( 'url' )
33const pkg = require ( './package.json' )
44const { send} = require ( 'micro' )
5+ const origin = process . env . ALLOW_ORIGIN
56const allowHeaders = [
67 'accept-encoding' ,
78 'accept-language' ,
@@ -37,18 +38,19 @@ const exposeHeaders = [
3738 'vary' ,
3839 'x-github-request-id' ,
3940]
40- const cors = require ( './micro-cors.js' ) ( { allowHeaders, exposeHeaders} )
4141const fetch = require ( 'node-fetch' )
42+ const cors = require ( './micro-cors.js' ) ( { allowHeaders, exposeHeaders, origin} )
43+ const allow = require ( './allow-request.js' )
4244
4345async function service ( req , res ) {
44- let p = url . parse ( req . url , true ) . path
45- let parts = p . match ( / \/ ( [ ^ \/ ] * ) \/ ( . * ) / )
46- if ( parts === null ) {
46+ let u = url . parse ( req . url , true )
47+
48+ if ( u . pathname === '/' ) {
4749 res . setHeader ( 'content-type' , 'text/html' )
4850 let html = `<!DOCTYPE html>
4951 <html>
50- <title>cors-buster </title>
51- <h1>CORS Buster! 👻⃠ </h1>
52+ <title>@isomorphic-git/ cors-proxy </title>
53+ <h1>@isomorphic-git/cors-proxy </h1>
5254 <h2>See docs: <a href="https://npmjs.org/package/${ pkg . name } ">https://npmjs.org/package/${ pkg . name } </a></h2>
5355 <h2>Authenticity</h2>
5456 This is a publicly available service. As such you may wonder if it is safe to trust.
@@ -58,42 +60,25 @@ async function service (req, res) {
5860 The cloud hosting provider keeps log of all requests. That log is public and available on this page: <a href="/_logs">/_logs</a>.
5961 It records the URL, origin IP, referer, and user-agent. None of the sensitive HTTP headers (including those used for
6062 HTTP Basic Auth and HTTP Token auth) are ever logged.
61- <h2>Request API</h2>
62- ${ process . env . NOW_URL } /domain/path?query
63- <ul>
64- <li>domain - the destination host</li>
65- <li>path - the rest of the URL</li>
66- <li>query - optional query parameters</li>
67- </ul>
68- Example: ${ process . env . NOW_URL } /github.com/wmhilton/cors-buster?service=git-upload-pack
69- <h2>Supported Protocols</h2>
70- In order to protect users who might send their usernames and passwords through the proxy,
71- all requests must be made using HTTPS. Plain old HTTP is insecure and therefore not allowed.
72- This proxy cannot be used to make requests to HTTP-only sites.
73- <h2>Supported HTTP Methods</h2>
74- <ul>
75- <li>All - OPTIONS, GET, POST, PUT, DELETE, etc</li>
76- </ul>
77- <h2>Supported Query Parameters</h2>
78- <ul>
79- <li>All URL query parameters are passed on as-is to the destination address.</li>
80- </ul>
81- <h2>Supported Headers</h2>
82- <ul>
83- ${ allowHeaders . map ( x => `<li>${ x } </li>` ) . join ( '\n' ) }
84- </ul>
8563 </html>
8664 `
8765 return send ( res , 400 , html )
8866 }
8967
68+ if ( ! allow ( req , u ) ) {
69+ // Don't waste my precious bandwidth
70+ return send ( res , 403 , '' )
71+ }
72+
9073 let headers = { }
9174 for ( let h of allowHeaders ) {
9275 if ( req . headers [ h ] ) {
9376 headers [ h ] = req . headers [ h ]
9477 }
9578 }
9679
80+ let p = u . path
81+ let parts = p . match ( / \/ ( [ ^ \/ ] * ) \/ ( .* ) / )
9782 let pathdomain = parts [ 1 ]
9883 let remainingpath = parts [ 2 ]
9984 console . log ( `https://${ pathdomain } /${ remainingpath } ` )
0 commit comments