Skip to content

Commit afa8496

Browse files
authored
Save correct IP address to database (librespeed#457)
1 parent 5bddaac commit afa8496

File tree

3 files changed

+24
-20
lines changed

3 files changed

+24
-20
lines changed

backend/getIP.php

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
/*
44
* This script detects the client's IP address and fetches ISP info from ipinfo.io/
5-
* Output from this script is a JSON string composed of 2 objects: a string called processedString which contains the combined IP, ISP, Contry and distance as it can be presented to the user; and an object called rawIspInfo which contains the raw data from ipinfo.io (will be empty if isp detection is disabled).
5+
* Output from this script is a JSON string composed of 2 objects: a string called processedString which contains the combined IP, ISP, Country and distance as it can be presented to the user; and an object called rawIspInfo which contains the raw data from ipinfo.io (will be empty if isp detection is disabled).
66
* Client side, the output of this script can be treated as JSON or as regular text. If the output is regular text, it will be shown to the user as is.
77
*/
88

@@ -11,24 +11,7 @@
1111
define('API_KEY_FILE', 'getIP_ipInfo_apikey.php');
1212
define('SERVER_LOCATION_CACHE_FILE', 'getIP_serverLocation.php');
1313

14-
/**
15-
* @return string
16-
*/
17-
function getClientIp()
18-
{
19-
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
20-
$ip = $_SERVER['HTTP_CLIENT_IP'];
21-
} elseif (!empty($_SERVER['HTTP_X_REAL_IP'])) {
22-
$ip = $_SERVER['HTTP_X_REAL_IP'];
23-
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
24-
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
25-
$ip = preg_replace('/,.*/', '', $ip); # hosts are comma-separated, client is first
26-
} else {
27-
$ip = $_SERVER['REMOTE_ADDR'];
28-
}
29-
30-
return preg_replace('/^::ffff:/', '', $ip);
31-
}
14+
require_once 'getIP_util.php';
3215

3316
/**
3417
* @param string $ip

backend/getIP_util.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
/**
4+
* @return string
5+
*/
6+
function getClientIp() {
7+
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
8+
$ip = $_SERVER['HTTP_CLIENT_IP'];
9+
} elseif (!empty($_SERVER['HTTP_X_REAL_IP'])) {
10+
$ip = $_SERVER['HTTP_X_REAL_IP'];
11+
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
12+
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
13+
$ip = preg_replace('/,.*/', '', $ip); # hosts are comma-separated, client is first
14+
} else {
15+
$ip = $_SERVER['REMOTE_ADDR'];
16+
}
17+
18+
return preg_replace('/^::ffff:/', '', $ip);
19+
}
20+

results/telemetry.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
require 'telemetry_settings.php';
44
require_once 'telemetry_db.php';
5+
require_once '../backend/getIP_util.php';
56

6-
$ip = $_SERVER['REMOTE_ADDR'];
7+
$ip = getClientIp();
78
$ispinfo = $_POST['ispinfo'];
89
$extra = $_POST['extra'];
910
$ua = $_SERVER['HTTP_USER_AGENT'];

0 commit comments

Comments
 (0)