generatePacScript = function() {
var domain, i, lines, mode, node, p, part, parts, proxyString, proxyStrings, reversedDomainTree, source, url, _i, _j, _k, _l, _len, _len1, _len2, _ref, _ref1, _ref2, _ref3;
mode = $rootScope.mode;
if (mode !== MODES.AUTO && mode !== MODES.ALWAYS) {
mode = MODES.AUTO
}
proxyStrings = [];
_ref = ($rootScope.proxies || []).slice(0, 2);
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
p = _ref[_i];
proxyStrings.push("" + p.scheme + " " + p.host + ":" + p.port)
}
proxyString = proxyStrings.join(';');
lines = [];
lines.push(['function Find', 'roxyForURL(url, host) {\n'].join('P'));
lines.push("var D = \"DIRECT\";");
lines.push("var p='" + proxyString + "';\n");
lines.push("if (shExpMatch(host, '10.[0-9]+.[0-9]+.[0-9]+')) return D;");
lines.push("if (shExpMatch(host, '172.[0-9]+.[0-9]+.[0-9]+')) return D;");
lines.push("if (shExpMatch(host, '192.168.[0-9]+.[0-9]+')) return D;");
_ref1 = $rootScope.urlRules || {};
for (url in _ref1) {
p = _ref1[url];
lines.push("if (url == '" + url + "') return '" + p.scheme + " " + p.host + ":" + p.port + "';")
}
lines.push("if (url.indexOf('https://www.google.com/complete/search?client=chrome-omni') == 0)");
lines.push("\treturn D;");
lines.push("if (url.indexOf('http://clients1.google.com/generate_204') == 0)");
lines.push("\treturn D;");
lines.push("if (url.indexOf('http://chart.apis.google.com/') == 0)");
lines.push("\treturn D;");
lines.push("if (url.indexOf('http://toolbarqueries.google.com') == 0)");
lines.push("\treturn D;\n");
lines.push("var i = url.indexOf('_HXPROXY=');");
lines.push("if (i >= 0) return url.substr(i+9).replace('+', ' ');\n");
for (_j = 0, _len1 = WHITE_LIST_DOMAINS.length; _j < _len1; _j++) {
domain = WHITE_LIST_DOMAINS[_j];
lines.push("if (dnsDomainIs(host, '" + domain + "')) return D;")
}
lines.push("\n");
if (mode === MODES.AUTO) {
reversedDomainTree = {};
_ref2 = userDomains.names();
for (_k = 0, _len2 = _ref2.length; _k < _len2; _k++) {
domain = _ref2[_k];
node = reversedDomainTree;
parts = domain.toLowerCase().split('.').reverse();
for (i = _l = 0, _ref3 = parts.length - 1; 0 <= _ref3 ? _l <= _ref3 : _l >= _ref3; i = 0 <= _ref3 ? ++_l : --_l) {
part = parts[i];
if (i === parts.length - 1) {
node[part] = 1
} else {
if (node[part] === 1) {
break
}
if (node[part] == null) {
node[part] = {}
}
node = node[part]
}
}
}
lines.push("var node = " + (JSON.stringify(reversedDomainTree)) + ";");
lines.push("var hostParts = host.toLowerCase().split('.');");
lines.push("for (var i=hostParts.length - 1; i >= 0; i --) {");
lines.push(" var part = hostParts[i];");
lines.push(" node = node[part];");
lines.push(" if (node == undefined || node == 1) break;");
lines.push("}");
lines.push("if (node == 1)");
lines.push(" return p;\n")
} else {
lines.push('return p;')
}
lines.push('return D;');
lines.push("}");
source = lines.join('\n');
localStorage.setItem("pac", source); //把PAC脚本保存到Local Storage
return source
}