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

Add proxy support #95

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
0.12.2 - 2018-07-12

- Update npm dependencies, including request, to fix yet another vulnerablility
- Add option to pass proxy configuration to websocket

0.12.1 — 2016-09-15

- Update npm dependencies, including request, to fix vulnerability (#89)
Expand Down
7 changes: 6 additions & 1 deletion README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,12 @@ var ddpclient = new DDPClient({
useSockJs: true,
// Use a full url instead of a set of `host`, `port` and `ssl`
// do not set `useSockJs` option if `url` is used
url: 'wss://example.com/websocket'
url: 'wss://example.com/websocket',
// The contents of `proxyOpts` are passed to the underlying
// Websocket client.
proxyOpts: {
origin: 'http://username:[email protected]'
}
});

/*
Expand Down
17 changes: 13 additions & 4 deletions examples/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

var DDPClient = require("../lib/ddp-client");

var ddpclient = new DDPClient({
var ddpclientOpts = {
// All properties optional, defaults shown
host : "localhost",
host : process.env.host || "localhost",
port : 3000,
ssl : false,
autoReconnect : true,
Expand All @@ -20,7 +20,16 @@ var ddpclient = new DDPClient({
// Use a full url instead of a set of `host`, `port` and `ssl`
// do not set `useSockJs` option if `url` is used
url: 'wss://example.com/websocket'
});
};

if (process.env.proxy_origin) {
console.log("Using proxy:", process.env.proxy_origin);
ddpclientOpts.proxyOpts = {
origin: process.env.proxy_origin
}
}

var ddpclient = new DDPClient(ddpclientOpts);

/*
* Connect to the Meteor Server
Expand All @@ -29,7 +38,7 @@ ddpclient.connect(function(error, wasReconnect) {
// If autoReconnect is true, this callback will be invoked each time
// a server connection is re-established
if (error) {
console.log("DDP connection error!");
console.log("DDP connection error!", error);
return;
}

Expand Down
12 changes: 10 additions & 2 deletions lib/ddp-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ var DDPClient = function(opts) {
self.port = opts.port || 3000;
self.path = opts.path;
self.ssl = opts.ssl || self.port === 443;
self.tlsOpts = opts.tlsOpts || {};
self.tlsOpts = opts.tlsOpts;
self.proxyOpts = opts.proxyOpts;
self.useSockJs = opts.useSockJs || false;
self.autoReconnect = ("autoReconnect" in opts) ? opts.autoReconnect : true;
self.autoReconnectTimer = ("autoReconnectTimer" in opts) ? opts.autoReconnectTimer : 500;
Expand Down Expand Up @@ -411,7 +412,14 @@ DDPClient.prototype._buildWsUrl = function(path) {

DDPClient.prototype._makeWebSocketConnection = function(url) {
var self = this;
self.socket = new WebSocket.Client(url, null, self.tlsOpts);
var options = {};
if (self.tlsOpts) {
options.tls = self.tlsOpts;
}
if (self.proxyOpts) {
options.proxy = self.proxyOpts;
}
self.socket = new WebSocket.Client(url, null, options);
self._prepareHandlers();
};

Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@
"url": "https://github.com/oortcloud/node-ddp-client.git"
},
"dependencies": {
"ddp-underscore-patched": "0.8.1-2",
"ddp-ejson": "0.8.1-3",
"ddp-underscore-patched": "0.8.1-2",
"faye-websocket": "0.11.0",
"request": "2.74.x"
"request": "^2.87.0"
},
"devDependencies": {
"mocha": "~3.0.2",
"mocha": "^5.2.0",
"sinon": "~1.17.5",
"rewire": "~2.5.2"
},
Expand Down
25 changes: 23 additions & 2 deletions test/ddp-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,30 @@ describe("Connect to remote server", function() {
it('should propagate tls options if specified', function() {
var tlsOpts = {
'ca': ['fake_pem_content']
}
};
new DDPClient({'host': 'myserver.com', 'port': 443, 'tlsOpts': tlsOpts}).connect();
assert.deepEqual(wsConstructor.args, [['wss://myserver.com:443/websocket', null, tlsOpts]]);
assert.deepEqual(wsConstructor.args, [['wss://myserver.com:443/websocket', null, { tls: tlsOpts }]]);
});

it('should propagate proxy options if specified', function() {
var proxyOpts = {
origin: 'http://username:[email protected]',
headers: {'User-Agent': 'node'}
};
new DDPClient({'host': 'myserver.com', 'port': 443, 'proxyOpts': proxyOpts}).connect();
assert.deepEqual(wsConstructor.args, [['wss://myserver.com:443/websocket', null, { proxy: proxyOpts }]]);
});

it('should propagate both tls and proxy options if specified', function() {
var tlsOpts = {
'ca': ['fake_pem_content']
};
var proxyOpts = {
origin: 'http://username:[email protected]',
headers: {'User-Agent': 'node'}
};
new DDPClient({'host': 'myserver.com', 'port': 443, 'tlsOpts': tlsOpts, 'proxyOpts': proxyOpts}).connect();
assert.deepEqual(wsConstructor.args, [['wss://myserver.com:443/websocket', null, { tls: tlsOpts, proxy: proxyOpts }]]);
});

it('should connect to the provided url', function() {
Expand Down