0

I am deploying my laravel application using docker + kubernetes and aws load balancer.

There is one middleware 'EventLogger' which is run after user logs in to the system. This logger logs the user's IP address and saves into mongo db.

Here is that middleware:

<?php

namespace App\Http\Middleware;

use Closure;
use Auth;
use App\Models\EventLogger as Logger;
use App\Models\OauthClient;
use Jenssegers\Agent\Agent;
use App\Models\Advertisement;
use App\Jobs\ProcessEventLogger;

class EventLogger
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next, $event = null)
    {
        //$request->request->add(["event" => $event]);
        $request->merge(["event" => $event]);
        return $next($request);
    }

    public function terminate($request, $response)
    {
        if($response->getStatusCode() !=  200){
            return;
        }

        $response_contents = $response->getContent();

        //allow only status = true responses
        if(false == json_decode($response_contents)->status){
            return;
        }

        if (env('APP_ENV') === 'testing') {
            $client_id = 3;
        } else{
            if ($request->event == Logger::LOGIN) {
                $client_id = $request->client_id;
            } else {
                $client_id = Auth::user()->token()->client_id;
            }
        }

        $agent = new Agent();
        $agent->browser();
        $agent->device();
        $agent->platform();

        ProcessEventLogger::dispatch($request->all(),$response,  Auth::user()->id, $request->ip(),$client_id, $response_contents, $request->coin_id,$agent->browser(),$agent->device(),$agent->platform());
   }
}

ProcessEventLogger is the background queued job, which is working fine.

The problem is $request->ip() always returns 127.0.0.1

I have tried most of the solutions which are answered here but there is one solution which uses trusted proxies of laravel middleware.

How can I use that feature, as I do not want protected $proxies = '*' and also I can't provide proxy ip address as everytime I restart the pod, new ip address is generated.

Please guide me in this situation.

Dinesh Suthar
  • 853
  • 1
  • 17
  • 41

1 Answers1

1

as I do not want protected $proxies = '*'

Sorry, looks like you don't have many options. Either you will have to know the IP Range to allow. OR Configure your web server accordingly. Quoting from Symfony Doc which Laravel uses.

Some reverse proxies (like AWS Elastic Load Balancing) don’t have a static IP address or even a range that you can target with the CIDR notation. In this case, you’ll need to - very carefully - trust all proxies.

Configure your web server(s) to not respond to traffic from any clients other than your load balancers. For AWS, this can be done with security groups.

Once you’ve guaranteed that traffic will only come from your trusted reverse proxies, configure Symfony to always trust incoming request:

https://symfony.com/doc/current/deployment/proxies.html#but-what-if-the-ip-of-my-reverse-proxy-changes-constantly

Stephan Vierkant
  • 9,674
  • 8
  • 61
  • 97
Jigar
  • 3,256
  • 1
  • 30
  • 51
  • Thank you @Jigar, ultimately I had to allow all the proxies in the middleware and will configure the webserver to drive traffic through selected proxies only. – Dinesh Suthar Jan 22 '21 at 18:17