getIP.php can now estimate distance between client and server (default enabled, km)

This commit is contained in:
dosse91 2018-02-23 08:45:54 +01:00
parent d9b551c5c8
commit 73669632e8
4 changed files with 61 additions and 9 deletions

7
doc.md
View file

@ -1,7 +1,7 @@
# HTML5 Speedtest
> by Federico Dossena
> Version 4.5.2, February 9, 2018
> Version 4.5.3, February 23, 2018
> [https://github.com/adolfintel/speedtest/](https://github.com/adolfintel/speedtest/)
@ -217,6 +217,11 @@ w.postMessage('start '+JSON.stringify(params))
* __Important:__ On Firefox, it is better to run the upload test last
* __getIp_ispInfo__: if true, the server will try to get ISP info and pass it along with the IP address. This will add `isp=true` to the request to `url_getIp`. getIP.php accomplishes this using ipinfo.io
* Default: `true`
* __getIp_ispInfo_distance__: if true, the server will try to get an estimate of the distance from the client to the speedtest server. This will add a `distance` argument to the request to `url_getIp`. `__getIp_ispInfo__` must be enabled in order for this to work. getIP.php accomplishes this using ipinfo.io
* `km`: estimate distance in kilometers
* `mi`: estimate distance in miles
* not set: do not measure distance
* Default: `km`
* __enable_quirks__: enables browser-specific optimizations. These optimizations override some of the default settings. They do not override settings that are explicitly set.
* Default: `true`
* __garbagePhp_chunkSize__: size of chunks sent by garbage.php in megabytes

View file

@ -11,12 +11,58 @@
$ip=$_SERVER['REMOTE_ADDR'];
}
$ip=preg_replace("/^::ffff:/", "", $ip);
$isp="";
/**
* Optimized algorithm from http://www.codexworld.com
*
* @param float $latitudeFrom
* @param float $longitudeFrom
* @param float $latitudeTo
* @param float $longitudeTo
*
* @return float [km]
*/
function distance($latitudeFrom, $longitudeFrom, $latitudeTo, $longitudeTo){
$rad = M_PI / 180;
$theta = $longitudeFrom - $longitudeTo;
$dist = sin($latitudeFrom * $rad) * sin($latitudeTo * $rad) + cos($latitudeFrom * $rad) * cos($latitudeTo * $rad) * cos($theta * $rad);
return acos($dist) / $rad * 60 * 1.853;
}
if(isset($_GET["isp"])){
$json = file_get_contents("https://ipinfo.io/".$ip."/json");
$details = json_decode($json,true);
if(array_key_exists("org",$details)) $isp.=$details["org"]; else $isp.="Unknown ISP";
if(array_key_exists("country",$details)) $isp.=" (".$details["country"].")";
$isp="";
try{
$json = file_get_contents("https://ipinfo.io/".$ip."/json");
$details = json_decode($json,true);
if(array_key_exists("org",$details)) $isp.=$details["org"]; else $isp.="Unknown ISP";
if(array_key_exists("country",$details)) $isp.=", ".$details["country"];
$clientLoc=NULL; $serverLoc=NULL;
if(array_key_exists("loc",$details)) $clientLoc=$details["loc"];
if(isset($_GET["distance"])){
if($clientLoc){
$json = file_get_contents("https://ipinfo.io/json");
$details = json_decode($json,true);
if(array_key_exists("loc",$details)) $serverLoc=$details["loc"];
if($serverLoc){
try{
$clientLoc=explode(",",$clientLoc);
$serverLoc=explode(",",$serverLoc);
$dist=distance($clientLoc[0],$clientLoc[1],$serverLoc[0],$serverLoc[1]);
if($_GET["distance"]=="mi"){
$dist/=1.609344;
$dist=round($dist,-1);
if($dist<15) $dist="<15";
$isp.=" (".$dist." mi)";
}else if($_GET["distance"]=="km"){
$dist=round($dist,-1);
if($dist<20) $dist="<20";
$isp.=" (".$dist." km)";
}
}catch(Exception $e){}
}
}
}
}catch(Exception $ex){
$isp="Unknown ISP";
}
echo $ip." - ".$isp;
} else echo $ip;
?>

View file

@ -1,5 +1,5 @@
/*
HTML5 Speedtest v4.5.2
HTML5 Speedtest v4.5.3
by Federico Dossena
https://github.com/adolfintel/speedtest/
GNU LGPLv3 License
@ -33,6 +33,7 @@ var settings = {
url_ping: 'empty.php', // path to an empty file, used for ping test. must be relative to this js file
url_getIp: 'getIP.php', // path to getIP.php relative to this js file, or a similar thing that outputs the client's ip
getIp_ispInfo: true, //if set to true, the server will include ISP info with the IP address
getIp_ispInfo_distance: 'km', //km or mi=estimate distance from server in km/mi; set to false to disable distance estimation. getIp_ispInfo must be enabled in order for this to work
xhr_dlMultistream: 10, // number of download streams to use (can be different if enable_quirks is active)
xhr_ulMultistream: 3, // number of upload streams to use (can be different if enable_quirks is active)
xhr_multistreamDelay: 300, //how much concurrent requests should be delayed
@ -168,7 +169,7 @@ function getIp (done) {
tlog('getIp failed')
done()
}
xhr.open('GET', settings.url_getIp + url_sep(settings.url_getIp) + (settings.getIp_ispInfo?"isp=true":"") + 'r=' + Math.random(), true)
xhr.open('GET', settings.url_getIp + url_sep(settings.url_getIp) + (settings.getIp_ispInfo?("isp=true"+(settings.getIp_ispInfo_distance?("&distance="+settings.getIp_ispInfo_distance+"&"):"&")):"&") + 'r=' + Math.random(), true)
xhr.send()
}
// download test, calls done function when it's over

File diff suppressed because one or more lines are too long