@@ -43,8 +43,10 @@ public function runQuery(Server $server): ?array
4343 return null ;
4444 }
4545
46- $ ip = config ('player-counter.use_alias ' ) && is_ip ($ server ->allocation ->alias ) ? $ server ->allocation ->alias : $ server ->allocation ->ip ;
47- $ ip = is_ipv6 ($ ip ) ? '[ ' . $ ip . '] ' : $ ip ;
46+ $ host = self ::getHost ($ server ->allocation );
47+ if ($ host === false ) {
48+ return null ;
49+ }
4850
4951 $ port = $ server ->allocation ->port + ($ this ->query_port_offset ?? 0 );
5052
@@ -59,17 +61,67 @@ public function runQuery(Server $server): ?array
5961 /** @var QueryTypeService $service */
6062 $ service = app (QueryTypeService::class);
6163
62- return $ service ->get ($ this ->query_type )?->process($ server , $ ip , $ port );
64+ return $ service ->get ($ this ->query_type )?->process($ server , $ host , $ port );
6365 }
6466
6567 public static function canRunQuery (?Allocation $ allocation ): bool
68+ {
69+ return self ::getHost ($ allocation ) !== false ;
70+ }
71+
72+ private static function isValidHost (string $ address ): bool
73+ {
74+ return self ::normaliseIpAddress ($ address ) !== false ||
75+ filter_var ($ address , FILTER_VALIDATE_DOMAIN , FILTER_FLAG_HOSTNAME ) !== false ;
76+ }
77+
78+ private static function normaliseIpAddress (string $ address ): bool |string
79+ {
80+ $ address = inet_pton ($ address );
81+ if ($ address === false ) {
82+ return false ;
83+ }
84+
85+ $ address = inet_ntop ($ address );
86+ if ($ address === false ) {
87+ return false ;
88+ }
89+
90+ if (filter_var ($ address , FILTER_VALIDATE_IP , FILTER_FLAG_IPV6 )) {
91+ if ($ address === ':: ' ) {
92+ return false ;
93+ }
94+ return "[ " . $ address . "] " ;
95+ } elseif (filter_var ($ address , FILTER_VALIDATE_IP , FILTER_FLAG_IPV4 )) {
96+ if ($ address === '0.0.0.0 ' ) {
97+ return false ;
98+ }
99+ return $ address ;
100+ }
101+
102+ return false ;
103+ }
104+
105+ protected static function getHost (?Allocation $ allocation ): bool |string
66106 {
67107 if (!$ allocation ) {
68108 return false ;
69109 }
70110
71- $ ip = config ('player-counter.use_alias ' ) && is_ip ($ allocation ->alias ) ? $ allocation ->alias : $ allocation ->ip ;
111+ $ address = false ;
112+
113+ if (config ('player-counter.use_alias ' ) && !is_null ($ allocation ->alias ) && self ::isValidHost ($ allocation ->alias )) {
114+ $ address = $ allocation ->alias ;
115+ } elseif (self ::isValidHost ($ allocation ->ip )) {
116+ $ address = $ allocation ->ip ;
117+ } else {
118+ return false ;
119+ }
120+
121+ if (($ ip = self ::normaliseIpAddress ($ address )) !== false ) {
122+ $ address = $ ip ;
123+ }
72124
73- return ! in_array ( $ ip , [ ' 0.0.0.0 ' , ' :: ' ]) ;
125+ return $ address ;
74126 }
75127}
0 commit comments