Skip to content

Commit 0521d5d

Browse files
committed
Refactored whois-related logic
1 parent 75348d5 commit 0521d5d

File tree

3 files changed

+80
-61
lines changed

3 files changed

+80
-61
lines changed

db.js

+35-20
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,48 @@ const argv = require('yargs').argv
44
const sqlite3 = require('sqlite3').verbose()
55
let db
66

7-
exports.openOrCreate = function () {
8-
db = new sqlite3.Database('whois-cache.sqlite3', initDatabase)
7+
exports.openOrCreate = async () => {
8+
return new Promise(resolve => {
9+
db = new sqlite3.Database('whois-cache.sqlite3', async () => {
10+
await initDatabase()
11+
resolve()
12+
})
13+
})
914
}
1015

11-
const initDatabase = () => {
12-
db.run(`
13-
CREATE TABLE IF NOT EXISTS whois (
14-
domain TEXT PRIMARY KEY,
15-
registrantName TEXT,
16-
registrantOrganization TEXT,
17-
registrantCountry TEXT
18-
)
19-
`)
16+
const initDatabase = async () => {
17+
return new Promise(resolve => {
18+
return db.run(`
19+
CREATE TABLE IF NOT EXISTS whois (
20+
domain TEXT PRIMARY KEY,
21+
registrantName TEXT,
22+
registrantOrganization TEXT,
23+
registrantCountry TEXT
24+
)
25+
`, () => {
26+
resolve()
27+
})
28+
})
2029
}
2130

22-
exports.insertDomain = (whoisObj) => {
23-
db.run(`
24-
REPLACE INTO whois VALUES (
25-
'${whoisObj.domain}',
26-
'${whoisObj.registrantName}',
27-
'${whoisObj.registrantOrganization}',
28-
'${whoisObj.registrantCountry}'
31+
exports.insertDomain = async whoisObj => {
32+
return new Promise(resolve => {
33+
let statement = db.prepare('REPLACE INTO whois VALUES (?,?,?,?)')
34+
35+
statement.run(
36+
whoisObj.domain,
37+
whoisObj.registrantName,
38+
whoisObj.registrantOrganization,
39+
whoisObj.registrantCountry
2940
)
30-
`)
41+
42+
statement.finalize(() => {
43+
resolve()
44+
})
45+
})
3146
}
3247

