Exemple #1
0
        private void DoRun()
        {
            OpenVPNClient client = null;

            lock (clientSync)
            {
                if (openVPNClient == null)
                {
                    string errMsg = "OpenVPNTunnel::DoRun(): OpenVPN client is not initialized";

                    EddieLogger.Error(errMsg);

                    throw new Exception(errMsg);
                }

                client = openVPNClient;

                // Do NOT call m_client.Start under m_clientSync'lock
            }

            // Dispatcher must be instantiated before starting to allow handling stop requests from outside while the client isn't started yet

            // RunDispatcher();

            NativeMethods.EddieLibraryResult result = client.Start();

            if (result.code != NativeMethods.ResultCode.SUCCESS)
            {
                string errMsg = string.Format("OpenVPNTunnel::DoRun(): Failed to start OpenVPN client. {0}", result.description);

                EddieLogger.Error(errMsg);

                throw new Exception(errMsg);
            }
        }
        public override void OnCreate()
        {
            base.OnCreate();

            if (!m_initialized)
            {
                EddieLogger.Init(this);

                EddieLogger.Info("Initializing eddie library");
                EddieLogger.Info("{0} - {1}", NativeMethods.EddieLibrary.LibraryQualifiedName(), NativeMethods.EddieLibrary.LibraryReleaseDate());
                EddieLogger.Info("Eddie Library API level {0}", NativeMethods.EddieLibrary.LibraryApiLevel());

                NativeMethods.EddieLibraryResult result = NativeMethods.EddieLibrary.Init();

                if (result.code == NativeMethods.ResultCode.SUCCESS)
                {
                    m_initialized = true;

                    EddieLogger.Info("Eddie Library: OpenVPN initialization succeeded");
                }
                else
                {
                    EddieLogger.Error("Eddie Library: OpenVPN initialization failed. {0}", result.description);
                }
            }
        }
Exemple #3
0
        public NativeMethods.ovpn3_transport_stats GetTransportStats()
        {
            lock (clientSync)
            {
                if (openVPNClient == null)
                {
                    string errMsg = "OpenVPNTunnel::GetTransportStats(): OpenVPN client is not initialized";

                    EddieLogger.Error(errMsg);

                    throw new Exception(errMsg);
                }

                NativeMethods.ovpn3_transport_stats stats = new NativeMethods.ovpn3_transport_stats();

                NativeMethods.EddieLibraryResult result = openVPNClient.GetTransportStats(ref stats);

                if (result.code != NativeMethods.ResultCode.SUCCESS)
                {
                    string errMsg = string.Format("OpenVPNTunnel::GetTransportStats(): Failed to get OpenVPN transport stats. {0}", result.description);

                    EddieLogger.Error(errMsg);

                    throw new Exception(errMsg);
                }

                return(stats);
            }
        }
Exemple #4
0
        private void DoSetOption(string option, string value)
        {
            NativeMethods.EddieLibraryResult result = openVPNClient.SetOption(option, value);

            if (result.code != NativeMethods.ResultCode.SUCCESS)
            {
                string errMsg = String.Format("OpenVPNTunnel::DoSetOption(): Failed to set option '{0}' with value '{1}'. {2}", option, value, result.description);

                EddieLogger.Error(errMsg);

                throw new Exception(errMsg);
            }
        }
Exemple #5
0
        public void Cleanup()
        {
            OpenVPNClient client = null;

            lock (clientSync)
            {
                if (openVPNClient == null)
                {
                    string errMsg = "OpenVPNTunnel::Cleanup(): OpenVPN client is not initialized";

                    EddieLogger.Error(errMsg);

                    throw new Exception(errMsg);
                }

                client = openVPNClient;

                openVPNClient = null;
            }

            try
            {
                NativeMethods.EddieLibraryResult result = client.Stop();

                if (result.code != NativeMethods.ResultCode.SUCCESS)
                {
                    string errMsg = string.Format("OpenVPNTunnel::Cleanup(): Failed to stop OpenVPN client. {0}", result.description);

                    EddieLogger.Error(errMsg);

                    throw new Exception(errMsg);
                }
            }
            finally
            {
                ClearContexts();
            }
        }
        public override void OnTerminate()
        {
            if (m_initialized)
            {
                m_initialized = false;

                NativeMethods.EddieLibraryResult result = NativeMethods.EddieLibrary.Cleanup();

                if (result.code != NativeMethods.ResultCode.SUCCESS)
                {
                    EddieLogger.Error("Eddie Library: OpenVPN cleanup failed. {0}", result.description);
                }

                result = NativeMethods.EddieLibrary.DisposeClient();

                if (result.code != NativeMethods.ResultCode.SUCCESS)
                {
                    EddieLogger.Error("Eddie Library: Failed to dispose OpenVPN client. '{0}'", result.description);
                }
            }

            base.OnTerminate();
        }
