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);
                }
            }
        }
Beispiel #2
0
        private void WaitForVpnThreadToFinish()
        {
            if (vpnThread == null)
            {
                return;
            }

            try
            {
                vpnThread.Join(THREAD_MAX_JOIN_TIME);
            }
            catch (Java.Lang.InterruptedException)
            {
                EddieLogger.Error("VPNService.WaitVpnThreadToFinish(): VPN thread has been interrupted");
            }
            finally
            {
                if (vpnThread.IsAlive)
                {
                    EddieLogger.Error("VPNService.WaitVpnThreadToFinish(): VPN thread did not end");
                }
                else
                {
                    EddieLogger.Info("VPN thread execution has completed");
                }
            }
        }
Beispiel #3
0
        private void ResumeVPNAfterSeconds(int seconds)
        {
            EddieLogger.Info("VPN will be resumed in {0} seconds", seconds);

            System.Timers.Timer timer = new System.Timers.Timer();

            timer.Elapsed += (sender, args) =>
            {
                EddieLogger.Info("Trying to resume VPN");

                NetworkStatusChanged(VPNAction.RESUME);

                timer.Enabled = false;
            };

            timer.Interval = seconds * 1000;
            timer.Enabled  = true;
        }
Beispiel #4
0
        private void DoStart(Bundle data)
        {
            LastError = "";

            DoChangeStatus(VPN.Status.CONNECTING);

            if ((Application as AndroidApplication).Initialized)
            {
                try
                {
                    TunnelSetup(data);
                }
                catch (Exception e)
                {
                    LastError = "Tunnel start failed: " + e.Message;

                    DoStopService();
                }

                Java.Lang.Thread newVpnTask = SupportTools.StartThread(new Java.Lang.Runnable(() =>
                {
                    EddieLogger.Info("Starting VPN thread");

                    vpnTunnel.Run();
                }));

                if (newVpnTask != null)
                {
                    vpnThread = newVpnTask;
                }
            }
            else
            {
                LastError = "Initialization failed";

                DoStopService();
            }
        }
Beispiel #5
0
        public void OnNetworkStatusConnected()
        {
            string text, server = "";
            Dictionary <string, string> pData = settingsManager.SystemLastProfileInfo;

            EddieLogger.Info("Network is connected to {0}", NetworkStatusReceiver.GetNetworkDescription());

            NetworkStatusChanged(OpenVPNTunnel.VPNAction.RESUME);

            if (pData.Count > 0 && pData.ContainsKey("server"))
            {
                server = pData["server"];
            }

            text = String.Format(Resources.GetString(Resource.String.notification_text), server);

            if (!NetworkStatusReceiver.GetNetworkDescription().Equals(""))
            {
                text += " " + String.Format(Resources.GetString(Resource.String.notification_network), NetworkStatusReceiver.GetNetworkDescription());
            }

            UpdateNotification(text);
        }
Beispiel #6
0
        public void OnNetworkTypeChanged()
        {
            EddieLogger.Info("Network type has changed to {0}", NetworkStatusReceiver.GetNetworkDescription());

            NetworkStatusChanged(OpenVPNTunnel.VPNAction.NETWORK_TYPE_CHANGED);
        }
Beispiel #7
0
        public void OnNetworkStatusNotConnected()
        {
            EddieLogger.Info("Network is not connected");

            NetworkStatusChanged(OpenVPNTunnel.VPNAction.PAUSE);
        }
Beispiel #8
0
        public void OnNetworkStatusSuspended()
        {
            EddieLogger.Info("Network is suspended");

            NetworkStatusChanged(OpenVPNTunnel.VPNAction.PAUSE);
        }
Beispiel #9
0
        public void OnNetworkStatusIsDisonnecting()
        {
            EddieLogger.Info("Network is disconnecting");

            NetworkStatusChanged(OpenVPNTunnel.VPNAction.PAUSE);
        }
Beispiel #10
0
        // NetworkStatusReceiver

        public void OnNetworkStatusNotAvailable()
        {
            EddieLogger.Info("Network is not available");

            NetworkStatusChanged(OpenVPNTunnel.VPNAction.PAUSE);
        }
Beispiel #11
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);
        }
Beispiel #12
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);
        }
