File tree Expand file tree Collapse file tree 5 files changed +34
-1
lines changed
main/php/com/selfcoders/phpdyndns
test/php/com/selfcoders/phpdyndns Expand file tree Collapse file tree 5 files changed +34
-1
lines changed Original file line number Diff line number Diff line change 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" ,
Original file line number Diff line number Diff line change 11<?php
22use com \selfcoders \phpdyndns \config \Config ;
33use com \selfcoders \phpdyndns \ErrorCode ;
4+ use com \selfcoders \phpdyndns \IPUtils ;
45use com \selfcoders \phpdyndns \NSUpdate ;
56
67require_once __DIR__ . "/vendor/autoload.php " ;
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
4344if ($ username === null or $ password === null ) {
4445 header ("WWW-Authenticate: Basic realm= \"DynDNS Update \"" );
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments