Exemple #1
0
        /// <summary>
        ///		启动
        /// </summary>
        /// <param name="server">服务器</param>
        /// <param name="mode">模式</param>
        /// <returns>是否启动成功</returns>
        public bool Start(Models.Server server, Models.Mode mode)
        {
            var result = false;

            switch (server.Type)
            {
            case "Socks5":
                if (mode.Type == 4)
                {
                    result = false;
                }
                else
                {
                    result = true;
                }
                break;

            case "Shadowsocks":
                KillProcess("Shadowsocks");
                if (pSSController == null)
                {
                    pSSController = new SSController();
                }
                result = pSSController.Start(server, mode);
                break;

            case "ShadowsocksR":
                KillProcess("ShadowsocksR");
                if (pSSRController == null)
                {
                    pSSRController = new SSRController();
                }
                result = pSSRController.Start(server, mode);
                break;

            case "VMess":
                KillProcess("v2ray");
                if (pVMessController == null)
                {
                    pVMessController = new VMessController();
                }
                result = pVMessController.Start(server, mode);
                break;

            default:
                break;
            }

            if (result)
            {
                if (mode.Type == 0)
                {
                    if (pNFController == null)
                    {
                        pNFController = new NFController();
                    }
                    // 进程代理模式,启动 NF 控制器
                    result = pNFController.Start(server, mode);
                }
                else if (mode.Type == 1)
                {
                    if (pTUNTAPController == null)
                    {
                        pTUNTAPController = new TUNTAPController();
                    }
                    // TUN/TAP 黑名单代理模式,启动 TUN/TAP 控制器
                    result = pTUNTAPController.Start(server, mode);
                }
                else if (mode.Type == 2)
                {
                    if (pTUNTAPController == null)
                    {
                        pTUNTAPController = new TUNTAPController();
                    }
                    // TUN/TAP 白名单代理模式,启动 TUN/TAP 控制器
                    result = pTUNTAPController.Start(server, mode);
                }
                else if (mode.Type == 3 || mode.Type == 5)
                {
                    if (pHTTPController == null)
                    {
                        pHTTPController = new HTTPController();
                    }
                    // HTTP 系统代理和 Socks5 和 HTTP 代理模式,启动 HTTP 控制器
                    result = pHTTPController.Start(server, mode);
                }
                else if (mode.Type == 4)
                {
                    // Socks5 代理模式,不需要启动额外的控制器
                }
                else
                {
                    result = false;
                }
            }

            if (!result)
            {
                Stop();
            }

            return(result);
        }
Exemple #2
0
        /// <summary>
        ///     启动
        /// </summary>
        /// <param name="server">服务器</param>
        /// <param name="mode">模式</param>
        /// <returns>是否启动成功</returns>
        public async Task <bool> Start(Server server, Mode mode)
        {
            Logging.Info($"启动主控制器: {server.Type} [{mode.Type}]{mode.Remark}");

            #region Record Settings

            _httpPort          = Global.Settings.HTTPLocalPort;
            _socks5Port        = Global.Settings.Socks5LocalPort;
            _redirectorTCPPort = Global.Settings.RedirectorTCPPort;
            _localAddress      = Global.Settings.LocalAddress;
            _savedServer       = server;
            _savedMode         = mode;

            #endregion

            FlushDNSResolverCache();
            _ = Task.Run(Firewall.AddNetchFwRules);

            bool result;
            if (server.Type == "Socks5")
            {
                result = mode.Type != 4;
            }
            else
            {
                pEncryptedProxyController = server.Type switch
                {
                    "SS" => new SSController(),
                    "SSR" => new SSRController(),
                    "VMess" => new VMessController(),
                    "Trojan" => new TrojanController(),
                    _ => pEncryptedProxyController
                };

                KillProcessByName(pEncryptedProxyController.MainFile);

                // 检查端口是否被占用
                var isPortNotAvailable = false;
                if (_savedServer.Type != "Socks5")
                {
                    isPortNotAvailable |= PortCheckAndShowMessageBox(_socks5Port, "Socks5");
                }

                switch (_savedMode.Type)
                {
                case 0:
                    isPortNotAvailable |= PortCheckAndShowMessageBox(_redirectorTCPPort, "Redirector TCP");
                    break;

                case 3:
                case 5:
                    isPortNotAvailable |= PortCheckAndShowMessageBox(_httpPort, "HTTP");
                    break;
                }

                if (isPortNotAvailable)
                {
                    Logging.Error("主控制器启动失败: 端口被占用");
                    return(false);
                }

                Global.MainForm.StatusText(i18N.Translate("Starting ", pEncryptedProxyController.Name));
                try
                {
                    result = await Task.Run(() => pEncryptedProxyController.Start(server, mode));
                }
                catch (Exception e)
                {
                    Logging.Error("加密代理启动失败,未处理异常: " + e);
                    result = false;
                }
            }

            if (result)
            {
                // 加密代理成功启动
                UsingPorts.Add(Global.Settings.Socks5LocalPort); // 记录Socks5使用端口

                switch (mode.Type)
                {
                case 0:     // 进程代理模式
                    pModeController = new NFController();
                    break;

                case 1:     // TUN/TAP 黑名单代理模式
                case 2:     // TUN/TAP 白名单代理模式
                    pModeController = new TUNTAPController();
                    break;

                case 3:
                case 5:
                    pModeController = new HTTPController();
                    break;

                case 4:     // Socks5 代理模式,不需要启动额外的Server
                    result = true;
                    break;
                }

                if (pModeController != null)
                {
                    Global.MainForm.StatusText(i18N.Translate("Starting ", pModeController.Name));
                    try
                    {
                        result = await Task.Run(() => pModeController.Start(server, mode));
                    }
                    catch (Exception e)
                    {
                        if (e is DllNotFoundException || e is FileNotFoundException)
                        {
                            MessageBoxX.Show(e.Message + "\n\n" + i18N.Translate("Missing File or runtime components"), owner: Global.MainForm);
                        }
                        else
                        {
                            Logging.Error("模式启动失败,未处理异常: " + e);
                        }
                        result = false;
                    }
                }

                if (result)
                {
                    // 成功启动

                    switch (mode.Type) // 记录使用端口
                    {
                    case 0:
                        UsingPorts.Add(_redirectorTCPPort);
                        break;

                    case 3:
                    case 5:
                        UsingPorts.Add(_httpPort);
                        break;
                    }

                    switch (mode.Type)
                    {
                    case 0:
                    case 1:
                    case 2:
                        _ = Task.Run(() =>
                        {
                            Global.MainForm.NatTypeStatusText(i18N.Translate("Starting NatTester"));
                            // Thread.Sleep(1000);
                            var(nttResult, natType, localEnd, publicEnd) = pNTTController.Start();
                            var country = Utils.Utils.GetCityCode(publicEnd);

                            if (nttResult)
                            {
                                Global.MainForm.NatTypeStatusText(natType, country);
                            }
                        });
                        break;
                    }
                }
            }

            if (!result)
            {
                Logging.Error("主控制器启动失败");
                try
                {
                    await Stop();
                }
                catch
                {
                    // ignored
                }
            }

            return(result);
        }
