Exemplo n.º 1
0
        private bool?UpdatedLeakProtectionStatus(VpnState state)
        {
            switch (state.Status)
            {
            case VpnStatus.Connecting:
            case VpnStatus.Reconnecting:
            {
                return(_serviceSettings.SplitTunnelSettings.Mode == SplitTunnelMode.Disabled ||
                       _serviceSettings.SplitTunnelSettings.Mode == SplitTunnelMode.Block);
            }

            case VpnStatus.Disconnecting:
            case VpnStatus.Disconnected:
            {
                if (state.Error == VpnError.None || !_serviceSettings.KillSwitchSettings.Enabled)
                {
                    return(false);
                }

                break;
            }
            }

            return(null);
        }
Exemplo n.º 2
0
        private void ManagementClient_StateChanged(object sender, EventArgs <VpnState> e)
        {
            _logger.Info($"ManagementClient: State changed to {e.Data.Status}");

            VpnState state = new(
                e.Data.Status,
                e.Data.Error,
                e.Data.LocalIp,
                e.Data.RemoteIp,
                _endpoint.VpnProtocol,
                _config.OpenVpnAdapter,
                e.Data.Label);

            if ((state.Status == VpnStatus.Pinging || state.Status == VpnStatus.Connecting || state.Status == VpnStatus.Reconnecting) &&
                string.IsNullOrEmpty(state.RemoteIp))
            {
                state = new VpnState(
                    state.Status,
                    VpnError.None,
                    string.Empty,
                    _endpoint.Server.Ip,
                    _endpoint.VpnProtocol,
                    state.OpenVpnAdapter,
                    _endpoint.Server.Label);
            }

            if (state.Status == VpnStatus.Disconnecting && !_disconnectAction.IsRunning)
            {
                _disconnectError = state.Error;
            }

            OnStateChanged(state);
        }
Exemplo n.º 3
0
        private void OnStateChanged(VpnStatus status)
        {
            VpnState state;

            switch (status)
            {
            case VpnStatus.Pinging:
            case VpnStatus.Connecting:
                state = new VpnState(status, VpnError.None, string.Empty, _endpoint.Server.Ip,
                                     _endpoint.VpnProtocol, _config.OpenVpnAdapter, _endpoint.Server.Label);
                break;

            case VpnStatus.Disconnecting:
            case VpnStatus.Disconnected:
                state = new VpnState(status, _disconnectError, _config?.VpnProtocol ?? VpnProtocol.Smart);
                break;

            default:
                state = new VpnState(status, VpnError.None, _config?.VpnProtocol ?? VpnProtocol.Smart);
                break;
            }

            _logger.Info($"OpenVpnConnection: State changed to {state.Status}, Error: {state.Error}");
            OnStateChanged(state);
        }
Exemplo n.º 4
0
        private void OnStateChanged(VpnRuntimeState runtimeState)
        {
            VpnState state = VpnState.Connected;

            if (!runtimeState.Connected)
            {
                if (runtimeState.ErrorMessage == null)
                {
                    state = VpnState.Idle;
                }
                else
                {
                    state = VpnState.Reconnecting;
                }
            }

            if (_coreData.ThisDevice.VpnState != state)
            {
                Log.Information($"Vpn state changing to {state} ({runtimeState.ErrorMessage})");
            }

            _coreData.ThisDevice.VpnServerIpAddress = runtimeState.ServerIpAddress;
            _coreData.ThisDevice.VpnState           = state;
            _coreData.ThisDevice.KPIs.Vpn.State     = state switch
            {
                VpnState.Connecting => IndicatorState.Warning,
                VpnState.Reconnecting => IndicatorState.Error,
                VpnState.Connected => IndicatorState.Ok,
                _ => IndicatorState.Disabled,
            };

            _coreData.ThisDevice.KPIs.Vpn.Sent     = runtimeState.SentKbs;
            _coreData.ThisDevice.KPIs.Vpn.Received = runtimeState.ReceivedKbs;
        }
 private void OnServerConnectionStatusChanged(object sender, ConnectionChangedEventArgs <GeographicalServer> e)
 {
     if (e.State == null)
     {
         this.SetDisconnected();
     }
     else if (e.State == 1)
     {
         if (this._previousState == 3)
         {
             this.SetDisconnected();
         }
         else if (this._previousState == 2)
         {
             this.SetConnecting();
         }
         else
         {
             this.SetDisconnected();
         }
     }
     else if (e.State == 2)
     {
         this.SetConnecting();
     }
     else if (e.State == 3)
     {
         this.SetConnected();
     }
     this._previousState = e.State;
 }
