/// <summary>
        /// End the request if path match one provided rules.
        /// </summary>
        /// <param name="context"></param>
        /// <param name="host"></param>
        /// <param name="vhost"></param>
        /// <param name="settings"></param>
        /// <returns></returns>
        public override async Task InvokeImpl(HttpContext context, string host, VHostOptions vhost, IConfigurationSection settings)
        {
            context.Items["bal-filter-end"] = false;
            FiltersOption options = settings.GetSection("Settings:RequestFilter").Get <FiltersOption>();

            foreach (var option in options.rules)
            {
                Regex r = new Regex(option.path);
                if (r.Match(context.Request.Path).Success)
                {
                    context.Items["bal-filter-end"] = true;
                    break;
                }
            }
        }
Ejemplo n.º 2
0
        public virtual bool IsActive(HttpContext context)
        {
            string host = context.Items["bal-host"] as string;

            if (string.IsNullOrEmpty(host))
            {
                throw new Exception("HOST is empty. Please check configuration.");
            }
            VHostOptions VHost = context.Items["bal-vhost"] as VHostOptions;

            if (VHost == null)
            {
                throw new Exception($"VHOST is missing for {host}. Please check configuration.");
            }
            return(VHost.Filters != null && VHost.Filters.Contains(this.Name, StringComparer.InvariantCultureIgnoreCase));
        }
Ejemplo n.º 3
0
        public async override Task InvokeImpl(HttpContext context, string host, VHostOptions vhost, IConfigurationSection settings)
        {
            host = context.Request.Host.Value;
            if (string.IsNullOrEmpty(host))
            {
                throw new Exception("HOST is empty. Please check configuration.");
            }
            vhost = BalancerSettings.Current.GetSettings <VHostOptions>(host);
            if (vhost == null || string.IsNullOrEmpty(vhost.Host))
            {
                throw new Exception($"VHOST is missing for {host}. Please check configuration.");
            }



            context.Items["bal-host"]  = host;
            context.Items["bal-vhost"] = vhost;
        }
Ejemplo n.º 4
0
        public override async Task Invoke(HttpContext context)
        {
            var endRequest = false;

            if (this.IsActive(context))
            {
                //object urlToProxy = null;
                string                host     = context.Items["bal-host"] as string;
                VHostOptions          vHost    = context.Items["bal-host"] as VHostOptions;
                IConfigurationSection settings = BalancerSettings.Current.GetSettingsSection(host);
                await InvokeImpl(context, host, vHost, settings);

                endRequest = this.Terminate(context);
            }
            if (!endRequest && NextStep != null)
            {
                await NextStep(context);
            }
        }
Ejemplo n.º 5
0
        public async override Task InvokeImpl(HttpContext context, string host, VHostOptions vhost, IConfigurationSection settings)
        {
            BalancerOptions options = new BalancerOptions();

            settings.Bind("Settings:Balancer", options);

            BalancerData balancerData = null;

            if (!data.ContainsKey(host))
            {
                balancerData = data[host] = new BalancerData();
                for (int i = 0; i < options.Nodes.Length; i++)
                {
                    balancerData.Scores[i] = 0;
                }
            }
            else
            {
                balancerData = data[host];
            }

            //Different logic basing on algoritm
            //TODO: make it generic using interface\implementation for algoritm
            if (options.Policy == "RoundRobin")
            {
                context.Items["bal-destination"] = RoundRobin(balancerData, options);
            }
            else if (options.Policy == "RequestCount")
            {
                context.Items["bal-destination"] = RequestCount(balancerData, options);
            }



            //Alter request
            context.Request.Headers["X-Forwarded-For"]   = context.Connection.RemoteIpAddress.ToString();
            context.Request.Headers["X-Forwarded-Proto"] = context.Request.Protocol.ToString();
            int port = context.Request.Host.Port ?? (context.Request.IsHttps ? 443 : 80);

            context.Request.Headers["X-Forwarded-Port"] = port.ToString();
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Entry point. Switch between websocket requests and regular http request
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public async override Task InvokeImpl(HttpContext context, string host, VHostOptions vhost, IConfigurationSection settings)
        {
            var _options = (context.Items["proxy-options"] ?? _defaultOptions) as InternalProxyOptions;

            var destination = (context.Items["bal-destination"]) as Node;

            if (destination == null)
            {
                destination = settings.GetSection("Settings:Proxy:DefaultDestination").Get <Node>();
            }
            var chost  = (destination == null) ? _options.Host : destination.Host;
            var cport  = (destination == null) ? _options.Port : destination.Port;
            var scheme = (destination == null) ? _options.Scheme : destination.Scheme;

            if (context.WebSockets.IsWebSocketRequest)
            {
                await HandleWebSocketRequest(context, _options, destination, chost, cport.Value, scheme);
            }
            else
            {
                await HandleHttpRequest(context, _options, destination, chost, cport.Value, scheme);
            }
        }
Ejemplo n.º 7
0
 public override Task InvokeImpl(HttpContext context, string host, VHostOptions vhost, IConfigurationSection settings)
 {
     return(null);
 }
Ejemplo n.º 8
0
 public abstract Task InvokeImpl(HttpContext context, string host, VHostOptions vhost, IConfigurationSection settings);