TR
games/xencenter-xenadmin-xcp-jumphp / src / Feature / Pools / HostAddresses.php · commit fb1b7c9
<?phpnamespace App\Feature\Pools;use App\Model\Connection;/** * Where the user's computer reaches each host of a pool. XAPI knows the hosts' management addresses, which a pool managed through a * VPN or a NAT does not route; so, in order: the address the user set (Server > Connection Addresses…, saved in the server's profile * as host uuid => address), the address the user typed for the master, the user's naming applied to that host * (Connection::sameNaming: typed host01.example.prod -> host02.example.prod), XAPI's address. */final class HostAddresses{    /** The address the user set for a host, or null. */    public static function userSet(Connection $c, string $host): ?string    {        $uuid = (string)$c->cache->field($host, 'uuid', '');        $address = trim((string)($c->profile->hostAddresses[$uuid] ?? ''));        return $address === '' ? null : $address;    }    /** The address to reach a host of the connection at (performance figures, consoles). */    public static function reachable(Connection $c, string $host): string    {        $set = self::userSet($c, $host);        if ($set !== null) {            return $set;        }        $master = (string)$c->cache->master();        if ($host === $master) {            return $c->profile->host;        }        return self::guess($c, $host) ?? (string)$c->cache->field($host, 'address', '');    }    /** The user's naming applied to a host other than the master (null: no naming to copy). */    public static function guess(Connection $c, string $host): ?string    {        $master = (string)$c->cache->master();        return $host === $master ? $c->profile->host : Connection::sameNaming($c->profile->host, self::shortNames($c, $master), self::shortNames($c, $host));    }    /** @return list<string> the short names of a host (its XAPI name and hostname, before the first dot), lower case */    public static function shortNames(Connection $c, string $host): array    {        $r = $c->cache->get($host) ?? [];        $out = [];        foreach ([(string)($r['name_label'] ?? ''), (string)($r['hostname'] ?? '')] as $name) {            if ($name !== '' && !\App\Model\Address::isIp($name)) {                $out[] = strtolower(explode('.', $name)[0]);            }        }        return array_values(array_unique($out));    }    /**     * What the Connection Addresses dialog saves: only addresses that are set, keyed by uuid.     *     * @param array<string, string> $byUuid     * @return array<string, string>     */    public static function clean(array $byUuid): array    {        $out = [];        foreach ($byUuid as $uuid => $address) {            $address = trim((string)$address);            if ($address !== '' && $uuid !== '') {                $out[(string)$uuid] = $address;            }        }        return $out;    }}