Exemplo n.º 1
0
        private void UpdateDownstreamHostAndPorts(OcelotRouteDto input, OcelotRoute route)
        {
            if (input.DownstreamHostAndPorts == null)
            {
                route.RemoveAllDownstreamHostAndPorts();
                return;
            }

            foreach (var host in input.DownstreamHostAndPorts)
            {
                var existing = route.FindDownstreamHostAndPort(host.Host, host.Port.Value);
                if (existing == null)
                {
                    route.AddDownstreamHostAndPort(host.Host, host.Port.Value);
                }
            }

            //TODO Copied with ToList to avoid modification of the collection in the for loop
            foreach (var host in route.DownstreamHostAndPorts.ToList())
            {
                if (!input.DownstreamHostAndPorts.Any(c => host.Equals(route.GlobalConfigurationId, route.Name, c.Host, c.Port.Value)))
                {
                    route.RemoveDownstreamHostAndPort(host.Host, host.Port);
                }
            }
        }
Exemplo n.º 2
0
        private void UpdateUpstreamHttpMethods(OcelotRouteDto input, OcelotRoute route)
        {
            if (input.UpstreamHttpMethods == null)
            {
                route.RemoveAllUpstreamHttpMethods();
                return;
            }

            foreach (var method in input.UpstreamHttpMethods)
            {
                var existing = route.FindUpstreamHttpMethod(method);
                if (existing == null)
                {
                    route.AddUpstreamHttpMethod(method);
                }
            }

            //TODO Copied with ToList to avoid modification of the collection in the for loop
            foreach (var method in route.UpstreamHttpMethods.ToList())
            {
                if (!input.UpstreamHttpMethods.Any(c => method.Equals(route.GlobalConfigurationId, route.Name, c)))
                {
                    route.RemoveUpstreamHttpMethod(method.Method);
                }
            }
        }
Exemplo n.º 3
0
        private void UpdateDelegatingHandlers(OcelotRouteDto input, OcelotRoute route)
        {
            if (input.DelegatingHandlers == null)
            {
                input.DelegatingHandlers = null;
                return;
            }

            foreach (var delegating in input.DelegatingHandlers)
            {
                var existing = route.FindDelegatingHandler(delegating);
                if (existing == null)
                {
                    route.AddDelegatingHandler(delegating);
                }
            }

            //TODO Copied with ToList to avoid modification of the collection in the for loop
            foreach (var delegating in route.DelegatingHandlers.ToList())
            {
                if (!input.DelegatingHandlers.Any(c => delegating.Equals(route.GlobalConfigurationId, route.Name, c)))
                {
                    route.RemoveDelegatingHandlers(delegating.Delegating);
                }
            }
        }
Exemplo n.º 4
0
        public List <string> Resolve(OcelotRoute source, OcelotRouteDto destination, List <string> destMember, ResolutionContext context)
        {
            if (source == null || source.DelegatingHandlers == null)
            {
                return(null);
            }

            return(source.DelegatingHandlers.Select(c => c.Delegating).ToList());
        }
Exemplo n.º 5
0
        public List <string> Resolve(OcelotRoute source, OcelotRouteDto destination, List <string> destMember, ResolutionContext context)
        {
            if (source == null || source.UpstreamHttpMethods == null)
            {
                return(null);
            }

            return(source.UpstreamHttpMethods.Select(c => c.Method).ToList());
        }
Exemplo n.º 6
0
        public virtual void AddRoutes(string name,
                                      string upstreamPathTemplate,
                                      string upstreamHost,
                                      string downstreamHttpMethod,
                                      string downstreamPathTemplate,
                                      string downstreamScheme,
                                      string key = null,
                                      string serviceNamespace   = null,
                                      string serviceName        = null,
                                      bool routeIsCaseSensitive = false,
                                      string requestIdKey       = null,
                                      bool dangerousAcceptAnyServerCertificateValidator = false,
                                      int timeout  = 5000,
                                      int sort     = 100,
                                      int priority = 1,
                                      List <string> upstreamHttpMethods = null,
                                      Dictionary <string, int> downstreamHostAndPorts = null)
        {
            var route = new OcelotRoute(
                Id,
                name,
                upstreamHost,
                upstreamPathTemplate,
                downstreamHttpMethod,
                downstreamPathTemplate,
                downstreamScheme,
                key,
                serviceNamespace,
                serviceName,
                routeIsCaseSensitive,
                requestIdKey,
                dangerousAcceptAnyServerCertificateValidator,
                timeout,
                sort,
                priority
                );

            if (upstreamHttpMethods != null)
            {
                foreach (var item in upstreamHttpMethods)
                {
                    route.AddUpstreamHttpMethod(item);
                }
            }
            if (downstreamHostAndPorts != null)
            {
                foreach (var item in downstreamHostAndPorts)
                {
                    route.AddDownstreamHostAndPort(item.Key, item.Value);
                }
            }

            Routes.Add(route);
        }
