|
| 1 | +/* |
| 2 | + reverse-proxy.js: Example of reverse proxying (with HTTPS support) |
| 3 | + Copyright (c) 2015 Alberto Pose <[email protected]> |
| 4 | + |
| 5 | + Permission is hereby granted, free of charge, to any person obtaining |
| 6 | + a copy of this software and associated documentation files (the |
| 7 | + "Software"), to deal in the Software without restriction, including |
| 8 | + without limitation the rights to use, copy, modify, merge, publish, |
| 9 | + distribute, sublicense, and/or sell copies of the Software, and to |
| 10 | + permit persons to whom the Software is furnished to do so, subject to |
| 11 | + the following conditions: |
| 12 | + |
| 13 | + The above copyright notice and this permission notice shall be |
| 14 | + included in all copies or substantial portions of the Software. |
| 15 | + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 16 | + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 17 | + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 18 | + NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 19 | + LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 20 | + OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 21 | + WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 22 | +*/ |
| 23 | + |
| 24 | +var http = require('http'), |
| 25 | + net = require('net'), |
| 26 | + httpProxy = require('http-proxy'), |
| 27 | + url = require('url'), |
| 28 | + util = require('util'); |
| 29 | + |
| 30 | +var proxy = httpProxy.createServer(); |
| 31 | + |
| 32 | +var server = http.createServer(function (req, res) { |
| 33 | + util.puts('Receiving reverse proxy request for:' + req.url); |
| 34 | + |
| 35 | + proxy.web(req, res, {target: req.url, secure: false}); |
| 36 | +}).listen(8213); |
| 37 | + |
| 38 | +server.on('connect', function (req, socket) { |
| 39 | + util.puts('Receiving reverse proxy request for:' + req.url); |
| 40 | + |
| 41 | + var serverUrl = url.parse('https://' + req.url); |
| 42 | + |
| 43 | + var srvSocket = net.connect(serverUrl.port, serverUrl.hostname, function() { |
| 44 | + socket.write('HTTP/1.1 200 Connection Established\r\n' + |
| 45 | + 'Proxy-agent: Node-Proxy\r\n' + |
| 46 | + '\r\n'); |
| 47 | + srvSocket.pipe(socket); |
| 48 | + socket.pipe(srvSocket); |
| 49 | + }); |
| 50 | +}); |
| 51 | + |
| 52 | +// Test with: |
| 53 | +// curl -vv -x http://127.0.0.1:8213 https://www.google.com |
| 54 | +// curl -vv -x http://127.0.0.1:8213 http://www.google.com |
0 commit comments