Example #1
0
 private static VpnHostContract Map(VpnHost host)
 {
     return(new VpnHostContract
     {
         Name = host.Name,
         Ip = host.Ip
     });
 }
Example #2
0
        private void InvokeConnecting()
        {
            VpnHost server = _servers.FirstOrDefault();

            if (!server.IsEmpty())
            {
                InvokeStateChanged(new VpnState(VpnStatus.Pinging, VpnError.None, string.Empty, server.Ip,
                                                _config.VpnProtocol, _config.OpenVpnAdapter, server.Label));
            }
        }
Example #3
0
 private static VpnHostContract Map(VpnHost host)
 {
     return(new()
     {
         Name = host.Name,
         Ip = host.Ip,
         Label = host.Label,
         X25519PublicKey = host.X25519PublicKey != null ? new ServerPublicKeyContract(host.X25519PublicKey) : null,
     });
 }
Example #4
0
        private IReadOnlyList <Task <VpnEndpoint> > EndpointCandidates(VpnHost server, VpnProtocol protocol)
        {
            _logger.Info($"Pinging VPN server {server.Ip} for {protocol} protocol");

            var timeoutTask = Task.Delay(EndpointTimeout);

            return((from pair in _config.Ports
                    where protocol == VpnProtocol.Auto || protocol == pair.Key
                    from port in pair.Value
                    select GetPortAlive(new VpnEndpoint(server, pair.Key, port), timeoutTask)).ToList());
        }
Example #5
0
        public void IsEmpty_ShouldBeTrue_WhenNew()
        {
            // Arrange
            var host = new VpnHost("name.com", "0.0.0.0", string.Empty);

            // Act
            var result = host.IsEmpty();

            // Assert
            result.Should().BeFalse();
        }
Example #6
0
        public void IsEmpty_ShouldBeTrue_WhenDefault()
        {
            // Arrange
            VpnHost host = default;

            // Act
            var result = host.IsEmpty();

            // Assert
            result.Should().BeTrue();
        }
Example #7
0
        private VpnState WithFallbackRemoteServer(VpnState state, VpnHost remoteServer)
        {
            if (state.Status == VpnStatus.Disconnecting ||
                state.Status == VpnStatus.Disconnected ||
                !string.IsNullOrEmpty(state.RemoteIp))
            {
                return(state);
            }

            return(state.WithRemoteIp(remoteServer.Ip, remoteServer.Label));
        }
Example #8
0
        public void Ip_ShouldBe_Ip()
        {
            // Arrange
            const string expected = "44.55.66.77";
            var          host     = new VpnHost("server-1.protonvpn.com", expected, string.Empty);

            // Act
            var result = host.Ip;

            // Assert
            result.Should().Be(expected);
        }
Example #9
0
        public void Name_ShouldBe_Name()
        {
            // Arrange
            const string expected = "server-1.protonvpn.com";
            var          host     = new VpnHost(expected, "127.0.0.1", string.Empty);

            // Act
            var result = host.Name;

            // Assert
            result.Should().Be(expected);
        }
Example #10
0
        private VpnEndpoint HandleBestEndpoint(VpnEndpoint bestEndpoint, VpnHost server)
        {
            bool isResponding = bestEndpoint.Port != 0;

            if (isResponding)
            {
                _logger.Info($"The endpoint {bestEndpoint.Server.Ip}:{bestEndpoint.Port} was the fastest to respond.");
                return(bestEndpoint);
            }

            _logger.Info($"No VPN port has responded for {server.Ip}.");
            return(VpnEndpoint.Empty);
        }
Example #11
0
        private async Task <VpnEndpoint> BestEndpoint(VpnHost server, VpnProtocol protocol, CancellationToken cancellationToken)
        {
            switch (protocol)
            {
            case VpnProtocol.Auto:
                var bestEndpoint = await BestEndpoint(EndpointCandidates(server, VpnProtocol.OpenVpnUdp));

                if (bestEndpoint.Port == 0 && !cancellationToken.IsCancellationRequested)
                {
                    bestEndpoint = await BestEndpoint(EndpointCandidates(server, VpnProtocol.OpenVpnTcp));
                }
                return(bestEndpoint);

            default:
                return(await BestEndpoint(EndpointCandidates(server, protocol)));
            }
        }
 private static VpnEndpoint Endpoint(VpnHost server, VpnProtocol protocol)
 {
     return(server.IsEmpty()
         ? VpnEndpoint.EmptyEndpoint
         : new VpnEndpoint(server, protocol));
 }
Example #13
0
 public VpnEndpoint(VpnHost server, VpnProtocol vpnProtocol, int port)
     : this(server, port)
 {
     VpnProtocol = vpnProtocol;
 }
Example #14
0
 public VpnEndpoint(VpnHost server, int port, bool isEmpty = false)
 {
     Server  = server;
     Port    = port;
     IsEmpty = isEmpty;
 }
Example #15
0
 public VpnEndpoint(VpnHost server, VpnProtocol vpnProtocol)
     : this(server, vpnProtocol, 0)
 {
 }
Example #16
0
 public VpnEndpoint(VpnHost server, VpnProtocol protocol, int port)
 {
     Server   = server;
     Protocol = protocol;
     Port     = port;
 }