mirror of
https://github.com/jumpserver/jumpserver.git
synced 2025-09-18 16:39:28 +00:00
perf: 优化使用两个 ip 库
This commit is contained in:
3
apps/common/utils/ip/geoip/GeoLite2-City.mmdb
Normal file
3
apps/common/utils/ip/geoip/GeoLite2-City.mmdb
Normal file
@@ -0,0 +1,3 @@
|
||||
version https://git-lfs.github.com/spec/v1
|
||||
oid sha256:860b4d38beff81667c64da41c026a7dd28c3c93a28ae61fefaa7c26875f35638
|
||||
size 73906864
|
1
apps/common/utils/ip/geoip/__init__.py
Normal file
1
apps/common/utils/ip/geoip/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
from .utils import *
|
44
apps/common/utils/ip/geoip/utils.py
Normal file
44
apps/common/utils/ip/geoip/utils.py
Normal file
@@ -0,0 +1,44 @@
|
||||
# -*- 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_by_geoip']
|
||||
reader = None
|
||||
|
||||
|
||||
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:
|
||||
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 {}
|
||||
|
||||
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
|
||||
|
||||
|
Reference in New Issue
Block a user