Exemplo n.º 7
0
        public List <RouteDelegatingHandler> Resolve(OcelotRouteDto source, OcelotRoute destination, List <RouteDelegatingHandler> destMember, ResolutionContext context)
        {
            if (source == null || source.DelegatingHandlers == null)
            {
                return(null);
            }
            foreach (var item in source.DelegatingHandlers)
            {
                destination.AddDelegatingHandler(item);
            }

            return(destination.DelegatingHandlers);
        }
Exemplo n.º 8
0
        public List <RouteDownstreamHostAndPort> Resolve(OcelotRouteDto source, OcelotRoute destination, List <RouteDownstreamHostAndPort> destMember, ResolutionContext context)
        {
            if (source == null || source.DownstreamHostAndPorts == null)
            {
                return(null);
            }

            foreach (var item in source.DownstreamHostAndPorts)
            {
                destination.AddDownstreamHostAndPort(item.Host, item.Port.Value);
            }

            return(destination.DownstreamHostAndPorts);
        }
Exemplo n.º 9
0
        public List <RouteUpstreamHttpMethod> Resolve(OcelotRouteDto source, OcelotRoute destination, List <RouteUpstreamHttpMethod> destMember, ResolutionContext context)
        {
            if (source == null || source.UpstreamHttpMethods == null)
            {
                return(null);
            }

            foreach (var item in source.UpstreamHttpMethods)
            {
                destination.AddUpstreamHttpMethod(item);
            }

            return(destination.UpstreamHttpMethods);
        }
Exemplo n.º 10
0
        private void UpdateSecurityOption(OcelotRouteDto input, OcelotRoute route)
        {
            if (input.SecurityOption == null)
            {
                route.SecurityOption = null;
                return;
            }

            if (route.SecurityOption == null)
            {
                route.SecurityOption = new RouteSecurityOption(route.GlobalConfigurationId, route.Name);
            }

            UpdateSecurityOptionIPBlocked(input.SecurityOption, route.SecurityOption);
            UpdateSecurityOptionIPAllowed(input.SecurityOption, route.SecurityOption);
        }
Exemplo n.º 11
0
        public List <RouteDownstreamHostAndPortDto> Resolve(OcelotRoute source, OcelotRouteDto destination, List <RouteDownstreamHostAndPortDto> destMember, ResolutionContext context)
        {
            if (source == null || source.DownstreamHostAndPorts == null)
            {
                return(null);
            }

            List <RouteDownstreamHostAndPortDto> dto = new List <RouteDownstreamHostAndPortDto>();

            foreach (var item in source.DownstreamHostAndPorts)
            {
                dto.Add(new RouteDownstreamHostAndPortDto {
                    Host = item.Host, Port = item.Port
                });
            }

            return(dto);
        }
