geocoder-php/BazingaGeocoderBundle

Can't make fake_ip to work

lori-neogen opened this issue · 17 comments

I read the docs, and tried in all combinations possible, but still if i dump $request->server->get('REMOTE_ADDR') it will always be 127.0.0.1
I had a working fake_ip config with older version (4.2.0 geocoder), but with the new one cant make it work.
config_dev.yml:

bazinga_geocoder:
  fake_ip:
    enabled: true
    local_ip: 192.168.99.1 # default 127.0.0.1
    ip: 123.123.123.123

any suggestion ?

So if your local_ip is 127.0.0.1 why you replaced it with 192.168.99.1? local_ip option was introduced in #284 when your VM is using another IP.

I'm using latest version atm.
The ip 192.168.99.1 is from the docs, i can replace it with other, but still 127.0.0.1 i get.
My problem is that is seems that no matter what i do it does not change the ip.

I think you are making wrong assumptions. FakeIpPlugin doesn't change the remote address, but instead it replaces the IP in the geocoder query handling when you call: $geocoder->geocodeQuery(GeocodeQuery::create($request->server->get('REMOTE_ADDR')));
Calling $request->server->get('REMOTE_ADDR') will always return your device IP when non in http context.

Ok, that's true i did mad a wrong assumption. But still doesn't work 🙂

my config:

bazinga_geocoder:
    fake_ip:
        enabled:              true
        local_ip:             81.12.153.3
        ip:                   81.12.153.3

then in a testController i use 2 dumps:

        dump($geoIP2->geocodeQuery(GeocodeQuery::create($request->server->get('REMOTE_ADDR'))));
        dump($geoIP2->geocodeQuery(GeocodeQuery::create('81.12.153.3')));

First dump:

AddressCollection {#111529 ▼
  -locations: []
}

Second dump

