/*
* Copyright (c) 2023 Proton AG
*
* This file is part of ProtonVPN.
*
* ProtonVPN is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ProtonVPN is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ProtonVPN. If not, see .
*/
using ProtonVPN.Client.Logic.Connection.Contracts.Validators;
using ProtonVPN.Logging.Contracts;
using ProtonVPN.Logging.Contracts.Events.NetworkLogs;
using ProtonVPN.OperatingSystems.Network.Contracts;
namespace ProtonVPN.Client.Logic.Connection.Validators;
public class NetworkAdapterValidator : INetworkAdapterValidator
{
private readonly INetworkInterfaceProvider _networkInterfaceProvider;
private readonly ILogger _logger;
public NetworkAdapterValidator(INetworkInterfaceProvider networkInterfaceProvider, ILogger logger)
{
_networkInterfaceProvider = networkInterfaceProvider;
_logger = logger;
}
public bool IsOpenVpnAdapterAvailable()
{
INetworkInterface? openVpnTapInterface = _networkInterfaceProvider.GetOpenVpnTapInterface();
INetworkInterface? openVpnTunInterface = _networkInterfaceProvider.GetOpenVpnTunInterface();
bool isOpenVpnAdapterAvailable = openVpnTapInterface != null || openVpnTunInterface != null;
LogIsOpenVpnAdapterAvailable(isOpenVpnAdapterAvailable,
CreateInterfaceLogMessage("TAP", openVpnTapInterface),
CreateInterfaceLogMessage("TUN", openVpnTunInterface));
return isOpenVpnAdapterAvailable;
}
private string CreateInterfaceLogMessage(string interfaceType, INetworkInterface? networkInterface)
{
if (networkInterface == null)
{
return $"The {interfaceType} adapter is unavailable.";
}
return $"The {interfaceType} adapter is available (Index: {networkInterface.Index}, " +
$"Name: '{networkInterface.Name}', Description: '{networkInterface.Description}').";
}
private void LogIsOpenVpnAdapterAvailable(bool isOpenVpnAdapterAvailable, params string[] openVpnInterfaces)
{
string logMessage = "Checking which OpenVPN adapters are available. " + string.Join(" ", openVpnInterfaces);
if (isOpenVpnAdapterAvailable)
{
_logger.Info(logMessage);
}
else
{
_logger.Warn(logMessage);
}
}
}