Exemplo n.º 1
0
        private static async Task <bool> StartServer(Server server, Mode mode)
        {
            switch (server.Type)
            {
            case "Socks5":
                return(true);

            case "SS":
                EncryptedProxyController = new SSController();
                break;

            case "SSR":
                EncryptedProxyController = new SSRController();
                break;

            case "VMess":
                EncryptedProxyController = new VMessController();
                break;

            case "Trojan":
                EncryptedProxyController = new TrojanController();
                break;

            default:
                Logging.Error("未知服务器类型");
                return(false);
            }

            Utils.Utils.KillProcessByName(EncryptedProxyController.MainFile);
            PortCheckAndShowMessageBox(Global.Settings.Socks5LocalPort, "Socks5");

            Global.MainForm.StatusText(i18N.Translate("Starting ", EncryptedProxyController.Name));
            if (await Task.Run(() => EncryptedProxyController.Start(server, mode)))
            {
                UsingPorts.Add(StatusPortInfoText.Socks5Port = Global.Settings.Socks5LocalPort);
                StatusPortInfoText.ShareLan = Global.Settings.LocalAddress == "0.0.0.0";
                return(true);
            }

            return(false);
        }
Exemplo n.º 2
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);
        }