AddressCollection {#111548 ▼
  -locations: array:1 [▼
    0 => Address {#111550 ▼
      -coordinates: Coordinates {#111552 ▶}
      -bounds: null
      -streetNumber: null
      -streetName: null
      -subLocality: null
      -locality: "Voluntari"
      -postalCode: "077190"
      -adminLevels: AdminLevelCollection {#111551 ▶}
      -country: Country {#111553 ▶}
      -timezone: "Europe/Bucharest"
      -providedBy: "geoip2"
    }
  ]
}

If you want to fake 127.0.0.1 to 81.12.153.3, then you don't need to configure the local_ip option.

Ok, that is how the old config was, without the local_ip option, but since it wasnt working, i tried that too. Even so, after removing the local_ip option, same result.

Then that's strange, because FakeIpPlugin is covered by tests :)
Could you try to dump $_SERVER['REMOTE_ADDR'], $this->needle and $replacement in FakeIpPlugin code?

I did try, but doesnt get dumped for some reason. I'm sure the plugin is ok, but i can't make it work.
in config.yml i have

bazinga_geocoder:
    providers:
        googleMaps:
            aliases:
                - google_maps
            factory: Bazinga\GeocoderBundle\ProviderFactory\GoogleMapsFactory
            locale: "%locale%"
            cache: 'app.simple_cache'
            options:
                httplug_client: '@httplug.client' # When using HTTPlugBundle
                api_key: "%google_maps_server_api_key%"
                region: ro
        geoIp2:
            factory: Bazinga\GeocoderBundle\ProviderFactory\GeoIP2Factory
            options:
                provider: 'service'
                provider_service: 'app.geoip2.reader'

and
in services.yml

    app.geoip2.reader:
        class: GeoIp2\Database\Reader
        arguments:
            - '%kernel.root_dir%/data/GeoIP2.mmdb'

I'm using symfony 3.4.40 , and i'm upgrading packakes so i can upgrade to symfony 4
I'm sure it's a misconfig, but i just cant find it.

In your config.yml fake_ip option is missing or you just excluded it now?

the fake_ip option was in config_dev.yml not in config.yml
Do i need some config also in config.yml ? i want to use the faker only in dev mode.

Could you paste the output from php bin/console debug:config BazingaGeocoderBundle?

Current configuration for "BazingaGeocoderBundle"
=================================================

bazinga_geocoder:
    providers:
        googleMaps:
            aliases:
                - google_maps
            factory: Bazinga\GeocoderBundle\ProviderFactory\GoogleMapsFactory
            locale: en
            cache: app.simple_cache
            options:
                httplug_client: '@httplug.client'
                api_key: '%env(GOOGLE_MAPS_SERVER_API_KEY)%'
                region: ro
            cache_lifetime: null
            cache_precision: null
            limit: null
            logger: null
            plugins: {  }
        geoIp2:
            factory: Bazinga\GeocoderBundle\ProviderFactory\GeoIP2Factory
            options:
                provider: service
                provider_service: app.geoip2.reader
            cache: null
            cache_lifetime: null
            cache_precision: null
            limit: null
            locale: null
            logger: null
            aliases: {  }
            plugins: {  }
    fake_ip:
        enabled: true
        ip: 81.12.153.3
        local_ip: 127.0.0.1
        use_faker: false
    profiling:
        enabled: true

Looks like it's enabled, but if debugging FakeIpPlugin dump doesn't show up, I'd look if It's really included in the container.
Also could you paste output of php bin/console debug:container bazinga_geocoder.provider --show-arguments?

 Select one of the following services to display its information:
  [0] bazinga_geocoder.provider.googleMaps.cache
  [1] bazinga_geocoder.provider.googleMaps.locale
  [2] bazinga_geocoder.provider.googleMaps.profiler
  [3] bazinga_geocoder.provider.googleMaps
  [4] bazinga_geocoder.provider.geoIp2.profiler
  [5] bazinga_geocoder.provider.geoIp2
 > 5


Information for Service "bazinga_geocoder.provider.geoIp2"
==========================================================

 ---------------- --------------------------------------------------------------- 
  Option           Value                                                          
 ---------------- --------------------------------------------------------------- 
  Service ID       bazinga_geocoder.provider.geoIp2                               
  Class            Geocoder\Plugin\PluginProvider                                 
  Tags             bazinga_geocoder.provider                                      
  Public           no                                                             
  Synthetic        no                                                             
  Lazy             no                                                             
  Shared           yes                                                            
  Abstract         no                                                             
  Autowired        no                                                             
  Autoconfigured   no                                                             
  Factory Class    Bazinga\GeocoderBundle\ProviderFactory\PluginProviderFactory   
  Factory Method   createPluginProvider                                           
  Arguments        Array (2 element(s))                                           
                   Service(Bazinga\GeocoderBundle\ProviderFactory\GeoIP2Factory)  
                   Array (2 element(s))                                           
 ---------------- --------------------------------------------------------------- 

I have tested with a simple configuration:

bazinga_geocoder:
    fake_ip: 123.123.123.123
    providers:
        ip:
            factory: Bazinga\GeocoderBundle\ProviderFactory\FreeGeoIpFactory
<?php

declare(strict_types=1);

namespace App\Command;

use Geocoder\Provider\Provider;
use Geocoder\Query\GeocodeQuery;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

final class TestCommand extends Command
{
    protected static $defaultName = 'test';

    private Provider $ipGeocoder;

    public function __construct(Provider $ipGeocoder)
    {
        parent::__construct();

        $this->ipGeocoder = $ipGeocoder;
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $this->ipGeocoder->geocodeQuery(GeocodeQuery::create('127.0.0.1'));

        return 0;
    }
}

And added dumps to FakeIpPlugin and for me it properly replaces 127.0.0.1 with 123.123.123.123.

Hmm.. i tried in 2 projects
symfony 4.4 - working
symfony 3.4 - not working for some reasone. Probably the problem is from the project somewhere...
Thx!

Finally i made it work.
I was using Docker, so i configured the local_ip but i didtnt understood from documentation that i need to put the actual local ip there.
So if my ip from docker was 172.18.0.1 then i needed to put there that particular IP so when in FapeIpPlugin could replace the ip corectly here:
str_replace($this->needle, $replacement, $query->getText(), $count);