odoo / odoo

Odoo. Open Source Apps To Grow Your Business.
https://www.odoo.com
Other
37.41k stars 24.32k forks source link

GeoIP not working behind reverse proxy #106065

Open olivierb2 opened 1 year ago

olivierb2 commented 1 year ago

Impacted versions: 15.0

Steps to reproduce:

Current behavior: GeoIP returns empty response

Expected behavior: Returning country and other GeoIP information

Details:

Dear support,

I'm using Odoo CE behind a Nginx reverse proxy. Despite I'm correctly forwarding remote_addr in X-Real-IP and also populate X-Forwarded-For, GeoIP use proxy IP. I quickly had a look what does proxy_mode = True, but it seems only related to X-Forwarded-Host header.

I made a durty fix by patching file /usr/lib/python3/dist-packages/odoo/addons/http_routing/models/ir_http.py with the following code.

    @classmethod
    def _geoip_resolve(cls):
        if 'geoip' not in request.session or request.session.geoip == {}:
            record = {}
            if not config['proxy_mode']:
                if odoo._geoip_resolver and request.httprequest.remote_addr:
                    record = odoo._geoip_resolver.resolve(request.httprequest.remote_addr) or {}
                request.session['geoip'] = record
            else:
                try:
                    if odoo._geoip_resolver and request.httprequest.headers["X-Real-IP"]:
                        record = odoo._geoip_resolver.resolve(request.httprequest.headers["X-Real-IP"]) or {}
                    request.session['geoip'] = record
                except:
                    pass
rk-aix commented 1 year ago

Hi Olivier,

I had a similar problem on Ubuntu 20.04 nginx/1.23.2 and Odoo 14 & 15, I assume you have Oddo 15 running in the virtual environment. If you only install geoip2 in the "normal" environment (e.g. via "pip3 install geoip2"), it won't work properly. After I installed geoip2 under VENV everything runs smoothly ;-)

Maybe this will work for you too... Kind regards rkaix

olivierb2 commented 9 months ago

Hi Rkaix,

I deployed to new Odoo 16.0 instance today, and seems this issue is still here. I do not use any virtual env and geoip package is used correctly with my code patch.

It's not even worth, I was requiring to patch two fonctions from /usr/lib/python3/dist-packages/odoo/http.py:

    @property
    def geoip(self):
        """
        Get the remote address geolocalisation.

        When geolocalization is successful, the return value is a
        dictionary whose format is:

            {'city': str, 'country_code': str, 'country_name': str,
             'latitude': float, 'longitude': float, 'region': str,
             'time_zone': str}

        When geolocalization fails, an empty dict is returned.
        """
        if '_geoip' not in self.session:
            was_dirty = self.session.is_dirty
            self.session._geoip = (self.registry['ir.http']._geoip_resolve()
                                   if self.db else self._geoip_resolve())
            self.session.is_dirty = was_dirty
        self.session._geoip = self._geoip_resolve()
        return self.session._geoip

    def _geoip_resolve(self):
        if config['proxy_mode']:
            if not (root.geoip_resolver and self.httprequest.headers["X-Real-IP"]):
                return {}
            print(self.httprequest.headers["X-Real-IP"])
            return root.geoip_resolver.resolve(self.httprequest.headers["X-Real-IP"]) or {}
        else:
            if not (root.geoip_resolver and self.httprequest.remote_addr):
                return {}
            return root.geoip_resolver.resolve(self.httprequest.remote_addr) or {}
rk-aix commented 9 months ago

Hi Olivier,

sure that you're on 16.0 and not 16.2 ??

Did you consider the tip from Romain Derie (https://github.com/rdeodoo) ?

-> ~2 weeks ago: https://github.com/odoo/odoo/issues/141657) -> Have you correctly added the new geoip2 python dependency, required since Odoo 16.2 ?

geoip2==2.9.0 gevent==1.5.0 ; python_version == '3.7' gevent==20.9.0 ; python_version > '3.7' and python_version <= '3.9' gevent==21.8.0 ; python_version > '3.9' # (Jammy)

https://github.com/odoo/odoo/commit/c59750d8244068e191a78631f8e0fd3d09067e3d#diff-4d7c51b1efe9043e44439a949dfd92e5827321b34082903477fd04876edb7552R9

Bye & good luck rkaix

olivierb2 commented 9 months ago

I'm not sure which version I'm using. I'm running on Debian with Official repository and up to date odoo version 16.0.20231115. I understood Odoo is a rolling release with daily builds on specific major version.

The change you are mentionning seems to be in branch 17.0 but not in 16.0. I didn't tested yet with 17.0 as there is too much missing OCA modules yet.

GeoIP is working on my end, but the not behind a reverse proxy.

rk-aix commented 9 months ago

Sorry Olivier, my mistake, I was on the wrong path. Odoo 16.2 and 16.3 are only available for odoo.sh / online, you are already on the latest CE version...

Nevertheless, I had a similar problem with an 'old' Nginx version V.18.x some time ago. Which version are you running?

Maybe the discussion here with Sion Sweet in Odoo-forum will help you. See below, my reference to Nginx PPA by Ondřej.

Discussion with Sion: https://www.odoo.com/de_DE/forum/hilfe-1/help-multi-website-geoip-redirect-not-working-214014

Direct link to Ondřej's PPA's: https://launchpad.net/~ondrej

Good luck Regards rkaix

olivierb2 commented 9 months ago

I'm under nginx 1.18, but I assume my problem is clearly identified: I see nowhere in the code where X-Real-IP is used, so I'm just wondering if someone already used GeoIP with reverse proxy.

No bid deal anyway, my fix works like a charm anyway.

rk-aix commented 9 months ago

Hi Olivier,

that was exactly what i had supposed, Nginx. 18.x does not communicate correctly with Odoo's geolocation or Odoo does not interpret it correctly. In shell 'mmdblookup' runs correctly, so I decided to compile Nginx 18.x with GeoIP2-Module support with the following results: GeoIp2 runs but data is still not given to Odoo, therefore no chance... Since I updated to Ondrej's new PPA (Nginx > 1.25.3 - from the year 2020 with GeoIp2-Module support enabled), it runs like a charm in all versions 14, 15 and 16.

So i think it's worth a try.

Have a nice weekend Bye rkaix