Skip to content

Commit 22e69e7

Browse files
committed
Add support for X-Forwarded-For header (e.g. if accessed via reverse proxy)
1 parent 7d851b5 commit 22e69e7

File tree

5 files changed

+34
-1
lines changed

5 files changed

+34
-1
lines changed

config.sample.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"server": "localhost",
33
"ttl": 60,
44
"nsupdate_options": "-k /path/to/keyfile",
5+
"trusted_proxies": ["172.18.0.2"],
56
"users": {
67
"myuser": {
78
"password_hash": "$5$qS83kPfObw5$hOL2P9IwOdGXOIhyoy3hir5KN4YT7x2gauQMHSOHxv2",

index.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
use com\selfcoders\phpdyndns\config\Config;
33
use com\selfcoders\phpdyndns\ErrorCode;
4+
use com\selfcoders\phpdyndns\IPUtils;
45
use com\selfcoders\phpdyndns\NSUpdate;
56

67
require_once __DIR__ . "/vendor/autoload.php";
@@ -38,7 +39,7 @@
3839
$username = $_GET["username"] ?? $_SERVER["PHP_AUTH_USER"] ?? null;
3940
$password = $_GET["password"] ?? $_SERVER["PHP_AUTH_PW"] ?? null;
4041
$hostname = $_GET["hostname"] ?? null;
41-
$ipAddress = $_GET["ipaddress"] ?? $_SERVER["REMOTE_ADDR"];
42+
$ipAddress = $_GET["ipaddress"] ?? IPUtils::getClientIP($config->trustedProxies);
4243

4344
if ($username === null or $password === null) {
4445
header("WWW-Authenticate: Basic realm=\"DynDNS Update\"");
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
namespace com\selfcoders\phpdyndns;
3+
4+
class IPUtils
5+
{
6+
public static function getClientIP($trustedProxies = [])
7+
{
8+
if (isset($_SERVER["X_FORWARDED_FOR"]) and in_array($_SERVER["REMOTE_ADDR"], $trustedProxies)) {
9+
$ips = explode(",", $_SERVER["X_FORWARDED_FOR"]);
10+
return trim(end($ips));
11+
}
12+
13+
return $_SERVER["REMOTE_ADDR"];
14+
}
15+
}

src/main/php/com/selfcoders/phpdyndns/config/Config.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ class Config
2121
* @var string
2222
*/
2323
public $nsupdateOptions = "";
24+
/**
25+
* @var string[]
26+
*/
27+
public $trustedProxies = [];
2428

2529
/**
2630
* @param string $username
@@ -45,8 +49,19 @@ public function setUsers(array $users)
4549
}
4650
}
4751

52+
/**
53+
* @param string $options
54+
*/
4855
public function setNsupdateOptions(string $options)
4956
{
5057
$this->nsupdateOptions = $options;
5158
}
59+
60+
/**
61+
* @param string[] $ips
62+
*/
63+
public function setTrustedProxies(array $ips)
64+
{
65+
$this->trustedProxies = $ips;
66+
}
5267
}

src/test/php/com/selfcoders/phpdyndns/ConfigTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public function testConfig(): void
3636
$this->assertEquals("localhost", $this->config->server);
3737
$this->assertEquals(60, $this->config->ttl);
3838
$this->assertEquals("-k /path/to/keyfile", $this->config->nsupdateOptions);
39+
$this->assertEquals(["172.18.0.2"], $this->config->trustedProxies);
3940

4041
$user = $this->config->getUser("myuser");
4142

0 commit comments

Comments
 (0)