8000 Merge branch 'master' into experimental · kprog/node-http-proxy@8f28618 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8f28618

Browse files
committed
Merge branch 'master' into experimental
2 parents d3486a6 + 0911c17 commit 8f28618

File tree

9 files changed

+424
-142
lines changed

9 files changed

+424
-142
lines changed

README.md

Lines changed: 79 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -216,24 +216,56 @@ proxyServerWithForwarding.listen(80);
216216

217217
The forwarding option can be used in conjunction with the proxy table options by simply including both the 'forward' and 'router' properties in the options passed to 'createServer'.
218218

219-
### Using node-http-proxy from the command line
220-
When you install this package with npm, a node-http-proxy binary will become available to you. Using this binary is easy with some simple options:
221219

220+
## Using HTTPS
221+
You have all the full flexibility of node-http-proxy offers in HTTPS as well as HTTP. The two basic scenarios are: with a stand-alone proxy server or in conjunction with another HTTPS server.
222+
223+
### Proxying to HTTP from HTTPS
224+
This is probably the most common use-case for proxying in conjunction with HTTPS. You have some front-facing HTTPS server, but all of your internal traffic is HTTP. In this way, you can reduce the number of servers to which your CA and other important security files are deployed and reduce the computational overhead from HTTPS traffic.
225+
226+
Using HTTPS in `node-http-proxy` is relatively straight-forward:
227+
222228
``` js
223-
usage: node-http-proxy [options]
229+
var fs = require('fs'),
230+
http = require('http'),
231+
https = require('https'),
232+
httpProxy = require('http-proxy');
233+
234+
var options = {
235+
https: {
236+
key: fs.readFileSync('path/to/your/key.pem', 'utf8'),
237+
cert: fs.readFileSync('path/to/your/cert.pem', 'utf8')
238+
}
239+
};
224240

225-
All options should be set with the syntax --option=value
241+
//
242+
// Create a standalone HTTPS proxy server
243+
//
244+
httpProxy.createServer(8000, 'localhost', options).listen(8001);
226245

227-
options:
228-
--port PORT Port that the proxy server should run on
229-
--target HOST:PORT Location of the server the proxy will target
230-
--config OUTFILE Location of the configuration file for the proxy server
231-
--silent Silence the log output from the proxy server
232-
-h, --help You're staring at it
246+
//
247+
// Create an instance of HttpProxy to use with another HTTPS server
248+
//
249+
var proxy = new httpProxy.HttpProxy();
250+
https.createServer(options.https, function (req, res) {
251+
proxy.proxyRequest(req, res, {
252+
host: 'localhost',
253+
port: 8000
254+
})
255+
}).listen(8002);
256+
257+
//
258+
// Create the target HTTPS server for both cases
259+
//
260+
http.createServer(function (req, res) {
261+
res.writeHead(200, { 'Content-Type': 'text/plain' });
262+
res.write('hello https\n');
263+
res.end();
264+
}).listen(8000);
233265
```
234266

235-
### Proxying over HTTPS
236-
You have all the full flexibility of node-http-proxy offers in HTTPS as well as HTTP. The two basic scenarios are: with a stand-alone proxy server or in conjunction with another HTTPS server.
267+
### Proxying to HTTPS from HTTPS
268+
Proxying from HTTPS to HTTPS is essentially the same as proxying from HTTPS to HTTP, but you must include `target` option in when calling `httpProxy.createServer` or instantiating a new instance of `HttpProxy`.
237269

238270
``` js
239271
var fs = require('fs'),
@@ -244,6 +276,9 @@ var options = {
244276
https: {
245277
key: fs.readFileSync('path/to/your/key.pem', 'utf8'),
246278
cert: fs.readFileSync('path/to/your/cert.pem', 'utf8')
279+
},
280+
target: {
281+
https: true // This could also be an Object with key and cert properties
247282
}
248283
};
249284

@@ -255,7 +290,12 @@ httpProxy.createServer(8000, 'localhost', options).listen(8001);
255290
//
256291
// Create an instance of HttpProxy to use with another HTTPS server
257292
//
258-
var proxy = new httpProxy.HttpProxy({ https: true });
293+
var proxy = new httpProxy.HttpProxy({
294+
target: {
295+
https: true
296+
}
297+
});
298+
259299
https.createServer(options.https, function (req, res) {
260300
proxy.proxyRequest(req, res, {
261301
host: 'localhost',
@@ -273,7 +313,7 @@ https.createServer(options.https, function (req, res) {
273313
}).listen(8000);
274314
```
275315

