/// <summary> /// Initializes a new instance of the <see cref="MainWindowViewModel"/> class. /// </summary> public MainWindowViewModel() { // Initialize connectionStatus in the view model connectionStatus = new Models.ConnectionStatus { Status = Models.ConnectionState.Unprotected, }; // Intialize the selected item of the server list RefreshServerListSelectedItem(initialLoad: true); // Get the number of user devices and current device if exists JSONStructures.Device currentDevice = null; if (Manager.Account.Config.FxALogin.User != null) { currentDevice = Manager.Account.Config.FxALogin.User.Devices.Find(d => d.PublicKey == Manager.Account.Config.FxALogin.PublicKey); UserNumDevices = Manager.Account.Config.FxALogin.User.Devices.Count(); } // Determine initial page to display in the ViewFrame if (Manager.Account.LoginState == FxA.LoginState.LoggedIn && currentDevice != null) { InitialViewFrameSourceType = typeof(UI.MainView); } else { InitialViewFrameSourceType = typeof(UI.LinkAccountView); } }
/// <summary> /// Updates the tunnel connection status and its associated UI. /// </summary> /// <param name="newConnectionStatus">The new tunnel connection status.</param> public void UpdateConnectionStatus(Models.ConnectionStatus newConnectionStatus) { UpdateNetworkSpeedHistory(LastConnectionStatus, newConnectionStatus); // Save connection status LastConnectionStatus = newConnectionStatus; // Update connection bytes received UpdateRxUI(newConnectionStatus.RxBytes); // Update connection bytes sent UpdateTxUI(newConnectionStatus.TxBytes); // Update connection handshake UpdateLastHandshakeUI(newConnectionStatus.LastHandshakeTimeSec); // Update connection stability UpdateConnectionStabilityUI(newConnectionStatus.ConnectionStability); // Update connection state UpdateConnectionStateUI(newConnectionStatus.Status); // Update tray UpdateTrayUI(newConnectionStatus.Status, newConnectionStatus.ConnectionStability); // Update network UpdateNetworkUI(newConnectionStatus.Status, newConnectionStatus.ConnectionStability); }
private void UpdateNetworkSpeedHistory(Models.ConnectionStatus oldConnectionStatus, Models.ConnectionStatus newConnectionStatus) { double.TryParse(newConnectionStatus.RxBytes, out double newRx); double.TryParse(oldConnectionStatus.RxBytes, out double oldRx); double.TryParse(newConnectionStatus.TxBytes, out double newTx); double.TryParse(oldConnectionStatus.TxBytes, out double oldTx); var curDownloadSpeed = Math.Round(Math.Max((newRx - oldRx) * 8, 0), 1); var curUploadSpeed = Math.Round(Math.Max((newTx - oldTx) * 8, 0), 1); Queue <double> speeds = Manager.MainWindowViewModel.DownloadSpeedHistory; speeds.Enqueue(curDownloadSpeed); RemoveOldData(speeds); Manager.MainWindowViewModel.DownloadSpeedHistory = speeds; Manager.MainWindowViewModel.DownloadSpeedHistoryString = string.Join(",", speeds); Manager.MainWindowViewModel.IsDownloadIdle = GetIsIdle(speeds); Tuple <string, string> lastDownloadSpeed = GetNetworkSpeedUnits(curDownloadSpeed); Manager.MainWindowViewModel.LastDownloadSpeed = lastDownloadSpeed.Item1; Manager.MainWindowViewModel.LastDownloadSpeedUnits = lastDownloadSpeed.Item2; speeds = Manager.MainWindowViewModel.UploadSpeedHistory; speeds.Enqueue(curUploadSpeed); RemoveOldData(speeds); Manager.MainWindowViewModel.UploadSpeedHistory = speeds; Manager.MainWindowViewModel.UploadSpeedHistoryString = string.Join(",", speeds); Manager.MainWindowViewModel.IsUploadIdle = GetIsIdle(speeds); Tuple <string, string> lastUploadSpeed = GetNetworkSpeedUnits(curUploadSpeed); Manager.MainWindowViewModel.LastUploadSpeed = lastUploadSpeed.Item1; Manager.MainWindowViewModel.LastUploadSpeedUnits = lastUploadSpeed.Item2; }
private Models.ConnectionStatus ParseStatusResponse(IPCMessage message) { var currentTime = DateTimeOffset.UtcNow.ToUnixTimeSeconds(); var connectionStatus = new Models.ConnectionStatus { PrivateKey = message["private_key"].FirstOrDefault(), ListenPort = message["listen_port"].FirstOrDefault(), PublicKey = message["public_key"].FirstOrDefault(), PresharedKey = message["preshared_key"].FirstOrDefault(), ProtocolVersion = message["protocol_version"].FirstOrDefault(), Endpoint = message["endpoint"].FirstOrDefault(), LastHandshakeTimeSec = message["last_handshake_time_sec"].FirstOrDefault(), LastHandshakeTimeNsec = message["last_handshake_time_nsec"].FirstOrDefault(), TxBytes = message["tx_bytes"].FirstOrDefault(), RxBytes = message["rx_bytes"].FirstOrDefault(), PersistenKeepaliveInterval = message["persistent_keepalive_interval"].FirstOrDefault(), AllowedIp = string.Join(", ", message["allowed_ip"]), ErrNo = message["errno"].FirstOrDefault(), ConnectionStability = Models.ConnectionStability.Stable, }; // Set new uptime if (uptimeStart == DateTime.MinValue) { uptimeStart = DateTime.Now; } // Check whether there has been any data received in the past X seconds long.TryParse(connectionStatus.RxBytes, out long currentTxBytes); if (currentTxBytes - previousTxBytes != 0) { previousTxQueryTime = currentTime; } if (DateTime.Now.Subtract(uptimeStart).TotalSeconds > ProductConstants.TunnelInitialGracePeriodTimeout) { long.TryParse(connectionStatus.LastHandshakeTimeSec, out long lastHandshakeTime); var connectionStability = Models.ConnectionStability.Stable; if (currentTime - previousTxQueryTime > ProductConstants.TunnelUnstableTimeout) { connectionStability = Models.ConnectionStability.Unstable; } // Check when was the last time the handshake has been received if (connectionStability == Models.ConnectionStability.Unstable && currentTime - lastHandshakeTime > ProductConstants.TunnelNoSignalTimeout) { connectionStability = Models.ConnectionStability.NoSignal; } connectionStatus.ConnectionStability = connectionStability; } previousTxBytes = currentTxBytes; // Mark connection as protected, because the named pipe to WireGuard is up connectionStatus.Status = Models.ConnectionState.Protected; return(connectionStatus); }
/// <summary> /// Retrieves the current connection status of the Tunnel. /// </summary> public void RequestConnectionStatus() { // Do tunnel service checks if (Service.IsTunnelServiceRunning()) { // Service is now running, clear transitioning states and query statistics ClearConnectionTransitionState(); // Sends the connection statistics request to the broker if (!QueryConnectionStatisticsFromBroker()) { Manager.ConnectionStatusUpdater.RequestConnectionStatusTcs.TrySetResult(true); var newConnectionStatus = new Models.ConnectionStatus() { Status = Models.ConnectionState.Protected, ConnectionStability = Models.ConnectionStability.NoSignal }; Manager.ConnectionStatusUpdater.UpdateConnectionStatus(newConnectionStatus); } return; } // Service is not running, mark the request connection status task to be complete Manager.ConnectionStatusUpdater.RequestConnectionStatusTcs.SetResult(true); if (!Service.IsTunnelServiceRunning() && IsDisconnecting) { // Service is not running, we should clear the connection transition state as the disconnection is now complete ClearConnectionTransitionState(); } // Broker process is running, but the tunnel service is not running if (IsConnecting == false) { // We are not in a connecting state, meaning if the tunnel service is down, the user is unprotected Manager.ConnectionStatusUpdater.UpdateConnectionStatus(new Models.ConnectionStatus() { Status = Models.ConnectionState.Unprotected }); return; } else if (IsConnecting == true) { // We are currently only connecting Manager.ConnectionStatusUpdater.UpdateConnectionStatus(new Models.ConnectionStatus() { Status = Models.ConnectionState.Connecting }); return; } else if (IsDisconnecting == true) { // We are currently only disconnecting Manager.ConnectionStatusUpdater.UpdateConnectionStatus(new Models.ConnectionStatus() { Status = Models.ConnectionState.Disconnecting }); return; } // No previous checks apply, the user is unprotected Manager.ConnectionStatusUpdater.UpdateConnectionStatus(new Models.ConnectionStatus() { Status = Models.ConnectionState.Unprotected }); }