|
| 1 | +const fetch = require('node-fetch'); |
| 2 | +const { consistentResultObj } = require('./util'); |
| 3 | + |
| 4 | +// Unique values in an array, comma-separated |
| 5 | +const uniqueCommaSep = arr => [...new Set(arr)].join(', '); |
| 6 | + |
| 7 | +// Find RDAP data entities that match a name |
| 8 | +const findEntities = (name, data) => data.entities && data.entities.filter(entity => |
| 9 | + entity.roles.map(role => role.trim().toLowerCase()).includes(name)); |
| 10 | + |
| 11 | +// Find a specific vcard for an RDAP entity |
| 12 | +const entityVcard = (entity, vcard) => { |
| 13 | + if (entity && entity.vcardArray && Array.isArray(entity.vcardArray) && entity.vcardArray.length > 1) { |
| 14 | + const entityFn = entity.vcardArray[1].find(card => card[0] === vcard); |
| 15 | + if (entityFn && Array.isArray(entityFn) && entityFn.length > 3) return entityFn[3]; |
| 16 | + } |
| 17 | +}; |
| 18 | + |
| 19 | +// Find a vcard name/handle for an RDAP entity |
| 20 | +const findEntityName = (name, data) => { |
| 21 | + const entities = findEntities(name, data); |
| 22 | + if (!entities) return; |
| 23 | + |
| 24 | + return uniqueCommaSep(entities.map(entity => entityVcard(entity, 'fn') || (entity && entity.handle)) || []); |
| 25 | +}; |
| 26 | + |
| 27 | +// Find a vcard email address for an RDAP entity |
| 28 | +const findEntityEmail = (name, data) => { |
| 29 | + const entities = findEntities(name, data); |
| 30 | + if (!entities) return; |
| 31 | + |
| 32 | + return uniqueCommaSep(entities.map(entity => entityVcard(entity, 'email')) || []); |
| 33 | +}; |
| 34 | + |
| 35 | +// Get a JS Date for a specific RDAP data event |
| 36 | +const findEventDate = (name, data) => { |
| 37 | + if (!data.events) return; |
| 38 | + const event = data.events.find(event => event.eventAction.trim().toLowerCase() === name); |
| 39 | + if (!event || !event.eventDate) return; |
| 40 | + return new Date(event.eventDate); |
| 41 | +}; |
| 42 | + |
| 43 | +// Find the ASN information from the RDAP data |
| 44 | +const findAsn = data => uniqueCommaSep((data.arin_originas0_originautnums || []).map(asn => asn.toString())); |
| 45 | + |
| 46 | +// Format RDAP CIDR data |
| 47 | +const formatCidr = cidr => cidr && (cidr.v4prefix || cidr.v6prefix) && cidr.length |
| 48 | + ? (cidr.v4prefix || cidr.v6prefix) + '/' + cidr.length.toString() |
| 49 | + : undefined; |
| 50 | + |
| 51 | +// Find the CIDR blocks from the RDAP data |
| 52 | +const findCidr = data => uniqueCommaSep((data.cidr0_cidrs || []).map(formatCidr).filter(cidr => cidr !== undefined)); |
| 53 | + |
| 54 | +// Find the abuse email in the RDAP data |
| 55 | +const findAbuseEmail = data => { |
| 56 | + const directAbuse = findEntityEmail('abuse', data); |
| 57 | + if (directAbuse) return directAbuse; |
| 58 | + |
| 59 | + const registrarEntities = findEntities('registrar', data); |
| 60 | + if (!registrarEntities) return; |
| 61 | + |
| 62 | + return findEntityEmail('abuse', { entities: registrarEntities.map(entity => entity.entities).flat(1) }); |
| 63 | +}; |
| 64 | + |
| 65 | +module.exports = async query => { |
| 66 | + const resp = await fetch(`https://rdap.cloud/api/v1/${query}`); |
| 67 | + const rawData = await resp.json().catch(() => false); |
| 68 | + const data = rawData && rawData.results && rawData.results[query]; |
| 69 | + |
| 70 | + // Ensure the data is there |
| 71 | + if (!data || !data.success || !data.data) |
| 72 | + return false; |
| 73 | + |
| 74 | + // Find the useful information for us |
| 75 | + const result = consistentResultObj({ |
| 76 | + name: data.data.name, |
| 77 | + registrant: findEntityName('registrant', data.data), |
| 78 | + asn: findAsn(data.data), |
| 79 | + registrar: findEntityName('registrar', data.data), |
| 80 | + registration: findEventDate('registration', data.data), |
| 81 | + expiration: findEventDate('expiration', data.data), |
| 82 | + cidr: findCidr(data.data), |
| 83 | + abuse: findAbuseEmail(data.data), |
| 84 | + }); |
| 85 | + |
| 86 | + // Return false if we found nothing, otherwise return the data |
| 87 | + return Object.values(result).every(x => x === undefined) ? false : result; |
| 88 | +}; |
0 commit comments