Example #1
0
        /// <summary>
        /// Initiates the connection to a VPN server.
        /// </summary>
        /// <param name="switchServer">True if we're switching servers instead of connecting for the first time.</param>
        /// <param name="previousServerCity">If switching, set to previous server city name for displaying in the UI.</param>
        /// <param name="switchServerCity">If switching, set to the new server city name for displaying in the UI.</param>
        /// <returns>True on success, false otherwise.</returns>
        public static bool Connect(bool switchServer = false, string previousServerCity = "", string switchServerCity = "")
        {
            ErrorHandling.DebugLogger.LogDebugMsg("Connect command initiated");
            var configuration = new WireGuard.Config(ProductConstants.FirefoxPrivateNetworkConfFile);

            string address = Manager.Settings.Network.IPv4Address;

            if (Manager.Settings.Network.EnableIPv6)
            {
                address += "," + Manager.Settings.Network.IPv6Address;
            }

            ErrorHandling.DebugLogger.LogDebugMsg("Setting endpoint to", Manager.MainWindowViewModel.ServerSelected.Endpoint);
            var currentServer = FxA.Cache.FxAServerList.GetServerByIP(Manager.MainWindowViewModel.ServerSelected.Endpoint);

            configuration.SetEndpoint(currentServer.GetEndpointWithRandomPort(), currentServer.PublicKey, ProductConstants.AllowedIPs, address, currentServer.DNSServerAddress);

            if (switchServer && Manager.MainWindowViewModel.Status == Models.ConnectionState.Protected)
            {
                // Update IP info.
                var ipInfo = new FxA.IpInfo();
                ipInfo.RetreiveIpInfo();

                // Set "switching" bindings so they show up in the UI
                Manager.MainWindowViewModel.IsServerSwitching   = true;
                Manager.MainWindowViewModel.SwitchingServerFrom = previousServerCity;
                Manager.MainWindowViewModel.SwitchingServerTo   = switchServerCity;
                Manager.ConnectionStatusUpdater.StartConnectionTransitionStopwatch();

                new Thread(() =>
                {
                    ErrorHandling.DebugLogger.LogDebugMsg("Switching endpoint to ", currentServer.Name, currentServer.IPv4Address);
                    Manager.Tunnel.SwitchServer(configuration.Peer.Endpoint, configuration.Peer.PublicKey);
                    Network.Pinger.Ping(currentServer.DNSServerAddress);
                }).Start();
                return(true);
            }
            else
            {
                ErrorHandling.DebugLogger.LogDebugMsg("Connecting to ", currentServer.Name, currentServer.IPv4Address);
                return(Manager.Tunnel.Connect());
            }
        }
        private void UpdateIpInfo(CancellationToken cancellationToken)
        {
            while (!cancellationToken.IsCancellationRequested)
            {
                if (Manager.Account.LoginState == FxA.LoginState.LoggedIn)
                {
                    var ipInfo = new FxA.IpInfo();
                    ipInfo.RetreiveIpInfo();

                    Application.Current.Dispatcher.Invoke(() =>
                    {
                        var owner = Application.Current.MainWindow;
                        if (owner != null)
                        {
                            ipInfo.RetreiveIpInfo();
                        }
                    });
                }

                cancellationToken.WaitHandle.WaitOne(TimeSpan.FromMinutes(ProductConstants.IpInfoRefreshPeriod));
            }
        }