From a1496579c4cd52401d8b48a2978269ddabef593c Mon Sep 17 00:00:00 2001 From: Alicia Sykes Date: Sat, 9 Jul 2022 20:29:36 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Lambda=20function=20to=20find=20IP?= =?UTF-8?q?=20of=20a=20domain?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/lambda/find-url-ip.js | 23 ++++++++++ src/pages/Results.tsx | 86 ++++++++++++++++++++++++++++-------- 2 files changed, 90 insertions(+), 19 deletions(-) create mode 100644 server/lambda/find-url-ip.js diff --git a/server/lambda/find-url-ip.js b/server/lambda/find-url-ip.js new file mode 100644 index 0000000..2d6dd3d --- /dev/null +++ b/server/lambda/find-url-ip.js @@ -0,0 +1,23 @@ +const dns = require('dns'); + +/* Lambda function to fetch the IP address of a given URL */ +exports.handler = function (event, context, callback) { + const addressParam = event.queryStringParameters.address; + const address = decodeURIComponent(addressParam) + .replaceAll('https://', '') + .replaceAll('http://', ''); + dns.lookup(address, (err, ip, family) => { + console.log(err); + if (err) { + callback(null, { + statusCode: 405, + body: JSON.stringify(err), + }) + } else { + callback(err, { + statusCode: 200, + body: JSON.stringify({ip, family}), + }); + } + }); +}; diff --git a/src/pages/Results.tsx b/src/pages/Results.tsx index 6cb5d20..8430520 100644 --- a/src/pages/Results.tsx +++ b/src/pages/Results.tsx @@ -4,53 +4,101 @@ import styled from 'styled-components'; import colors from 'styles/colors'; import Heading from 'components/Form/Heading'; +import Card from 'components/Form/Card'; import ServerLocationCard from 'components/Results/ServerLocation'; +import ServerInfoCard from 'components/Results/ServerInfo'; +import HostNamesCard from 'components/Results/HostNames'; +import keys from 'utils/get-keys'; +import { determineAddressType } from 'utils/address-type-checker'; -import { getLocation, ServerLocation } from 'utils/result-processer'; +import { + getLocation, ServerLocation, + getServerInfo, ServerInfo, + getHostNames, HostNames, +} from 'utils/result-processor'; const ResultsOuter = styled.div` display: flex; + flex-direction: column; `; -const ResultsInner = styled.section` - padding: 1rem; - margin: 1rem; +const ResultsContent = styled.section` width: 95vw; + display: flex; + flex-wrap: wrap; +`; + +const Header = styled(Card)` + margin: 1rem; + display: flex; + align-items: baseline; + justify-content: space-between; + padding: 0.5rem 1rem; `; interface ResultsType { serverLocation?: ServerLocation, + serverInfo?: ServerInfo, + hostNames?: HostNames, }; const Results = (): JSX.Element => { const [ results, setResults ] = useState({}); + const [ locationResults, setLocationResults ] = useState(); const { address } = useParams(); if (address) { console.log(decodeURIComponent(address)); } - const applyResults = (response: any) => { - console.log(response); - const serverLocation = getLocation(response); - setResults({...results, serverLocation }); - } useEffect(() => { - console.log('Will fetch....', process.env.SHODAN_API_KEY); - const apiKey = 'WB6B7tRAskjlmpVUrYfnU1CVGCIpUs1t'; - fetch(`https://api.shodan.io/shodan/host/${address}?key=${apiKey}`) - .then(response => response.json()) - .then(response => applyResults(response)) - .catch(err => console.error(err)); - }, [address]); + fetch(`https://ipapi.co/${address}/json/`) + .then(function(response) { + response.json().then(jsonData => { + console.log(jsonData); + setLocationResults(getLocation(jsonData)); + }); + }) + .catch(function(error) { + console.log(error) + }); + }, []); + + useEffect(() => { + const applyShodanResults = (response: any) => { + // const serverLocation = getLocation(response); + const serverInfo = getServerInfo(response); + const hostNames = getHostNames(response); + setResults({...results, serverInfo, hostNames }); + } + const fetchShodanData = () => { + const apiKey = keys.shodan; + fetch(`https://api.shodan.io/shodan/host/${address}?key=${apiKey}`) + .then(response => response.json()) + .then(response => { + if (!response.error) applyShodanResults(response) + }) + .catch(err => console.error(err)); + }; + const addressType = determineAddressType(address || ''); + if (addressType !== 'ipV4') { + // Not an IP address, get IP from URL + } else { + fetchShodanData(); + } + }, []); return ( - +
Results {address} - { results.serverLocation && } - +
+ + { locationResults && } + { results.serverInfo && } + { results.hostNames && } +
); }