perf: 修改ip城市获取算法

perf: 优化使用lfs
pull/7010/head
ibuler 2021-10-13 17:44:02 +08:00 committed by Jiangjie.Bai
parent b8c43f5944
commit 0207fe60c5
10 changed files with 54 additions and 29 deletions

2
.gitattributes vendored Normal file
View File

@ -0,0 +1,2 @@
*.mmdb filter=lfs diff=lfs merge=lfs -text
*.mo filter=lfs diff=lfs merge=lfs -text

View File

@ -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 *

View File

@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:860b4d38beff81667c64da41c026a7dd28c3c93a28ae61fefaa7c26875f35638
size 73906864

View File

@ -0,0 +1 @@
from .utils import *

View File

@ -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")

View File

@ -1,3 +0,0 @@
# -*- coding: utf-8 -*-
#
from .utils import *

Binary file not shown.

View File

@ -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)

View File

@ -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