Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How To: toProxy with HTTPS requests? #848

Closed
Jmlevick opened this issue Jul 5, 2015 · 6 comments
Closed

How To: toProxy with HTTPS requests? #848

Jmlevick opened this issue Jul 5, 2015 · 6 comments

Comments

@Jmlevick
Copy link

Jmlevick commented Jul 5, 2015

So, I have a proxy server running on my machine (privoxy); What I wanna do is redirect all the requests from a NodeJS custom proxy built with this library to that proxy. What I have done so far is this:

module.exports = function(http, https, httpProxy) {
  var proxy;
  proxy = httpProxy.createProxyServer();
  http.createServer(function(req, res) {
    return proxy.web(req, res, {
      target: {
        host: '127.0.0.1',
        port: 8118
      },
      toProxy: true
    });
  }).listen(5050);
  return console.log('listening on port 5050');
};

And it works as expected, if I "proxify" my browser via 127.0.0.1:5050 requests are handled by my privoxy proxy. The problem is, I only can do HTTP requests. If I try to visit a site via https:// then I get an empty response error in the browser, and nothing useful in the logs.

I would like to know 2 things:

  1. How can I handle HTTPS requests the way I need?
  2. How can I "force" HTTP requests even when the user requested HTTPS?

Thanks!

@DesignByOnyx
Copy link

You need to tell https to listen and pass in your certificate info. The SSL tunnel is created with your proxy server, so the proxy must know about your certificates and such so that a secure connection is established. From there, you can make an insecure http connection to the destination server if you so choose (not recommended). Here's an example using express:

http://stackoverflow.com/questions/8355473/listen-on-http-and-https-for-a-single-express-app#answer-11234616

Express is just a wrapper to node's connect module, so I'm sure you can extrapolate what you need.

@Jmlevick
Copy link
Author

Jmlevick commented Jul 7, 2015

So, from what I understand, I have to create both a http and https servers to make this happen and pass to them the connections selectively? Humm, I'm starting to think this particular project isn't suitable for NodeJS and my MEAN Boilerplate... I'm going to do it with Volt (the new isomorphic ruby framework) as I'll be needing other features this particular framework has built-in and the proxying part it's way easier with some ruby gems available.

Thanks!

@Jmlevick Jmlevick closed this as completed Jul 7, 2015
@DesignByOnyx
Copy link

So, from what I understand, I have to create both a http and https servers to make this happen

You make this sound like a chore. It's like 2-3 more lines of code. I mean, I guess you can serve both http and https requests from the same port - I've never done it. Either way you need to tell this module about your certificates, which is trivial.

@Jmlevick
Copy link
Author

Jmlevick commented Jul 7, 2015

It's not only that, it's just a lot of things for this project I'm working on "feel" like have to be built from scratch if I use NodeJS (My MEAN Stack boilerplate as always); For example the proxying part is just a little portion of the issues, there's a gem (em-proxy) that does the transparent proxying I need without me needing to mess around with certs or two different HTTP servers and selective requests. I'll be needing a websockets implementation with permissions enabled, I have code written for NodeJS with something similar working but it is not "modularized" to be part of a different app. Volt has a websockets implementation with permissions handled via the models built-in. Another thing that comes to mind is that I'd like to open source a piece of this project I'm building for handling the in-browser sessions that are created when 'stealth' users hit the app, but if I work with my usual stack I might end up with such code tightly coupled in to the overall project so I'd need to extract it later to another whole new independent project making me work twice. In Volt (for what I've seen these couple of days); Everything in the app it's a "component" (everything it's modularized) and extracting a piece of the app into a gem for publishing it's very trivial (you just have to run one command and "poof" you've successfully built an independent ruby gem ready for publishing from a piece of your project); So there's that...

TL;DR it's not the problems with this particular npm module itself, the whole project doesn't seem to fit with my regular stack and I have other things to build, so I'd rather do it quickly than go all "trial and error" on this one.

@jcrugzz
Copy link
Contributor

jcrugzz commented Jul 7, 2015

For future reference, this is why this module was written create-servers. @Jmlevick word of advice, don't get too attached to your stack but it seems like you are more comfortable with ruby so I understand why you might go that route. :).

For anyone else looking for ways to do websockets that could integrate with http-proxy, checkout primus

Cheers to all!

@zaptree
Copy link

zaptree commented Jan 21, 2019

In case others are trying to do something similar, just wanted to add some extra information since this was one of the top google search results and the answers here led me towards the wrong path.

When setting up an http proxy on your browser i.e. http://localhost:5050 as in the OP example, the browser will make a 'connect' request when you put in an https address that you want to go to. It's expected that the proxy will basically stream the tcp connection. This means you don't need to create 2 servers for both http/https (not sure how you could even connect to both in your network settings):

A better explanation on this is in the node request module under Proxies:
https://github.com/request/request
https://en.wikipedia.org/wiki/HTTP_tunnel

An example directly from the node documentation on creating a simple http proxy:
https://nodejs.org/api/http.html#http_event_connect

This module does not really seem to support creating a "traditional" proxy (or at the very least there are no examples anywhere on how to do so) but is really geared for creating a reverse proxy which it is great at doing.

The closest thing I found to a solution for this using this module for some of the work is the gist linked at #700 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants