Beispiel #1
0
        private static IObservable <ProxyModel> RemoveProxy(
            this ProxyPopupContext context,
            IProxyManager proxyManager,
            ProxyModel proxyModel)
        {
            if (proxyModel.Proxy != null && proxyModel.Proxy.Id != 0)
            {
                return(proxyManager.RemoveProxy(proxyModel.Proxy)
                       .Select(_ => proxyModel));
            }

            return(Observable.Return(proxyModel));
        }
Beispiel #2
0
        public static ProxyModel FromProxy(TdApi.Proxy proxy)
        {
            var model = new ProxyModel
            {
                Id        = proxy.Id,
                Proxy     = proxy,
                IsEnabled = proxy.IsEnabled,
                IsSaved   = proxy.Id != 0
            };

            model.Server   = proxy.Server ?? "";
            model.Port     = proxy.Port == 0 ? "" : proxy.Port.ToString();
            model.Username = "";
            model.Password = "";
            model.Secret   = "";

            switch (proxy.Type)
            {
            case TdApi.ProxyType.ProxyTypeSocks5 socks5:
                model.IsSocks5 = true;
                model.Username = socks5.Username ?? "";
                model.Password = socks5.Password ?? "";
                break;

            case TdApi.ProxyType.ProxyTypeHttp http:
                model.IsHttp   = true;
                model.Username = http.Username ?? "";
                model.Password = http.Password ?? "";
                break;

            case TdApi.ProxyType.ProxyTypeMtproto mtproto:
                model.IsMtProto = true;
                model.Secret    = mtproto.Secret ?? "";
                break;
            }

            model.IsServerInputVisible   = true;
            model.IsUsernameInputVisible = model.IsSocks5 || model.IsHttp;
            model.IsPasswordInputVisible = model.IsSocks5 || model.IsHttp;
            model.IsSecretInputVisible   = model.IsMtProto;

            model.IsRemovable = true;
            model.IsEditable  = true;

            return(model);
        }
Beispiel #3
0
        private static IObservable <ProxyModel> SaveProxy(
            this ProxyPopupContext context,
            IProxyManager proxyManager,
            ProxyModel proxyModel)
        {
            if (proxyModel.Proxy.Id == 0)
            {
                return(proxyManager
                       .AddProxy(proxyModel.ToProxy())
                       .Do(proxy => proxyModel.Proxy = proxy)
                       .Select(_ => proxyModel));
            }

            return(proxyManager
                   .UpdateProxy(proxyModel.Proxy)
                   .Do(proxy => proxyModel.Proxy = proxy)
                   .Select(_ => proxyModel));
        }
Beispiel #4
0
        private static IObservable <ProxyModel> AddProxy(
            this ProxyPopupContext context,
            IProxyManager proxyManager)
        {
            var proxy = new TdApi.Proxy
            {
                Server = null,
                Port   = 0,
                Type   = new TdApi.ProxyType.ProxyTypeSocks5
                {
                    Username = null,
                    Password = null
                }
            };

            var proxyModel = ProxyModel.FromProxy(proxy);

            return(Observable.Return(proxyModel));
        }
Beispiel #5
0
        private static IObservable <ProxyModel> EnableProxy(
            this ProxyPopupContext context,
            IProxyManager proxyManager,
            ProxyModel proxyModel)
        {
            if (!proxyModel.IsEnabled)
            {
                if (proxyModel.Proxy != null)
                {
                    return(proxyManager
                           .EnableProxy(proxyModel.Proxy)
                           .Select(_ => proxyModel));
                }

                return(proxyManager
                       .DisableProxy()
                       .Select(_ => proxyModel));
            }

            return(Observable.Return(proxyModel));
        }
Beispiel #6
0
        private static IDisposable BindList(
            this ProxyPopupContext context,
            IProxyManager proxyManager)
        {
            return(proxyManager.GetAllProxies()
                   .SubscribeOn(RxApp.TaskpoolScheduler)
                   .ObserveOn(RxApp.MainThreadScheduler)
                   .Accept(proxies =>
            {
                var disabledProxy = ProxyModel.DisabledProxy();
                disabledProxy.EnableCommand = context.EnableProxyCommand;

                var otherProxies = Enumerable.Select <TdApi.Proxy, ProxyModel>(proxies, p =>
                {
                    var proxyModel = ProxyModel.FromProxy(p);
                    proxyModel.RemoveCommand = context.RemoveProxyCommand;
                    proxyModel.EnableCommand = context.EnableProxyCommand;
                    return proxyModel;
                })
                                   .ToList();

                context.Proxies = new ObservableCollectionExtended <ProxyModel>();
                context.Proxies.Add(disabledProxy);
                context.Proxies.AddRange(otherProxies);

                if (context.SelectedProxy == null)
                {
                    context.SelectedProxy = otherProxies.FirstOrDefault(p => p.IsEnabled)
                                            ?? disabledProxy;
                }

                if (!Enumerable.Any <TdApi.Proxy>(proxies, p => p.IsEnabled))
                {
                    disabledProxy.IsEnabled = true;
                }
            }));
        }