Lambda function to find IP of a domain

This commit is contained in:
Alicia Sykes 2022-07-09 20:29:36 +01:00
parent 32cc42091d
commit a1496579c4
2 changed files with 90 additions and 19 deletions

View File

@ -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}),
});
}
});
};

View File

@ -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<ResultsType>({});
const [ locationResults, setLocationResults ] = useState<ServerLocation>();
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 (
<ResultsOuter>
<ResultsInner>
<Header as="header">
<Heading color={colors.primary} size="large">Results</Heading>
<Heading color={colors.textColor} size="medium">{address}</Heading>
{ results.serverLocation && <ServerLocationCard {...results.serverLocation} />}
</ResultsInner>
</Header>
<ResultsContent>
{ locationResults && <ServerLocationCard {...locationResults} />}
{ results.serverInfo && <ServerInfoCard {...results.serverInfo} />}
{ results.hostNames && <HostNamesCard hosts={results.hostNames} />}
</ResultsContent>
</ResultsOuter>
);
}