33-
exports.readDomain = (domain) => {
48+
exports.readDomain = domain => {
3449
return new Promise(resolve => {
3550
db.get(`SELECT * FROM whois WHERE domain = '${domain}'`, (err, row) => {
3651
if (err) {

index.js

+8-37
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
const utils = require('./utils.js')
44
const whoisDb = require('./db.js')
55
const argv = require('yargs').argv
6-
const async = require('async')
76
const puppeteer = require('puppeteer')
87
const devices = require('puppeteer/DeviceDescriptors')
98
const iPadPro = devices['iPad Pro landscape'];
@@ -14,10 +13,6 @@ const iPadPro = devices['iPad Pro landscape'];
1413
return
1514
}
1615

17-
// Open on init whois sqlite3 database.
18-
utils.log('Opening / initializing whois cache database...')
19-
whoisDb.openOrCreate()
20-
2116
utils.startTime('Test duration')
2217
utils.log('Starting the test...')
2318
const browser = await puppeteer.launch()
@@ -87,40 +82,16 @@ const iPadPro = devices['iPad Pro landscape'];
8782

8883
// If -l (=long) is set --> Collect whois data
8984
if (argv.l) {
85+
// Open on init whois sqlite3 database.
86+
utils.log('Opening / initializing whois cache database...')
87+
await whoisDb.openOrCreate()
88+
9089
utils.log('Collecting whois data for cross-domains...')
91-
let collectedWhoisData = []
92-
for (let key in data.resources) {
93-
if (data.resources.hasOwnProperty(key)) {
94-
utils.log(`Collecting whois data of ${key} sources...`)
95-
await new Promise(resolve => {
96-
// Limit concurrent whois data.resources to 30
97-
async.mapLimit(data.resources[key].requests, 30, async (urlObj) => {
98-
let rootDomain = utils.getRootDomain(urlObj.url)
99-
100-
// Fetch whois data only for cross-origins
101-
if (urlObj.crossOrigin && !utils.whoisDataValid(collectedWhoisData[rootDomain])) {
102-
let whoisData = await utils.getOwnerData(rootDomain)
103-
if (utils.whoisDataValid(whoisData)) {
104-
collectedWhoisData[rootDomain] = whoisData
105-
}
106-
}
107-
108-
if (urlObj.crossOrigin) {
109-
urlObj.ownerData = collectedWhoisData[rootDomain]
110-
}
111-
}, (err, results) => {
112-
if (err) {
113-
utils.log(err)
114-
}
115-
resolve()
116-
})
117-
})
118-
}
119-
}
120-
}
90+
data = await utils.collectWhoisData(data)
12191

122-
// Close the database as we don't need it anymore.
123-
whoisDb.close()
92+
// Close the database as we don't need it anymore.
93+
whoisDb.close()
94+
}
12495

12596
// Print results to stdout based on selected output.
12697
utils.log('Printing results...')

utils.js

+37-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const { spawn } = require('child_process')
44
const argv = require('yargs').argv
55
const URL = require('url-parse')
66
const parseDomain = require('parse-domain')
7+
const async = require('async')
78
const whoisDb = require('./db.js')
89

910
function getWhoisData (domain) {
@@ -160,7 +161,7 @@ exports.getOwnerData = async function (domain) {
160161

161162
// Save whois data to DB only if it's valid
162163
if (exports.whoisDataValid(data)) {
163-
whoisDb.insertDomain(data)
164+
await whoisDb.insertDomain(data)
164165
}
165166

166167
return data
@@ -228,9 +229,9 @@ Cross-origin percentage: ${data.resources[key].crossOriginPercentage}%`)
228229
console.log(`
229230
URL: ${exports.truncate.apply(requestObj.url, [100, false])}
230231
Owner data:
231-
Registrant Name: ${(requestObj.ownerData && requestObj.ownerData.registrantName) ? requestObj.ownerData.registrantName : ''}
232-
Registrant Organization: ${(requestObj.ownerData && requestObj.ownerData.registrantOrganization) ? requestObj.ownerData.registrantOrganization : ''}
233-
Registrant Country: ${(requestObj.ownerData && requestObj.ownerData.registrantCountry) ? requestObj.ownerData.registrantCountry : ''}`)
232+
Registrant Name: ${(requestObj.whoisData && requestObj.whoisData.registrantName) ? requestObj.whoisData.registrantName : ''}
233+
Registrant Organization: ${(requestObj.whoisData && requestObj.whoisData.registrantOrganization) ? requestObj.whoisData.registrantOrganization : ''}
234+
Registrant Country: ${(requestObj.whoisData && requestObj.whoisData.registrantCountry) ? requestObj.whoisData.registrantCountry : ''}`)
234235
}
235236
})
236237
}
@@ -312,3 +313,35 @@ exports.startTime = id => {
312313
exports.endTime = id => {
313314
!argv.silent && console.timeEnd(id)
314315
}
316+
317+
exports.collectWhoisData = async data => {
318+
let collectedWhoisData = []
319+
for (let key in data.resources) {
320+
exports.log(`Collecting whois data of ${key} sources...`)
321+
await new Promise(resolve => {
322+
// Limit concurrent whois queries to 30
323+
async.mapLimit(data.resources[key].requests, 30, async (requestObj) => {
324+
let rootDomain = exports.getRootDomain(requestObj.url)
325+
326+
// Fetch whois data only for cross-origins
327+
if (requestObj.crossOrigin && !exports.whoisDataValid(collectedWhoisData[rootDomain])) {
328+
let whoisData = await exports.getOwnerData(rootDomain)
329+
if (exports.whoisDataValid(whoisData)) {
330+
collectedWhoisData[rootDomain] = whoisData
331+
}
332+
}
333+
334+
if (requestObj.crossOrigin) {
335+
requestObj.whoisData = collectedWhoisData[rootDomain]
336+
}
337+
}, (err, results) => {
338+
if (err) {
339+
exports.log(err)
340+
}
341+
resolve()
342+
})
343+
})
344+
}
345+
346+
return data
347+
}

0 commit comments

Comments
 (0)