コード例 #1
0
        /// <summary>
        /// Start the OpenVPN container process
        /// </summary>
        /// <param name="configFile">Config file path</param>
        /// <param name="username">Login Username</param>
        /// <param name="password">Login Password</param>
        public static void Run(string configFile, string username, string password)
        {
            if (IsRunning)
            {
                return;
            }
            ExitEvent.Reset();
            OpenVpnContainerProcess = new Process
            {
                EnableRaisingEvents = true,
                StartInfo           = new ProcessStartInfo()
                {
                    FileName = "openvpn.exe",
                    RedirectStandardInput  = true,
                    RedirectStandardOutput = true,
                    RedirectStandardError  = true,
                    UseShellExecute        = false,
                    CreateNoWindow         = true,
                    Verb        = "runas",
                    WindowStyle = ProcessWindowStyle.Hidden,
                    Arguments   = "--config " + configFile + " --service " + ExitEvent.Event + " 0",
                },
            };
            var task = EventRegistrar.Status.AddTask("Starting OpenVPN service");

            OpenVpnContainerProcess.OutputDataReceived += OpenVpnContainerProcessOnOutputDataReceived;
            OpenVpnContainerProcess.ErrorDataReceived  += OpenVpnContainerProcessOnErrorDataReceived;
            OpenVpnContainerProcess.Exited             += OpenVpnContainerProcess_Exited;
            OpenVpnContainerProcess.Start();
            OpenVpnContainerProcess.BeginErrorReadLine();
            OpenVpnContainerProcess.BeginOutputReadLine();
            //Send username(dont have to wait)
            Write(username);
            //Wait for password prompt
            Thread.Sleep(1000);
            //Send password
            Write(password);
            task.Lock();
        }