Exemplo n.º 6
0
        public Task OnVpnStateChanged(VpnStateChangedEventArgs e)
        {
            if (!_config.MaintenanceTrackerEnabled)
            {
                return(Task.CompletedTask);
            }

            _state = e.State;

            if (e.State.Status == VpnStatus.Connected)
            {
                if (!_timer.IsEnabled)
                {
                    _timer.Interval = _config.MaintenanceCheckInterval;
                    _timer.Start();
                }
            }
            else
            {
                if (_timer.IsEnabled)
                {
                    _timer.Stop();
                }
            }

            return(Task.CompletedTask);
        }
        private void OnGeoServerConnectionStatusChanged(object sender, ConnectionChangedEventArgs <GeographicalServer> e)
        {
            switch (e.State)
            {
            case 0:
                this.ShowDisconnected();
                break;

            case 1:
                this.ShowDisconnecting();
                break;

            case 2:
                this.ShowConnecting(e.Server);
                break;

            case 3:
                this.ShowConnected(e.Server);
                break;

            default:
                throw new NotSupportedException();
            }
            this._currentServer = e.Server;
            this._currentState  = e.State;
        }
Exemplo n.º 8
0
 public void OnVpnDisconnected(VpnState state)
 {
     if (state.VpnProtocol != VpnProtocol.WireGuard)
     {
         RestoreNetworkSettings(state.VpnProtocol, state.OpenVpnAdapter);
     }
 }
Exemplo n.º 9
0
        private void OnStateChanged(VpnState state)
        {
            _disconnected = state.Status == VpnStatus.Disconnected;

            if (_connectRequested)
            {
                InvokeConnecting();

                if (_disconnected)
                {
                    _logger.Info("HandlingRequestsWrapper: Already disconnected, queuing Connect");
                    Queued(Connect);
                }

                return;
            }

            if (_disconnectRequested || _disconnecting)
            {
                if (state.Status == VpnStatus.Disconnecting || state.Status == VpnStatus.Disconnected)
                {
                    InvokeStateChanged(state.WithError(_disconnectError));
                    return;
                }

                InvokeDisconnecting();

                return;
            }

            if (_connecting && state.Status == VpnStatus.Disconnecting)
            {
                // Force disconnect if disconnected while connecting
                _disconnectRequested = true;
                _disconnectError     = state.Error == VpnError.None ? VpnError.Unknown : state.Error;
                _logger.Info("HandlingRequestsWrapper: Disconnecting unexpectedly, queuing Disconnect");
                Queued(Disconnect);
            }

            if (state.Status == VpnStatus.Disconnecting || state.Status == VpnStatus.Disconnected)
            {
                var error = state.Error == VpnError.None ? VpnError.Unknown : state.Error;

                if (_connecting)
                {
                    // Force disconnect if disconnected while connecting
                    _disconnectRequested = true;
                    _disconnectError     = error;
                    _logger.Info("HandlingRequestsWrapper: Disconnecting unexpectedly, queuing Disconnect");
                    Queued(Disconnect);
                    return;
                }

                InvokeStateChanged(state.WithError(error));
                return;
            }

            InvokeStateChanged(WithFallbackRemoteIp(state, _endpoint.Server.Ip));
        }
