コード例 #1
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));
        }
コード例 #2
0
        private IObservable <Unit> SaveProxy(IProxyManager proxyManager)
        {
            var proxyModel = Model.SelectedProxy;

            if (proxyModel != null)
            {
                if (proxyModel.Proxy.Id != 0)
                {
                    return(proxyManager.AddProxy(proxyModel.ToProxy())
                           .Do(proxy => proxyModel.Proxy = proxy)
                           .Select(_ => Unit.Default));
                }

                return(proxyManager.UpdateProxy(proxyModel.Proxy)
                       .Do(proxy => proxyModel.Proxy = proxy)
                       .Select(_ => Unit.Default));
            }

            return(Observable.Empty <Unit>());
        }
コード例 #3
0
        public override Task RunAsync()
        {
            if (string.IsNullOrEmpty(ProxyName))
            {
                ColoredConsole
                .Error
                .WriteLine(ErrorColor("Command must specify --name."));
                return(Task.CompletedTask);
            }

            if (!string.IsNullOrEmpty(Route) && !string.IsNullOrEmpty(TemplateName))
            {
                ColoredConsole
                .Error
                .WriteLine(ErrorColor("Only one of --route or --template values can be specified."));
                return(Task.CompletedTask);
            }

            IEnumerable <string> templates = null;

            if (string.IsNullOrEmpty(Route) &&
                string.IsNullOrEmpty(TemplateName))
            {
                if (Console.IsOutputRedirected || Console.IsInputRedirected)
                {
                    ColoredConsole
                    .Error
                    .WriteLine(ErrorColor("Running with stdin\\stdout redirected. Command must specify either --route or --template explicitly."))
                    .WriteLine(ErrorColor("See 'func help function' for more details"));
                    return(Task.CompletedTask);
                }
                else
                {
                    templates = _proxyManager.Templates;
                    ColoredConsole.Write("Select a template: ");
                    TemplateName = ConsoleHelper.DisplaySelectionWizard(templates);
                    ColoredConsole.WriteLine(TitleColor(TemplateName));
                }
            }


            if (!string.IsNullOrEmpty(TemplateName))
            {
                if (templates == null)
                {
                    templates = _proxyManager.Templates;
                }

                if (!templates.Contains(TemplateName))
                {
                    ColoredConsole.Error.WriteLine(ErrorColor($"Can't find template \"{TemplateName}\" "));
                    return(Task.CompletedTask);
                }

                _proxyManager.AddProxy(ProxyName, TemplateName);
                return(Task.CompletedTask);
            }

            if (!string.IsNullOrEmpty(BackendUrl) &&
                !BackendUrl.StartsWith("http://", StringComparison.OrdinalIgnoreCase) &&
                !BackendUrl.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
            {
                ColoredConsole.Error.WriteLine(ErrorColor($"backend-url {BackendUrl} must start with 'http://' or 'https://' "));
                return(Task.CompletedTask);
            }

            ProxyDefinition proxyDefinition = new ProxyDefinition();

            proxyDefinition.Condition       = new MatchCondition();
            proxyDefinition.Condition.Route = Route;

            proxyDefinition.BackendUri = BackendUrl;

            if (Methods != null && Methods.Any())
            {
                proxyDefinition.Condition.HttpMethods = Methods.ToArray();
            }

            _proxyManager.AddProxy(ProxyName, proxyDefinition);

            return(Task.CompletedTask);
        }