Beispiel #1
0
        protected override void OnStart(string[] args)
        {
            try
            {
                List <RegistryKey> rkOvpns = new List <RegistryKey>();

                // Search 64-bit registry, then 32-bit registry for OpenVpn
                var key = GetRegistrySubkey(RegistryView.Registry64);
                if (key != null)
                {
                    rkOvpns.Add(key);
                }
                key = GetRegistrySubkey(RegistryView.Registry32);
                if (key != null)
                {
                    rkOvpns.Add(key);
                }

                if (rkOvpns.Count() == 0)
                {
                    throw new Exception("Registry key missing");
                }

                var configDirsConsidered = new HashSet <string>();

                foreach (var rkOvpn in rkOvpns)
                {
                    try {
                        bool append = false;
                        {
                            var logAppend = (string)rkOvpn.GetValue("log_append");
                            if (logAppend[0] == '0' || logAppend[0] == '1')
                            {
                                append = logAppend[0] == '1';
                            }
                            else
                            {
                                throw new Exception("Log file append flag must be 1 or 0");
                            }
                        }

                        var config = new OpenVpnServiceConfiguration()
                        {
                            exePath       = (string)rkOvpn.GetValue("exe_path"),
                            configDir     = (string)rkOvpn.GetValue("config_dir"),
                            configExt     = "." + (string)rkOvpn.GetValue("config_ext"),
                            logDir        = (string)rkOvpn.GetValue("log_dir"),
                            logAppend     = append,
                            priorityClass = GetPriorityClass((string)rkOvpn.GetValue("priority")),

                            eventLog = EventLog,
                        };

                        if (configDirsConsidered.Contains(config.configDir))
                        {
                            continue;
                        }
                        configDirsConsidered.Add(config.configDir);

                        /// Only attempt to start the service
                        /// if openvpn.exe is present. This should help if there are old files
                        /// and registry settings left behind from a previous OpenVPN 32-bit installation
                        /// on a 64-bit system.
                        if (!File.Exists(config.exePath))
                        {
                            EventLog.WriteEntry("OpenVPN binary does not exist at " + config.exePath);
                            continue;
                        }

                        foreach (var configFilename in Directory.EnumerateFiles(config.configDir,
                                                                                "*" + config.configExt,
                                                                                System.IO.SearchOption.AllDirectories))
                        {
                            try {
                                var child = new OpenVpnChild(config, configFilename);
                                Subprocesses.Add(child);
                                child.Start();
                            }
                            catch (Exception e)
                            {
                                EventLog.WriteEntry("Caught exception " + e.Message + " when starting openvpn for "
                                                    + configFilename);
                            }
                        }
                    }
                    catch (NullReferenceException e) /* e.g. missing registry values */
                    {
                        EventLog.WriteEntry("Registry values are incomplete for " + rkOvpn.View.ToString() + e.StackTrace);
                    }
                }
            }
            catch (Exception e)
            {
                EventLog.WriteEntry("Exception occured during OpenVPN service start: " + e.Message + e.StackTrace);
                throw e;
            }
        }
Beispiel #2
0
        protected override void OnStart(string[] args)
        {
            try
            {
                List<RegistryKey> rkOvpns = new List<RegistryKey>();

                // Search 64-bit registry, then 32-bit registry for OpenVpn
                var key = GetRegistrySubkey(RegistryView.Registry64);
                if (key != null) rkOvpns.Add(key);
                key = GetRegistrySubkey(RegistryView.Registry32);
                if (key != null) rkOvpns.Add(key);

                if (rkOvpns.Count() == 0)
                    throw new Exception("Registry key missing");

                var configDirsConsidered = new HashSet<string>();

                foreach (var rkOvpn in rkOvpns)
                {
                    try {
                        bool append = false;
                        {
                            var logAppend = (string)rkOvpn.GetValue("log_append");
                            if (logAppend[0] == '0' || logAppend[0] == '1')
                                append = logAppend[0] == '1';
                            else
                                throw new Exception("Log file append flag must be 1 or 0");
                        }

                        var config = new OpenVpnServiceConfiguration()
                        {
                            exePath = (string)rkOvpn.GetValue("exe_path"),
                            configDir = (string)rkOvpn.GetValue("config_dir"),
                            configExt = "." + (string)rkOvpn.GetValue("config_ext"),
                            logDir = (string)rkOvpn.GetValue("log_dir"),
                            logAppend = append,
                            priorityClass = GetPriorityClass((string)rkOvpn.GetValue("priority")),

                            eventLog = EventLog,
                        };

                        if (configDirsConsidered.Contains(config.configDir)) {
                            continue;
                        }
                        configDirsConsidered.Add(config.configDir);

                        /// Only attempt to start the service
                        /// if openvpn.exe is present. This should help if there are old files
                        /// and registry settings left behind from a previous OpenVPN 32-bit installation
                        /// on a 64-bit system.
                        if (!File.Exists(config.exePath))
                        {
                            EventLog.WriteEntry("OpenVPN binary does not exist at " + config.exePath);
                            continue;
                        }

                        foreach (var configFilename in Directory.EnumerateFiles(config.configDir,
                                                                                "*" + config.configExt,
                                                                                System.IO.SearchOption.AllDirectories))
                        {
                            try {
                                var child = new OpenVpnChild(config, configFilename);
                                Subprocesses.Add(child);
                                child.Start();
                            }
                            catch (Exception e)
                            {
                                EventLog.WriteEntry("Caught exception " + e.Message + " when starting openvpn for "
                                    + configFilename);
                            }
                        }
                    }
                    catch (NullReferenceException e) /* e.g. missing registry values */
                    {
                        EventLog.WriteEntry("Registry values are incomplete for " + rkOvpn.View.ToString() + e.StackTrace);
                    }
                }

            }
            catch (Exception e)
            {
                EventLog.WriteEntry("Exception occured during OpenVPN service start: " + e.Message + e.StackTrace);
                throw e;
            }
        }