276-
### Proxying WebSockets
316+
## Proxying WebSockets
277317
Websockets are handled automatically when using the `httpProxy.createServer()`, but if you want to use it in conjunction with a stand-alone HTTP + WebSocket (such as [socket.io][5]) server here's how:
278318

279319
``` js
@@ -306,16 +346,40 @@ server.on('upgrade', function(req, socket, head) {
306346
});
307347
```
308348

349+
## Using node-http-proxy from the command line
350+
When you install this package with npm, a node-http-proxy binary will become available to you. Using this binary is easy with some simple options:
351+
352+
``` js
353+
usage: node-http-proxy [options]
354+
355+
All options should be set with the syntax --option=value
356+
357+
options:
358+
--port PORT Port that the proxy server should run on
359+
--target HOST:PORT Location of the server the proxy will target
360+
--config OUTFILE Location of the configuration file for the proxy server
361+
--silent Silence the log output from the proxy server
362+
-h, --help You're staring at it
363+
```
364+
309365
<br/>
310-
### Why doesn't node-http-proxy have more advanced features like x, y, or z?
366+
## Why doesn't node-http-proxy have more advanced features like x, y, or z?
311367

312368
If you have a suggestion for a feature currently not supported, feel free to open a [support issue][6]. node-http-proxy is designed to just proxy http requests from one server to another, but we will be soon releasing many other complimentary projects that can be used in conjunction with node-http-proxy.
313369

314370
## Run Tests
371+
The test suite is designed to fully cover the combinatoric possibilities of HTTP and HTTPS proxying:
372+
373+
1. HTTP --> HTTP
374+
2. HTTPS --> HTTP
375+
3. HTTPS --> HTTPS
376+
4. HTTP --> HTTPS
315377

316378
```
317379
vows test/*-test.js --spec
318380
vows test/*-test.js --spec --https
381+
vows test/*-test.js --spec --https --target=https
382+
vows test/*-test.js --spec --target=https
319383
```
320384
321385
<br/>

docs/balancing-proxy.html

