Re: [nodejs] http/ https server on same port

793 views
Skip to first unread message

Alex Kocharin

unread,
Feb 6, 2013, 2:26:52 PM2/6/13
to nod...@googlegroups.com
 
In order to do that you can create a tcp server (net module) and listen for any requests on a port. If it is plaintext request it's http, otherwise it's https. Simple enough.
 
You can even put ssh server on the same port if you aren't so scary of a witchcraft :)
 
So, the general idea is to create three servers, http, https and net. Net server would receive all connections and proxy them to others. I've done that using three different ports (1 external, 2 internal), but I'm quite sure you can simulate connection to a http(s) server internally without binding it to a port. Maybe it's "request" event I don't know, you can look in node.js source code how it can be done.
 
--
// alex
 
 
06.02.2013, 23:05, "V'Raj Kanwade" <viraj....@gmail.com>:
I am building a proxy server which needs to listen for both http and https proxy on same port.
 
http.createServer does not call response handler for https traffic.
 
So I created the server using net.createServer. The question I have is, how can I leverage the http functionality once I have the input request?
 
For eg. when I see the start of request is GET http://nodejs.org/ HTTP/1.1, I want to convert it into a http request so that the headers etc are parsed accordingly and if the request starts with CONNECT, I can implement my own tunneling?

 

--
--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en
 
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Mark Hahn

unread,
Feb 6, 2013, 2:35:14 PM2/6/13
to nodejs
Use https.createServer

Bradley Meck

unread,
Feb 6, 2013, 4:07:18 PM2/6/13
to nod...@googlegroups.com
A long time ago I built a proof of concept for something like this: https://github.com/bmeck/kitsune

After thinking about maintainability I decided to avoid doing this sort of behavior but left the code up as an example.

Isaac Schlueter

unread,
Feb 6, 2013, 7:21:08 PM2/6/13
to nodejs
Why would you want to do this?

Why wouldn't you want http on :80 and https on :443, so that you have
https://yourserver.com and http://yourserver.com instead of
http://blah.com and https://blah.com:80/ which looks funny and
strange?

Do something like this:

http.createServer(handler).listen(80);
https.createServer(keysAndStuff, handler).listen(443);
function handler(req, res) {
res.end('Hello, this is served to both http and https!\n');

Arunoda Susiripala

unread,
Feb 6, 2013, 9:40:27 PM2/6/13
to nod...@googlegroups.com
I think he is looking for this. 
--
Arunoda Susiripala


V'Raj Kanwade

unread,
Feb 7, 2013, 12:02:05 AM2/7/13
to nod...@googlegroups.com
Ok. Let me rephrase the question:

I am inside connectionHandler of net.createServer. Now when I determine the data from client is HTTP request, I want to convert it to HTTPRequest object so that I can leverage the HTTP headers, status code parsing etc.

It is some client requirement where the proxy has to listen to http, https and custom protocol on same port.

V'Raj Kanwade

unread,
Feb 7, 2013, 12:03:36 AM2/7/13
to nod...@googlegroups.com
I think you forgot link/ info.
> nodejs+unsubscribe@googlegroups.com

> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en
>
> ---
> You received this message because you are subscribed to the Google Groups
> "nodejs" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to nodejs+unsubscribe@googlegroups.com.

> For more options, visit https://groups.google.com/groups/opt_out.
>
>

--
--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+unsubscribe@googlegroups.com

For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.


Bryce Baril

unread,
Feb 6, 2013, 10:04:11 PM2/6/13
to nod...@googlegroups.com
Or better yet, redirect :80 to https://yourserver.com and wholly avoid mixed secure/insecure code paths.

  // a simple listener to redirect 80 to https
  http.createServer(function (req, res) {
    res.writeHead(301, {"Location": "https://" + HOST + req.url});
    res.end();
  }).listen(80);

V'Raj Kanwade

unread,
Feb 8, 2013, 1:45:16 AM2/8/13
to nod...@googlegroups.com
But then, integrating the custom protocol might not be possible right? The custom protocol is used for admin purposes. This is currently a python codebase. We are porting it to nodejs. So the client wants to keep the architecture same.

Ben Noordhuis

unread,
Feb 8, 2013, 11:34:23 AM2/8/13
to nod...@googlegroups.com
On Thu, Feb 7, 2013 at 6:02 AM, V'Raj Kanwade <viraj....@gmail.com> wrote:
> Ok. Let me rephrase the question:
>
> I am inside connectionHandler of net.createServer. Now when I determine the
> data from client is HTTP request, I want to convert it to HTTPRequest object
> so that I can leverage the HTTP headers, status code parsing etc.
>
> It is some client requirement where the proxy has to listen to http, https
> and custom protocol on same port.

I suggest you go with something like this:
https://gist.github.com/bnoordhuis/4740141

The salient part:

function tcpConnection(conn) {
conn.once('data', function(buf) {
// A TLS handshake record starts with byte 22.
var address = (buf[0] === 22) ? httpsAddress : httpAddress;
var proxy = net.createConnection(address, function() {
proxy.write(buf);
conn.pipe(proxy).pipe(conn);
});
});
}

You sniff the first packet and forward it to the right server,
depending on whether it's a TLS handshake. The HTTP and HTTPS servers
are listening on UNIX sockets in this example but that could be
regular TCP ports, of course.
Reply all
Reply to author
Forward
0 new messages