Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 59 additions & 5 deletions player-counter/src/Models/GameQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ public function runQuery(Server $server): ?array
return null;
}

$ip = config('player-counter.use_alias') && is_ip($server->allocation->alias) ? $server->allocation->alias : $server->allocation->ip;
$ip = is_ipv6($ip) ? '[' . $ip . ']' : $ip;
$host = self::getHost($server->allocation);
if ($host === false) {
return null;
}

$port = $server->allocation->port + ($this->query_port_offset ?? 0);

Expand All @@ -59,17 +61,69 @@ public function runQuery(Server $server): ?array
/** @var QueryTypeService $service */
$service = app(QueryTypeService::class);

return $service->get($this->query_type)?->process($server, $ip, $port);
return $service->get($this->query_type)?->process($server, $host, $port);
}

public static function canRunQuery(?Allocation $allocation): bool
{
return self::getHost($allocation) !== false;
}

private static function isValidHost(string $address): bool
{
return self::normaliseIpAddress($address) !== false ||
filter_var($address, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME) !== false;
}

private static function normaliseIpAddress(string $address): bool|string
{
$address = inet_pton($address);
if ($address === false) {
return false;
}

$address = inet_ntop($address);
if ($address === false) {
return false;
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.

if (filter_var($address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
if ($address === '::') {
return false;
}

return '[' . $address . ']';
} elseif (filter_var($address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
if ($address === '0.0.0.0') {
return false;
}

return $address;
}

return false;
}

protected static function getHost(?Allocation $allocation): bool|string
{
if (!$allocation) {
return false;
}

$ip = config('player-counter.use_alias') && is_ip($allocation->alias) ? $allocation->alias : $allocation->ip;
$address = false;

if (config('player-counter.use_alias') && $allocation->alias && self::isValidHost($allocation->alias)) {
$address = $allocation->alias;
} elseif (self::isValidHost($allocation->ip)) {
$address = $allocation->ip;
} else {
return false;
}

if (($ip = self::normaliseIpAddress($address)) !== false) {
$address = $ip;
}

return !in_array($ip, ['0.0.0.0', '::']);
return $address;
}
}