JN-Jones / visitor-log

A package for Laravel 4 to log all visitors
11 stars 8 forks source link

Request::getClientIp() not working on varnish #1

Closed Lamesya closed 10 years ago

Lamesya commented 10 years ago

Hi, Need help to fix this issue with regards about this Request::getClientIp(), it seems not working on varnish and it only output 127.0.0.1

I'm using Laravel 4.2 at this moment.

Bug fix: Visitor::clear();

from: where('updated_at', '<', time() - Config::get('visitor-log::onlinetime') * 60)

to: where('updated_at', '<=', date('Y-m-d H:i:s', time() - (Config::get('visitor-log::onlinetime') * 60)))

timestamp seems not same format with the DB data inside updated_at so i converted it to regular date format.

Added optional: added some wildcard site ignoring.

from: if(is_array($ignore) && in_array($page, $ignore)) to: if(is_array($ignore) && $this->inArray($page, $ignore))

private function inArray($needle, $haystack = array())
{
    foreach ($haystack as $key => $value)
    {
        unset ($haystack[$key]); 
        $pattern = preg_quote($value, '/'); 
        $pattern = str_replace( '\*' , '.*', $pattern);                 
        if(preg_match( '/^' . $pattern . '$/i' , $needle ))
        {
            return preg_match( '/^' . $pattern . '$/i' , $needle );
         }
     }       
}

hopefully this will help a bit, sorry for the bad English :)

Lamesya commented 10 years ago

To fixed the issue with Request::getClientIp() on varnish.

http://forumsarchive.laravel.io/viewtopic.php?id=8567, thanks to this guy :+1:

$request = Request::instance();
// Equivalent to the old $request->trustProxyData() - NOTE: Potential security issue, make sure this is what you want to do
$request->setTrustedProxies(array($request->server->get('REMOTE_ADDR')));
// The headers to trust from the source.
$request->setTrustedHeaderName(\Symfony\Component\HttpFoundation\Request::HEADER_CLIENT_IP, 'X-Forwarded-For');
$request->setTrustedHeaderName(\Symfony\Component\HttpFoundation\Request::HEADER_CLIENT_PROTO, 'X-Forwarded-Proto');

I just added this to VisitorLogServiceProvider.php then replace Request::getClientIp() to $request->getClientIp()

Now it works properly :)