diff --git a/server/lambda/find-url-ip.js b/server/lambda/find-url-ip.js index 2d6dd3d..437c3ca 100644 --- a/server/lambda/find-url-ip.js +++ b/server/lambda/find-url-ip.js @@ -7,7 +7,6 @@ exports.handler = function (event, context, callback) { .replaceAll('https://', '') .replaceAll('http://', ''); dns.lookup(address, (err, ip, family) => { - console.log(err); if (err) { callback(null, { statusCode: 405, diff --git a/server/lambda/web-check.js b/server/lambda/web-check.js new file mode 100644 index 0000000..2b6d28d --- /dev/null +++ b/server/lambda/web-check.js @@ -0,0 +1,71 @@ +const fetch = require('node-fetch'); + + +const endpoints = { + dnsRecords: 'https://api.securitytrails.com/v1/domain/{address}', + subDomains: 'https://api.securitytrails.com/v1/domain/{address}/subdomains?include_inactive=true', +}; + +const apiKeys = { + shodan: process.env.SHODAN_API_KEY, + securityTrails: process.env.SECURITY_TRAILS_API_KEY, +}; + +const makeEndpoint = (endpoint, address) => endpoint.replace('{address}', address); + +const errorObject = (msg, status) => { + return { + statusCode: status || 500, + body: JSON.stringify({ error: true, errorMsg: msg }), + } +}; + +/* Fetches list of DNS records for a given URL from SecurityTrails API */ +const getDnsInfo = async (url) => { + const stOptions = { + method: 'GET', + headers: { Accept: 'application/json', APIKEY: apiKeys.securityTrails } + }; + const dnsInfoRequest = await fetch(makeEndpoint(endpoints.dnsRecords, url), stOptions); + const dnsInfoResponse = await dnsInfoRequest.json(); + if (dnsInfoResponse) return null; + return dnsInfoResponse?.current_dns; +}; + +/* Fetches a list of a domain's sub-domains from SecurityTrails API */ +const getSubDomains = async (url) => { + const stOptions = { + method: 'GET', + headers: { Accept: 'application/json', APIKEY: apiKeys.securityTrails } + }; + const subDomainRequest = await fetch(makeEndpoint(endpoints.subDomains, url), stOptions); + const subDomainResponse = await subDomainRequest.json(); + if (!subDomainResponse.subdomains) return null; + return { + count: subDomainResponse?.subdomain_count, + list: subDomainResponse?.subdomains, + }; +}; + +exports.handler = async (event, context) => { + const addressParam = event.queryStringParameters.address; + if (!addressParam) return errorObject('An address must be specified'); + const address = decodeURIComponent(addressParam) + .replaceAll('https://', '') + .replaceAll('http://', ''); + + try { + const results = {}; + if (apiKeys.securityTrails) { + results.dnsRecords = await getDnsInfo(address); + results.subDomains = await getSubDomains(address); + } + + return { + statusCode: 200, + body: JSON.stringify(results), + }; + } catch (error) { + return errorObject('Failed to fetch data'); + } +}; diff --git a/src/pages/Results.tsx b/src/pages/Results.tsx index 775f71f..b319a92 100644 --- a/src/pages/Results.tsx +++ b/src/pages/Results.tsx @@ -117,6 +117,26 @@ const Results = (): JSX.Element => { fetchShodanData(); } }, [ipAddress]); + + /* Get WhoIs info for a given domain name */ + useEffect(() => { + const applyWhoIsResults = (response: any) => { + console.log('WhoIs Response', response); + } + const fetchWhoIsData = () => { + const apiKey = keys.whoApi; + fetch(`https://api.whoapi.com/?domain=${address}&r=whois&apikey=${apiKey}`) + .then(response => response.json()) + .then(response => { + if (!response.error) applyWhoIsResults(response) + }) + .catch(err => console.error(err)); + }; + + if (addressType === 'url') { + fetchWhoIsData(); + } + }, [addressType, address]); return ( diff --git a/src/utils/address-type-checker.ts b/src/utils/address-type-checker.ts index 7e3fcc3..636e477 100644 --- a/src/utils/address-type-checker.ts +++ b/src/utils/address-type-checker.ts @@ -8,7 +8,7 @@ export type AddressType = 'ipV4' | 'ipV6' | 'url' | 'err' | 'empt'; /* Checks if a given string looks like a URL */ const isUrl = (value: string):boolean => { const urlRegex= new RegExp('' - + /(?:(?:(https?|ftp):)?\/\/)/.source + // + /(?:(?:(https?|ftp):)?\/\/)/.source + /(?:([^:\n\r]+):([^@\n\r]+)@)?/.source + /(?:(?:www\.)?([^/\n\r]+))/.source + /(\/[^?\n\r]+)?/.source diff --git a/src/utils/get-keys.ts b/src/utils/get-keys.ts index bc4f85e..301f29e 100644 --- a/src/utils/get-keys.ts +++ b/src/utils/get-keys.ts @@ -1,6 +1,7 @@ const keys = { shodan: process.env.SHODAN_API_KEY, + whoApi: process.env.WHO_API_KEY, }; export default keys;