Skip to content

Commit

Permalink
WUI: fix private ipv4 address detection for open server
Browse files Browse the repository at this point in the history
  • Loading branch information
kanreisa committed Aug 20, 2017
1 parent 47c9364 commit a86c40d
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 39 deletions.
41 changes: 20 additions & 21 deletions .gitignore
@@ -1,21 +1,20 @@
/config.json
/rules.json
/etc
/etc/*
/recorded
/recorded/*
/data
/data/*
/log
/log/*
/.nave
/.nave/*
/.epgdump
/.epgdump/*
/tmp
/tmp/*
/usr
/usr/*
/node
/node_modules/*
!/node_modules/chinachu-common
/config.json
/rules.json
/etc
/etc/*
/recorded
/recorded/*
/data
/data/*
/log
/log/*
/.nave
/.nave/*
/.epgdump
/.epgdump/*
/tmp
/tmp/*
/usr
/usr/*
/node
/node_modules/*
1 change: 1 addition & 0 deletions .npmrc
@@ -0,0 +1 @@
package-lock = false
61 changes: 43 additions & 18 deletions app-wui.js
Expand Up @@ -37,13 +37,13 @@ const vm = require('vm');
const os = require('os');
const zlib = require('zlib');
const events = require('events');
const dns = require('dns');
const http = require('http');
const https = require('https');
const auth = require('http-auth');
const socketio = require('socket.io');
const chinachu = require('chinachu-common');
const S = require('string');
const ip = require("ip");
const geoip = require('geoip-lite');
const UPnPServer = require('chinachu-upnp-server');
const mdns = require('mdns-js');
Expand Down Expand Up @@ -231,24 +231,49 @@ if (config.wuiPort) {
if (openServerEnabled) {
openServer = http.createServer(httpServer);
openServer.timeout = 0;
dns.lookup(os.hostname(), function (err, hostIp) {
openServer.listen(config.wuiOpenPort || 20772, config.wuiOpenHost || hostIp, function () {
util.log('HTTP Open Server Listening on ' + util.inspect(openServer.address()));
if (config.wuiMdnsAdvertisement === true) {
// Start mDNS advertisement
openServerMdns = mdns.createAdvertisement(mdns.tcp('_http'), config.wuiOpenPort || 20772, {
name: 'Chinachu Open Server on ' + os.hostname(),
host: os.hostname(),
txt: {
txtvers: '1',
'Version': 'gamma',
'Password': false
}
});
openServerMdns.start();
util.log('HTTP Open Server mDNS advertising started.');
}

let hostIp = config.wuiOpenHost;
if (!hostIp) {
const addresses = [];

const interfaces = os.networkInterfaces();
Object.keys(interfaces).forEach(k => {
interfaces[k]
.filter(a => {
return (
a.family === "IPv4" &&
a.internal === false &&
ip.isPrivate(a.address) === true
);
})
.forEach(a => addresses.push(a.address));
});

hostIp = addresses[0];

console.log("============================================================");
console.log("Detected Private IPv4:", addresses);
console.log("Selected Private IPv4 for Open Server:", addresses[0]);
console.log("NOTE: set `wuiOpenHost` to fix address for listen.");
console.log("============================================================");
}

openServer.listen(config.wuiOpenPort || 20772, hostIp, () => {
util.log('HTTP Open Server Listening on ' + util.inspect(openServer.address()));
if (config.wuiMdnsAdvertisement === true) {
// Start mDNS advertisement
openServerMdns = mdns.createAdvertisement(mdns.tcp('_http'), config.wuiOpenPort || 20772, {
name: 'Chinachu Open Server on ' + os.hostname(),
host: os.hostname(),
txt: {
txtvers: '1',
'Version': 'gamma',
'Password': false
}
});
openServerMdns.start();
util.log('HTTP Open Server mDNS advertising started.');
}
});
}

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -23,13 +23,15 @@
"url": "https://github.com/kanreisa/Chinachu/issues"
},
"dependencies": {
"chinachu-common": "file:common",
"chinachu-upnp-server": "^0.0.2",
"dateformat": "~1.0.6-1.2.3",
"diskusage": "^0.1.5",
"easy-table": "~0.2.0",
"geoip-lite": "^1.1.6",
"hoard": "~0.1.5",
"http-auth": "^2.2.8",
"ip": "^1.1.5",
"mdns-js": "^0.5.0",
"mirakurun": "^2.3.0",
"mkdirp": "~0.3.5",
Expand Down

0 comments on commit a86c40d

Please sign in to comment.