Exemple #1
0
        /// <summary>
        ///     停止
        /// </summary>
        public static async Task Stop()
        {
            _httpPort     = _socks5Port = _redirectorTCPPort = 0;
            _localAddress = null;
            _savedMode    = null;
            _savedServer  = null;
            UsingPorts.Clear();

            var tasks = new Task[]
            {
                Task.Run(() => EncryptedProxyController?.Stop()),
                Task.Run(() => ModeController?.Stop()),
                Task.Run(() => NTTController.Stop())
            };
            await Task.WhenAll(tasks);
        }
Exemple #2
0
        private static async Task <bool> StartMode(Server server, Mode mode)
        {
            var port = 0;

            switch (mode.Type)
            {
            case 0:
                ModeController = new NFController();
                PortCheckAndShowMessageBox(port = Global.Settings.RedirectorTCPPort, "Redirector TCP");
                break;

            case 1:
            case 2:
                ModeController = new TUNTAPController();
                break;

            case 3:
            case 5:
                ModeController = new HTTPController();
                PortCheckAndShowMessageBox(port = Global.Settings.HTTPLocalPort, "HTTP");
                break;

            case 4:
                return(true);

            default:
                Logging.Error("未知模式类型");
                return(false);
            }

            Global.MainForm.StatusText(i18N.Translate("Starting ", ModeController.Name));
            if (await Task.Run(() => ModeController.Start(server, mode)))
            {
                switch (mode.Type)
                {
                case 3:
                case 5:
                    StatusPortInfoText.HttpPort = port;
                    break;
                }

                UsingPorts.Add(port);
                return(true);
            }

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

            var result = false;

            if (server.Type == "Socks5")
            {
                result = mode.Type != 4;
            }
            else
            {
                switch (server.Type)
                {
                case "SS":
                    pEncryptedProxyController = new SSController();
                    break;

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

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

                case "Trojan":
                    pEncryptedProxyController = new TrojanController();
                    break;
                }

                MainForm.Instance.StatusText(i18N.Translate("Starting ", pEncryptedProxyController.AkaName));
                if (pEncryptedProxyController.Ready)
                {
                    result = pEncryptedProxyController.Start(server, mode);
                }
            }

            if (result)
            {
                // 加密代理已启动
                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 && pModeController.Ready)
                {
                    MainForm.Instance.StatusText(i18N.Translate("Starting ", pModeController.AkaName));
                    result = pModeController.Start(server, mode);
                }

                switch (mode.Type)
                {
                case 0:
                case 1:
                case 2:
                    if (result)
                    {
                        Task.Run(() =>
                        {
                            MainForm.Instance.NatTypeStatusText(i18N.Translate("Starting NatTester"));
                            // Thread.Sleep(1000);
                            var(nttResult, natType, _, _) = pNTTController.Start();
                            if (nttResult)
                            {
                                MainForm.Instance.NatTypeStatusText(natType);
                            }
                        });
                    }
                    break;
                }
            }

            if (!result)
            {
                Stop();
            }

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

            var result = false;

            if (server.Type == "Socks5")
            {
                result = mode.Type != 4;
            }
            else
            {
                switch (server.Type)
                {
                case "SS":
                    pEncryptedProxyController = new SSController();
                    break;

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

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

                case "Trojan":
                    pEncryptedProxyController = new TrojanController();
                    break;
                }

                Global.MainForm.StatusText(i18N.Translate("Starting ", pEncryptedProxyController.Name));
                if (pEncryptedProxyController.Ready)
                {
                    result = pEncryptedProxyController.Start(server, mode);
                }
            }

            if (result)
            {
                Logging.Info("加密代理已启动");
                // 加密代理已启动
                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 && pModeController.Ready)
                {
                    Global.MainForm.StatusText(i18N.Translate("Starting ", pModeController.Name));
                    result = pModeController.Start(server, mode);
                }

                if (result)
                {
                    Logging.Info("模式已启动");
                    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)
            {
                Stop();
            }

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

            var result = false;

            if (server.Type == "Socks5")
            {
                result = mode.Type != 4;
            }
            else
            {
                switch (server.Type)
                {
                case "SS":
                    pEncryptedProxyController = new SSController();
                    break;

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

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

                case "Trojan":
                    pEncryptedProxyController = new TrojanController();
                    break;
                }

                KillProcessByName(pEncryptedProxyController.MainFile);

                // 检查端口是否被占用
                if (PortHelper.PortInUse(Global.Settings.Socks5LocalPort))
                {
                    MessageBoxX.Show(i18N.TranslateFormat("The {0} port is in use.", "Socks5"));
                    return(false);
                }

                if (PortHelper.PortInUse(Global.Settings.HTTPLocalPort))
                {
                    MessageBoxX.Show(i18N.TranslateFormat("The {0} port is in use.", "HTTP"));
                    return(false);
                }

                if (PortHelper.PortInUse(Global.Settings.RedirectorTCPPort, PortType.TCP))
                {
                    MessageBoxX.Show(i18N.TranslateFormat("The {0} port is in use.", "Redirector TCP"));
                    return(false);
                }

                Global.MainForm.StatusText(i18N.Translate("Starting ", pEncryptedProxyController.Name));
                result = pEncryptedProxyController.Start(server, mode);
            }

            if (result)
            {
                Logging.Info("加密代理已启动");
                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));
                    result = pModeController.Start(server, mode);
                }

                if (result)
                {
                    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)
            {
                Stop();
            }

            return(result);
        }
Exemple #6
0
        /// <summary>
        ///     启动
        /// </summary>
        /// <param name="server">服务器</param>
        /// <param name="mode">模式</param>
        /// <returns>是否启动成功</returns>
        public 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();

            bool result;
            if (server.Type == "Socks5")
            {
                result = mode.Type != 4;
            }
            else
            {
                switch (server.Type)
                {
                case "SS":
                    pEncryptedProxyController = new SSController();
                    break;

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

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

                case "Trojan":
                    pEncryptedProxyController = new TrojanController();
                    break;
                }

                KillProcessByName(pEncryptedProxyController.MainFile);

                // 检查端口是否被占用
                if (PortHelper.PortInUse(Global.Settings.Socks5LocalPort))
                {
                    MessageBoxX.Show(i18N.TranslateFormat("The {0} port is in use.", "Socks5"));
                    return(false);
                }

                if (PortHelper.PortInUse(Global.Settings.HTTPLocalPort))
                {
                    MessageBoxX.Show(i18N.TranslateFormat("The {0} port is in use.", "HTTP"));
                    return(false);
                }

                if (PortHelper.PortInUse(Global.Settings.RedirectorTCPPort, PortType.TCP))
                {
                    MessageBoxX.Show(i18N.TranslateFormat("The {0} port is in use.", "Redirector TCP"));
                    return(false);
                }

                Global.MainForm.StatusText(i18N.Translate("Starting ", pEncryptedProxyController.Name));
                result = pEncryptedProxyController.Start(server, mode);
            }

            if (result)
            {
                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));
                    result = pModeController.Start(server, mode);
                }

                if (result)
                {
                    #region Add UsingPorts

                    switch (mode.Type)
                    {
                    // 成功启动
                    case 3:
                    case 5:
                        UsingPorts.Add(Global.Settings.HTTPLocalPort);
                        break;

                    case 0:
                        UsingPorts.Add(Global.Settings.RedirectorTCPPort);
                        break;
                    }

                    if (server.Type != "Socks5")
                    {
                        UsingPorts.Add(Global.Settings.Socks5LocalPort);
                    }

                    #endregion

                    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("主控制器启动失败");
                Stop();
            }

            return(result);
        }