-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
347 lines (296 loc) · 9.65 KB
/
Copy pathutils.js
File metadata and controls
347 lines (296 loc) · 9.65 KB
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
'use strict'
const { spawn } = require('child_process')
const argv = require('yargs').argv
const URL = require('url-parse')
const parseDomain = require('parse-domain')
const async = require('async')
const whoisDb = require('./db.js')
function getWhoisData (domain) {
return new Promise(resolve => {
// Spawn a new process.
let collectedData = ''
const whois = spawn('whois', [ domain ], {shell: '/bin/bash'})
// Collect results to a variable.
whois.stdout.on('data', data => {
collectedData += data.toString()
})
// Reolve when the process exits.
whois.on('close', exitCode => {
clearTimeout(timerId)
resolve(collectedData)
})
// Limit the child process execution time to 5 seconds.
const timerId = setTimeout(() => {
whois.kill()
resolve(collectedData)
}, 5000)
})
}
function extractOwnerData (whoisData, domain) {
// Pick only the lines we're interested in
let lines = whoisData.split('\n').filter(line => {
if (
line.includes('Registrant Name') ||
line.includes('Registrant Organization') ||
line.includes('Registrant Country')
) {
return true
}
})
// Parse the data on the lines to objects
let whoisObj = {}
whoisObj.domain = domain
whoisObj.registrantName = ''
whoisObj.registrantOrganization = ''
whoisObj.registrantCountry = ''
lines.forEach(line => {
let t = line.split(':')
if (t.length > 1) {
t.shift()
}
t = t.join('').trim()
if (line.includes('Registrant Name')) {
whoisObj.registrantName = t
} else if (line.includes('Registrant Organization')) {
whoisObj.registrantOrganization = t
} else if (line.includes('Registrant Country')) {
whoisObj.registrantCountry = t
}
})
return whoisObj
}
function extractFiOwnerData (whoisData, domain) {
// Pick only the lines we're interested in
let lines = whoisData.split('\n').filter(line => {
if (
line.includes('name...............:') ||
line.includes('country............:')
) {
return true
}
})
// Parse the data on the lines to objects
let whoisObj = {}
whoisObj.domain = domain
whoisObj.registrantName = ''
whoisObj.registrantOrganization = ''
whoisObj.registrantCountry = ''
lines.forEach(line => {
let t = line.split(':')
if (t.length > 1) {
t.shift()
}
t = t.join('').trim()
if (line.includes('name...............:')) {
whoisObj.registrantName = t
} else if (line.includes('country............:')) {
whoisObj.registrantCountry = t
}
})
return whoisObj
}
function extractJpOwnerData (whoisData, domain) {
// Pick only the lines we're interested in
let lines = whoisData.split('\n').filter(line => {
if (
line.includes('[Registrant]') ||
line.includes('[Name]')
) {
return true
}
})
// Parse the data on the lines to objects
let whoisObj = {}
whoisObj.domain = domain
whoisObj.registrantName = ''
whoisObj.registrantOrganization = ''
whoisObj.registrantCountry = ''
lines.forEach(line => {
let t = line.split(']')
if (t.length > 1) {
t.shift()
}
t = t.join('').trim()
if (line.includes('[Registrant]')) {
whoisObj.registrantOrganization = t
} else if (line.includes('[Name]')) {
whoisObj.registrantName = t
}
})
return whoisObj
}
exports.getOwnerData = async function (domain) {
/**
* Check the local whois database for the
* data before running a new whois query
*/
let data = await whoisDb.readDomain(domain)
if (data) {
return data
}
data = await getWhoisData(domain)
if (domain.endsWith('.fi')) {
data = extractFiOwnerData(data, domain)
} else if (domain.endsWith('.jp')) {
data = extractJpOwnerData(data, domain)
} else {
data = extractOwnerData(data, domain)
}
// Save whois data to DB only if it's valid
if (exports.whoisDataValid(data)) {
await whoisDb.insertDomain(data)
}
return data
}
exports.truncate = function (n, useWordBoundary) {
if (this.length <= n) { return this }
var subString = this.substr(0, n - 1)
return (useWordBoundary
? subString.substr(0, subString.lastIndexOf(' '))
: subString) + '...'
}
exports.sleep = function (ms) {
return new Promise(resolve => {
setTimeout(resolve, ms)
})
}
exports.getRootDomain = function (url) {
const domainObj = parseDomain(url)
/**
* parseDomain returns null if given only a root
* domain like example.com instead of www.example.com
*/
if (!domainObj) {
return ''
}
return `${domainObj.domain}.${domainObj.tld}`
}
exports.getDomain = function (url) {
return new URL(url).origin
}
exports.whoisDataValid = function (whoisObj) {
if (
whoisObj && (
whoisObj.registrantName ||
whoisObj.registrantOrganization ||
whoisObj.registrantCountry
)
) {
return true
}
return false
}
exports.printResultsToConsole = data => {
for (let key in data.resources) {
if (data.resources.hasOwnProperty(key)) {
console.log(`
Resource type: ${key}
Total: ${data.resources[key].totalCount}
Same origin: ${data.resources[key].sameOriginCount}
Cross-origin: ${data.resources[key].crossOriginCount}
Cross-origin percentage: ${data.resources[key].crossOriginPercentage}%`)
if (argv.l && data.resources[key].crossOriginCount) {
console.log(`Cross origin resources:`)
data.resources[key].requests.forEach(requestObj => {
if (requestObj.crossOrigin) {
console.log(`
URL: ${exports.truncate.apply(requestObj.url, [100, false])}
Owner data:
Registrant Name: ${(requestObj.whoisData && requestObj.whoisData.registrantName) ? requestObj.whoisData.registrantName : ''}
Registrant Organization: ${(requestObj.whoisData && requestObj.whoisData.registrantOrganization) ? requestObj.whoisData.registrantOrganization : ''}
Registrant Country: ${(requestObj.whoisData && requestObj.whoisData.registrantCountry) ? requestObj.whoisData.registrantCountry : ''}`)
}
})
}
}
}
console.log(`
#####################################################
Total requests: ${data.totalRequests}
Total ${argv.ignoreSubdomains ? 'same root domain' : 'same domain'} requests: ${data.totalSameOriginRequests}
Total ${argv.ignoreSubdomains ? 'cross-domain' : 'cross-origin'} requests: ${data.totalCrossOriginRequests}
Total ${argv.ignoreSubdomains ? 'cross-domain' : 'cross-origin'} percentage: ${data.crossOriginPercentage}%
#####################################################`)
}
exports.printResultsAsJson = data => {
console.log(JSON.stringify(data))
}
exports.analyzeData = (data, compareToOrigins) => {
// Add new properties to data
data.totalRequests = 0
data.totalSameOriginRequests = 0
data.totalCrossOriginRequests = 0
data.crossOriginPercentage = null
// For each resource type
for (let resourceType in data.resources) {
// Add new properties to resource type
data.resources[resourceType].totalCount = 0
data.resources[resourceType].sameOriginCount = 0
data.resources[resourceType].crossOriginCount = 0
data.resources[resourceType].crossOriginPercentage = null
// Loop over all requests on it
data.resources[resourceType].requests.forEach(request => {
let requestOrigin = (argv.ignoreSubdomains) ? exports.getRootDomain(request.url) : exports.getDomain(request.url)
// Total count for single resource type
data.resources[resourceType].totalCount += 1
// Total counts for same-origin and cross-origin for single resource type
if (compareToOrigins.includes(requestOrigin)) {
data.resources[resourceType].sameOriginCount += 1
} else {
data.resources[resourceType].crossOriginCount += 1
}
// Mark each request as either cross-origin or not
request.crossOrigin = !(compareToOrigins.includes(requestOrigin))
})
// Calculate cross-origin percentage per resource type
data.resources[resourceType].crossOriginPercentage = parseFloat(
(data.resources[resourceType].crossOriginCount / data.resources[resourceType].totalCount * 100).toFixed(2)
)
// Count totals for all resources
data.totalRequests += data.resources[resourceType].totalCount
data.totalSameOriginRequests += data.resources[resourceType].sameOriginCount
data.totalCrossOriginRequests += data.resources[resourceType].crossOriginCount
}
// Calculate cross-origin percentage for whole data
data.crossOriginPercentage = parseFloat(
(data.totalCrossOriginRequests / data.totalRequests * 100).toFixed(2)
)
return data
}
exports.log = message => {
!argv.silent && console.log(message)
}
exports.startTime = id => {
!argv.silent && console.time(id)
}
exports.endTime = id => {
!argv.silent && console.timeEnd(id)
}
exports.collectWhoisData = async data => {
let collectedWhoisData = []
for (let key in data.resources) {
exports.log(`Collecting whois data of ${key} sources...`)
await new Promise(resolve => {
// Limit concurrent whois queries to 30
async.mapLimit(data.resources[key].requests, 30, async (requestObj) => {
let rootDomain = exports.getRootDomain(requestObj.url)
// Fetch whois data only for cross-origins
if (requestObj.crossOrigin && !exports.whoisDataValid(collectedWhoisData[rootDomain])) {
let whoisData = await exports.getOwnerData(rootDomain)
if (exports.whoisDataValid(whoisData)) {
collectedWhoisData[rootDomain] = whoisData
}
}
if (requestObj.crossOrigin) {
requestObj.whoisData = collectedWhoisData[rootDomain]
}
}, (err, results) => {
if (err) {
exports.log(err)
}
resolve()
})
})
}
return data
}