private const int SERVICE_START_TIMEOUT   = 20000; //20 secs

        internal static bool checkDaemon(string daemonName)
        {
            ShrewNotifier.Log(string.Format("Checking daemon status: {0}", daemonName), ShrewConnectionStatus.Pending);

            ServiceController sc = new ServiceController(daemonName);

            switch (sc.Status)
            {
            case ServiceControllerStatus.Running:
                ShrewNotifier.Log("    Running", ShrewConnectionStatus.Pending);
                return(true);

            case ServiceControllerStatus.Stopped:
                ShrewNotifier.Log("    Stopped", ShrewConnectionStatus.Pending);
                return(StartService(daemonName));

            case ServiceControllerStatus.Paused:
                ShrewNotifier.Log("    Paused, aborting vpn connect.", ShrewConnectionStatus.Disconnected);
                return(false);

            case ServiceControllerStatus.StopPending:
                ShrewNotifier.Log("    Stopping, aborting vpn connect.", ShrewConnectionStatus.Disconnected);
                return(false);

            case ServiceControllerStatus.StartPending:
                ShrewNotifier.Log("    Starting, aborting vpn connect.", ShrewConnectionStatus.Disconnected);
                return(false);

            default:
                ShrewNotifier.Log("    Unknown state, aborting vpn connect.", ShrewConnectionStatus.Disconnected);
                return(false);
            }
        }
 private void ConnectProcess()
 {
     this.failedConnectAttempts = 0;
     if (!DaemonUtils.checkDaemon(IKE_DAEMON_NAME))
     {
         return;
     }
     if (!DaemonUtils.checkDaemon(IPSEC_DAEMON_NAME))
     {
         return;
     }
     ShrewNotifier.Log("Starting connect attempts.", ShrewConnectionStatus.Pending);
     while (!this.ConnectAttempt() && !this.shuttingDown)
     {
         ShrewNotifier.SetStatus(ShrewConnectionStatus.Disconnected);
         failedConnectAttempts++;
         if (this.failedConnectAttempts % 10 == 0)
         {
             ShrewNotifier.Log("Failed 10 connect attempts in a row, restarting daemons.", ShrewConnectionStatus.Pending);
             DaemonUtils.ResartService(IKE_DAEMON_NAME);
             DaemonUtils.ResartService(IPSEC_DAEMON_NAME);
         }
         ShrewNotifier.Log(string.Format("Connect attempt {0} failed!", failedConnectAttempts), ShrewConnectionStatus.Disconnected);
     }
     if (this.shuttingDown)
     {
         return;
     }
     ShrewNotifier.SetStatus(ShrewConnectionStatus.Connected);
     ShrewNotifier.Log("Connection established.", ShrewConnectionStatus.Connected);
     ShrewNotifier.Log("------------------------------------------------", ShrewConnectionStatus.Pending);
 }
Beispiel #3
0
 private void disconnectButton_Click(object sender, EventArgs e)
 {
     this.destroyConnection();
     connectPanel.Visible = true;
     statusPanel.Visible  = false;
     statusTextBox.Text   = "";
     ShrewNotifier.ResetLog();
 }
 private void CleanUp()
 {
     ShrewNotifier.Log("Destroying any pre-existing vpn clients.", ShrewConnectionStatus.Pending);
     if (this.monitorTimer != null)
     {
         this.monitorTimer.Dispose();
         this.monitorTimer = null;
     }
     vpnClient.KillAll();
 }
 private void RunMonitorProcess(object state)
 {
     lock (this)
     {
         if (!VerifyConnected())
         {
             ShrewNotifier.Log("Shrew connection lost!", ShrewConnectionStatus.Disconnected);
             ShrewNotifier.SetStatus(ShrewConnectionStatus.Pending);
             Connect();
         }
     }
 }
        private bool ConnectAttempt()
        {
            CleanUp();
            vpnClient.ExecuteClient();
            long startTimeTicks = DateTime.UtcNow.Ticks;

            ShrewNotifier.Log("Waiting for shrew connection...", ShrewConnectionStatus.Pending);
            while (DateTime.UtcNow.Ticks < startTimeTicks + (TimeSpan.TicksPerMillisecond * CONNECT_TIMEOUT_MS))
            {
                if (VerifyConnected())
                {
                    StartMonitorThread();
                    return(true);
                }
                Thread.Sleep(WAIT_INTERVAL_MS);
            }
            return(false);
        }
        internal static bool StartService(string daemonName)
        {
            ServiceController sc = new ServiceController(daemonName);

            ShrewNotifier.Log("    Starting daemon...", ShrewConnectionStatus.Pending);
            sc.Start();
            try
            {
                sc.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 0, 0, SERVICE_START_TIMEOUT));
            }
            catch (Exception)
            {
                ShrewNotifier.Log("    Unable to start daemon.", ShrewConnectionStatus.Disconnected);
                return(false);
            }
            ShrewNotifier.Log("    Daemon running.", ShrewConnectionStatus.Pending);
            return(true);
        }
        internal static bool ResartService(string daemonName)
        {
            ServiceController sc = new ServiceController(daemonName);

            ShrewNotifier.Log(string.Format("Restarting daemon: {0}", daemonName), ShrewConnectionStatus.Pending);
            sc.Refresh();
            Thread.Sleep(2000);
            sc.Refresh();
            try
            {
                sc.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 0, 0, SERVICE_RESTART_TIMEOUT));
            }
            catch (Exception)
            {
                ShrewNotifier.Log("    Unable to restart daemon.", ShrewConnectionStatus.Disconnected);
                return(false);
            }
            ShrewNotifier.Log("    Daemon running.", ShrewConnectionStatus.Pending);
            return(true);
        }
        private const int SERVICE_START_TIMEOUT   = 20000; //20 secs

        internal static bool checkDaemon(string daemonName)
        {
            ShrewNotifier.Log(string.Format("Checking daemon status: {0}", daemonName), ShrewConnectionStatus.Pending);

            ServiceController sc = new ServiceController(daemonName);

            try
            {
                var scName = sc.ServiceName;
            } catch (InvalidOperationException exc) {
                throw new InvalidOperationException(String.Format("Service \"{0}\" does not exist or is not available.", daemonName), exc);
            }

            switch (sc.Status)
            {
            case ServiceControllerStatus.Running:
                ShrewNotifier.Log("    Running", ShrewConnectionStatus.Pending);
                return(true);

            case ServiceControllerStatus.Stopped:
                ShrewNotifier.Log("    Stopped", ShrewConnectionStatus.Pending);
                return(StartService(daemonName));

            case ServiceControllerStatus.Paused:
                ShrewNotifier.Log("    Paused, aborting vpn connect.", ShrewConnectionStatus.Disconnected);
                return(false);

            case ServiceControllerStatus.StopPending:
                ShrewNotifier.Log("    Stopping, aborting vpn connect.", ShrewConnectionStatus.Disconnected);
                return(false);

            case ServiceControllerStatus.StartPending:
                ShrewNotifier.Log("    Starting, aborting vpn connect.", ShrewConnectionStatus.Disconnected);
                return(false);

            default:
                ShrewNotifier.Log("    Unknown state, aborting vpn connect.", ShrewConnectionStatus.Disconnected);
                return(false);
            }
        }
 public void Shutdown()
 {
     this.shuttingDown = true;
     ShrewNotifier.SetStatus(ShrewConnectionStatus.Pending);
     this.CleanUp();
 }