Add 'proxied' option for running the server behind a reverse proxy

This commit is contained in:
lmb 2014-10-20 10:16:54 +01:00
parent d1a24d5711
commit 054145343e
2 changed files with 18 additions and 1 deletions

View File

@ -55,6 +55,18 @@ var server = PeerServer({
}); });
``` ```
#### Running PeerServer behind a reverse proxy
Make sure to set the `proxied` option, otherwise IP based limiting will fail.
The option is passed verbatim to the
[expressjs `trust proxy` setting](http://expressjs.com/4x/api.html#app-settings)
if it is truthy.
```javascript
var PeerServer = require('peer').PeerServer;
var server = PeerServer({port: 9000, path: '/myapp', proxied: true});
```
### Combining with existing express app ### Combining with existing express app
```javascript ```javascript

View File

@ -15,7 +15,8 @@ function ExpressPeerServer(server, options) {
key: 'peerjs', key: 'peerjs',
ip_limit: 5000, ip_limit: 5000,
concurrent_limit: 5000, concurrent_limit: 5000,
allow_discovery: false allow_discovery: false,
proxied: false
}, options); }, options);
// Connected clients // Connected clients
@ -27,6 +28,10 @@ function ExpressPeerServer(server, options) {
// Mark concurrent users per ip // Mark concurrent users per ip
app._ips = {}; app._ips = {};
if (options.proxied) {
app.set('trust proxy', options.proxied);
}
app.on('mount', function() { app.on('mount', function() {
if (!server) { if (!server) {
throw new Error('Server is not passed to constructor - '+ throw new Error('Server is not passed to constructor - '+