Exemple #3
0
        /// <summary>
        ///		启动
        /// </summary>
        /// <param name="server">服务器</param>
        /// <param name="mode">模式</param>
        /// <returns>是否启动成功</returns>
        public bool Start(Server server, Mode mode)
        {
            FlushDNSResolverCache();

            var result = false;

            switch (server.Type)
            {
            case "Socks5":
                if (mode.Type == 4)
                {
                    result = false;
                }
                else
                {
                    result = true;
                }
                break;

            case "SS":
                KillProcess("Shadowsocks");
                if (pSSController == null)
                {
                    pSSController = new SSController();
                }
                result = pSSController.Start(server, mode);
                break;

            case "SSR":
                KillProcess("ShadowsocksR");
                if (pSSRController == null)
                {
                    pSSRController = new SSRController();
                }
                result = pSSRController.Start(server, mode);
                break;

            case "VMess":
                KillProcess("v2ray");
                if (pVMessController == null)
                {
                    pVMessController = new VMessController();
                }
                result = pVMessController.Start(server, mode);
                break;

            case "Trojan":
                KillProcess("Trojan");
                if (pTrojanController == null)
                {
                    pTrojanController = new TrojanController();
                }
                result = pTrojanController.Start(server, mode);
                break;
            }

            if (result)
            {
                if (mode.Type == 0)
                {
                    if (pNFController == null)
                    {
                        pNFController = new NFController();
                    }
                    if (pNTTController == null)
                    {
                        pNTTController = new NTTController();
                    }
                    // 进程代理模式,启动 NF 控制器
                    result = pNFController.Start(server, mode, false);
                    if (!result)
                    {
                        MainForm.Instance.StatusText($"{i18N.Translate("Status")}{i18N.Translate(": ")}{i18N.Translate("Restarting Redirector")}");
                        Logging.Info("正常启动失败后尝试停止驱动服务再重新启动");
                        //正常启动失败后尝试停止驱动服务再重新启动
                        result = pNFController.Start(server, mode, true);
                    }
                    else
                    {
                        Task.Run(() => pNTTController.Start());
                    }
                }
                else if (mode.Type == 1)
                {
                    if (pTUNTAPController == null)
                    {
                        pTUNTAPController = new TUNTAPController();
                    }
                    if (pNTTController == null)
                    {
                        pNTTController = new NTTController();
                    }
                    // TUN/TAP 黑名单代理模式,启动 TUN/TAP 控制器
                    result = pTUNTAPController.Start(server, mode);
                    if (result)
                    {
                        Task.Run(() => pNTTController.Start());
                    }
                }
                else if (mode.Type == 2)
                {
                    if (pTUNTAPController == null)
                    {
                        pTUNTAPController = new TUNTAPController();
                    }
                    if (pNTTController == null)
                    {
                        pNTTController = new NTTController();
                    }
                    // TUN/TAP 白名单代理模式,启动 TUN/TAP 控制器
                    result = pTUNTAPController.Start(server, mode);
                    if (result)
                    {
                        Task.Run(() => pNTTController.Start());
                    }
                }
                else if (mode.Type == 3 || mode.Type == 5)
                {
                    if (pHTTPController == null)
                    {
                        pHTTPController = new HTTPController();
                    }
                    // HTTP 系统代理和 Socks5 和 HTTP 代理模式,启动 HTTP 控制器
                    result = pHTTPController.Start(server, mode);
                }
                else if (mode.Type == 4)
                {
                    // Socks5 代理模式,不需要启动额外的控制器
                }
                else
                {
                    result = false;
                }
            }

            if (!result)
            {
                Stop();
            }

            return(result);
        }