Lines changed: 90 additions & 0 deletions
63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<!DOCTYPE html> <html> <head> <title>balancing-proxy.js</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <link rel="stylesheet" media="all" href="docco.css" /> </head> <body> <div id="container"> <div id="background"></div> <div id="jump_to"> Jump To &hellip; <div id="jump_wrapper"> <div id="jump_page"> <a class="source" href="balancing-proxy.html"> balancing-proxy.html </a> <a class="source" href="node-http-proxy.html"> node-http-proxy.html </a> <a class="source" href="proxy-table.html"> proxy-table.html </a> </div> </div> </div> <table cellpadding="0" cellspacing="0"> <thead> <tr> <th class="docs"> <h1> balancing-proxy.js </h1> </th> <th class="code"> </th> </tr> </thead> <tbody> <tr id="section-1"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-1">&#182;</a> </div> </td> <td class="code"> <div class="highlight"><pre><span class="cm">/*</span>
2+
<span class="cm"> balancing-proxy.js: Transparent Load-Balancing Optimized HTTP Proxy </span>
3+
4+
<span class="cm"> Copyright (c) 2011 Charlie Robbins </span>
5+
6+
<span class="cm"> Permission is hereby granted, free of charge, to any person obtaining</span>
7+
<span class="cm"> a copy of this software and associated documentation files (the</span>
8+
<span class="cm"> &quot;Software&quot;), to deal in the Software without restriction, including</span>
9+
<span class="cm"> without limitation the rights to use, copy, modify, merge, publish,</span>
10+
<span class="cm"> distribute, sublicense, and/or sell copies of the Software, and to</span>
11+
<span class="cm"> permit persons to whom the Software is furnished to do so, subject to</span>
12+
<span class="cm"> the following conditions:</span>
13+
14+
<span class="cm"> The above copyright notice and this permission notice shall be</span>
15+
<span class="cm"> included in all copies or substantial portions of the Software.</span>
16+
17+
<span class="cm"> THE SOFTWARE IS PROVIDED &quot;AS IS&quot;, WITHOUT WARRANTY OF ANY KIND,</span>
18+
<span class="cm"> EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF</span>
19+
<span class="cm"> MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND</span>
20+
<span class="cm"> NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE</span>
21+
<span class="cm"> LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION</span>
22+
<span class="cm"> OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION</span>
23+
<span class="cm"> WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.</span>
24+
25+
<span class="cm">*/</span>
26+
27+
<span class="kd">var</span> <span class="nx">net</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;net&#39;</span><span class="p">),</span>
28+
<span class="nx">HTTPParser</span> <span class="o">=</span> <span class="nx">process</span><span class="p">.</span><span class="nx">binding</span><span class="p">(</span><span class="s1">&#39;http_parser&#39;</span><span class="p">).</span><span class="nx">HTTPParser</span><span class="p">,</span>
29+
<span class="nx">streams</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;morestreams&#39;</span><span class="p">);</span>
30+
31+
<span class="nx">exports</span><span class="p">.</span><span class="nx">createServer</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">()</span> <span class="p">{</span>
32+
<span class="kd">var</span> <span class="nx">args</span> <span class="o">=</span> <span class="nb">Array</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">slice</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="nx">arguments</span><span class="p">),</span>
33+
<span class="nx">callback</span> <span class="o">=</span> <span class="k">typeof</span> <span class="nx">args</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">===</span> <span class="s1">&#39;function&#39;</span> <span class="o">&amp;&amp;</span> <span class="nx">args</span><span class="p">.</span><span class="nx">shift</span><span class="p">(),</span>
34+
<span class="nx">options</span> <span class="o">=</span> <span class="p">{},</span> <span class="nx">port</span><span class="p">,</span> <span class="nx">host</span><span class="p">,</span> <span class="nx">server</span><span class="p">;</span>
35+
36+
<span class="nx">server</span> <span class="o">=</span> <span class="nx">net</span><span class="p">.</span><span class="nx">createServer</span><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">socket</span><span class="p">)</span> <span class="p">{</span>
37+
<span class="kd">var</span> <span class="nx">buffer</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">streams</span><span class="p">.</span><span class="nx">BufferedStream</span><span class="p">(),</span>
38+
<span class="nx">parser</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">HTTPParser</span><span class="p">(</span><span class="s1">&#39;request&#39;</span><span class="p">);</span>
39+
40+
<span class="nx">parser</span><span class="p">.</span><span class="nx">onHeaderField</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">b</span><span class="p">,</span> <span class="nx">start</span><span class="p">,</span> <span class="nx">len</span><span class="p">)</span> <span class="p">{</span>
41+
<span class="kd">var</span> <span class="nx">slice</span> <span class="o">=</span> <span class="nx">b</span><span class="p">.</span><span class="nx">toString</span><span class="p">(</span><span class="s1">&#39;ascii&#39;</span><span class="p">,</span> <span class="nx">start</span><span class="p">,</span> <span class="nx">start</span> <span class="o">+</span> <span class="nx">len</span><span class="p">).</span><span class="nx">toLowerCase</span><span class="p">();</span>
42+
<span class="k">if</span> <span class="p">(</span><span class="nx">parser</span><span class="p">.</span><span class="nx">value</span> <span class="o">!=</span> <span class="kc">undefined</span><span class="p">)</span> <span class="p">{</span>
43+
<span class="nx">require</span><span class="p">(</span><span class="s1">&#39;eyes&#39;</span><span class="p">).</span><span class="nx">inspect</span><span class="p">(</span><span class="nx">parser</span><span class="p">.</span><span class="nx">value</span><span class="p">,</span> <span class="nx">parser</span><span class="p">.</span><span class="nx">field</span><span class="p">);</span>
44+
<span class="nx">parser</span><span class="p">.</span><span class="nx">field</span> <span class="o">=</span> <span class="kc">null</span><span class="p">;</span>
45+
<span class="nx">parser</span><span class="p">.</span><span class="nx">value</span> <span class="o">=</span> <span class="kc">null</span><span class="p">;</span>
46+
<span class="p">}</span>
47+
<span class="k">if</span> <span class="p">(</span><span class="nx">parser</span><span class="p">.</span><span class="nx">field</span><span class="p">)</span> <span class="p">{</span>
48+
<span class="nx">parser</span><span class="p">.</span><span class="nx">field</span> <span class="o">+=</span> <span class="nx">slice</span><span class="p">;</span>
49+
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
50+
<span class="nx">parser</span><span class="p">.</span><span class="nx">field</span> <span class="o">=</span> <span class="nx">slice</span><span class="p">;</span>
51+
<span class="p">}</span>
52+
<span class="p">};</span>
53+
54+
<span class="nx">parser</span><span class="p">.</span><span class="nx">onHeaderValue</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">b</span><span class="p">,</span> <span class="nx">start</span><span class="p">,</span> <span class="nx">len</span><span class="p">)</span> <span class="p">{</span>
55+
<span class="kd">var</span> <span class="nx">slice</span> <span class="o">=</span> <span class="nx">b</span><span class="p">.</span><span class="nx">toString</span><span class="p">(</span><span class="s1">&#39;ascii&#39;</span><span class="p">,</span> <span class="nx">start</span><span class="p">,</span> <span class="nx">start</span> <span class="o">+</span> <span class="nx">len</span><span class="p">);</span>
56+
<span class="k">if</span> <span class="p">(</span><span class="nx">parser</span><span class="p">.</span><span class="nx">value</span><span class="p">)</span> <span class="p">{</span>
57+
<span class="nx">parser</span><span class="p">.</span><span class="nx">value</span> <span class="o">+=</span> <span class="nx">slice</span><span class="p">;</span>
58+
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
59+
<span class="nx">parser</span><span class="p">.</span><span class="nx">value</span> <span class="o">=</span> <span class="nx">slice</span><span class="p">;</span>
60+
<span class="p">}</span>
61+
<span class="p">};</span>
62+
+
<span class="nx">parser</span><span class="p">.</span><span class="nx">socket</span> <span class="o">=</span> <span class="nx">socket</span><span class="p">;</span>
64+
65+
<span class="nx">socket</span><span class="p">.</span><span class="nx">ondata</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">d</span><span class="p">,</span> <span class="nx">start</span><span class="p">,</span> <span class="nx">end</span><span class="p">)</span> <span class="p">{</span>
66+
<span class="kd">var</span> <span class="nx">ret</span> <span class="o">=</span> <span class="nx">parser</span><span class="p">.</span><span class="nx">execute</span><span class="p">(</span><span class="nx">d</span><span class="p">,</span> <span class="nx">start</span><span class="p">,</span> <span class="nx">end</span> <span class="o">-</span> <span class="nx">start</span><span class="p">);</span>
67+
<span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">ret</span><span class="p">);</span>
68+
<span class="p">};</span>
69+
70+
<span class="nx">socket</span><span class="p">.</span><span class="nx">onend</span> <span class="o">=</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
71+
<span class="kd">var</span> <span class="nx">ret</span> <span class="o">=</span> <span class="nx">parser</span><span class="p">.</span><span class="nx">finish</span><span class="p">();</span>
72+
73+
<span class="k">if</span> <span class="p">(</span><span class="nx">ret</span> <span class="k">instanceof</span> <span class="nb">Error</span><span class="p">)</span> <span class="p">{</span>
74+
<span class="nx">socket</span><span class="p">.</span><span class="nx">destroy</span><span class="p">(</span><span class="nx">ret</span><span class="p">);</span>
75+
<span class="k">return</span><span class="p">;</span>
76+
<span class="p">}</span>
77+
78+
<span class="k">if</span> <span class="p">(</span><span class="nx">socket</span><span class="p">.</span><span class="nx">writable</span><span class="p">)</span> <span class="p">{</span>
79+
<span class="nx">socket</span><span class="p">.</span><span class="nx">end</span><span class="p">();</span>
80+
<span class="p">}</span>
81+
<span class="p">};</span>
82+
83+
<span class="nx">socket</span><span class="p">.</span><span class="nx">write</span><span class="p">(</span><span class="s1">&#39;hello world&#39;</span><span class="p">);</span>
84+
<span class="nx">socket</span><span class="p">.</span><span class="nx">end</span><span class="p">();</span>
85+
<span class="p">});</span>
86+
87+
<span class="k">return</span> <span class="nx">server</span><span class="p">;</span>
88+
<span class="p">};</span>
89+
90+
</pre></div> </td> </tr> </tbody> </table> </div> </body> </html>

0 commit comments

Comments
 (0)
0