protected override void OnCreate2(Bundle?savedInstanceState)
        {
            base.OnCreate2(savedInstanceState);

            this.SetSupportActionBarWithNavigationClick(binding !.toolbar, true);

            R.Subscribe(() =>
            {
                Title = ViewModel !.Title;
                if (binding == null)
                {
                    return;
                }
                binding.tvSystemProxyIp.Text              = AppResources.Settings_Proxy_SystemProxyIp;
                binding.tvCustomDNS.Text                  = AppResources.Settings_Proxy_CustomDNS;
                binding.tvProgramStartupRunProxy.Text     = AppResources.CommunityFix_AutoRunProxy;
                binding.tvOnlyEnableProxyScript.Text      = AppResources.CommunityFix_ProxyOnlyOnScript;
                binding.tvProxySettingsSocks5.Text        = AppResources.Settings_Proxy_Socks5Proxy;
                binding.tvSocks5ProxyPortId.Text          = AppResources.Settings_Proxy_ProxyPort;
                binding.tvProxySettingsTwoLevelAgent.Text = AppResources.Settings_Proxy_TwoLevelAgent;
                binding.tvTwoLevelAgentProxyType.Text     = AppResources.Settings_Proxy_ProxyType;
                binding.tvTwoLevelAgentIp.Text            = AppResources.Settings_Proxy_IPAddress;
                binding.tvTwoLevelAgentPortId.Text        = AppResources.Settings_Proxy_Port;
                binding.tvTwoLevelAgentUserName.Text      = AppResources.Settings_Proxy_UserName;
                binding.tvTwoLevelAgentPassword.Text      = AppResources.Settings_Proxy_Password;
                binding.tvIsVpnMode.Text                  = AppResources.CommunityFix_IsVpnMode;
            }).AddTo(this);

            CreateArrayAdapter(binding.tbSystemProxyIp,
                               items: ViewModel !.SystemProxyIps);
            CreateArrayAdapter(binding.tbCustomDNS,
                               items: ViewModel !.ProxyDNSs);
            CreateArrayAdapter(binding.tbTwoLevelAgentProxyType,
                               items: ViewModel.ProxyTypes.Select(x => x.ToString()));

            SetOnClickListener(
                binding.layoutRootProgramStartupRunProxy,
                binding.layoutRootOnlyEnableProxyScript,
                binding.layoutRootProxySettingsSocks5,
                binding.layoutRootProxySettingsTwoLevelAgent,
                binding.layoutRootIsVpnMode);

            SetProgramStartupRunProxy();
            SetOnlyEnableProxyScript();
            SetProxySettingsSocks5();
            SetProxySettingsTwoLevelAgent();
            SetIsVpnMode();

            #region Binding TextBox

            binding.tbSystemProxyIp.Text         = SystemProxyIp.Value;
            binding.tbSystemProxyIp.TextChanged += (_, _) =>
            {
                var value = binding.tbSystemProxyIp.Text;
                if (IPAddress.TryParse(value, out var _))
                {
                    SystemProxyIp.Value = value;
                }
            };

            binding.tbCustomDNS.Text         = ProxyMasterDns.Value;
            binding.tbCustomDNS.TextChanged += (_, _) =>
            {
                var value = binding.tbCustomDNS.Text;
                if (IPAddress.TryParse(value, out var _))
                {
                    ProxyMasterDns.Value = value;
                }
            };

            binding.tbSocks5ProxyPortId.Text         = Socks5ProxyPortId.Value.ToString();
            binding.tbSocks5ProxyPortId.TextChanged += (_, _) =>
            {
                var value = binding.tbSocks5ProxyPortId.Text;
                if (ModelValidatorProvider.IsPortId(value, out var value2))
                {
                    Socks5ProxyPortId.Value = value2;
                }
            };

            binding.tbTwoLevelAgentIp.Text         = TwoLevelAgentIp.Value;
            binding.tbTwoLevelAgentIp.TextChanged += (_, _) =>
            {
                var value = binding.tbTwoLevelAgentIp.Text;
                if (IPAddress.TryParse(value, out var _))
                {
                    TwoLevelAgentIp.Value = value;
                }
            };

            binding.tbTwoLevelAgentPortId.Text         = TwoLevelAgentPortId.Value.ToString();
            binding.tbTwoLevelAgentPortId.TextChanged += (_, _) =>
            {
                var value = binding.tbTwoLevelAgentPortId.Text;
                if (ModelValidatorProvider.IsPortId(value, out var value2))
                {
                    TwoLevelAgentPortId.Value = value2;
                }
            };

            binding.tbTwoLevelAgentUserName.Text         = TwoLevelAgentUserName.Value;
            binding.tbTwoLevelAgentUserName.TextChanged += (_, _) =>
            {
                var value = binding.tbTwoLevelAgentUserName.Text;
                TwoLevelAgentUserName.Value = value;
            };

            binding.tbTwoLevelAgentPassword.Text         = TwoLevelAgentPassword.Value;
            binding.tbTwoLevelAgentPassword.TextChanged += (_, _) =>
            {
                var value = binding.tbTwoLevelAgentPassword.Text;
                TwoLevelAgentPassword.Value = value;
            };

            binding.tbTwoLevelAgentProxyType.Text =
                ((ExternalProxyType)TwoLevelAgentProxyType.Value).ToString();
            binding.tbTwoLevelAgentProxyType.TextChanged += (_, _) =>
            {
                var value = binding.tbTwoLevelAgentProxyType.Text;
                if (Enum.TryParse <ExternalProxyType>(value, true, out var value2))
                {
                    TwoLevelAgentProxyType.Value = (short)value2;
                }
            };

            #endregion
        }
