1 | var crypto = require('crypto');
|
2 | var Map = require("collections/fast-map");
|
3 | var Set = require("collections/fast-set");
|
4 | var URI = require('crawler-ninja-uri');
|
5 |
|
6 | var CONTENT_TYPE_HEADER = "content-type";
|
7 | var CONTENT_LENGTH_HEADER = "content-length";
|
8 |
|
9 | var ERROR_CODE_TIMEOUT = "ETIMEDOUT";
|
10 | var ERROR_DNS_LOOKUP = "ENOTFOUND";
|
11 |
|
12 | var STATUS_DNS_LOOKUP_ERROR = "DNS lookup failed";
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | function Plugin() {
|
20 |
|
21 | this.name = "Audit-Plugin";
|
22 | this.resources = new Map();
|
23 | this.duplicateContents = new Map();
|
24 | this.inLinks = new Map();
|
25 | this.outLinks = new Map();
|
26 | this.externalLinks = new Map();
|
27 | this.unparsedLinks = new Set();
|
28 | this.images = new Map();
|
29 | this.errors = new Set();
|
30 | this.redirects = new Map();
|
31 |
|
32 |
|
33 | }
|
34 |
|
35 | Plugin.prototype.error = function (error, result, callback) {
|
36 | this.errors.add({uri : result.uri, error : error});
|
37 | if (error.code == ERROR_CODE_TIMEOUT) {
|
38 | var resourceInfo = this.getresourceInfo(result.uri);
|
39 | resourceInfo.statusCode = 408;
|
40 | }
|
41 | if (error.code == ERROR_DNS_LOOKUP) {
|
42 | var resourceInfo = this.getresourceInfo(result.uri);
|
43 | resourceInfo.statusCode = STATUS_DNS_LOOKUP_ERROR;
|
44 | }
|
45 |
|
46 | callback();
|
47 | }
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 | Plugin.prototype.crawl = function(result, $, callback) {
|
59 |
|
60 |
|
61 | if (result.statusCode >= 200 && result.statusCode <= 299 ) {
|
62 | this.analyzeResource(result, $);
|
63 | }
|
64 |
|
65 |
|
66 | if (result.statusCode >= 300 && result.statusCode <= 399 ) {
|
67 |
|
68 | this.analyzeRedirect(result);
|
69 | }
|
70 |
|
71 |
|
72 | if (result.statusCode >= 400 && result.statusCode <= 599 ) {
|
73 |
|
74 | this.analyzeHttpError(result);
|
75 | }
|
76 |
|
77 | callback();
|
78 |
|
79 |
|
80 | }
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 | Plugin.prototype.analyzeResource = function(result, $) {
|
90 |
|
91 | var resourceInfo = this.getresourceInfo(result.uri);
|
92 |
|
93 | resourceInfo.statusCode = result.statusCode;
|
94 | resourceInfo.responseTime = result.responseTime;
|
95 |
|
96 | resourceInfo.size = result.body.length;
|
97 | resourceInfo.contentType = result.headers["content-type"];
|
98 |
|
99 |
|
100 | resourceInfo.headers = result.headers;
|
101 |
|
102 |
|
103 | if ($) {
|
104 | var titleElement = $("title");
|
105 | resourceInfo.title = ! titleElement ? "" : titleElement.text();
|
106 | resourceInfo.titleLen = ! titleElement ? 0 : resourceInfo.title.length;
|
107 |
|
108 | var description = $('meta[name=description]').attr("content");
|
109 | resourceInfo.description = ! description ? "" : description;
|
110 | resourceInfo.descriptionLen = ! resourceInfo.description ? 0 : resourceInfo.description.length;
|
111 |
|
112 | var keywords = $('meta[name=keywords]').attr("content");
|
113 | resourceInfo.keywords = ! keywords ? "" : keywords;
|
114 | resourceInfo.keywordsLen = ! resourceInfo.keywords ? 0 : resourceInfo.keywords.length;
|
115 |
|
116 | var refresh = $('meta[http-equiv=Refresh]').attr("content");
|
117 | resourceInfo.refresh = ! refresh ? "" : refresh
|
118 |
|
119 | var canonicalLink = $('link[rel=canonical]').attr("href");
|
120 | resourceInfo.canonicalLink = ! canonicalLink ? "" : canonicalLink;
|
121 |
|
122 | resourceInfo.wordCount = this.getNumberOfWords($);
|
123 |
|
124 | resourceInfo.h1 = this.getHeaders($, "h1");
|
125 | resourceInfo.h2 = this.getHeaders($, "h2");
|
126 |
|
127 |
|
128 | var shasum = crypto.createHash('sha1');
|
129 | shasum.update(result.body);
|
130 | resourceInfo.hash = shasum.digest('hex');
|
131 | this.addHash(result.uri,resourceInfo.hash);
|
132 | }
|
133 |
|
134 | this.resources.set(result.uri, resourceInfo);
|
135 | }
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 | Plugin.prototype.analyzeRedirect = function(result) {
|
144 |
|
145 | var resourceInfo = this.getresourceInfo(result.uri);
|
146 |
|
147 | resourceInfo.statusCode = result.statusCode;
|
148 | resourceInfo.responseTime = result.responseTime;
|
149 |
|
150 | resourceInfo.size = result.body.length;
|
151 | resourceInfo.contentType = result.headers["content-type"];
|
152 |
|
153 |
|
154 | resourceInfo.headers = result.headers;
|
155 |
|
156 | this.resources.set(result.uri, resourceInfo);
|
157 |
|
158 | addToListMap(this.outLinks, result.uri, {page: result.headers["location"], anchor : 'Redirect', isDoFollow : true});
|
159 | }
|
160 |
|
161 |
|
162 |
|
163 | Plugin.prototype.analyzeHttpError = function(result) {
|
164 |
|
165 | var resourceInfo = this.getresourceInfo(result.uri);
|
166 |
|
167 | resourceInfo.statusCode = result.statusCode;
|
168 | resourceInfo.responseTime = result.responseTime;
|
169 |
|
170 | resourceInfo.headers = result.headers;
|
171 |
|
172 | this.resources.set(result.uri, resourceInfo);
|
173 |
|
174 | }
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 | Plugin.prototype.crawlLink = function(page, link, anchor, isDoFollow, callback) {
|
187 |
|
188 |
|
189 | addToListMap(this.outLinks, page, {page: link, anchor : anchor, isDoFollow : isDoFollow});
|
190 |
|
191 |
|
192 | addToListMap(this.inLinks, link, {page: page, anchor : anchor, isDoFollow : isDoFollow});
|
193 |
|
194 |
|
195 | if (URI.host(page) != URI.host(link)) {
|
196 |
|
197 | addToListMap(this.externalLinks, link, page);
|
198 | }
|
199 |
|
200 | callback();
|
201 |
|
202 | }
|
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 |
|
213 |
|
214 | Plugin.prototype.crawlImage = function(page, link, alt, callback) {
|
215 |
|
216 |
|
217 | addToListMap(this.outLinks, page, {page: link, anchor : alt, isDoFollow : null});
|
218 |
|
219 |
|
220 | if (URI.host(page) == URI.host(link)) {
|
221 | addToListMap(this.images, link, {page: page, alt : alt});
|
222 | }
|
223 |
|
224 | callback();
|
225 |
|
226 | }
|
227 |
|
228 |
|
229 |
|
230 |
|
231 |
|
232 |
|
233 |
|
234 |
|
235 | Plugin.prototype.crawlRedirect = function(from, to, statusCode, callback) {
|
236 | this.redirects.set(from, {'to': to, 'statusCode' : statusCode});
|
237 | callback();
|
238 | }
|
239 |
|
240 |
|
241 |
|
242 |
|
243 |
|
244 |
|
245 |
|
246 |
|
247 | Plugin.prototype.getresourceInfo = function(url) {
|
248 |
|
249 |
|
250 | if (this.resources.has(url)) {
|
251 |
|
252 | return this.resources.get(url);
|
253 |
|
254 | }
|
255 | else {
|
256 | var resourceInfo = {url : url};
|
257 | this.resources.set(url, resourceInfo);
|
258 | return resourceInfo;
|
259 |
|
260 | }
|
261 |
|
262 | }
|
263 |
|
264 |
|
265 |
|
266 |
|
267 |
|
268 |
|
269 |
|
270 |
|
271 | Plugin.prototype.addHash = function(pageUri, hex) {
|
272 |
|
273 | addToListMap(this.duplicateContents, hex, pageUri);
|
274 |
|
275 | };
|
276 |
|
277 |
|
278 |
|
279 |
|
280 |
|
281 |
|
282 |
|
283 | Plugin.prototype.getNumberOfWords = function($) {
|
284 | var s = $('body').text();
|
285 | var counter = 0;
|
286 | counter = s.split(' ').length;
|
287 |
|
288 | return counter;
|
289 |
|
290 | };
|
291 |
|
292 |
|
293 |
|
294 |
|
295 |
|
296 |
|
297 |
|
298 | Plugin.prototype.getHeaders = function($, headerTag) {
|
299 | headers = [];
|
300 | $(headerTag).each(function(index,headerTag) {
|
301 | var headerText = $(headerTag).text();
|
302 | headers.push({"text" : headerText, "len" : headerText.length});
|
303 |
|
304 | });
|
305 |
|
306 | return headers;
|
307 | };
|
308 |
|
309 |
|
310 |
|
311 |
|
312 |
|
313 |
|
314 |
|
315 |
|
316 |
|
317 |
|
318 |
|
319 | var addToListMap = function(map, key, value) {
|
320 |
|
321 | var list = [];
|
322 |
|
323 | if (map.has(key)) {
|
324 |
|
325 | list = map.get(key);
|
326 |
|
327 | if (!list)
|
328 | list = [];
|
329 |
|
330 | }
|
331 | list.push(value);
|
332 | map.set(key, list);
|
333 |
|
334 | }
|
335 |
|
336 |
|
337 | module.exports.Plugin = Plugin;
|