diff --git a/lib/ip-util.js b/lib/ip-util.js new file mode 100644 index 0000000..9f830db --- /dev/null +++ b/lib/ip-util.js @@ -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); + } +}; + + + diff --git a/routes/admin.js b/routes/admin.js index 2b94b02..e508372 100644 --- a/routes/admin.js +++ b/routes/admin.js @@ -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'); @@ -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 = ""; - return res.json(script); - } - 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 = ""; + return res.json(script); + } + script = ""; + res.json(script); + }); }); };