Exemplo n.º 2
0
        private ProxyService()
        {
            mCurrent = this;

            ProxyDomains = new SourceList <AccelerateProjectGroupDTO>();
            ProxyScripts = new SourceList <ScriptDTO>();

            ProxyDomains
            .Connect()
            .ObserveOn(RxApp.MainThreadScheduler)
            .Sort(SortExpressionComparer <AccelerateProjectGroupDTO> .Ascending(x => x.Order).ThenBy(x => x.Name))
            .Bind(out _ProxyDomainsList)
            .Subscribe(_ => SelectGroup = ProxyDomains.Items.FirstOrDefault());


            this.WhenValueChanged(x => x.ProxyStatus, false)
            .Subscribe(async x =>
            {
                if (x)
                {
                    httpProxyService.ProxyDomains   = EnableProxyDomains;
                    httpProxyService.IsEnableScript = ProxySettings.IsEnableScript.Value;
                    httpProxyService.Scripts        = EnableProxyScripts;
                    if (IApplication.IsDesktopPlatform)
                    {
                        httpProxyService.IsOnlyWorkSteamBrowser = ProxySettings.IsOnlyWorkSteamBrowser.Value;
                        httpProxyService.IsSystemProxy          = ProxySettings.EnableWindowsProxy.Value;
                        httpProxyService.IsProxyGOG             = ProxySettings.IsProxyGOG.Value;
                    }
                    else
                    {
                        httpProxyService.IsSystemProxy = true;
                    }

                    // macOS 上目前因权限问题仅支持 0.0.0.0(IPAddress.Any)
                    httpProxyService.ProxyIp = (!OperatingSystem2.IsMacOS && IPAddress2.TryParse(ProxySettings.SystemProxyIp.Value, out var ip)) ? ip : IPAddress.Any;

                    httpProxyService.Socks5ProxyEnable = ProxySettings.Socks5ProxyEnable.Value;
                    httpProxyService.Socks5ProxyPortId = ProxySettings.Socks5ProxyPortId.Value;
                    if (!ModelValidatorProvider.IsPortId(httpProxyService.Socks5ProxyPortId))
                    {
                        httpProxyService.Socks5ProxyPortId = ProxySettings.DefaultSocks5ProxyPortId;
                    }

                    //httpProxyService.HostProxyPortId = ProxySettings.HostProxyPortId;
                    httpProxyService.TwoLevelAgentEnable = ProxySettings.TwoLevelAgentEnable.Value;

                    httpProxyService.TwoLevelAgentProxyType = (ExternalProxyType)ProxySettings.TwoLevelAgentProxyType.Value;
                    if (!httpProxyService.TwoLevelAgentProxyType.IsDefined())
                    {
                        httpProxyService.TwoLevelAgentProxyType = IHttpProxyService.DefaultTwoLevelAgentProxyType;
                    }

                    httpProxyService.TwoLevelAgentIp     = IPAddress2.TryParse(ProxySettings.TwoLevelAgentIp.Value, out var ip_t) ? ip_t.ToString() : IPAddress.Loopback.ToString();
                    httpProxyService.TwoLevelAgentPortId = ProxySettings.TwoLevelAgentPortId.Value;
                    if (!ModelValidatorProvider.IsPortId(httpProxyService.TwoLevelAgentPortId))
                    {
                        httpProxyService.TwoLevelAgentPortId = ProxySettings.DefaultTwoLevelAgentPortId;
                    }
                    httpProxyService.TwoLevelAgentUserName = ProxySettings.TwoLevelAgentUserName.Value;
                    httpProxyService.TwoLevelAgentPassword = ProxySettings.TwoLevelAgentPassword.Value;

                    httpProxyService.ProxyDNS = IPAddress2.TryParse(ProxySettings.ProxyMasterDns.Value, out var dns) ? dns : null;

                    this.RaisePropertyChanged(nameof(EnableProxyDomains));
                    this.RaisePropertyChanged(nameof(EnableProxyScripts));

                    if (!httpProxyService.IsSystemProxy)
                    {
                        const ushort httpsPort = 443;
                        var inUse = httpProxyService.PortInUse(httpsPort);
                        if (inUse)
                        {
                            string?error_CommunityFix_StartProxyFaild443 = null;
                            if (OperatingSystem2.IsWindows)
                            {
                                var p = SocketHelper.GetProcessByTcpPort(httpsPort);
                                if (p != null)
                                {
                                    error_CommunityFix_StartProxyFaild443 = AppResources.CommunityFix_StartProxyFaild443___.Format(httpsPort, p.ProcessName, p.Id);
                                }
                            }
                            error_CommunityFix_StartProxyFaild443 ??= AppResources.CommunityFix_StartProxyFaild443_.Format(httpsPort);
                            Toast.Show(error_CommunityFix_StartProxyFaild443);
                            return;
                        }
                    }

                    var isRun = await httpProxyService.StartProxy();

                    if (isRun)
                    {
                        if (!httpProxyService.IsSystemProxy)
                        {
                            if (httpProxyService.ProxyDomains.Any_Nullable())
                            {
                                var localhost = IPAddress.Any.Equals(httpProxyService.ProxyIp) ? IPAddress.Loopback.ToString() : httpProxyService.ProxyIp.ToString();

                                var hosts = httpProxyService.ProxyDomains !.SelectMany(s =>
                                {
                                    if (s == null)
                                    {
                                        return(default !);