/// <summary> /// This method fires when the Immediate Access service is stopping, before allowing the service to stop /// this method will first disconnect the managed VPN profiles. /// </summary> /// <returns>Task: To allow this method to run asyncronously.</returns> public static async Task Stop() { Logger.Info("Service: Stopping..."); HealthCheckTimer.Enabled = NetEventCoolTimer.Enabled = false; await VpnControl.Disconnect(); Logger.Info("Service: Stopped."); }
/// <summary> /// This method contains the main logic for the Immediate Access service. In a nutshell, this method tests if the probe is available, and if it is not, /// selects and then connects to a VPN profile. /// </summary> /// <returns>Task: to enable threading of this method to allow for canceling.</returns> private static async Task EnsureConnectionToIntranet() { try { UpdatePolicy(); if (IsCurrentlyEnsuring || !IsNetworkAvailable || !PolicyReader.IsServiceEnabled()) { return; } EnsuranceCancel = new CancellationTokenSource(); IsCurrentlyEnsuring = true; if (await TestNetwork.IsProbeAvailable()) { IsCurrentlyEnsuring = false; await VpnControl.Disconnect(); return; } if (await VpnControl.IsConnected() != null) { IsCurrentlyEnsuring = false; return; } EnsuranceCancel.Token.ThrowIfCancellationRequested(); int attempts = (int)PolicyReader.Policies["VpnServerConnectAttempts"]; while (attempts-- > 0) { EnsuranceCancel.Token.ThrowIfCancellationRequested(); if (!await TestNetwork.SelectOnlineVpnProfile()) { IsCurrentlyEnsuring = false; return; } if (await VpnControl.Connect(EnsuranceCancel.Token)) { IsCurrentlyEnsuring = false; _ = EnsureConnectionToIntranet(); return; } if (attempts > 0) { Logger.Warning("Couldn't connect to VPN for some reason. Trying again in 5 seconds..."); await Task.Delay(5000); } } IsCurrentlyEnsuring = false; } catch (OperationCanceledException) { IsCurrentlyEnsuring = false; _ = EnsureConnectionToIntranet(); } }