Skip to content

Commit

Permalink
Merge pull request #184 from skypanther/TIMOB-18162
Browse files Browse the repository at this point in the history
[TIMOB-18162] Autodetect proxy configuration within ti setup
  • Loading branch information
sujmishra committed Feb 12, 2015
2 parents 57119df + a12d414 commit 75385da
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 7 deletions.
56 changes: 56 additions & 0 deletions lib/commands/lib/proxy_detect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
var exec = require('child_process').exec;

/**
* Looks for proxy settings in some common places like ENV vars, Mac's networksetup
* @param {Function} callback - Function to run when a value has been determined
*/
exports.detect = function(callback) {
if (process.platform === 'darwin') {
['Ethernet', 'Wi-Fi'].forEach(function(svc) {
// while this runs for both interfaces, only one will typically be active
exec("networksetup -getsecurewebproxy \"" + svc + "\"", function (err, stdout, stderr) {
// if both http & https are configured, the https proxy is preferentially returned
if (stdout.indexOf("Enabled: Yes") !== -1) {
callback(parseNetSetup(stdout));
}
});
exec("networksetup -getwebproxy \"" + svc + "\"", function (err, stdout, stderr) {
if (stdout.indexOf("Enabled: Yes") !== -1) {
callback(parseNetSetup(stdout));
}
});
});
} else {
if (process.env['https_proxy'] != undefined) {
// if both configured, https proxy is preferentially returned
callback(parseEnv(process.env['https_proxy']));
} else if (process.env['http_proxy'] != undefined) {
callback(parseEnv(process.env['http_proxy']));
}
}
callback(false);
};

function parseNetSetup(str) {
var m = str.replace(/\n/g, '').match(/Enabled: YesServer: ((?:http|https)+:\/\/.*)Port: (\d*)Authenticated Proxy Enabled: (\S*)/);
return {
valid: m !== null,
server: (m && m[1]) ? m[1] : '',
port: (m && m[2]) ? m[2] : '',
fullAddress: ((m && m[1]) ? m[1] : '') + ((m && m[2]) ? ':' + m[2] : ''),
authenticated: (m && m[3]) ? m[3] : ''
};
}
function parseEnv(env) {
var p = env.split(':');
// must account for proxies in the form http://user:pass@example.com:8080
if (p && p.length && p.length > 1) {
return {
valid: true,
server: p[0] + ':' + p[1],
port: (p.length > 2) ? p[2] : '',
fullAddress: p[0] + ':' + p[1] + ((p.length > 2) ? p[2] : ''),
authenticated: false
};
}
}
34 changes: 27 additions & 7 deletions lib/commands/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var appc = require('node-appc'),
fields = require('fields'),
fs = require('fs'),
path = require('path'),
proxyDetector = require('./lib/proxy_detect'),
request = require('request'),
temp = require('temp'),
url = require('url'),
Expand All @@ -28,7 +29,8 @@ var appc = require('node-appc'),
mixObj = appc.util.mixObj,
i18n = appc.i18n(__dirname),
__ = i18n.__,
__f = i18n.__f;
__f = i18n.__f,
proxy = [];

/** Setup command description. */
exports.desc = __('sets up the Titanium CLI');
Expand All @@ -45,6 +47,11 @@ exports.extendedDesc = __f('commands/setup');
*/
exports.config = function (logger, config, cli) {
fields.setup({ colors: cli.argv.colors });
proxyDetector.detect(function(prxy) {
// this may be async depending on platform
// so detecting done here during config
proxy.push(prxy);
});

return {
noAuth: true,
Expand Down Expand Up @@ -607,11 +614,11 @@ SetupScreens.prototype.check = function check(callback) {
outObj;
if (config.get('cli.httpProxyServer')) {
var proxyParts = config.get('cli.httpProxyServer').split(':');
if(proxyParts && proxyParts.length) {
if(proxyParts.length === 2) {
if (proxyParts && proxyParts.length) {
if (proxyParts.length === 2) {
javaArgs.push('-D' + proxyParts[0] + ".proxyHost=" + proxyParts[1].replace('\/\/', ''));
}
else if(proxyParts.length > 2) {
else if (proxyParts.length > 2) {
javaArgs.push('-D' + proxyParts[0] + ".proxyHost=" + proxyParts[1].replace('\/\/', ''));
javaArgs.push('-D' + proxyParts[0] + ".proxyPort=" + proxyParts[2]);
}
Expand All @@ -623,9 +630,9 @@ SetupScreens.prototype.check = function check(callback) {
// skip java tests
} else {
appc.subprocess.run(executable, javaArgs, function (code, out, err) {
if(err) {
if (err) {
r.javaResults.push('dashboard.appcelerator.com');
} else if(code && code !== '400') {
} else if (code && code !== '400') {
r.javaResults.push('dashboard.appcelerator.com');
}
});
Expand Down Expand Up @@ -1253,9 +1260,22 @@ SetupScreens.prototype.app = function app(callback) {
* @param {Function} callback - Function to be called when the prompting finishes
*/
SetupScreens.prototype.network = function app(callback) {
var defaultProxy = '';
if (this._config.get('cli.httpProxyServer')) {
defaultProxy = this._config.get('cli.httpProxyServer');
} else if (proxy.length > 0) {
var i = 0, len = proxy.length;
for(; i < len; i++) {
if (proxy[i] && proxy[i].valid) {
defaultProxy = proxy[i].fullAddress;
console.log(defaultProxy)
break;
}
}
}
var httpProxyServer = {
promptLabel: __('Proxy server URL'),
default: this._config.get('cli.httpProxyServer'),
default: defaultProxy,
validate: function (value) {
var u = url.parse(value);
if (!/^https?\:$/.test(u.protocol)) {
Expand Down

0 comments on commit 75385da

Please sign in to comment.