Example #1
0
        public void Start()
        {
            m_command = new ElevatedProcess.Command();

            if (Platform.Instance.NeedExecuteOutsideAppPath(ExePath))
            {
                string tempPathToDelete = Utils.GetTempPath() + "/openvpn-" + RandomGenerator.GetHash();
                if (Platform.Instance.FileExists(tempPathToDelete))
                {
                    Platform.Instance.FileDelete(tempPathToDelete);
                }
                System.IO.File.Copy(ExePath, tempPathToDelete);

                ExePath = tempPathToDelete;

                DeleteAfterStart = true;
            }

            if (IsHummingbird)
            {
                m_command.Parameters["command"]     = "hummingbird";
                m_command.Parameters["gui-version"] = Constants.Name + Constants.VersionDesc;
            }
            else
            {
                m_command.Parameters["command"] = "process_openvpn";
            }
            m_command.Parameters["path"]   = ExePath;
            m_command.Parameters["config"] = ConfigPath;

            m_command.ExceptionEvent += delegate(ElevatedProcess.Command cmd, string message)
            {
                StdErr.Write("Error: " + message);
            };

            m_command.ReceiveEvent += delegate(ElevatedProcess.Command cmd, string data)
            {
                string feedbackType = data.Substring(0, 6);
                string feedbackData = data.Substring(7);

                if (feedbackType == "stdout")
                {
                    StdOut.Write(feedbackData);
                }
                else if (feedbackType == "stderr")
                {
                    StdErr.Write(feedbackData);
                }
                else if (feedbackType == "procid")
                {
                    m_pid = Conversions.ToInt32(feedbackData);
                    if (DeleteAfterStart)
                    {
                        Platform.Instance.FileDelete(ExePath);
                    }
                }
                else if (feedbackType == "return")
                {
                }
            };

            m_command.CompleteEvent += delegate(ElevatedProcess.Command cmd)
            {
                StdOut.Stop();
                StdErr.Stop();
                if (EndEvent != null)
                {
                    EndEvent();
                }
            };
            m_command.DoASync();
        }