/// <summary> /// Initiate connection. /// </summary> /// <returns>WCF response.</returns> public Response Connect() { try { WaitForConnectionStateChange(Models.ConnectionState.Unprotected, TimeSpan.FromSeconds(10)); if (Manager.ConnectionStatusUpdater.LastConnectionStatus.Status == Models.ConnectionState.Unprotected) { var configuration = new WireGuard.Config(ProductConstants.FirefoxPrivateNetworkConfFile); string address = Manager.Settings.Network.IPv4Address; if (Manager.Settings.Network.EnableIPv6) { address += "," + Manager.Settings.Network.IPv6Address; } var currentServer = FxA.Cache.FxAServerList.GetServerByIndex(0); configuration.SetEndpoint(FxA.Cache.FxAServerList.GetServerIPByIndex(0), FxA.Cache.FxAServerList.GetServerPublicKeyByIndex(0), ProductConstants.AllowedIPs, address, currentServer.DNSServerAddress); bool result = Manager.Tunnel.Connect(); return(new Response(result ? 200 : 500, result ? "successfully connect!" : "fail to connect")); } return(new Response(200, "successfully connect!")); } catch (Exception ex) { return(new Response(500, ex.Message, ex.StackTrace)); } }
/// <summary> /// Processes login response from the FxA API and adds a new Account device. /// </summary> /// <param name="fxaJson">JSON string received from FxA containing login data.</param> /// <returns>True on successful processing of the login response.</returns> public bool ProcessLogin(string fxaJson) { // Initialize a new login session configuration Manager.Account.Config = new FxA.Config(fxaJson); // Generate a new WireGuard keypair in preparation for adding a new Account device var keys = WireGuard.Keypair.Generate(); Manager.Account.Config.FxALogin.PublicKey = keys.Public; // Save login session to user's appdata folder Manager.Account.Config.SaveFxAToken(); Manager.Account.Config.WriteFxAUserToFile(ProductConstants.FxAUserFile); // Set the account login state to logged in Manager.Account.LoginState = FxA.LoginState.LoggedIn; // Added a new account device through the FxA API, using the newly generated keypair var devices = new FxA.Devices(); var deviceName = string.Format("{0} ({1} {2})", System.Environment.MachineName, System.Environment.OSVersion.Platform, System.Environment.OSVersion.Version); var deviceAddResponse = devices.AddDevice(deviceName, keys.Public); // Upon successful addition of a new device, save the device interface to the WireGuard configuration file if (deviceAddResponse != null) { var conf = new WireGuard.Config(keys.Private, deviceAddResponse.IPv4Address + "," + deviceAddResponse.IPv6Address, string.Empty); conf.WriteToFile(ProductConstants.FirefoxPrivateNetworkConfFile); return(true); } return(false); }
/// <summary> /// Sets the latest selected item from the server list and sets it to serverListSelectedItem. /// </summary> /// <param name="initialLoad">Are we currently loading the.</param> public void RefreshServerListSelectedItem(bool initialLoad = false) { // Cannot set selected item if server list contains no servers if (FxA.Cache.FxAServerList.GetServerList().Count() == 0) { return; } // Set the selected item of the server list to the server specificed in the current WireGuard configuration. If it doesn't exist, default to the first server try { var previouslySelectedServerIndex = 0; if (initialLoad) { // Get the saved WireGuard configuration var configuration = new WireGuard.Config(ProductConstants.FirefoxPrivateNetworkConfFile); previouslySelectedServerIndex = FxA.Cache.FxAServerList.GetServerIndexByIP(configuration.GetPeerEndpointWithoutPort()); } else { // Get the selected server prior to server list refresh previouslySelectedServerIndex = FxA.Cache.FxAServerList.GetServerIndexByIP(serverListSelectedItem.Endpoint); } serverListSelectedItem = FxA.Cache.FxAServerList.GetServerList()[previouslySelectedServerIndex]; } catch (Exception) { serverListSelectedItem = FxA.Cache.FxAServerList.GetServerList()[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); ErrorHandling.DebugLogger.LogDebugMsg("Setting endpoint to", Manager.MainWindowViewModel.ServerListSelectedItem.Endpoint); var currentServer = FxA.Cache.FxAServerList.GetServerByIP(Manager.MainWindowViewModel.ServerListSelectedItem.Endpoint); configuration.SetEndpoint(currentServer.GetEndpointWithRandomPort(), currentServer.PublicKey, ProductConstants.AllowedIPs, currentServer.DNSServerAddress); if (switchServer && Manager.MainWindowViewModel.Status == Models.ConnectionState.Protected) { // Set "switching" bindings so they show up in the UI Manager.MainWindowViewModel.IsServerSwitching = true; Manager.MainWindowViewModel.SwitchingServerFrom = previousServerCity; Manager.MainWindowViewModel.SwitchingServerTo = switchServerCity; 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()); } }
/// <summary> /// Sets the latest selected item from the server list and sets it to serverListSelectedItem. /// </summary> /// <param name="initialLoad">Are we currently loading the.</param> public void RefreshServerListSelectedItem(bool initialLoad = false) { // Cannot set selected item if server list contains no servers if (FxA.Cache.FxAServerList.GetServerCitiesList().Count() == 0) { return; } // Set the selected item of the server list to the server specificed in the current WireGuard configuration. If it doesn't exist, default to random server in US try { var previouslySelectedServerIndex = 0; if (initialLoad || newUserSignIn) { newUserSignIn = false; // Get the saved WireGuard configuration var configuration = new WireGuard.Config(ProductConstants.FirefoxPrivateNetworkConfFile); previouslySelectedServerIndex = FxA.Cache.FxAServerList.GetServerIndexByIP(configuration.GetPeerEndpointWithoutPort()); } else { // Get the selected server prior to server list refresh previouslySelectedServerIndex = FxA.Cache.FxAServerList.GetServerIndexByIP(ServerSelected.Endpoint); } var selectedServerCity = FxA.Cache.FxAServerList.GetServerItems()[previouslySelectedServerIndex].City; ServerCityListSelectedItem = FxA.Cache.FxAServerList.GetServerCitiesList().FirstOrDefault(x => x.City == selectedServerCity); } catch (Exception) { Random rand = new Random(); var serverCitiesInDefaultServerCounty = FxA.Cache.FxAServerList.GetServerCitiesList().Where(x => x.Country == DefaultServerCountry); if (serverCitiesInDefaultServerCounty.Count() > 0) { ServerCityListSelectedItem = serverCitiesInDefaultServerCounty.ElementAt(rand.Next(0, serverCitiesInDefaultServerCounty.Count())); } else { ServerCityListSelectedItem = FxA.Cache.FxAServerList.GetServerCitiesList()[rand.Next(0, FxA.Cache.FxAServerList.GetServerCitiesList().Count)]; } } finally { UpdateServerSelection(); } }
/// <summary> /// Initiate connection. /// </summary> /// <returns>WCF response.</returns> public Response Connect() { try { if (Manager.Tunnel.ConnectionStatus().Status == Models.ConnectionState.Unprotected) { var configuration = new WireGuard.Config(ProductConstants.FirefoxPrivateNetworkConfFile); var currentServer = FxA.Cache.FxAServerList.GetServerByIndex(0); configuration.SetEndpoint(FxA.Cache.FxAServerList.GetServerIPByIndex(0), FxA.Cache.FxAServerList.GetServerPublicKeyByIndex(0), ProductConstants.AllowedIPs, currentServer.DNSServerAddress); bool result = Manager.Tunnel.Connect(); return(new Response(result ? 200 : 500, result ? "successfully connect!" : "fail to connect")); } return(new Response(200, "successfully connect!")); } catch (Exception ex) { return(new Response(500, ex.Message, ex.StackTrace)); } }