Beispiel #13
0
        private void OnEvent(ref NativeMethods.EddieLibraryEvent oe)
        {
            try
            {
                switch (oe.type)
                {
                case NativeMethods.EventType.MESSAGE:
                case NativeMethods.EventType.INFO:
                {
                    EddieLogger.Info("{0}: {1}", oe.name, oe.info);
                }
                break;

                case NativeMethods.EventType.WARN:
                {
                    EddieLogger.Warning("OpenVPN {0}: {1}", oe.name, oe.info);
                }
                break;

                case NativeMethods.EventType.ERROR:
                case NativeMethods.EventType.FATAL_ERROR:
                {
                    if (IgnoredError(oe.name))
                    {
                        EddieLogger.Warning("OpenVPN {0}: {1}", oe.name, oe.info);
                    }
                    else
                    {
                        // It seems OpenVPN is having BIG troubles with the connection
                        // In order to prevent worse conditions, try to lock the VPN (in case it is possible)

                        EddieLogger.Error("OpenVPN3 Fatal Error {0}: {1}", oe.name, oe.info);

                        NetworkStatusChanged(VPNAction.LOCK);

                        vpnService.DoChangeStatus(VPN.Status.LOCKED);

                        AlertNotification(vpnService.Resources.GetString(Resource.String.connection_vpn_error));
                    }
                }
                break;

                case NativeMethods.EventType.FORMAL_WARNING:
                {
                    if (IgnoredError(oe.name))
                    {
                        EddieLogger.Warning("OpenVPN {0}: {1}", oe.name, oe.info);
                    }
                    else
                    {
                        // It seems OpenVPN is having troubles with the connection
                        // In order to prevent worse conditions, lock the VPN

                        EddieLogger.Error("OpenVPN3 {0}: {1}", oe.name, oe.info);

                        NetworkStatusChanged(VPNAction.LOCK);

                        vpnService.DoChangeStatus(VPN.Status.LOCKED);

                        AlertNotification(vpnService.Resources.GetString(Resource.String.connection_vpn_formal_warning));
                    }
                }
                break;

                case NativeMethods.EventType.TUN_ERROR:
                case NativeMethods.EventType.CLIENT_RESTART:
                case NativeMethods.EventType.AUTH_FAILED:
                case NativeMethods.EventType.CERT_VERIFY_FAIL:
                case NativeMethods.EventType.TLS_VERSION_MIN:
                case NativeMethods.EventType.CLIENT_HALT:
                case NativeMethods.EventType.CLIENT_SETUP:
                case NativeMethods.EventType.CONNECTION_TIMEOUT:
                case NativeMethods.EventType.INACTIVE_TIMEOUT:
                case NativeMethods.EventType.DYNAMIC_CHALLENGE:
                case NativeMethods.EventType.PROXY_NEED_CREDS:
                case NativeMethods.EventType.PROXY_ERROR:
                case NativeMethods.EventType.TUN_SETUP_FAILED:
                case NativeMethods.EventType.TUN_IFACE_CREATE:
                case NativeMethods.EventType.TUN_IFACE_DISABLED:
                case NativeMethods.EventType.EPKI_ERROR:
                case NativeMethods.EventType.EPKI_INVALID_ALIAS:
                {
                    // These OpenVPN events may cause a fatal error
                    // In order to prevent worse conditions, lock the VPN

                    EddieLogger.Error("OpenVPN {0}: {1}", oe.name, oe.info);

                    NetworkStatusChanged(VPNAction.LOCK);

                    vpnService.DoChangeStatus(VPN.Status.LOCKED);

                    AlertNotification(vpnService.Resources.GetString(Resource.String.connection_vpn_formal_warning));
                }
                break;

                case NativeMethods.EventType.CONNECTED:
                {
                    if (oe.data.ToInt64() != 0)
                    {
                        NativeMethods.ovpn3_connection_data connectionData = Marshal.PtrToStructure <NativeMethods.ovpn3_connection_data>(oe.data);

                        EddieLogger.Info("CONNECTED: defined={0}, user={1}, serverHost={2}, serverPort={3}, serverProto={4}, serverIp={5}, vpnIp4={6}, vpnIp6={7}, gw4={8}, gw6={9}, clientIp={10}, tunName={11}", connectionData.defined, connectionData.user, connectionData.serverHost, connectionData.serverPort, connectionData.serverProto, connectionData.serverIp, connectionData.vpnIp4, connectionData.vpnIp6, connectionData.gw4, connectionData.gw6, connectionData.clientIp, connectionData.tunName);
                    }
                }
                break;

                case NativeMethods.EventType.TRANSPORT_ERROR:
                case NativeMethods.EventType.RELAY_ERROR:
                case NativeMethods.EventType.DISCONNECTED:
                {
                    EddieLogger.Warning("OpenVPN {0} - {1}: {2}", oe.type, oe.name, oe.info);
                }
                break;

                default:
                {
                    EddieLogger.Debug("OpenVPN Event: type={0}, name={1}, info={2}, data={3}", oe.type, oe.name, oe.info, oe.data.ToString());
                }
                break;
                }
            }
            catch (Exception e)
            {
                EddieLogger.Error("OnEvent", e);
            }
        }