Detecting Cloudflare and replacing proxy IP with real IP

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
10 years ago
We use Cloudflare with NOP for SSL and and caching, it works great.

We routinely bypass Cloudflare when we access our admin site to fulfil orders. By default when customers access NOP though Cloudflare the customer IP gets replaced by the Cloudflare Proxy IP. I have modified the code below to detect if the visitor came via Cloudflare and replace the IP with the correct one or to use the normal method.

Enjoy.



Nop.Core.WebHelper

        /// <summary>
        /// Get context IP address
        /// </summary>
        /// <returns>URL referrer</returns>
        public virtual string GetCurrentIpAddress()
        {
                if (_httpContext.Request.Headers["CF-Connecting-IP"] != null &&
                   _httpContext.Request.Headers["CF-Connecting-IP"].Length > 1)
                {
                    return _httpContext.Request.Headers["CF-Connecting-IP"];    
                }
                else
                {
                    return _httpContext.Request.UserHostAddress;
                }[code]
                
            
            return string.Empty;
        }
[/code]
10 years ago
Updated for 3.1




            if (_httpContext.Request.Headers["CF-Connecting-IP"] != null &&
               _httpContext.Request.Headers["CF-Connecting-IP"].Length > 1)
            {
                return _httpContext.Request.Headers["CF-Connecting-IP"];
            }
            else
            {
                  if (_httpContext != null &&
                      _httpContext.Request != null &&
                      _httpContext.Request.UserHostAddress != null)
                      return _httpContext.Request.UserHostAddress;
            }
            return string.Empty;

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.