-
Notifications
You must be signed in to change notification settings - Fork 7
/
PhantomServer.js
670 lines (610 loc) · 21.8 KB
/
PhantomServer.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
/**
* Created by xiyuan_fengyu on 2017/2/16.
*/
var webpage = require('webpage');
var system = require('system');
var fs = require('fs');
var tag_req_crawl = "/crawl?";
var tag_return = "{\"return\":";
var tag_screenshot = "{\"screenshot\":";
var tag_newImgStartLoad = "{\"newImgStartLoad\":";
var tag_newImgLoadFinish = "{\"newImgLoadFinish\":";
var tag_download = "{\"download\":";
var tag_error = "{\"error\":";
var flag_server_start = "server.start";
var jspiderHome = system.args[2];
var srcPath = system.args[3];
fs.changeWorkingDirectory(jspiderHome);
phantom.libraryPath = srcPath;
//添加配置
try {
var configStr = fs.read("config/phantom.json");
var config = JSON.parse(configStr);
config.page = config.page || {};
//是否加载图片的配置
if (!config.page.loadImages) {
config.page.abortImgRequest = true;
config.page.loadImages = true;
}
//浏览器窗口大小配置
if (config.page.viewportSize) {
config.page.viewportSize.width = config.page.viewportSize.width || 1920;
config.page.viewportSize.height = config.page.viewportSize.height || 1080;
}
else {
config.page.viewportSize = {
width: 1920,
height: 1080
};
}
phantom.pageSettings = config.page || {};
phantom.logSettings = config.log || {};
var cookies = config.cookies;
for (var i = 0, len = cookies.length; i < len; i++) {
addCookie(cookies[i]);
}
}
catch (e) {}
var server = require('webserver').create();
var port = parseInt(system.args[1]);
server.listen(port, function(req, res){
var reqUrl = req.url;
var isBadRequest = true;
var matcher;
if (reqUrl.startsWith(tag_req_crawl)) {
matcher = reqUrl.match(/^\/crawl\?js=(.*?)&timeout=(.*?)&url=(.*?)$/);
if (matcher) {
isBadRequest = false;
crawl(decodeURI(matcher[1]), decodeURI(matcher[2]), decodeURI(matcher[3]), res);
}
}
if (isBadRequest) {
response(res, {
error: "BadRequest",
url: reqUrl
});
}
});
function checkTime() {
var time = parseInt(fs.read("time"));
if (new Date().getTime() - time >= 3000) {
// Worker 已停止,phantomjs自动停止
phantom.exit(0);
}
else {
setTimeout(function () {
checkTime();
}, 2000);
}
}
sendFlag(flag_server_start);
checkTime();
function prepareJs(curPage, params) {
return curPage.evaluate(function (userDatas) {
//植入常用代码
//用户数据
window.userDatas = userDatas;
//资源请求成功的记录,用于查看bodySize
window.resources = [];
//发送消息给phantom, msg 必须是字符串
window.sendMsgToPhantom = function (msg) {
var xhr = new XMLHttpRequest();
xhr.open("POST", "http://localhost/sendMsgToPhantom", true);
xhr.send(msg);
};
//获取所有满足 条件 的链接,条件可以是一个函数或则正则表达式
window.links = function (condition) {
var arr = [];
var map = {};
jQuery("a").each(function () {
var href = this.href;
if (map[href] == null) {
if (condition == null
|| (typeof condition == "string" && href.match(condition))
|| (typeof condition == "function" && condition(this))) {
map[href] = true;
}
}
});
for (var key in map) {
arr.push(key);
}
return arr;
};
//截图
window.screenshot = function (picName, selectorOrObj, quality) {
var dom = null;
if (selectorOrObj) {
if (typeof selectorOrObj == "string") {
dom = document.querySelector(selectorOrObj);
}
else if (selectorOrObj.length) {
dom = selectorOrObj[0];
}
else {
dom = selectorOrObj;
}
}
var imgSrc = dom ? dom.src : null;
if (imgSrc) {
var newImg = document.createElement("img");
newImg.id = "screenshot_" + new Date().getTime() + "_" + parseInt(Math.random() * 10000);
newImg.src = imgSrc;
newImg.style.position = "fixed";
newImg.style.left = 0;
newImg.style.top = 0;
newImg.style.width = "auto";
newImg.style.zIndex = 0xffffff;
newImg.style.display = "none";
newImg.onload = function () {
sendMsgToPhantom(JSON.stringify({
newImgLoadFinish: newImg.src,
page: location.href,
status: true
}));
sendMsgToPhantom(JSON.stringify({
screenshot: picName || "screenshot.jpeg",
quality: quality || 100,
selector: "#" + newImg.id,
rect: {
left: 0,
top: 0,
width: newImg.width,
height: newImg.height
}
}));
};
newImg.onerror = function () {
document.body.removeChild(newImg);
sendMsgToPhantom(JSON.stringify({
newImgLoadFinish: newImg.src,
page: location.href,
status: false
}));
};
document.body.appendChild(newImg);
sendMsgToPhantom(JSON.stringify({
newImgStartLoad: newImg.src,
page: location.href
}));
}
else {
if (dom == null) {
dom = document.body;
}
sendMsgToPhantom(JSON.stringify({
screenshot: picName || "screenshot.jpeg",
quality: quality || 100,
selector: null,
rect: dom.getBoundingClientRect()
}));
}
};
//下载
window.download = function (url, savePath) {
url += (url.match("\\?") ? "&" : "?") + "fileDownload=true";
sendMsgToPhantom(JSON.stringify({
download: url,
savePath: savePath
}));
};
//返回结果
window.sendResult = function(obj) {
sendMsgToPhantom(JSON.stringify({
return: obj
}));
};
//返回错误
window.sendError = function(error, msg) {
sendMsgToPhantom(JSON.stringify({
error: error,
msg: msg,
url: location.href
}));
};
//等待某个条件达成后执行回调函数
window.waitFor = function(condition, callback, delay, timeout, startTime) {
//默认延迟10毫秒
delay = delay || 10;
//默认超时时间30秒, -1为永不超时
timeout = timeout || 30000;
startTime = startTime || new Date().getTime();
setTimeout(function () {
if (condition == true || (typeof condition == "function" && condition() == true)) {
if (typeof callback == "function") {
callback(true);
}
}
else if (timeout == -1 || new Date().getTime() - startTime < timeout) {
waitFor(condition, callback, delay, timeout, startTime);
}
else {
callback(false);
}
}, delay);
};
//加载js
window.loadScript = function() {
//检查参数是否正确
var len = arguments.length;
var callback = arguments[0];
if (len != 2 || typeof callback != "function") {
throw "at least 2 arguments for loadScript required, and the first one must be a callback function"
}
var progress = {};
var onScriptLoad = function (src) {
progress[src] = true;
for (var key in progress) {
if (progress[key] == false) {
return;
}
}
callback();
};
var scripts = [];
var jsSrcAndCheckers = arguments[1];
for (var src in jsSrcAndCheckers) {
var checker = jsSrcAndCheckers[src];
if (checker == true || (typeof checker == "function" && checker())) {
var script = document.createElement("script");
script.type = "text/javascript";
if (script.readyState) {
script.onreadystatechange = function () {
if (this.readyState == "loaded" || this.readyState == "complete") {
this.onreadystatechange = null;
onScriptLoad(this.src);
}
};
} else
{
script.onload = function () {
onScriptLoad(this.src);
};
}
script.src = src;
progress[src] = false;
scripts.push(script);
}
}
if (scripts.length > 0) {
//必须要等待网页就绪后,才能插入元素
if (document.body) {
for (var i = 0, sLen = scripts.length; i < sLen; i++) {
document.body.appendChild(scripts[i]);
}
}
else {
window.onload = function () {
for (var i = 0, sLen = scripts.length; i < sLen; i++) {
document.body.appendChild(scripts[i]);
}
};
}
}
else {
callback();
}
};
//加载jQuery
window.loadJQuery = function (callback, src) {
src = src || "http://cdn.bootcss.com/jquery/3.1.1/jquery.min.js";
var srcAndChecker = {};
srcAndChecker[src] = function() {
return typeof jQuery == "undefined";
};
loadScript(function () {
window.$ = jQuery;
callback();
}, srcAndChecker);
};
}, params.userDatas);
}
function onPageLoadFinished(page, params, status) {
params.status[page.targetUrl] = [status, new Date().getTime() - page.startLoadTime];
if (status != "success") {
crawlResponse({
error: "OpenFailed",
msg: "fail to load this page",
url: page.targetUrl,
original: params.original != page.targetUrl ? params.original : null
}, params);
}
else if (params.js) {
prepareJs(page, params);
page.injectJs(phantom.libraryPath + "/" + params.js);
page.onUrlChanged = function(targetUrl) {
prepareJs(page, params);
page.injectJs(phantom.libraryPath + "/" + params.js);
};
page.onPageCreated = function(newPage) {
if (phantom.pageSettings) {
newPage.settings = phantom.pageSettings;
}
newPage.onLoadFinished = function(status1) {
params.pages.push(newPage);
newPage.targetUrl = newPage.url;
onPageLoadFinished(newPage, params, status1);
};
setPageListener(newPage, params);
};
}
}
function setPageListener(page, params) {
//由于有些网站重写了console,导致console.log失效,所以injectJs通过发送请求和拦截请求的方式向phatntomjs传递消息
//阻止图片请求(根据url和img元素的src来匹配,所以并不能保证所有的图片请求被拦截)
page.onResourceRequested = function(requestData, networkRequest) {
if (phantom.pageSettings && phantom.pageSettings.abortImgRequest) {
//图片请求过滤
var lowerCaseUrl = requestData.url.toLowerCase();
var match = lowerCaseUrl.match("\\.(jpg|jpeg|png|bmp|gif|ico)");
if (lowerCaseUrl.indexOf("filedownload=true") > -1) {
return;
}
else if (match != null) {
networkRequest.abort();
}
else if (lowerCaseUrl.match("\\.(js|css|json)")) {
//
}
else {
var accept = "";
for (var i = 0, len = requestData.headers.length; i < len; i++) {
var item = requestData.headers[i];
if (item.name == "Accept") {
accept = item.value;
break;
}
}
if (accept.indexOf("image/") > -1) {
networkRequest.abort();
}
}
}
if (requestData.url.startsWith("http://localhost/sendMsgToPhantom")) {
page.onConsoleMessage(requestData.postData);
networkRequest.abort();
}
else {
if (phantom.logSettings.onResourceRequested) {
console.log("\nrequest: " + JSON.stringify(networkRequest) + "\n" + JSON.stringify(requestData) + "\n");
}
}
};
page.onResourceReceived = function(response) {
if (phantom.logSettings.onResourceReceived) {
console.log("\nresponse: " + JSON.stringify(response) + "\n");
}
page.evaluate(function (response) {
window.resources = window.resources || [];
window.resources.push(response);
}, response);
};
page.onConsoleMessage = function(msg) {
println(msg.substring(0, Math.min(500, msg.length)));
if (msg.substring(0, tag_return.length) == tag_return || msg.substring(0, tag_error.length) == tag_error) {
crawlResponse(msg, params);
}
else if (msg.substring(0, tag_screenshot.length) == tag_screenshot) {
var json = JSON.parse(msg);
tryScreenshot(page, json);
}
else if (msg.substring(0, tag_newImgStartLoad.length) == tag_newImgStartLoad) {
page.imgLoadingNum = (page.imgLoadingNum || 0) + 1;
}
else if (msg.substring(0, tag_newImgLoadFinish.length) == tag_newImgLoadFinish) {
page.imgLoadingNum = (page.imgLoadingNum || 0) - 1;
}
else if (msg.substring(0, tag_download.length) == tag_download) {
var json = JSON.parse(msg);
var response = page.evaluate(function(url) {
var xhr = new XMLHttpRequest();
xhr.overrideMimeType('text/plain; charset=x-user-defined');
xhr.open("GET", url, false);
xhr.send();
var byteStr = "";
for (var i = 0, len = xhr.response.length; i < len; ++i) {
var c = xhr.response.charCodeAt(i);
byteStr += String.fromCharCode(c & 0xff);
}
return byteStr;
}, json.download);
fs.write("download/" + json.savePath, response, "b");
}
};
page.onError = function(msg, trace) {
if (trace && trace.length > 0 ) {
var msgStack = [
"\nError in page: " + page.url,
msg
];
trace.forEach(function(t) {
msgStack.push(t.file + ': ' + t.line + (t.function ? ' (in function "' + t.function +'")' : ''));
});
console.error(msgStack.join('\n'));
//只有用户本地js引发的异常,文件地址如:phantomjs://code/BilibiliComment.js
if (trace[0].file.startsWith("phantomjs://code/")) {
crawlResponse({
error: "JsException",
msg: msg,
url: page.url
}, params);
}
}
};
page.onAlert = function(msg) {
console.log("alert: " + msg);
};
}
function tryScreenshot(page, json, fromQueue) {
page.screenshotTasks = page.screenshotTasks || [];
if (fromQueue != true && page.screenshotTasks.length > 0) {
page.screenshotTasks.push(json);
}
else {
if (json.selector && json.rect) {
page.clipRect = json.rect;
var found = page.evaluate(function (domSelector) {
var dom = document.querySelector(domSelector);
if (dom) {
dom.style.display = "block";
return true;
}
}, json.selector);
if (found) {
page.render("screenshot/" + json.screenshot, {quality: json.quality});
page.evaluate(function (domSelector) {
var dom = document.querySelector(domSelector);
if (dom) {
dom.remove();
}
}, json.selector);
}
}
else {
page.clipRect = json.rect;
page.render("screenshot/" + json.screenshot, {quality: json.quality});
}
setTimeout(function () {
try {
if (page.screenshotTasks.length > 0) {
var nextTask = page.screenshotTasks[0];
page.screenshotTasks = page.screenshotTasks.slice(1);
tryScreenshot(page, nextTask, true);
}
}
catch (e) {}
}, 50);
}
}
function crawl(js, timeout, url, res) {
//解析用户的额外数据
var splitArr = url.split("userDatas=");
var userDatas = {};
if (splitArr.length > 1) {
var userDatasStr = splitArr[1];
userDatasStr = decodeURIComponent(userDatasStr);
try {
userDatas = JSON.parse(userDatasStr);
}
catch (e) {
}
if (userDatas.proxyIp && userDatas.proxyPort) {
phantom.setProxy(userDatas.proxyIp, userDatas.proxyPort);
}
//清除url中的userDatas部分
url = splitArr[0].substring(0, splitArr[0].length - 1);
}
var page = webpage.create();
var params = {
taskId: new Date().getTime() + "_" + parseInt(Math.random() * 10000),
changeIndex: -1,
original: url,
js: js,
res: res,
globalData: {},
status: {},
userDatas: userDatas,
pages: [page],
timeover: false
};
if (phantom.pageSettings) {
page.settings = phantom.pageSettings;
page.viewportSize = phantom.pageSettings.viewportSize;
}
page.startLoadTime = new Date().getTime();
page.targetUrl = url;
page.open(url, function (status) {
onPageLoadFinished(page, params, status);
});
setPageListener(page, params);
//任务超时
try {
timeout = parseInt(timeout) * 1000
}
catch (e) {
timeout = 30000
}
setTimeout(function () {
if (params.isResClosed != true) {
params.timeover = true;
crawlResponse({
error: "TaskTimeout",
msg: "task timeout!",
url: url
}, params);
}
}, timeout);
}
function crawlResponse(result, params) {
if (params.isResClosed != true) {
//把status信息嵌入到result中
if (typeof result != "string") {
result.status = params.status;
}
else {
result = result.substring(0, result.length - 1) + ",\"status\":" + JSON.stringify(params.status) + "}";
}
waitForScreenshot(result, params);
}
}
function waitForScreenshot(result, params) {
var finish = true;
if (!params.timeover) {
for (var i = 0, len = params.pages.length; i < len; i++) {
var page = params.pages[i];
if ((page.imgLoadingNum != null && page.imgLoadingNum > 0) || (page.screenshotTasks && page.screenshotTasks.length > 0)) {
finish = false;
break;
}
}
}
if (finish) {
response(params.res, result);
for (var i = 0, len = params.pages.length; i < len; i++) {
params.pages[i].close();
}
params.isResClosed = true;
}
else {
setTimeout(function () {
waitForScreenshot(result, params);
}, 100);
}
}
function addCookie(cookie) {
var domain = cookie.domain;
var path = cookie.path;
var split = cookie.cookie.split("; ");
for (var i = 0, len = split.length; i < len; i++) {
var keyVal = split[i];
var equalIndex = keyVal.indexOf("=");
if (equalIndex > -1) {
var key = keyVal.substring(0, equalIndex);
var value = keyVal.substring(equalIndex + 1);
phantom.addCookie({
name: key,
value: value,
domain: domain,
path: path,
});
}
}
}
function response(res, result) {
if (typeof result != "string") {
result = JSON.stringify(result);
}
res.write(result);
res.close();
}
function sendFlag(flag) {
console.log("flag://" + flag);
}
String.prototype.startsWith = function (prefix) {
var len = this.length;
var prefixLen = prefix.length;
return prefixLen <= len && prefix == this.substring(0, prefixLen);
};
function println(obj) {
console.log(obj == null ? "" : JSON.stringify(obj));
}