Skip to content

Commit

Permalink
Initial implementation of ipinfo.io offline database
Browse files Browse the repository at this point in the history
  • Loading branch information
adolfintel committed Jul 28, 2024
1 parent b419726 commit dfddcb2
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 11 deletions.
Binary file added backend/country_asn.mmdb
Binary file not shown.
Binary file added backend/geoip2.phar
Binary file not shown.
58 changes: 53 additions & 5 deletions backend/getIP.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,11 @@ function getIpInfoTokenString()
*/
function getIspInfo($ip)
{
$json = file_get_contents('https://ipinfo.io/' . $ip . '/json' . getIpInfoTokenString());
$token=getIpInfoTokenString();
if (empty($token)){
return null;
}
$json = file_get_contents('https://ipinfo.io/' . $ip . '/json' . $token);
if (!is_string($json)) {
return null;
}
Expand Down Expand Up @@ -334,6 +338,23 @@ function calculateDistance($clientLocation, $serverLocation, $unit)
return null;
}

require_once("geoip2.phar");
use MaxMind\Db\Reader;
/**
* @param string $ip
*
* @return string
*/
function getIspInfo_offlineDb($ip){
$reader = new Reader('country_asn.mmdb');
$record = $reader->get($ip);
if(is_null($record)){
return "{}";
}else{
return $record;
}
}

/**
* @return void
*/
Expand Down Expand Up @@ -387,6 +408,29 @@ function sendResponse(
]);
}

/**
* @param string $ip
* @param array|null $rawIspInfo
*
* @return void
*/
function sendResponse_offlineDb(
$ip,
$rawIspInfo = null
) {
$processedString = $ip;
if(is_array($rawIspInfo)){
$processedString .= ' - ' . $rawIspInfo['as_name'] . ', ' . $rawIspInfo['country_name'];
}else{
$processedString .= ' - Unknown ISP';
}
sendHeaders();
echo json_encode([
'processedString' => $processedString,
'rawIspInfo' => $rawIspInfo ?: '',
]);
}

$ip = getClientIp();

$localIpInfo = getLocalOrPrivateIpInfo($ip);
Expand All @@ -402,7 +446,11 @@ function sendResponse(
}

$rawIspInfo = getIspInfo($ip);
$isp = getIsp($rawIspInfo);
$distance = getDistance($rawIspInfo);

sendResponse($ip, $isp, $distance, $rawIspInfo);
if (is_null($rawIspInfo)){
$rawIspInfo = getIspInfo_offlineDb($ip);
sendResponse_offlineDb($ip,$rawIspInfo);
}else{
$isp = getIsp($rawIspInfo);
$distance = getDistance($rawIspInfo);
sendResponse($ip, $isp, $distance, $rawIspInfo);
}
Empty file modified backend/getIP_util.php
100644 → 100755
Empty file.
9 changes: 4 additions & 5 deletions doc.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# LibreSpeed

> by Federico Dossena
> Version 5.2.4
> Version 5.4
> [https://github.com/librespeed/speedtest/](https://github.com/librespeed/speedtest/)
## Introduction
Expand Down Expand Up @@ -57,9 +57,8 @@ Put all files on your web server via FTP or by copying them directly. You can in
__Important:__ The speed test needs write permissions in the installation folder!

#### ipinfo.io
The speed test uses [ipinfo.io](https://ipinfo.io) to detect ISP and distance from server. This is completely optional and can be disabled if you want (see speed test settings), but it is enabled by default, and if you expect more than ~500 tests per day, you will need to sign up to [ipinfo.io](https://ipinfo.io) and edit `backend/getIP_ipInfo_apikey.php` to set your access token.

IpInfo.io has kindly offered free access to their APIs for users of this project; if you're interested, contact me at [[email protected]](mailto:[email protected]) and provide a description of what you intend to do with the project, and you'll get the API key.
The speed test uses the [ipinfo.io](https://ipinfo.io) offline database to detect ISP and country (CC-BY-SA 4.0 license), enabled by default.
It's possible to use the full [ipinfo.io](https://ipinfo.io) API to detect distance from server as well. This is completely optional and can be enabled by obtaining an [access token](https://ipinfo.io) and putting it in `backend/getIP_ipInfo_apikey.php`. When this feature is enabled, the offline database will only be used as fallback.

#### Telemetry and results sharing
The test supports storing test results and can generate shareable images that users can embed in forum signatures and such.
Expand Down Expand Up @@ -361,7 +360,7 @@ __Main parameters:__
* __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 speed test 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
* __getIp_ispInfo_distance__: if true, the server will try to get an estimate of the distance from the client to the speed test 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 (API key required)
* `km`: estimate distance in kilometers
* `mi`: estimate distance in miles
* not set: do not measure distance
Expand Down
Empty file modified results/sanitycheck.php
100644 → 100755
Empty file.
Empty file modified results/telemetry_mssql.sql
100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion speedtest.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function Speedtest() {
this._settings = {}; //settings for the speed test worker
this._state = 0; //0=adding settings, 1=adding servers, 2=server selection done, 3=test running, 4=done
console.log(
"LibreSpeed by Federico Dossena v5.3.1 - https://github.com/librespeed/speedtest"
"LibreSpeed by Federico Dossena v5.4 - https://github.com/librespeed/speedtest"
);
}

Expand Down

0 comments on commit dfddcb2

Please sign in to comment.