Exemple #7
0
        public VPN.Status NetworkStatusChanged(VPNAction action)
        {
            if (vpnClientStatus == VPN.Status.LOCKED)
            {
                return(vpnClientStatus);
            }

            lock (clientSync)
            {
                if (openVPNClient == null)
                {
                    return(vpnClientStatus);
                }

                switch (action)
                {
                case VPNAction.RESUME:
                {
                    if (vpnClientStatus == VPN.Status.PAUSED)
                    {
                        EddieLogger.Info("Network is now connected, trying to resume VPN");

                        NativeMethods.EddieLibraryResult result = openVPNClient.Resume();

                        if (result.code == NativeMethods.ResultCode.SUCCESS)
                        {
                            EddieLogger.Info("Successfully resumed VPN");

                            vpnClientStatus = VPN.Status.CONNECTED;
                        }
                        else
                        {
                            EddieLogger.Error(string.Format("Failed to resume VPN. {0}", result.description));

                            vpnClientStatus = VPN.Status.UNKNOWN;
                        }
                    }
                }
                break;

                case VPNAction.PAUSE:
                case VPNAction.NETWORK_TYPE_CHANGED:
                {
                    EddieLogger.Info("Network status has changed, trying to pause VPN");

                    NativeMethods.EddieLibraryResult result = openVPNClient.Pause("Network status changed");

                    if (result.code == NativeMethods.ResultCode.SUCCESS)
                    {
                        EddieLogger.Info("Successfully paused VPN");

                        vpnClientStatus = VPN.Status.PAUSED;
                    }
                    else
                    {
                        EddieLogger.Error(string.Format("Failed to pause VPN. {0}", result.description));

                        vpnClientStatus = VPN.Status.NOT_CONNECTED;
                    }
                }
                break;

                case VPNAction.LOCK:
                {
                    EddieLogger.Info("VPN error detected. Locking VPN");

                    NativeMethods.EddieLibraryResult result = openVPNClient.Pause("Lock VPN");

                    if (result.code == NativeMethods.ResultCode.SUCCESS)
                    {
                        EddieLogger.Info("Successfully locked VPN");

                        vpnClientStatus = VPN.Status.LOCKED;
                    }
                    else
                    {
                        EddieLogger.Error(string.Format("Failed to lock VPN. {0}", result.description));

                        vpnClientStatus = VPN.Status.NOT_CONNECTED;
                    }
                }
                break;
                }
            }

            UpdateNotification(vpnClientStatus);

            return(vpnClientStatus);
        }
Exemple #8
0
        public VPN.Status HandleScreenChanged(bool active)
        {
            if (vpnClientStatus == VPN.Status.LOCKED)
            {
                return(vpnClientStatus);
            }

            lock (clientSync)
            {
                if (openVPNClient == null)
                {
                    return(vpnClientStatus);
                }

                if (active)
                {
                    if (vpnClientStatus == VPN.Status.PAUSED)
                    {
                        EddieLogger.Info("Screen is now on, trying to resume VPN");

                        NativeMethods.EddieLibraryResult result = openVPNClient.Resume();

                        if (result.code == NativeMethods.ResultCode.SUCCESS)
                        {
                            EddieLogger.Info("Successfully resumed VPN");

                            vpnClientStatus = VPN.Status.CONNECTED;
                        }
                        else
                        {
                            EddieLogger.Error(string.Format("Failed to resume VPN. {0}", result.description));

                            vpnClientStatus = VPN.Status.UNKNOWN;
                        }
                    }
                }
                else
                {
                    if (settingsManager.SystemPauseVpnWhenScreenIsOff)
                    {
                        EddieLogger.Info("Screen is now off, trying to pause VPN");

                        NativeMethods.EddieLibraryResult result = openVPNClient.Pause("Screen is off");

                        if (result.code == NativeMethods.ResultCode.SUCCESS)
                        {
                            EddieLogger.Info("Successfully paused VPN");

                            vpnClientStatus = VPN.Status.PAUSED;
                        }
                        else
                        {
                            EddieLogger.Error(string.Format("Failed to pause VPN. {0}", result.description));

                            vpnClientStatus = VPN.Status.NOT_CONNECTED;
                        }
                    }
                }
            }

            UpdateNotification(vpnClientStatus);

            return(vpnClientStatus);
        }