-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
117 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
node_modules | ||
.idea | ||
.DS_Store | ||
test | ||
.editorconfig | ||
.eslintrc.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
const fetch = require('node-fetch'); | ||
const { uniqueCommaSep, consistentResultObj, consistentResult } = require('./util'); | ||
|
||
const findAbuseEmail = data => { | ||
const abuse = data.contacts && data.contacts.abuse; | ||
if (!abuse || !Array.isArray(abuse)) return; | ||
return uniqueCommaSep(data.contacts.abuse); | ||
}; | ||
|
||
module.exports = async query => { | ||
const resp = await fetch(`https://cfwho.com/api/v1/${query}`); | ||
const rawData = await resp.json().catch(() => false); | ||
const data = rawData && rawData[query]; | ||
|
||
// Ensure the data is there | ||
if (!data || !data.success) | ||
return false; | ||
|
||
// Find the useful information for us | ||
const result = consistentResultObj({ | ||
name: data.netname, | ||
asn: data.asn, | ||
cidr: data.network, | ||
abuse: findAbuseEmail(data), | ||
}); | ||
|
||
// Done | ||
return consistentResult(result); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,35 @@ | ||
const rdapLookup = require('./rdap'); | ||
const whoisLookup = require('./whois'); | ||
const cfwhoLookup = require('./cfwho'); | ||
const { consistentResultObj, consistentResult } = require('./util'); | ||
|
||
// TODO: Ingest cfwho.com as third source of info | ||
const combineResults = dataArr => { | ||
const result = {}; | ||
for (const data of dataArr) | ||
for (const key of data) | ||
if (data[key] && !result[key]) result[key] = data[key]; | ||
return consistentResult(consistentResultObj(result)); | ||
}; | ||
|
||
module.exports = async (query, combine = false) => { | ||
module.exports = async (query, first = false) => { | ||
// Do the RDAP lookup | ||
const rdap = await rdapLookup(query); | ||
|
||
// If we have RDAP data and don't need to combine, return it | ||
if (rdap && !combine) return rdap; | ||
// If we have RDAP data and want just the first, return it | ||
if (rdap && first) return rdap; | ||
|
||
// Doo the WHOIS lokkup | ||
// Do the WHOIS lokkup | ||
const whois = await whoisLookup(query); | ||
|
||
// Combine if we can and need to (preferring RDAP) | ||
if (combine && (rdap || whois)) return { ...(whois || {}), ...(rdap || {}) }; | ||
// If we have WHOIS data and want just the first, return it | ||
if (whois && first) return whois; | ||
|
||
// Do the cfwho lokkup | ||
const cfwho = await cfwhoLookup(query); | ||
|
||
// If we have cfwho data and want just the first, return it | ||
if (cfwho && first) return cfwho; | ||
|
||
// Return just the WHOIS data | ||
return whois; | ||
// Combine the results (preferring RDAP, WHOIS, then cfwho) | ||
return combineResults([rdap, whois, cfwho]); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,19 @@ | ||
// Unique values in an array, comma-separated | ||
module.exports.uniqueCommaSep = arr => [...new Set(arr)].join(', '); | ||
|
||
const trimmed = item => item && `${item}`.trim(); | ||
|
||
// Consistent keys for the response from the package | ||
module.exports.consistentResultObj = data => ({ | ||
name: data.name || undefined, | ||
registrant: data.registrant || undefined, | ||
asn: data.asn || undefined, | ||
registrar: data.registrar || undefined, | ||
registration: data.registration || undefined, | ||
expiration: data.expiration || undefined, | ||
cidr: data.cidr || undefined, | ||
abuse: data.abuse || undefined, | ||
name: trimmed(data.name) || undefined, | ||
registrant: trimmed(data.registrant) || undefined, | ||
asn: trimmed(data.asn) || undefined, | ||
registrar: trimmed(data.registrar) || undefined, | ||
registration: data.registration || undefined, | ||
expiration: data.expiration || undefined, | ||
cidr: trimmed(data.cidr) || undefined, | ||
abuse: trimmed(data.abuse) || undefined, | ||
}); | ||
|
||
// If we found nothing, the package returns false | ||
module.exports.consistentResult = data => Object.values(data).every(x => x === undefined) ? false : data; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
const assert = require('assert').strict; | ||
const cfwhoLookup = require('../../src/cfwho'); | ||
|
||
module.exports = async () => { | ||
// cfwho does not support domains | ||
const resultDomain = await cfwhoLookup('digitalocean.com'); | ||
assert.equal(resultDomain, false); | ||
|
||
// Data here is potentially fragile | ||
const resultIp = await cfwhoLookup('104.16.181.15'); | ||
assert.deepEqual(resultIp, { | ||
name: 'CLOUDFLARENET, US', | ||
registrant: undefined, | ||
asn: '13335', | ||
registrar: undefined, | ||
registration: undefined, | ||
expiration: undefined, | ||
cidr: '104.16.176.0/20', | ||
abuse: '[email protected]', | ||
}); | ||
|
||
// cfwho does not support ASNs | ||
const resultAsn = await cfwhoLookup('13335'); | ||
assert.equal(resultAsn, false); | ||
|
||
const resultRandom = await cfwhoLookup('this-is-not-a-valid-query'); | ||
assert.equal(resultRandom, false); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters