-
Notifications
You must be signed in to change notification settings - Fork 0
/
launcher.js
222 lines (215 loc) · 7.88 KB
/
launcher.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// launch browsers && take screenshots
var fs = require('fs');
var path = require('path');
var webdriverio = require('webdriverio');
var nodejsFsUtils = require('nodejs-fs-utils');
var extDir = path.join(__dirname, 'host-switch-plus');
var tmpDir = process.env.TMPDIR;
var encoding = { encoding: 'utf8' }
var defaultModelJs = fs.readFileSync(path.join(extDir, 'js', 'background.js'), encoding);
var fecha = require('fecha');
var cwd = process.cwd();
var yaml = require('js-yaml');
var request = require('superagent');
/**
* @description
{
"0": {
"ip": "100.80.21.71:9876",
"domain": "*.bbb.com",
"tags": "",
"note": "",
"status": 1,
"order": 1,
"uptime": "2017-06-19 16:59:54",
"id": 0
}
}
* @param {*} hosts
*/
function hostFormat(hosts) {
var _hosts = [], cnt = 0;
hosts.forEach(function(host, index) {
host = host.trim();
if (host.indexOf('#') === 0) return;
host = host.split(' ');
if (host.length < 2) return;
var domain = host[1].split(',');
domain.forEach(function(item) {
_hosts.push({
ip: host[0],
domain: item.trim(),
status: 1,
order: index,
id: cnt,
uptime: "2017-06-19 16:59:54",
tags: host.slice(2).join(' '),
note: ""
});
cnt++;
});
});
return _hosts;
}
var allTmpDirs = {};
function genDefaultOptions(options) {
var gid = 'hsp_' + (options.gid || fecha.format(Date.now(), 'YYYY-MM-DD_HH:mm:SS_s'));
var isMobile = options.isMobile;
var hosts = options.hosts;
var tmpExtDir = extDir;
var rewriteUrls = options.rewriteUrls;
// inject hosts
if ((hosts instanceof Array) && hosts.length || rewriteUrls) {
tmpExtDir = path.join(tmpDir, gid);
nodejsFsUtils.copySync(extDir, tmpExtDir, function(err) {
err && console.log(err);
});
var content = defaultModelJs.replace('/*__hosts__placeholder__*/', 'results = ' + JSON.stringify(hostFormat(hosts)) + ';').replace('/*__ruleDomains__placeholder__*/', 'ruleDomains = ' + JSON.stringify(rewriteUrls || {}) + ';');
fs.writeFileSync(path.join(tmpExtDir, 'js', 'background.js'), content, encoding);
allTmpDirs[tmpExtDir] = options.mode;
}
var desiredCapabilities = {
browserName: 'chrome',
chromeOptions: {
mobileEmulation: {
"deviceName": "iPhone 6"
},
args:['load-extension=' + tmpExtDir].concat(options._mode ? [] : ['window-size=375,800'])
}
};
if (!isMobile) {
delete desiredCapabilities.chromeOptions.mobileEmulation;
}
var config = {
hosts: hosts,
tmpExtDir: hosts && tmpExtDir,
browser: {
desiredCapabilities: desiredCapabilities
}
}
return config;
}
function render(screenshots) {
var html = '<p>Hosts 配置</p><textarea>' + screenshots.hosts + '</textarea>';
screenshots = screenshots.screenshots;
if (screenshots instanceof Array) {
screenshots.forEach(function(item) {
html += '<div class="screenshot-item">';
html += '<p><a target="_blank" href="' + item.url.url + '">' + item.url.url + '</a> ' + item.url.name + '</p>';
html += '<img src="data:image/png;base64,' + item.screenshot.value + '"/>'
html += '</div>';
}, this);
} else {
html = '<p class="warning">no screenshots taken</p>';
}
return html;
}
/**
* @description launch a browser & screenshot
* @return webdriverio browser instance
*/
exports.launch = function(options, url) {
options._mode = options.mode === 'browsing';
var config = genDefaultOptions(options);
var outProm = Promise.resolve();
var sessionInfo = path.resolve(cwd, '.auto-session.yaml');
if (options._mode) {
if (fs.existsSync(sessionInfo)) {
var sessionData = yaml.safeLoad(fs.readFileSync(sessionInfo, encoding));
url = url || 'about:blank';
if (sessionData.dev) {
// try to connect and reload
outProm = outProm.then(function() {
return new Promise(function(resolve) {
var wdPath = "http://127.0.0.1:4444/wd/hub/session/" + sessionData.dev;
request
.post(wdPath + "/execute")
.timeout({
response: 2000,
deadline: 2000
})
.send({ script: "new Image().src=\"//--auto-regression-testing.com\";location.reload()", args: [] }) // sends a JSON post body
.end(function(err){
// 删除
if (err) {
if (err.timeout) {
console.log('[WARNING] Since the initial tab has been close,you must exit the associated chrome session manually!')
} else {
request.delete(wdPath).end();
}
}
resolve(err ? null : 'share last create session');
});
})
})
}
}
}
outProm.then(function(shallReturn) {
if (shallReturn) return;
var prom = Promise.resolve();
var driver = webdriverio.remote(config.browser, 'promiseChain');
var browser = driver.init({}).pause(500); // what **ck must pause here??
var urls = options.urls;
// var mode = options.mode;
var response = {
hosts: config.hosts,
screenshots: []
};
if (options._mode) {
return browser.url(url).then(function(session) {
if (session && session.state === 'success') {
var sessionId = session.sessionId;
fs.writeFileSync(sessionInfo, 'dev: ' + sessionId, encoding);
} else {
return Promise.reject('start browser failed');
}
});
}
urls.forEach(function(url) {
prom = prom.then(function() {
return new Promise(function(resolve, reject) {
browser
.url(url.url)
.pause(1000)
.screenshot()
.then(function(base64) {
response.screenshots.push({
url: url,
screenshot: base64
});
resolve();
}, reject);
});
});
});
function clear() {
console.log('close browser');
if (config.tmpExtDir) {
nodejsFsUtils.rmdirsSync(config.tmpExtDir);
delete allTmpDirs[config.tmpExtDir];
}
}
prom = prom.then(function() {
clear();
return browser
.end()
.then(function() {
response.html = fs.readFileSync(path.join(__dirname, 'tpl', 'view.html'), encoding).replace('<!--html-->', render(response));
return response;
});
}, function(err) {
console.log('err', err);
browser.end();
clear();
});
})
return outProm;
}
process.on('exit', function() {
for (var tmp in allTmpDirs) {
if (fs.existsSync(tmp) && allTmpDirs[tmp] !== 'browsing') {
nodejsFsUtils.rmdirsSync(tmp);
}
}
});