Exemple #1
0
        public override Task <ProcessRuleResult> ProcessAsync(string callerIP, Scheme scheme, string host, int port, string path, string pathAndQuery, NameValueCollection query, NameValueCollection headers, StaticFileProcessor parent)
        {
            string rScheme; if (this.SetScheme)

            {
                rScheme = (this.Scheme ?? Scheme).ToString();
            }
            else
            {
                rScheme = scheme.ToString();
            }
            string rHost; if (this.SetHostName)

            {
                rHost = (this.HostName ?? host).ToString();
            }
            else
            {
                rHost = host;
            }
            string rPath; if (this.SetPath)

            {
                rPath = (this.Path ?? path).ToString();
            }
            else
            {
                rPath = path;
            }
            string rPort; if (this.SetPort)

            {
                rPort = (this.Port ?? port).ToString();
            }
            else
            {
                rPort = port.ToString();
            }
            string redirectURL = rScheme + "://" + rHost + ":" + rPort + rPath;

            return(Task.FromResult(new ProcessRuleResult()
            {
                IsSuccess = true, ActionType = ActionType.Redirect, RedirectURL = redirectURL, Message = "Redirected to '" + redirectURL + "'"
            }));;
        }
Exemple #2
0
 public override Task <ProcessRuleResult> ProcessAsync(string callerIP, Scheme scheme, string host, int port, string path, string pathAndQuery, NameValueCollection query, NameValueCollection headers, StaticFileProcessor parent)
 {
     return(Task.FromResult(new ProcessRuleResult()
     {
         IsSuccess = true, ActionType = ActionType.Deny, Message = "Request denied!"
     }));
 }
Exemple #3
0
 public virtual Task <ProcessRuleResult> ProcessAsync(string callerIP, Scheme scheme, string host, int port, string path, string pathAndQuery, NameValueCollection query, NameValueCollection headers, StaticFileProcessor parent)
 {
     return(null);
 }
Exemple #4
0
        public override async Task <ProcessRuleResult> ProcessAsync(string callerIP, Scheme scheme, string host, int port, string path, string pathAndQuery, NameValueCollection query, NameValueCollection headers, StaticFileProcessor parent)
        {
            HTTPStreamResult httpStreamResult = await parent.ProcessRequestAsync(callerIP, path, this.AlternativeWWWDirectory).ConfigureAwait(false);

            if ((int)httpStreamResult.HttpStatusCode >= 200 && (int)httpStreamResult.HttpStatusCode < 300)
            {
                return(new ProcessRuleResult()
                {
                    IsSuccess = true, ActionType = ActionType.AlternativeWWWDirectory, HttpStreamResult = httpStreamResult, Message = "Served '" + httpStreamResult.FileToRead + "'"
                });
            }
            else
            {
                return(new ProcessRuleResult()
                {
                    IsSuccess = false,
                    ActionType = ActionType.AlternativeWWWDirectory,
                    SendErrorResponsePage_HttpStatusCode = httpStreamResult.HttpStatusCode,
                    LogLevel = LogLevel.Warning,
                    Message = httpStreamResult.ErrorMsg
                });
            }
        }
Exemple #5
0
        public override async Task <ProcessRuleResult> ProcessAsync(string callerIP, Scheme scheme, string host, int port, string path, string pathAndQuery, NameValueCollection query, NameValueCollection headers, StaticFileProcessor parent)
        {
            string portStr = port.ToString();
            string pScheme; if (this.SetScheme)

            {
                pScheme = (this.Scheme ?? Scheme).ToString();
            }
            else
            {
                pScheme = scheme.ToString();
            }
            string pHost; if (this.SetHostName)

            {
                pHost = (this.HostName ?? host).ToString();
            }
            else
            {
                pHost = host;
            }
            string pPath = null;

            if (this.SetPath)
            {
                if (this.UsePathVariable)
                {
                    // {$Path|^.sdsdd$}
                    string          newPath = this.PathAndQuery?.Replace("%PATH%", this.PathAndQuery);
                    Regex           regex   = new Regex(@"\%[^\%]*\%", RegexOptions.IgnoreCase);
                    MatchCollection matches = regex.Matches(this.PathAndQuery);
                    foreach (Match match in matches)
                    {
                        string innerRegex       = match.Value.Trim('%');
                        var    regex2           = new Regex(innerRegex, RegexOptions.IgnoreCase);
                        string innerRegexResult = regex2.Match(pathAndQuery).Value;
                        newPath = newPath?.Replace(match.Value, innerRegexResult);
                    }
                    pPath = newPath;
                }
                else
                {
                    pPath = path;
                }
            }
            else
            {
                pPath = path;
            }
            string pPort; if (this.SetPort)

            {
                pPort = (this.Port ?? port).ToString();
            }
            else
            {
                pPort = portStr;
            }
            string rProxyURL = pScheme + "://" + pHost + ":" + pPort + pPath;


            //AddOriginal Headers
            HttpClient httpClient = ((HttpServer)parent).HttpClient;
            var        items = headers.AllKeys.SelectMany(headers.GetValues, (k, v) => new { key = k, value = v });
            string     originalHost = null;
            bool       AddXFFor = true; bool AddXFProto = true; bool AddXFHost = true; bool AddXFPort = true;

            foreach (var item in items)
            {
                if (string.Equals(item.key, "Host"))
                {
                    originalHost = item.value; continue;
                }                                                                            // AddHost = false;
                if (string.Equals(item.key, "X-Forwarded-For"))
                {
                    AddXFFor = false;
                }
                if (string.Equals(item.key, "X-Forwarded-Proto"))
                {
                    AddXFProto = false;
                }
                if (string.Equals(item.key, "X-Forwarded-Host"))
                {
                    AddXFHost = false;
                }
                if (string.Equals(item.key, "X-Forwarded-Port"))
                {
                    AddXFPort = false;
                }
                httpClient.DefaultRequestHeaders.Add(item.key, item.value);
            }
            if (originalHost == null)
            {
                originalHost = host;
            }

            //Add Proxy Headers
            if (AddXFFor)
            {
                httpClient.DefaultRequestHeaders.Add("X-Forwarded-For", callerIP);
            }
            if (AddXFProto)
            {
                httpClient.DefaultRequestHeaders.Add("X-Forwarded-Proto", pScheme);
            }
            if (AddXFHost)
            {
                httpClient.DefaultRequestHeaders.Add("X-Forwarded-Host", originalHost);
            }
            if (AddXFPort)
            {
                httpClient.DefaultRequestHeaders.Add("X-Forwarded-Port", portStr);
            }


            HttpResponseMessage resp = await httpClient.GetAsync(rProxyURL);

            Stream rProxyResult = await resp.Content.ReadAsStreamAsync();

            string contentType = null;

            if (resp.IsSuccessStatusCode)
            {
                if (resp.Content.Headers.Contains("Content-Type"))
                {
                    contentType = resp.Content.Headers.GetValues("Content-Type").FirstOrDefault();
                }
            }
            if (resp.IsSuccessStatusCode)
            {
                return(new ProcessRuleResult()
                {
                    IsSuccess = true,
                    ActionType = ActionType.ReverseProxy,
                    HttpStreamResult = new HTTPStreamResult(false, null, resp.StatusCode, contentType, rProxyResult, null, false),
                    Message = rProxyURL
                });
            }
            else
            {
                return(new ProcessRuleResult()
                {
                    IsSuccess = false, ActionType = ActionType.ReverseProxy, SendErrorResponsePage_HttpStatusCode = resp.StatusCode,
                    LogLevel = LogLevel.Warning, Message = rProxyURL + " > " + resp.ReasonPhrase
                });
            }
        }