Exemple #1
0
        public void UpdateSubscriptionProcess(Config config, bool blProxy, Action <bool, string> update)
        {
            _config     = config;
            _updateFunc = update;

            _updateFunc(false, ResUI.MsgUpdateSubscriptionStart);

            if (config.subItem == null || config.subItem.Count <= 0)
            {
                _updateFunc(false, ResUI.MsgNoValidSubscription);
                return;
            }

            Task.Run(async() =>
            {
                foreach (var item in config.subItem)
                {
                    if (item.enabled == false)
                    {
                        continue;
                    }
                    string id        = item.id.TrimEx();
                    string url       = item.url.TrimEx();
                    string userAgent = item.userAgent.TrimEx();
                    string groupId   = item.groupId.TrimEx();
                    string hashCode  = $"{item.remarks}->";
                    if (Utils.IsNullOrEmpty(id) || Utils.IsNullOrEmpty(url))
                    {
                        //_updateFunc(false, $"{hashCode}{ResUI.MsgNoValidSubscription}");
                        continue;
                    }

                    _updateFunc(false, $"{hashCode}{ResUI.MsgStartGettingSubscriptions}");
                    var result = await(new DownloadHandle()).DownloadStringAsync(url, blProxy, userAgent);

                    _updateFunc(false, $"{hashCode}{ResUI.MsgGetSubscriptionSuccessfully}");
                    if (Utils.IsNullOrEmpty(result))
                    {
                        _updateFunc(false, $"{hashCode}{ResUI.MsgSubscriptionDecodingFailed}");
                    }
                    else
                    {
                        int ret = ConfigHandler.AddBatchServers(ref config, result, id, groupId);
                        if (ret > 0)
                        {
                            _updateFunc(false, $"{hashCode}{ResUI.MsgUpdateSubscriptionEnd}");
                        }
                        else
                        {
                            _updateFunc(false, $"{hashCode}{ResUI.MsgFailedImportSubscription}");
                        }
                    }
                    _updateFunc(false, $"-------------------------------------------------------");
                }
                _updateFunc(true, $"{ResUI.MsgUpdateSubscriptionEnd}");
            });
        }
Exemple #2
0
        public int AddBatchServers(Config config, string clipboardData, string subid = "")
        {
            int counter;

            int _Add()
            {
                return(ConfigHandler.AddBatchServers(ref config, clipboardData, subid));
            }

            counter = _Add();
            if (counter < 1)
            {
                clipboardData = Utils.Base64Decode(clipboardData);
                counter       = _Add();
            }

            return(counter);
        }
Exemple #3
0
        public void UpdateSubscriptionProcess(Config config, string groupId, bool blProxy, Action <bool, string> update)
        {
            _config     = config;
            _updateFunc = update;

            _updateFunc(false, ResUI.MsgUpdateSubscriptionStart);

            if (config.subItem == null || config.subItem.Count <= 0)
            {
                _updateFunc(false, ResUI.MsgNoValidSubscription);
                return;
            }

            Task.Run(async() =>
            {
                //Turn off system proxy
                bool bSysProxyType = false;
                if (!blProxy && config.sysProxyType == ESysProxyType.ForcedChange)
                {
                    bSysProxyType       = true;
                    config.sysProxyType = ESysProxyType.ForcedClear;
                    SysProxyHandle.UpdateSysProxy(config, false);
                    Thread.Sleep(3000);
                }

                foreach (var item in config.subItem)
                {
                    if (item.enabled == false)
                    {
                        continue;
                    }
                    if (!Utils.IsNullOrEmpty(groupId) && item.groupId != groupId)
                    {
                        continue;
                    }

                    string id        = item.id.TrimEx();
                    string url       = item.url.TrimEx();
                    string userAgent = item.userAgent.TrimEx();
                    //string groupId = item.groupId.TrimEx();
                    string hashCode = $"{item.remarks}->";
                    if (Utils.IsNullOrEmpty(id) || Utils.IsNullOrEmpty(url))
                    {
                        //_updateFunc(false, $"{hashCode}{ResUI.MsgNoValidSubscription}");
                        continue;
                    }

                    var downloadHandle    = new DownloadHandle();
                    downloadHandle.Error += (sender2, args) =>
                    {
                        _updateFunc(false, $"{hashCode}{args.GetException().Message}");
                    };

                    _updateFunc(false, $"{hashCode}{ResUI.MsgStartGettingSubscriptions}");
                    var result = await downloadHandle.DownloadStringAsync(url, blProxy, userAgent);
                    if (blProxy && Utils.IsNullOrEmpty(result))
                    {
                        result = await downloadHandle.DownloadStringAsync(url, false, userAgent);
                    }

                    if (Utils.IsNullOrEmpty(result))
                    {
                        _updateFunc(false, $"{hashCode}{ResUI.MsgSubscriptionDecodingFailed}");
                    }
                    else
                    {
                        _updateFunc(false, $"{hashCode}{ResUI.MsgGetSubscriptionSuccessfully}");
                        if (result.Length < 99)
                        {
                            _updateFunc(false, $"{hashCode}{result}");
                        }

                        int ret = ConfigHandler.AddBatchServers(ref config, result, id, item.groupId.TrimEx());
                        _updateFunc(false,
                                    ret > 0
                                ? $"{hashCode}{ResUI.MsgUpdateSubscriptionEnd}"
                                : $"{hashCode}{ResUI.MsgFailedImportSubscription}");
                    }
                    _updateFunc(false, "-------------------------------------------------------");
                }
                //restore system proxy
                if (bSysProxyType)
                {
                    config.sysProxyType = ESysProxyType.ForcedChange;
                    SysProxyHandle.UpdateSysProxy(config, false);
                }
                _updateFunc(true, $"{ResUI.MsgUpdateSubscriptionEnd}");
            });
        }