Exemplo n.º 10
0
 public void OnVpnConnecting(VpnState state)
 {
     if (state.VpnProtocol == VpnProtocol.OpenVpnTcp || state.VpnProtocol == VpnProtocol.OpenVpnUdp)
     {
         ApplyNetworkSettings(state.VpnProtocol, state.OpenVpnAdapter);
         _wintunRegistryFixer.EnsureTunAdapterRegistryIsCorrect();
     }
 }
Exemplo n.º 11
0
 public void OnVpnDisconnected(VpnState state)
 {
     if (state.Error == VpnError.None)
     {
         DisableSplitTunnel();
         _appFilter.RemoveAll();
     }
 }
Exemplo n.º 12
0
        private void EnableReversed(VpnState state)
        {
            _splitTunnelClient.EnableIncludeMode(
                _reverseSplitTunnelApps.Value(),
                _serviceSettings.SplitTunnelSettings.Ips,
                state.LocalIp);

            _reverseEnabled = true;
        }
Exemplo n.º 13
0
        private void Origin_StateChanged(object sender, EventArgs <VpnState> e)
        {
            VpnState state = e.Data;

            _logger.Info($"VPN state changed: {state.Status}, Error: {state.Error}, LocalIP: {state.LocalIp}, " +
                         $"RemoteIP: {state.RemoteIp}, Label: {state.Label}");

            OnStateChanged(state);
        }
Exemplo n.º 14
0
 private VpnStateContract Map(VpnState state)
 {
     return(new VpnStateContract(
                Map(state.Status),
                Map(state.Error),
                state.RemoteIp,
                _killSwitch.ExpectedLeakProtectionStatus(state),
                Map(state.Protocol)));
 }
Exemplo n.º 15
0
        private void OnStateChanged(VpnState state)
        {
            if (state == null)
            {
                return;
            }

            StateChanged?.Invoke(this, new EventArgs <VpnState>(state));
        }
Exemplo n.º 16
0
 private bool ReconnectRequired(VpnState state)
 {
     return(!_reconnectPending &&
            !_disconnecting &&
            (state.Status == VpnStatus.Disconnecting ||
             state.Status == VpnStatus.Disconnected) &&
            (state.Error == VpnError.NetshError ||
             state.Error == VpnError.TimeoutError ||
             state.Error == VpnError.Unknown));
 }
Exemplo n.º 17
0
 private VpnState CreateVpnState(VpnState state, VpnStatus?status = null, VpnProtocol?protocol = null)
 {
     return(new(
                status ?? state.Status,
                state.Error,
                state.LocalIp,
                state.RemoteIp,
                protocol ?? state.Protocol,
                _endpoint?.Server.Label ?? string.Empty));
 }
Exemplo n.º 18
0
        private void OnStateChanged(VpnState state)
        {
            if (state.Status != _prevState.Status ||
                state.Error != _prevState.Error ||
                !string.Equals(state.RemoteIp, _prevState.RemoteIp))
            {
                StateChanged?.Invoke(this, new EventArgs <VpnState>(state));
            }

            _prevState = state;
        }
Exemplo n.º 19
0
        private void Origin_StateChanged(object sender, EventArgs <VpnState> e)
        {
            var state = new VpnState(
                e.Data.Status,
                e.Data.Error,
                e.Data.LocalIp,
                e.Data.RemoteIp,
                _vpnEndpoint.Protocol);

            StateChanged?.Invoke(this, new EventArgs <VpnState>(state));
        }
Exemplo n.º 20
0
        private VpnState WithFallbackRemoteIp(VpnState state, string remoteIp)
        {
            if (state.Status == VpnStatus.Disconnecting ||
                state.Status == VpnStatus.Disconnected ||
                !string.IsNullOrEmpty(state.RemoteIp))
            {
                return(state);
            }

            return(state.WithRemoteIp(remoteIp));
        }
Exemplo n.º 21
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));
        }
Exemplo n.º 22
0
        public override void OnVpnStateChanged(VpnState state)
        {
            Connected = state.Status.Equals(VpnStatus.Connected) &&
                        state.Server is Server server &&
                        server.IsSecureCore() &&
                        server.ExitCountry.Equals(CountryCode);

            foreach (var s in Servers)
            {
                s.OnVpnStateChanged(state);
            }
        }
