⏲ Resolves concurency issues

This commit is contained in:
Alicia Sykes 2022-07-17 15:06:02 +01:00
parent ee1a9389e4
commit 9018f6fdd1

View File

@ -9,7 +9,7 @@ import ServerLocationCard from 'components/Results/ServerLocation';
import ServerInfoCard from 'components/Results/ServerInfo'; import ServerInfoCard from 'components/Results/ServerInfo';
import HostNamesCard from 'components/Results/HostNames'; import HostNamesCard from 'components/Results/HostNames';
import keys from 'utils/get-keys'; import keys from 'utils/get-keys';
import { determineAddressType } from 'utils/address-type-checker'; import { determineAddressType, AddressType } from 'utils/address-type-checker';
import { import {
getLocation, ServerLocation, getLocation, ServerLocation,
@ -45,48 +45,78 @@ interface ResultsType {
const Results = (): JSX.Element => { const Results = (): JSX.Element => {
const [ results, setResults ] = useState<ResultsType>({}); const [ results, setResults ] = useState<ResultsType>({});
const [ locationResults, setLocationResults ] = useState<ServerLocation>(); const [ locationResults, setLocationResults ] = useState<ServerLocation>();
const [ ipAddress, setIpAddress ] = useState<undefined | string>(undefined);
const [ addressType, setAddressType ] = useState<AddressType>('empt');
const { address } = useParams(); const { address } = useParams();
if (address) {
console.log(decodeURIComponent(address));
}
useEffect(() => { useEffect(() => {
fetch(`https://ipapi.co/${address}/json/`) setAddressType(determineAddressType(address || ''));
if (addressType === 'ipV4') {
setIpAddress(address);
}
}, []);
/* Get IP address from URL */
useEffect(() => {
const fetchIpAddress = () => {
fetch(`/find-url-ip?address=${address}`)
.then(function(response) {
console.log(response);
response.json().then(jsonData => {
console.log('Get IP Address', jsonData);
setIpAddress(jsonData.ip);
});
})
.catch(function(error) {
console.log(error)
});
};
if (!ipAddress) {
fetchIpAddress();
}
}, [address]);
/* Get IP address location info */
useEffect(() => {
const fetchIpLocation = () => {
fetch(`https://ipapi.co/${ipAddress}/json/`)
.then(function(response) { .then(function(response) {
response.json().then(jsonData => { response.json().then(jsonData => {
console.log(jsonData);
setLocationResults(getLocation(jsonData)); setLocationResults(getLocation(jsonData));
}); });
}) })
.catch(function(error) { .catch(function(error) {
console.log(error) console.log(error)
}); });
}, []); };
if (ipAddress) {
fetchIpLocation();
}
}, [ipAddress]);
/* Get hostnames and server info from Shodan */
useEffect(() => { useEffect(() => {
const applyShodanResults = (response: any) => { const applyShodanResults = (response: any) => {
// const serverLocation = getLocation(response);
const serverInfo = getServerInfo(response); const serverInfo = getServerInfo(response);
const hostNames = getHostNames(response); const hostNames = getHostNames(response);
setResults({...results, serverInfo, hostNames }); setResults({...results, serverInfo, hostNames });
} }
const fetchShodanData = () => { const fetchShodanData = () => {
const apiKey = keys.shodan; const apiKey = keys.shodan;
fetch(`https://api.shodan.io/shodan/host/${address}?key=${apiKey}`) fetch(`https://api.shodan.io/shodan/host/${ipAddress}?key=${apiKey}`)
.then(response => response.json()) .then(response => response.json())
.then(response => { .then(response => {
if (!response.error) applyShodanResults(response) if (!response.error) applyShodanResults(response)
}) })
.catch(err => console.error(err)); .catch(err => console.error(err));
}; };
const addressType = determineAddressType(address || '');
if (addressType !== 'ipV4') {
// Not an IP address, get IP from URL if (ipAddress) {
} else {
fetchShodanData(); fetchShodanData();
} }
}, []); }, [ipAddress]);
return ( return (
<ResultsOuter> <ResultsOuter>