/
index.js
144 lines (128 loc) · 4.63 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/**
* @file open browser window
* @author zdying
*/
var os = require('os');
var path = require('path');
var configUtil = require('./configUtil');
var childProcess = require('child_process');
var platform = os.platform();
var browsers = {
'chrome': {
darwin: 'com.google.Chrome',
appName: 'Google Chrome',
win32: 'chrome.exe',
linux: 'google-chrome'
},
'firefox': {
darwin: 'org.mozilla.firefox',
appName: 'firefox',
win32: 'firefox.exe'
},
'opera': {
darwin: 'com.operasoftware.Opera',
appName: 'Opera',
win32: 'opera.exe'
},
'safari': {
darwin: 'com.apple.Safari',
appName: 'Safari',
win32: 'safari.exe'
},
};
/**
* Browser
* @namespace op-browser
*/
module.exports = {
/**
* open browser window, if the `pacFileURL` is not empty, will use `proxy auto-configuration`
* @memberof op-browser
* @param {String} browser the browser's name
* @param {String} url the url that to open
* @param {String} proxyURL the proxy url, format: `http://<hostname>[:[port]]`
* @param {String} pacFileURL the proxy url, format: `http://<hostname>[:[port]]/[pac-file-name]`
* @param {String} dataDir the user data directory
* @param {String} bypassList the list of hosts for whom we bypass proxy settings and use direct connections. the list of hosts for whom we bypass proxy settings and use direct connections, See: "[net/proxy/proxy_bypass_rules.h](https://cs.chromium.org/chromium/src/net/proxy_resolution/proxy_bypass_rules.h?sq=package:chromium&type=cs)" for the format of these rules
* @return {Promise}
*/
open: function (browser, url, proxyURL, pacFileURL, dataDir, bypassList) {
// Firefox pac set
// http://www.indexdata.com/connector-platform/enginedoc/proxy-auto.html
// http://kb.mozillazine.org/Network.proxy.autoconfig_url
// user_pref("network.proxy.autoconfig_url", "http://us2.indexdata.com:9005/id/cf.pac");
// user_pref("network.proxy.type", 2);
return this.detect(browser).then(function (browserPath) {
if (!browserPath) {
throw Error('[Error] can not find browser ' + browser);
} else {
dataDir = dataDir || path.join(os.tmpdir(), 'op-browser');
var commandOptions = configUtil[browser](dataDir, url, browserPath, proxyURL, pacFileURL, bypassList);
return new Promise(function (resolve, reject) {
childProcess.execFile(path.resolve(browserPath), commandOptions, {maxBuffer: 50000 * 1024}, function (err) {
if (err) {
reject(err);
} else {
resolve({
path: browserPath,
cmdOptions: commandOptions,
proxyURL: proxyURL,
pacFileURL: pacFileURL
});
}
});
});
}
});
},
/**
* detect browser, return the browser's path
* @memberof op-browser
* @param {String} name the browser name
* @return {Promise}
*/
detect: function (name) {
var result = '';
var cmd = '';
var info = browsers[name];
return new Promise(function (resolve, reject) {
if (!info) {
reject(Error('Browser not supported.'));
}
try {
switch (platform) {
case 'darwin':
cmd = 'mdfind "kMDItemCFBundleIdentifier==' + info.darwin + '" | head -1';
result = childProcess.execSync(cmd).toString().trim();
result += '/Contents/MacOS/' + info.appName;
result = result.replace(/\s/g, '\\ ');
break;
case 'win32':
// windows chrome path:
// HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe
// reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe" /v Path
cmd = 'reg query "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\' + info.win32 + '" /ve';
// result:
/*
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\firefox.exe
(默认) REG_SZ C:\Program Files\Mozilla Firefox\firefox.exe
*/
result = childProcess.execSync(cmd).toString().trim();
result = result.split('\n').pop().split(/\s+REG_SZ\s+/).pop();
result = result.replace(/^"|"$/g, '');
break;
case 'linux':
if(info.linux){
result = path.join('/usr/bin',info.linux)
}
break;
default:
result = name;
}
} catch (e) {
reject(Error('Can not find browser info', name));
}
resolve(result.trim());
});
}
};