Exemplo n.º 12
0
        protected virtual void UpdateOcelotRoute(List <OcelotRouteDto> input, Ocelot ocelot)
        {
            if (input == null)
            {
                ocelot.RemoveAllRoutes();
                return;
            }

            foreach (var routeDto in input)
            {
                var existing = ocelot.FindRoute(routeDto.Name);
                if (existing == null)
                {
                    var route = new OcelotRoute(
                        ocelot.Id,
                        routeDto.Name,
                        routeDto.UpstreamHost,
                        routeDto.UpstreamPathTemplate,
                        routeDto.DownstreamHttpMethod,
                        routeDto.DownstreamPathTemplate,
                        routeDto.DownstreamScheme,
                        routeDto.Key,
                        routeDto.ServiceNamespace,
                        routeDto.ServiceName,
                        routeDto.RouteIsCaseSensitive ?? false,
                        routeDto.RequestIdKey,
                        routeDto.DangerousAcceptAnyServerCertificateValidator ?? false,
                        routeDto.Timeout,
                        routeDto.Sort ?? 100,
                        routeDto.Priority ?? 1);


                    route.HttpHandlerOption    = GetRouteHttpHandlerOption(ocelot.Id, routeDto.Name, routeDto.HttpHandlerOption);
                    route.AuthenticationOption = GetRouteAuthenticationOption(ocelot.Id, routeDto.Name, routeDto.AuthenticationOption);
                    route.RateLimitOption      = GetRouteRateLimitRule(ocelot.Id, routeDto.Name, routeDto.RateLimitOption);
                    route.LoadBalancerOption   = GetRouteLoadBalancerOption(ocelot.Id, routeDto.Name, routeDto.LoadBalancerOption);
                    route.QoSOption            = GetRouteQoSOption(ocelot.Id, routeDto.Name, routeDto.QoSOption);
                    route.CacheOption          = GetRouteCacheOption(ocelot.Id, routeDto.Name, routeDto.CacheOption);

                    UpdateDelegatingHandlers(routeDto, route);
                    UpdateDownstreamHostAndPorts(routeDto, route);
                    UpdateUpstreamHttpMethods(routeDto, route);
                    UpdateSecurityOption(routeDto, route);

                    ocelot.Routes.Add(route);
                }
                else
                {
                    existing.UpstreamHost           = routeDto.UpstreamHost;
                    existing.UpstreamPathTemplate   = routeDto.UpstreamPathTemplate;
                    existing.DownstreamScheme       = routeDto.DownstreamScheme;
                    existing.DownstreamPathTemplate = routeDto.DownstreamPathTemplate;
                    existing.DownstreamHttpMethod   = routeDto.DownstreamHttpMethod;
                    existing.Key = routeDto.Key;
                    existing.ServiceNamespace     = routeDto.ServiceNamespace;
                    existing.ServiceName          = routeDto.ServiceName;
                    existing.RouteIsCaseSensitive = routeDto.RouteIsCaseSensitive ?? false;
                    existing.RequestIdKey         = routeDto.RequestIdKey;
                    existing.DangerousAcceptAnyServerCertificateValidator = routeDto.DangerousAcceptAnyServerCertificateValidator ?? false;
                    existing.Timeout  = routeDto.Timeout;
                    existing.Sort     = routeDto.Sort ?? 100;
                    existing.Priority = routeDto.Priority ?? 1;

                    existing.HttpHandlerOption    = GetRouteHttpHandlerOption(ocelot.Id, routeDto.Name, routeDto.HttpHandlerOption);
                    existing.AuthenticationOption = GetRouteAuthenticationOption(ocelot.Id, routeDto.Name, routeDto.AuthenticationOption);
                    existing.RateLimitOption      = GetRouteRateLimitRule(ocelot.Id, routeDto.Name, routeDto.RateLimitOption);
                    existing.LoadBalancerOption   = GetRouteLoadBalancerOption(ocelot.Id, routeDto.Name, routeDto.LoadBalancerOption);
                    existing.QoSOption            = GetRouteQoSOption(ocelot.Id, routeDto.Name, routeDto.QoSOption);
                    existing.CacheOption          = GetRouteCacheOption(ocelot.Id, routeDto.Name, routeDto.CacheOption);

                    UpdateDelegatingHandlers(routeDto, existing);
                    UpdateDownstreamHostAndPorts(routeDto, existing);
                    UpdateUpstreamHttpMethods(routeDto, existing);
                    UpdateSecurityOption(routeDto, existing);
                }
            }

            //TODO Handle Update state

            //Copied with ToList to avoid modification of the collection in the for loop
            foreach (var route in ocelot.Routes.ToList())
            {
                if (!input.Any(c => route.Equals(route.GlobalConfigurationId, c.Name)))
                {
                    ocelot.RemoveRoute(route.Name);
                }
            }
        }