diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 000000000..35ca802a3 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +*.mmdb filter=lfs diff=lfs merge=lfs -text +*.mo filter=lfs diff=lfs merge=lfs -text diff --git a/apps/common/utils/__init__.py b/apps/common/utils/__init__.py index b55f22af1..d76814506 100644 --- a/apps/common/utils/__init__.py +++ b/apps/common/utils/__init__.py @@ -5,8 +5,8 @@ from .common import * from .django import * from .encode import * from .http import * -from .ipip import * from .crypto import * from .random import * from .jumpserver import * from .ip import * +from .geoip import * diff --git a/apps/common/utils/geoip/GeoLite2-City.mmdb b/apps/common/utils/geoip/GeoLite2-City.mmdb new file mode 100644 index 000000000..c3b9d8bac --- /dev/null +++ b/apps/common/utils/geoip/GeoLite2-City.mmdb @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:860b4d38beff81667c64da41c026a7dd28c3c93a28ae61fefaa7c26875f35638 +size 73906864 diff --git a/apps/common/utils/geoip/__init__.py b/apps/common/utils/geoip/__init__.py new file mode 100644 index 000000000..16281fe0b --- /dev/null +++ b/apps/common/utils/geoip/__init__.py @@ -0,0 +1 @@ +from .utils import * diff --git a/apps/common/utils/geoip/utils.py b/apps/common/utils/geoip/utils.py new file mode 100644 index 000000000..e80c0b262 --- /dev/null +++ b/apps/common/utils/geoip/utils.py @@ -0,0 +1,46 @@ +# -*- coding: utf-8 -*- +# +import os +import ipaddress + +import geoip2.database +from geoip2.errors import GeoIP2Error +from django.utils.translation import ugettext_lazy as _ +from django.conf import settings + +__all__ = ['get_ip_city'] +reader = None + + +def get_ip_city(ip): + if not ip or '.' not in ip or not isinstance(ip, str): + return _("Invalid ip") + if ':' in ip: + return 'IPv6' + global reader + if reader is None: + path = os.path.join(os.path.dirname(__file__), 'GeoLite2-City.mmdb') + reader = geoip2.database.Reader(path) + + try: + is_private = ipaddress.ip_address(ip.strip()).is_private + if is_private: + return _('LAN') + except ValueError: + return _("Invalid ip") + + try: + response = reader.city(ip) + except GeoIP2Error: + return _("Unknown ip") + + names = response.city.names + if not names: + names = response.country.names + + 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/ipip/__init__.py b/apps/common/utils/ipip/__init__.py deleted file mode 100644 index 103b36654..000000000 --- a/apps/common/utils/ipip/__init__.py +++ /dev/null @@ -1,3 +0,0 @@ -# -*- coding: utf-8 -*- -# -from .utils import * diff --git a/apps/common/utils/ipip/ipipfree.ipdb b/apps/common/utils/ipip/ipipfree.ipdb deleted file mode 100755 index 911e1ac33..000000000 Binary files a/apps/common/utils/ipip/ipipfree.ipdb and /dev/null differ diff --git a/apps/common/utils/ipip/utils.py b/apps/common/utils/ipip/utils.py deleted file mode 100644 index 9dd571a8f..000000000 --- a/apps/common/utils/ipip/utils.py +++ /dev/null @@ -1,24 +0,0 @@ -# -*- coding: utf-8 -*- -# -import os -from django.utils.translation import ugettext as _ - -import ipdb - -__all__ = ['get_ip_city'] -ipip_db = None - - -def get_ip_city(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 = list(set(ipip_db.find(ip, 'CN'))) - if '' in info: - info.remove('') - return ' '.join(info) diff --git a/apps/locale/zh/LC_MESSAGES/djangojs.mo b/apps/locale/zh/LC_MESSAGES/djangojs.mo index c02ebe0ed..57f81568c 100644 Binary files a/apps/locale/zh/LC_MESSAGES/djangojs.mo and b/apps/locale/zh/LC_MESSAGES/djangojs.mo differ diff --git a/requirements/requirements.txt b/requirements/requirements.txt index 9142dcaad..a33745ae1 100644 --- a/requirements/requirements.txt +++ b/requirements/requirements.txt @@ -79,7 +79,6 @@ rest_condition==1.0.3 python-ldap==3.3.1 tencentcloud-sdk-python==3.0.477 django-radius==1.4.0 -ipip-ipdb==1.2.1 django-redis-sessions==0.6.1 unicodecsv==0.14.1 python-daemon==2.2.3 @@ -119,3 +118,4 @@ PyMySQL==1.0.2 cx-Oracle==8.2.1 psycopg2-binary==2.9.1 alibabacloud_dysmsapi20170525==2.0.2 +geoip2==4.4.0