Skip to content

Commit

Permalink
fix the way of get the local ip
Browse files Browse the repository at this point in the history
  • Loading branch information
52u committed Mar 4, 2015
1 parent 012c353 commit c81f40b
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 19 deletions.
59 changes: 59 additions & 0 deletions lib/ip-util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
var os = require('os');
var child_proc = require('child_process');

exports.getLocalIP = function (callback) {
var getIPApp = undefined;
var matches = [];
var pmHosts = [];
var filterRE = undefined;
var pingResult = null;
var pmHost = null;
var IPv4, hostName, i;
hostName = os.hostname();
if ('win32' == os.platform()) {
getIPApp = child_proc.spawn("ipconfig", null);
// only get the IPv4 address
filterRE = /\b(IPv4|IP\s)[^:\r\r\n]+:\s+([^\s]+)/g;
getIPApp.on('exit', function (code, signal) {
matches = pingResult.match(filterRE) || [];
for (var i = 0; i < matches.length; i++) {
var host = matches[i].split(':')[1];

// trim the spaces in the string's start/end position.
host = host.replace(/(^[\s]*)|([\s]*$)/g, "");
pmHosts.push(host);
}

if (pmHosts.length > 0)
pmHost = pmHosts[0];
// do other things
callback(pmHost);

});

getIPApp.stdout.on('data', function (data) {
// get ping result.
pingResult = pingResult + data.toString();
});
}
else {
if (os.networkInterfaces().en0 && os.networkInterfaces().en0.length) {
for (i = 0; i < os.networkInterfaces().en0.length; i++) {
if (os.networkInterfaces().en0[i].family == 'IPv4') {
IPv4 = os.networkInterfaces().en0[i].address;
}
}

} else if (os.networkInterfaces().eth0 && os.networkInterfaces().eth0.length) {
for (i = 0; i < os.networkInterfaces().eth0.length; i++) {
if (os.networkInterfaces().eth0[i].family == 'IPv4') {
IPv4 = os.networkInterfaces().eth0[i].address;
}
}
}
callback(IPv4);
}
};



40 changes: 21 additions & 19 deletions routes/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var jsonWebToken = require('jsonwebtoken');
var unless = require('express-unless');
var request = require('request');
var git_util = require('../lib/git-util');
var ip_util = require('../lib/ip-util');
var config = require('../config');
var db = require('../db/index');
var packagejson = require('../package');
Expand Down Expand Up @@ -79,25 +80,26 @@ exports.route('/login').post(function (req, res) {
});

var upgrade = function (params, req, res) {
request.post({
url: config.upgradeUrl,
form: {domain: params.domain, name: params.name, version: params.version}
}, function (err, httpResponse, body) {
if (err) {
res.send({
error: 'get remote version Failed!'
});
return;
}
var json = JSON.parse(body);
var script;
if (json && 200 === json.flag) {
script = "<script>$('#checkDiv').show();</script>";
return res.json(script);
}
script = "<script>console.log('" + json.message + "');</script>";
res.json(script);

ip_util.getLocalIP(function (ip) {
request.post({
url: config.upgradeUrl,
form: {domain: params.domain, name: params.name, version: params.version, ip: ip}
}, function (err, httpResponse, body) {
if (err) {
res.send({
error: 'get remote version Failed!'
});
return;
}
var json = JSON.parse(body);
var script;
if (json && 200 === json.flag) {
script = "<script>$('#checkDiv').show();</script>";
return res.json(script);
}
script = "<script>console.log('" + json.message + "');</script>";
res.json(script);
});
});
};

Expand Down

0 comments on commit c81f40b

Please sign in to comment.