Exemple #4
0
        public void UpdateSubscriptionProcess(Config config, bool blProxy, Action <bool, string> update)
        {
            _config     = config;
            _updateFunc = update;

            _updateFunc(false, UIRes.I18N("MsgUpdateSubscriptionStart"));

            if (config.subItem == null || config.subItem.Count <= 0)
            {
                _updateFunc(false, UIRes.I18N("MsgNoValidSubscription"));
                return;
            }

            for (int k = 1; k <= config.subItem.Count; k++)
            {
                string id        = config.subItem[k - 1].id.TrimEx();
                string url       = config.subItem[k - 1].url.TrimEx();
                string userAgent = config.subItem[k - 1].userAgent.TrimEx();
                string groupId   = config.subItem[k - 1].groupId.TrimEx();
                string hashCode  = $"{k}){config.subItem[k - 1].remarks}->";
                if (config.subItem[k - 1].enabled == false)
                {
                    continue;
                }
                if (Utils.IsNullOrEmpty(id) || Utils.IsNullOrEmpty(url))
                {
                    _updateFunc(false, $"{hashCode}{UIRes.I18N("MsgNoValidSubscription")}");
                    continue;
                }

                DownloadHandle downloadHandle3 = new DownloadHandle();
                downloadHandle3.UpdateCompleted += (sender2, args) =>
                {
                    if (args.Success)
                    {
                        _updateFunc(false, $"{hashCode}{UIRes.I18N("MsgGetSubscriptionSuccessfully")}");
                        //string result = Utils.Base64Decode(args.Msg);
                        string result = args.Msg;
                        if (Utils.IsNullOrEmpty(result))
                        {
                            _updateFunc(false, $"{hashCode}{UIRes.I18N("MsgSubscriptionDecodingFailed")}");
                            return;
                        }

                        //ConfigHandler.RemoveServerViaSubid(ref config, id);
                        //_updateFunc(false, $"{hashCode}{UIRes.I18N("MsgClearSubscription")}");
                        //  RefreshServers();
                        int ret = ConfigHandler.AddBatchServers(ref config, result, id, groupId);
                        if (ret > 0)
                        {
                            // RefreshServers();
                        }
                        else
                        {
                            _updateFunc(false, $"{hashCode}{UIRes.I18N("MsgFailedImportSubscription")}");
                        }
                        _updateFunc(true, $"{hashCode}{UIRes.I18N("MsgUpdateSubscriptionEnd")}");
                    }
                    else
                    {
                        _updateFunc(false, args.Msg);
                    }
                };
                downloadHandle3.Error += (sender2, args) =>
                {
                    _updateFunc(false, args.GetException().Message);
                };

                WebProxy webProxy = blProxy ? new WebProxy(Global.Loopback, _config.GetLocalPort(Global.InboundHttp)) : null;
                downloadHandle3.WebDownloadString(url, webProxy, userAgent);

                _updateFunc(false, $"{hashCode}{UIRes.I18N("MsgStartGettingSubscriptions")}");
            }
        }