Пример #1
0
        /// <summary>
        ///     卸载 NF 驱动
        /// </summary>
        /// <returns>是否成功卸载</returns>
        public static bool UninstallDriver()
        {
            Global.MainForm.StatusText(i18N.TranslateFormat("Uninstalling {0}", "NF Service"));
            Logging.Info("卸载 NF 驱动");
            try
            {
                if (NFService.Status == ServiceControllerStatus.Running)
                {
                    NFService.Stop();
                    NFService.WaitForStatus(ServiceControllerStatus.Stopped);
                }
            }
            catch (Exception)
            {
                // ignored
            }

            if (!File.Exists(SystemDriver))
            {
                return(true);
            }
            NFAPI.nf_unRegisterDriver("netfilter2");
            File.Delete(SystemDriver);

            return(true);
        }
Пример #2
0
        public static bool UninstallDriver()
        {
            try
            {
                if (NFService.Status == ServiceControllerStatus.Running)
                {
                    NFService.Stop();
                    NFService.WaitForStatus(ServiceControllerStatus.Stopped);
                }
            }
            catch (Exception)
            {
                // ignored
            }

            if (!File.Exists(SystemDriver))
            {
                return(true);
            }
            try
            {
                NFAPI.nf_unRegisterDriver("netfilter2");

                File.Delete(SystemDriver);
                return(true);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Пример #3
0
        /// <summary>
        ///     卸载 NF 驱动
        /// </summary>
        /// <returns>是否成功卸载</returns>
        public static bool UninstallDriver()
        {
            Global.MainForm.StatusText("Uninstall netfilter2");
            Logging.Info("卸载NF驱动");
            try
            {
                if (NFService.Status == ServiceControllerStatus.Running)
                {
                    NFService.Stop();
                    NFService.WaitForStatus(ServiceControllerStatus.Stopped);
                }
            }
            catch (Exception)
            {
                // ignored
            }

            if (!File.Exists(SystemDriver))
            {
                return(true);
            }

            try
            {
                NFAPI.nf_unRegisterDriver("netfilter2");
            }
            catch (Exception e)
            {
                Logging.Error(e.ToString());
                return(false);
            }

            File.Delete(SystemDriver);
            return(true);
        }
Пример #4
0
        public bool UninstallDriver()
        {
            try
            {
                var service = new ServiceController("netfilter2");
                if (service.Status == ServiceControllerStatus.Running)
                {
                    service.Stop();
                    service.WaitForStatus(ServiceControllerStatus.Stopped);
                }
            }
            catch (Exception)
            {
                // ignored
            }

            if (!File.Exists(_driverPath))
            {
                return(true);
            }
            try
            {
                NFAPI.nf_unRegisterDriver("netfilter2");

                File.Delete(_driverPath);
                _systemDriverVersion = "";
                return(true);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Пример #5
0
        private void UninstallServiceToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Enabled = false;
            StatusText(i18N.Translate("Uninstalling Service"));

            Task.Run(() =>
            {
                var driver = $"{Environment.SystemDirectory}\\drivers\\netfilter2.sys";
                if (File.Exists(driver))
                {
                    try
                    {
                        var service = new ServiceController("netfilter2");
                        if (service.Status == ServiceControllerStatus.Running)
                        {
                            service.Stop();
                            service.WaitForStatus(ServiceControllerStatus.Stopped);
                        }
                    }
                    catch (Exception)
                    {
                        // 跳过
                    }

                    try
                    {
                        NFAPI.nf_unRegisterDriver("netfilter2");

                        File.Delete(driver);

                        MessageBox.Show(this, i18N.Translate("Service has been uninstalled"), i18N.Translate("Information"), MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(this, i18N.Translate("Error") + i18N.Translate(": ") + ex, i18N.Translate("Information"), MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                else
                {
                    MessageBox.Show(this, i18N.Translate("Service has been uninstalled"), i18N.Translate("Information"), MessageBoxButtons.OK, MessageBoxIcon.Information);
                }

                Enabled = true;
            });
        }
Пример #6
0
        /// <summary>
        ///		启动
        /// </summary>
        /// <param name="server">服务器</param>
        /// <param name="mode">模式</param>
        /// <param name="StopServiceAndRestart">先停止驱动服务再重新启动</param>
        /// <returns>是否成功</returns>
        public bool Start(Server server, Mode mode, bool StopServiceAndRestart)
        {
            if (!StopServiceAndRestart)
            {
                MainForm.Instance.StatusText(i18N.Translate("Starting Redirector"));
            }

            if (!File.Exists("bin\\Redirector.exe"))
            {
                return(false);
            }

            // 检查驱动是否存在
            if (File.Exists(driverPath))
            {
                // 生成系统版本
                var version    = $"{Environment.OSVersion.Version.Major.ToString()}.{Environment.OSVersion.Version.Minor.ToString()}";
                var driverName = "";

                switch (version)
                {
                case "10.0":
                    driverName = "Win-10.sys";
                    break;

                case "6.3":
                case "6.2":
                    driverName = "Win-8.sys";
                    break;

                case "6.1":
                case "6.0":
                    driverName = "Win-7.sys";
                    break;

                default:
                    Logging.Info($"不支持的系统版本:{version}");
                    return(false);
                }

                // 检查驱动版本号
                var SystemfileVerInfo = FileVersionInfo.GetVersionInfo(driverPath);
                var BinFileVerInfo    = FileVersionInfo.GetVersionInfo(string.Format("bin\\{0}", driverName));

                if (!SystemfileVerInfo.FileVersion.Equals(BinFileVerInfo.FileVersion))
                {
                    Logging.Info("开始更新驱动");
                    // 需要更新驱动
                    try
                    {
                        var service = new ServiceController("netfilter2");
                        if (service.Status == ServiceControllerStatus.Running)
                        {
                            service.Stop();
                            service.WaitForStatus(ServiceControllerStatus.Stopped);
                        }
                        NFAPI.nf_unRegisterDriver("netfilter2");

                        //删除老驱动
                        File.Delete(driverPath);
                        if (!InstallDriver())
                        {
                            return(false);
                        }

                        Logging.Info($"驱动更新完毕,当前驱动版本:{BinFileVerInfo.FileVersion}");
                    }
                    catch (Exception)
                    {
                        Logging.Info("更新驱动出错");
                    }
                }
            }
            else
            {
                if (!InstallDriver())
                {
                    return(false);
                }
            }

            try
            {
                // 启动驱动服务
                var service = new ServiceController("netfilter2");
                if (service.Status == ServiceControllerStatus.Running && StopServiceAndRestart)
                {
                    // 防止其他程序占用 重置 NF 百万连接数限制
                    service.Stop();
                    service.WaitForStatus(ServiceControllerStatus.Stopped);
                    MainForm.Instance.StatusText(i18N.Translate("Starting netfilter2 Service"));
                    service.Start();
                }
                else if (service.Status == ServiceControllerStatus.Stopped)
                {
                    MainForm.Instance.StatusText(i18N.Translate("Starting netfilter2 Service"));
                    service.Start();
                }
            }
            catch (Exception e)
            {
                Logging.Info(e.ToString());

                var result = NFAPI.nf_registerDriver("netfilter2");
                if (result != NF_STATUS.NF_STATUS_SUCCESS)
                {
                    Logging.Info($"注册驱动失败,返回值:{result}");
                    return(false);
                }
            }

            var processes = "";

            foreach (var proc in mode.Rule)
            {
                processes += proc;
                processes += ",";
            }
            processes += "NTT.exe";

            Instance = MainController.GetProcess();
            Instance.StartInfo.FileName  = "bin\\Redirector.exe";
            Instance.StartInfo.Arguments = "";

            if (server.Type != "Socks5")
            {
                Instance.StartInfo.Arguments += $"-r 127.0.0.1:{Global.Settings.Socks5LocalPort} -p \"{processes}\"";
            }
            else
            {
                var result = DNS.Lookup(server.Hostname);
                if (result == null)
                {
                    Logging.Info("无法解析服务器 IP 地址");
                    return(false);
                }

                Instance.StartInfo.Arguments += $"-r {result}:{server.Port} -p \"{processes}\"";
                if (!string.IsNullOrWhiteSpace(server.Username) && !string.IsNullOrWhiteSpace(server.Password))
                {
                    Instance.StartInfo.Arguments += $" -username \"{server.Username}\" -password \"{server.Password}\"";
                }
            }

            if (File.Exists("logging\\redirector.log"))
            {
                File.Delete("logging\\redirector.log");
            }

            Instance.StartInfo.Arguments += $" -t {Global.Settings.RedirectorTCPPort}";
            Logging.Info(Instance.StartInfo.Arguments);
            Instance.OutputDataReceived += OnOutputDataReceived;
            Instance.ErrorDataReceived  += OnOutputDataReceived;
            State = State.Starting;
            Instance.Start();
            Instance.BeginOutputReadLine();
            Instance.BeginErrorReadLine();

            for (var i = 0; i < 10; i++)
            {
                Thread.Sleep(1000);

                if (State == State.Started)
                {
                    return(true);
                }
            }

            Logging.Info("NF 进程启动超时");
            Stop();
            return(false);
        }