Exemplo n.º 23
0
        private void SetPropertiesOnVpnStateChanged(VpnStateChangedEventArgs e)
        {
            if (!string.IsNullOrEmpty(e.State.EntryIp))
            {
                LastServer = LastServerCandidates.ServerByEntryIpAndLabel(e.State.EntryIp, e.State.Label);
            }

            State          = new VpnState(e.State.Status, LastServer);
            NetworkBlocked = e.NetworkBlocked;

            RaiseVpnStateChanged(new VpnStateChangedEventArgs(State, e.Error, e.NetworkBlocked, e.Protocol));
        }
Exemplo n.º 24
0
        public void OnVpnConnecting(VpnState state)
        {
            DisableReversed();
            Disable();
            _appFilter.RemoveAll();
            _permittedRemoteAddress.RemoveAll();

            if (_serviceSettings.SplitTunnelSettings.Mode == SplitTunnelMode.Permit)
            {
                _appFilter.Add(_serviceSettings.SplitTunnelSettings.AppPaths, Action.SoftBlock);
            }
        }
Exemplo n.º 25
0
        private void UpdateLeakProtectionStatus(VpnState state)
        {
            switch (UpdatedLeakProtectionStatus(state))
            {
            case true:
                _firewall.EnableLeakProtection(state.RemoteIp);
                break;

            case false:
                _firewall.DisableLeakProtection();
                break;
            }
        }
Exemplo n.º 26
0
        public void OnVpnStateChanged(VpnStateChangedEventArgs e)
        {
            var state = e.State;

            if (!string.IsNullOrEmpty(state.EntryIp))
            {
                _lastServer = _lastServerCandidates.ServerByEntryIp(state.EntryIp);
            }

            State           = new VpnState(state.Status, _lastServer);
            _networkBlocked = e.NetworkBlocked;

            RaiseVpnStateChanged(new VpnStateChangedEventArgs(State, e.Error, e.NetworkBlocked, e.Protocol));
        }
Exemplo n.º 27
0
        public void ExpectedLeakProtectionStatus_ShouldBe_Firewall_LeakProtectionEnabled_WhenOtherStatus(VpnStatus status, bool leakProtectionEnabled)
        {
            // Arrange
            var state = new VpnState(status);

            _firewall.LeakProtectionEnabled.Returns(leakProtectionEnabled);
            var killSwitch = new Service.KillSwitch.KillSwitch(_firewall, _serviceSettings);

            // Act
            var result = killSwitch.ExpectedLeakProtectionStatus(state);

            //Assert
            result.Should().Be(leakProtectionEnabled);
        }
Exemplo n.º 28
0
        private VpnState Filtered(VpnState state)
        {
            if (ShouldSuppress(state))
            {
                return(null);
            }

            if (ShouldBeReconnecting(state))
            {
                return(CreateVpnState(state, VpnStatus.Reconnecting, VpnProtocol.Auto));
            }

            return(CreateVpnState(state));
        }
Exemplo n.º 29
0
        public Task OnVpnStateChanged(VpnStateChangedEventArgs e)
        {
            _vpnState = e.State;

            if (e.State.Status.Equals(VpnStatus.Connected))
            {
                StartTimer();
            }
            else
            {
                StopTimer();
            }

            return(Task.CompletedTask);
        }
Exemplo n.º 30
0
        public async Task GetState()
        {
            var state = await _vpnServiceManager.ConnectionState();

            var protocol = VpnProtocol.Auto;

            if (state != null)
            {
                _lastServer = _lastServerCandidates.ServerByEntryIp(state.EntryIp);
                State       = new VpnState(state.Status, _lastServer);
                protocol    = state.Protocol;
            }

            RaiseVpnStateChanged(new VpnStateChangedEventArgs(State, VpnError.None, false, protocol));
        }