Can I create a HTTP proxy with glitch?


#1

My understanding so far is that an proxy server is ‘just a web server’, but I haven’t been able to receive proxying requests. My purpose is to set up a proxy server that I can control so that I can debug a proxy authentication issue for a different project. Please advise. Thanks.

I have this so far:

var http = require('http');
http.createServer(function(request, response) {
  var host = request.headers['host'];
  console.log(`port: ${process.env.PORT}, host: ${host}`);
  
  request.addListener('data', function(chunk) {
    console.log(chunk.toString());
  });
  
  request.addListener('end', function() {
    response.statusCode=407;
    response.statusMessage='Auth please.';
    response.end();
  });
}).listen(process.env.PORT);

I can do curl http://proxy-project and I see my logging messages and am getting back a 407 response.

But curl with proxy:
curl -v -x http://proxy-project:80 http://target-url does not produce logging messages and returns this response

* Rebuilt URL to: http://target-url/
*   Trying 34.192.211.247...
* TCP_NODELAY set
* Connected to proxy-project (34.192.211.247) port 80 (#0)
> GET http://target-url/ HTTP/1.1
> Host: target-url
> User-Agent: curl/7.54.0
> Accept: */*
> Proxy-Connection: Keep-Alive
> 
< HTTP/1.1 404 Not Found
< Date: Fri, 22 Dec 2017 06:30:15 GMT
< Transfer-Encoding: chunked
< Connection: keep-alive
< x-powered-by: Express
< 
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>👻 Project Not Found</title>
    <meta name="viewport" content="initial-scale=1, width=device-width">
    <link rel="stylesheet" type="text/css" href="https://gomix.com/themes/sugar.css">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.3/jquery.js"></script>
    <script src="https://gomix.com/error-page.js"></script>
  </head>
  <body class="error-page ghost-border">
    <div class="container">

      <h1>
        Project Not Found
      </h1>


      <h2>
        Maybe a typo? Or maybe it's been renamed?
      </h2>

      <div class="actions">
        <a href="https://gomix.com">
          <div class="gomix-logo bird">
          </div>
        </a>
      </div>
    </div>

    <canvas id="stars"></canvas>

  </body>
</html>
* Connection #0 to host proxy-project left intact

#2

If that’s all of the code in your project, then there’s nothing there to do the proxying. I’m not sure why you’re getting a Project Not Found response there. Take a look at http-proxy on npm: https://www.npmjs.com/package/httpp-proxy


#3

I am not interested in making a complete proxy, just something where I can debug proxy authentication.

I think I am getting the 404 because curl’s proxying code is not sending a host name header to glitch, so glitch doesn’t know which project to forward the request to.


#4

That’s possible. Our proxy needs that info to find your project.


#5

Hi @iamphi,

Instead of curl -v -x http://proxy-project:80 http://target-url, can you try something like this (ie. add the Host header so that the Glitch platform knows which project to hit):

$ curl -v -H 'Host: proxy-project' -x http://proxy-project:80 http://target-url

I’ve never used proxies with curl before so I’m not sure that will work but it’s worth a quick go perhaps.