From 62a7fb3c04a8eedbe8785aa73c81842ae44a8311 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20D=C3=BCrnberger?= Date: Sun, 19 Feb 2017 17:09:17 +0100 Subject: [PATCH 1/2] 1.0.3 - added automatic selection of iface for subscription server --- index.js | 27 +++++++++++++++++++++++++-- package.json | 4 ++-- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index cf25874..3ad0983 100644 --- a/index.js +++ b/index.js @@ -5,7 +5,7 @@ var et = require('elementtree'); var parseUrl = require('url').parse; var os = require('os'); var concat = require('concat-stream'); -var address = require('network-address'); +var ip = require('ip'); var debug = require('debug')('upnp-device-client'); var pkg = require('./package.json'); @@ -369,7 +369,9 @@ DeviceClient.prototype.ensureEventingServer = function(callback) { }); - this.server.listen(0, address.ipv4()); + // be sure that we are listening on the correct interface where the client resides + var iface = this.getIfaceForUrl(this.url); + this.server.listen(0, ip.address(iface)); } if(!this.listening) { @@ -393,6 +395,27 @@ DeviceClient.prototype.releaseEventingServer = function() { }; +DeviceClient.prototype.getIfaceForUrl = function(_url) { + var options = parseUrl(_url); + var interfaces = os.networkInterfaces(); + var retIface = ""; + + Object.keys(interfaces).map(function (nic) { + for (var i = 0; i < interfaces[nic].length; i++) + { + if(interfaces[nic][i].family.toLowerCase() == "ipv4") + { + var base1 = ip.mask(interfaces[nic][i].address, interfaces[nic][i].netmask); + var base2 = ip.mask(options.hostname, interfaces[nic][i].netmask); // TODO: maybe resolve IP here from hostname if hostname is not an ip + if (base1 == base2) + retIface = nic; + } + } + }); + return retIface; +}; + + function parseEvents(buf) { var events = []; var doc = et.parse(buf.toString()); diff --git a/package.json b/package.json index 531a321..b052c38 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "upnp-device-client", - "version": "1.0.2", + "version": "1.0.3", "description": "A simple and versatile UPnP device client", "author": "thibauts", "license": "MIT", @@ -9,7 +9,7 @@ "concat-stream": "^1.4.8", "debug": "^2.1.3", "elementtree": "~0.1.6", - "network-address": "^1.0.0" + "ip": "^1.1.4" }, "scripts": { "test": "echo \"Error: no test specified\" && exit 1" From d624f6a8acf389108669858f1fa26345f6ba2057 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20D=C3=BCrnberger?= Date: Mon, 20 Feb 2017 21:36:49 +0100 Subject: [PATCH 2/2] 1.0.3 added 200 OK response to the subscription listener --- index.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/index.js b/index.js index 3ad0983..3440f75 100644 --- a/index.js +++ b/index.js @@ -364,6 +364,9 @@ DeviceClient.prototype.ensureEventingServer = function(callback) { listener(e); }); }); + + // be sure we quit response by sending back a 200 OK, otherwise well developed UPNP Devices will kick us out of their subscription list + res.end(); }));