Skip to content

Commit

Permalink
getIP.php can now estimate distance between client and server (defaul…
Browse files Browse the repository at this point in the history
…t enabled, km)
  • Loading branch information
adolfintel committed Feb 23, 2018
1 parent d9b551c commit 7366963
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 9 deletions.
7 changes: 6 additions & 1 deletion doc.md
Original file line number Diff line number Diff line change
@@ -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/)

Expand Down Expand Up @@ -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
Expand Down
56 changes: 51 additions & 5 deletions getIP.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
?>
5 changes: 3 additions & 2 deletions speedtest_worker.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading

0 comments on commit 7366963

Please sign in to comment.