From 2a8f8dd709f296747bbf9a4aee1e49eece812b88 Mon Sep 17 00:00:00 2001 From: ibuler Date: Mon, 16 May 2022 12:18:23 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E4=B8=A4=E4=B8=AA=20ip=20=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/common/utils/__init__.py | 1 - apps/common/utils/{geoip => ip}/__init__.py | 0 .../utils/{ => ip}/geoip/GeoLite2-City.mmdb | 0 apps/common/utils/ip/geoip/__init__.py | 1 + apps/common/utils/{ => ip}/geoip/utils.py | 20 ++++++++-------- apps/common/utils/ip/ipip/__init__.py | 3 +++ apps/common/utils/ip/ipip/ipipfree.ipdb | 3 +++ apps/common/utils/ip/ipip/utils.py | 23 +++++++++++++++++++ apps/common/utils/{ip.py => ip/utils.py} | 18 +++++++++++++++ 9 files changed, 57 insertions(+), 12 deletions(-) rename apps/common/utils/{geoip => ip}/__init__.py (100%) rename apps/common/utils/{ => ip}/geoip/GeoLite2-City.mmdb (100%) create mode 100644 apps/common/utils/ip/geoip/__init__.py rename apps/common/utils/{ => ip}/geoip/utils.py (69%) create mode 100644 apps/common/utils/ip/ipip/__init__.py create mode 100644 apps/common/utils/ip/ipip/ipipfree.ipdb create mode 100644 apps/common/utils/ip/ipip/utils.py rename apps/common/utils/{ip.py => ip/utils.py} (75%) diff --git a/apps/common/utils/__init__.py b/apps/common/utils/__init__.py index d76814506..87d36595b 100644 --- a/apps/common/utils/__init__.py +++ b/apps/common/utils/__init__.py @@ -9,4 +9,3 @@ from .crypto import * from .random import * from .jumpserver import * from .ip import * -from .geoip import * diff --git a/apps/common/utils/geoip/__init__.py b/apps/common/utils/ip/__init__.py similarity index 100% rename from apps/common/utils/geoip/__init__.py rename to apps/common/utils/ip/__init__.py diff --git a/apps/common/utils/geoip/GeoLite2-City.mmdb b/apps/common/utils/ip/geoip/GeoLite2-City.mmdb similarity index 100% rename from apps/common/utils/geoip/GeoLite2-City.mmdb rename to apps/common/utils/ip/geoip/GeoLite2-City.mmdb diff --git a/apps/common/utils/ip/geoip/__init__.py b/apps/common/utils/ip/geoip/__init__.py new file mode 100644 index 000000000..16281fe0b --- /dev/null +++ b/apps/common/utils/ip/geoip/__init__.py @@ -0,0 +1 @@ +from .utils import * diff --git a/apps/common/utils/geoip/utils.py b/apps/common/utils/ip/geoip/utils.py similarity index 69% rename from apps/common/utils/geoip/utils.py rename to apps/common/utils/ip/geoip/utils.py index e80c0b262..5e829f610 100644 --- a/apps/common/utils/geoip/utils.py +++ b/apps/common/utils/ip/geoip/utils.py @@ -8,11 +8,11 @@ from geoip2.errors import GeoIP2Error from django.utils.translation import ugettext_lazy as _ from django.conf import settings -__all__ = ['get_ip_city'] +__all__ = ['get_ip_city_by_geoip'] reader = None -def get_ip_city(ip): +def get_ip_city_by_geoip(ip): if not ip or '.' not in ip or not isinstance(ip, str): return _("Invalid ip") if ':' in ip: @@ -32,15 +32,13 @@ def get_ip_city(ip): try: response = reader.city(ip) except GeoIP2Error: - return _("Unknown ip") + return {} - names = response.city.names - if not names: - names = response.country.names + city_names = response.city.names or {} + lang = settings.LANGUAGE_CODE[:2] + if lang == 'zh': + lang = 'zh-CN' + city = city_names.get(lang, _("Unknown")) + return city - if 'en' in settings.LANGUAGE_CODE and 'en' in names: - return names['en'] - elif 'zh-CN' in names: - return names['zh-CN'] - return _("Unknown ip") diff --git a/apps/common/utils/ip/ipip/__init__.py b/apps/common/utils/ip/ipip/__init__.py new file mode 100644 index 000000000..103b36654 --- /dev/null +++ b/apps/common/utils/ip/ipip/__init__.py @@ -0,0 +1,3 @@ +# -*- coding: utf-8 -*- +# +from .utils import * diff --git a/apps/common/utils/ip/ipip/ipipfree.ipdb b/apps/common/utils/ip/ipip/ipipfree.ipdb new file mode 100644 index 000000000..ddd90522c --- /dev/null +++ b/apps/common/utils/ip/ipip/ipipfree.ipdb @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b82b874152c798dda407ffe7544e1f5ec67efa1f5c334efc0d3893b8053b4be1 +size 3649897 diff --git a/apps/common/utils/ip/ipip/utils.py b/apps/common/utils/ip/ipip/utils.py new file mode 100644 index 000000000..ef97f4794 --- /dev/null +++ b/apps/common/utils/ip/ipip/utils.py @@ -0,0 +1,23 @@ +# -*- coding: utf-8 -*- +# +import os +from django.utils.translation import ugettext as _ + +import ipdb + +__all__ = ['get_ip_city_by_ipip'] +ipip_db = None + + +def get_ip_city_by_ipip(ip): + global ipip_db + if not ip or not isinstance(ip, str): + return _("Invalid ip") + if ':' in ip: + return 'IPv6' + if ipip_db is None: + ipip_db_path = os.path.join(os.path.dirname(__file__), 'ipipfree.ipdb') + ipip_db = ipdb.City(ipip_db_path) + + info = ipip_db.find_info(ip, 'CN') + return {'city': info.city_name, 'country': info.country_name} diff --git a/apps/common/utils/ip.py b/apps/common/utils/ip/utils.py similarity index 75% rename from apps/common/utils/ip.py rename to apps/common/utils/ip/utils.py index 0000a5da7..e75ecb85c 100644 --- a/apps/common/utils/ip.py +++ b/apps/common/utils/ip/utils.py @@ -1,4 +1,9 @@ from ipaddress import ip_network, ip_address +from django.conf import settings +from django.utils.translation import gettext_lazy as _ + +from .ipip import get_ip_city_by_ipip +from .geoip import get_ip_city_by_geoip def is_ip_address(address): @@ -66,3 +71,16 @@ def contains_ip(ip, ip_group): return True return False + + +def get_ip_city(ip): + info = get_ip_city_by_ipip(ip) + city = info.get('city', _("Unknown")) + country = info.get('country') + + # 国内城市 并且 语言是中文就使用国内 + is_zh = settings.LANGUAGE_CODE.startswith('zh') + if country == '中国' and is_zh: + return city + else: + return get_ip_city_by_geoip(ip)