<?php

declare(strict_types=1);

/*
 * eduVPN - End-user friendly VPN.
 *
 * Copyright: 2014-2023, The Commons Conservancy eduVPN Programme
 * SPDX-License-Identifier: AGPL-3.0+
 */

namespace Vpn\Portal;

use Vpn\Portal\Cfg\ProfileConfig;
use Vpn\Portal\Cfg\WireGuardConfig;
use Vpn\Portal\Exception\ProtocolException;

class Protocol
{
    /**
     * Determine which VPN protocol and transport to use based on the Server
     * and Client preferences.
     *
     * @param array{openvpn+udp:bool,openvpn+tcp:bool,wireguard+udp:bool,wireguard+tcp:bool} $clientProtoSupport
     *
     * @return array<string>
     */
    public static function determine(WireGuardConfig $wgConfig, ProfileConfig $profileConfig, array $clientProtoSupport, ?string $publicKey, bool $preferTcp): array
    {
        if (null === $publicKey) {
            // in APIv3 it is not required to provide a WireGuard public key,
            // if it was NOT provided, there is no WireGuard support
            $clientProtoSupport['wireguard+udp'] = false;
            $clientProtoSupport['wireguard+tcp'] = false;
        }

        $protoPriority = [];
        $serverPreference = self::serverPreference($wgConfig, $profileConfig);
        $clientProtoList = self::clientProtoList($clientProtoSupport);

        // we look at the prioritized list of protocols/transports of the
        // server and pick out the ones the client supports (while keeping the
        // order preferred by the server)
        foreach ($serverPreference as $serverProto) {
            if (in_array($serverProto, $clientProtoList, true)) {
                $protoPriority[] = $serverProto;
            }
        }

        if (0 === count($protoPriority)) {
            throw new ProtocolException(
                sprintf(
                    'no common VPN protocol [CLIENT=%s SERVER=%s]',
                    0 !== count($clientProtoList) ? implode(',', $clientProtoList) : 'None',
                    0 !== count($serverPreference) ? implode(',', $serverPreference) : 'None'
                )
            );
        }

        // if the VPN client prefers TCP, we'll reorganize the list by moving
        // the entries with TCP transport to the start of the list of
        // protocols...
        if ($preferTcp) {
            usort($protoPriority, function (string $a, string $b) {
                if (self::isTcp($a) && self::isUdp($b)) {
                    return -1;
                }
                if (self::isUdp($a) && self::isTcp($b)) {
                    return 1;
                }

                return 0;
            });
        }

        return $protoPriority;
    }

    /**
     * We only take the Accept header serious if we detect at least one
     * mime-type we recognize, otherwise we assume it is garbage and consider
     * it as "not sent".
     *
     * @return array{openvpn+udp:bool,openvpn+tcp:bool,wireguard+udp:bool,wireguard+tcp:bool}
     */
    public static function parseMimeType(?string $httpAccept): array
    {
        if (null === $httpAccept) {
            return ['openvpn+udp' => true, 'openvpn+tcp' => true, 'wireguard+udp' => true, 'wireguard+tcp' => false];
        }

        $oUdpSupport = false;
        $oTcpSupport = false;
        $wUdpSupport = false;
        $wTcpSupport = false;
        $takeSerious = false;

        $mimeTypeList = explode(',', $httpAccept);
        foreach ($mimeTypeList as $mimeType) {
            $mimeType = trim($mimeType);
            if ('application/x-openvpn-profile' === $mimeType) {
                $oUdpSupport = true;
                $oTcpSupport = true;
                $takeSerious = true;
            }
            if ('application/x-wireguard-profile' === $mimeType) {
                $wUdpSupport = true;
                $takeSerious = true;
            }
            if ('application/x-wireguard+tcp-profile' === $mimeType) {
                $wTcpSupport = true;
                $takeSerious = true;
            }

        }
        if (false === $takeSerious) {
            return ['openvpn+udp' => true, 'openvpn+tcp' => true, 'wireguard+udp' => true, 'wireguard+tcp' => false];
        }

        return ['openvpn+udp' => $oUdpSupport, 'openvpn+tcp' => $oTcpSupport, 'wireguard+udp' => $wUdpSupport, 'wireguard+tcp' => $wTcpSupport];
    }

    /**
     * @param array{openvpn+udp:bool,openvpn+tcp:bool,wireguard+udp:bool,wireguard+tcp:bool} $clientProtoSupport
     *
     * @return array<string>
     */
    private static function clientProtoList(array $clientProtoSupport): array
    {
        $protoList = [];
        foreach ($clientProtoSupport as $k => $v) {
            if ($v) {
                $protoList[] = $k;
            }
        }

        return $protoList;
    }

    /**
     * Returns the list of supported protocols by the server, in order of
     * preference, most preferred first.
     *
     * @return array<string>
     */
    private static function serverPreference(WireGuardConfig $wgConfig, ProfileConfig $profileConfig): array
    {
        $serverSupported = [];
        if ($profileConfig->oSupport()) {
            if ($profileConfig->oTcpSupport()) {
                $serverSupported[] = 'openvpn+udp';
            }
            if ($profileConfig->oUdpSupport()) {
                $serverSupported[] = 'openvpn+tcp';
            }
        }

        if ($profileConfig->wSupport()) {
            if (!$wgConfig->onlyProxy()) {
                $serverSupported[] = 'wireguard+udp';
            }
            if ($wgConfig->enableProxy()) {
                $serverSupported[] = 'wireguard+tcp';
            }
        }

        // return entries with OpenVPN first
        if ('openvpn' === $profileConfig->preferredProto()) {
            usort($serverSupported, function (string $a, string $b) {
                if (self::isOpenVpn($a) && self::isWireGuard($b)) {
                    return -1;
                }
                if (self::isWireGuard($a) && self::isOpenVpn($b)) {
                    return 1;
                }

                return 0;
            });
        }

        // return entries with WireGuard first
        if ('wireguard' === $profileConfig->preferredProto()) {
            usort($serverSupported, function (string $a, string $b) {
                if (self::isWireGuard($a) && self::isOpenVpn($b)) {
                    return -1;
                }
                if (self::isOpenVpn($a) && self::isWireGuard($b)) {
                    return 1;
                }

                return 0;
            });
        }

        return $serverSupported;
    }

    /**
     * Helper function to determine whether the protocol is WireGuard.
     */
    private static function isWireGuard(string $p): bool
    {
        return 'wireguard+udp' === $p || 'wireguard+tcp' === $p;
    }

    /**
     * Helper function to determine whether the protocol is OpenVPN.
     */
    private static function isOpenVpn(string $p): bool
    {
        return 'openvpn+udp' === $p || 'openvpn+tcp' === $p;
    }

    /**
     * Helper function to determine whether the transport is TCP.
     */
    private static function isTcp(string $p): bool
    {
        return 'openvpn+tcp' === $p || 'wireguard+tcp' === $p;
    }

    /**
     * Helper function to determine whether the transport is UDP.
     */
    private static function isUdp(string $p): bool
    {
        return 'openvpn+udp' === $p || 'wireguard+udp' === $p;
    }
}
