export const deg2rad = ( deg ) => deg * ( Math . PI / 180 ) ; export const getDistanceFromLatLonInKm = ( lat1, lon1, lat2, lon2 ) => { const R = 6371 ; // Radius of the earth in km const dLat = deg2rad ( lat2 - lat1 ) ; // deg2rad below const dLon = deg2rad ( lon2 - lon1 ) ; const a = Math . sin ( dLat / 2 ) * Math . sin ( dLat / 2 ) + Math . cos ( deg2rad ( lat1 ) ) * Math . cos ( deg2rad ( lat2 ) ) * Math . sin ( dLon / 2 ) * Math . sin ( dLon / 2 ) ; const c = 2 * Math . atan2 ( Math . sqrt ( a ) , Math . sqrt ( 1 - a ) ) ; const d = R * c ; // Distance in km return d ; } ; export const fetchIpLocation = ( ip ) => fetch ( `http://ip-api.com/json/${ip}?fields=status,message,lat,lon&lang=ru` ) . then ( ( res ) => res . json ( ) . catch ( ( ) => null ) ) . then ( ( json ) => { if ( null === json || json ?. status !== 'success' ) return null ; return json ; } ) ; export const distanceBetweenIps = async ( ip1, ip2 ) => { const _ip1 = await fetchIpLocation ( ip1 ) ; const _ip2 = await fetchIpLocation ( ip2 ) ; if ( null === _ip1 || null === _ip2 ) return null ; return getDistanceFromLatLonInKm ( _ip1 . lat , _ip1 . lon , _ip2 . lat , _ip2 . lon ) ; } ; distanceBetweenIps ( '185.169.134.107' , '8.8.8.8' ) . then ( console . log ) ;