mirror of https://github.com/jumpserver/jumpserver
parent
a08dd5ee72
commit
041302d5d2
|
@ -13,10 +13,6 @@ reader = None
|
||||||
|
|
||||||
|
|
||||||
def get_ip_city_by_geoip(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:
|
|
||||||
return 'IPv6'
|
|
||||||
global reader
|
global reader
|
||||||
if reader is None:
|
if reader is None:
|
||||||
path = os.path.join(os.path.dirname(__file__), 'GeoLite2-City.mmdb')
|
path = os.path.join(os.path.dirname(__file__), 'GeoLite2-City.mmdb')
|
||||||
|
@ -32,7 +28,7 @@ def get_ip_city_by_geoip(ip):
|
||||||
try:
|
try:
|
||||||
response = reader.city(ip)
|
response = reader.city(ip)
|
||||||
except GeoIP2Error:
|
except GeoIP2Error:
|
||||||
return {}
|
return _("Unknown")
|
||||||
|
|
||||||
city_names = response.city.names or {}
|
city_names = response.city.names or {}
|
||||||
lang = settings.LANGUAGE_CODE[:2]
|
lang = settings.LANGUAGE_CODE[:2]
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
#
|
#
|
||||||
import os
|
import os
|
||||||
from django.utils.translation import ugettext as _
|
|
||||||
|
|
||||||
import ipdb
|
import ipdb
|
||||||
|
|
||||||
|
@ -11,13 +10,13 @@ ipip_db = None
|
||||||
|
|
||||||
def get_ip_city_by_ipip(ip):
|
def get_ip_city_by_ipip(ip):
|
||||||
global ipip_db
|
global ipip_db
|
||||||
if not ip or not isinstance(ip, str):
|
|
||||||
return _("Invalid ip")
|
|
||||||
if ':' in ip:
|
|
||||||
return 'IPv6'
|
|
||||||
if ipip_db is None:
|
if ipip_db is None:
|
||||||
ipip_db_path = os.path.join(os.path.dirname(__file__), 'ipipfree.ipdb')
|
ipip_db_path = os.path.join(os.path.dirname(__file__), 'ipipfree.ipdb')
|
||||||
ipip_db = ipdb.City(ipip_db_path)
|
ipip_db = ipdb.City(ipip_db_path)
|
||||||
|
try:
|
||||||
info = ipip_db.find_info(ip, 'CN')
|
info = ipip_db.find_info(ip, 'CN')
|
||||||
|
except ValueError:
|
||||||
|
return None
|
||||||
|
if not info:
|
||||||
|
raise None
|
||||||
return {'city': info.city_name, 'country': info.country_name}
|
return {'city': info.city_name, 'country': info.country_name}
|
||||||
|
|
|
@ -74,13 +74,18 @@ def contains_ip(ip, ip_group):
|
||||||
|
|
||||||
|
|
||||||
def get_ip_city(ip):
|
def get_ip_city(ip):
|
||||||
info = get_ip_city_by_ipip(ip)
|
if not ip or not isinstance(ip, str):
|
||||||
city = info.get('city', _("Unknown"))
|
return _("Invalid ip")
|
||||||
country = info.get('country')
|
if ':' in ip:
|
||||||
|
return 'IPv6'
|
||||||
|
|
||||||
# 国内城市 并且 语言是中文就使用国内
|
info = get_ip_city_by_ipip(ip)
|
||||||
is_zh = settings.LANGUAGE_CODE.startswith('zh')
|
if info:
|
||||||
if country == '中国' and is_zh:
|
city = info.get('city', _("Unknown"))
|
||||||
return city
|
country = info.get('country')
|
||||||
else:
|
|
||||||
return get_ip_city_by_geoip(ip)
|
# 国内城市 并且 语言是中文就使用国内
|
||||||
|
is_zh = settings.LANGUAGE_CODE.startswith('zh')
|
||||||
|
if country == '中国' and is_zh:
|
||||||
|
return city
|
||||||
|
return get_ip_city_by_geoip(ip)
|
||||||
|
|
Loading…
Reference in New Issue