Skip to content

Cloudflare CF-Connecting-IP save in ip_address #1041

@aftabkhaliq86

Description

@aftabkhaliq86

PHP Version

8.1

Laravel Version

10.0

Package Version

13.7.2

Description

i have added the custom IpAddressResolver to save user real ip
Currently, its saving Cloudflare IP, not the original IP wvwn though modified to save the Cloudflare IP with header CF-Connecting-IP . my solution works fine with local and auditing in queue but in production my solutions saves 127.0.0.1

class irUserIPResolver implements  \OwenIt\Auditing\Contracts\Resolver
{
    public static function resolve(Auditable $auditable)
    {
       return  request()->header('CF-Connecting-IP')??request()->ip();
        Log::build([
            'driver' => 'single',
            'path' => storage_path('logs/audit_logs.log'),
            'level' => 'info',
        ])->info('Audirt Logs ',[
            'CF-Connecting-IP' => request()->header('CF-Connecting-IP'),
            'X-Forwarded-For' => trim(explode(',', request()->header('X-Forwarded-For'))[0]),
            'fallback' => request()->ip(),
            'url' => request()->fullUrl(),
        ]);
        // Cloudflare real IP headers
        if (request()->hasHeader('CF-Connecting-IP')) {
            return request()->header('CF-Connecting-IP');
        }

        if (request()->hasHeader('X-Forwarded-For')) {
            // May contain multiple IPs → take the first (real client)
            return trim(explode(',', request()->header('X-Forwarded-For'))[0]);
        }

        // Fallback to Laravel default
        return request()->ip();
    }
}

Steps To Reproduce

'queue' => [
        'enable'     => true,
        'connection' => env('QUEUE_CONNECTION', 'database'),
        'queue'      => 'default',
        'delay'      => 2,
    ],

cloudflare protected site and server using loadbalancer

'resolvers' => [
        'ip_address' => \Modules\IRemitify\Resolvers\irUserIPResolver::class,
        'user_agent' => OwenIt\Auditing\Resolvers\UserAgentResolver::class,
        'url'        => OwenIt\Auditing\Resolvers\UrlResolver::class,
    ],

Possible Solutions

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions