mirror of
https://github.com/jumpserver/jumpserver.git
synced 2025-09-06 18:00:57 +00:00
[Update] 更新index view
This commit is contained in:
@@ -5,7 +5,8 @@ import base64
|
||||
import logging
|
||||
import uuid
|
||||
|
||||
from paramiko.rsakey import RSAKey
|
||||
import requests
|
||||
import ipaddress
|
||||
from django.conf import settings
|
||||
from django.contrib.auth.mixins import UserPassesTestMixin
|
||||
from django.contrib.auth import authenticate
|
||||
@@ -14,13 +15,7 @@ from django.core.cache import cache
|
||||
|
||||
from common.tasks import send_mail_async
|
||||
from common.utils import reverse, get_object_or_none
|
||||
from .models import User
|
||||
|
||||
|
||||
# try:
|
||||
# from io import StringIO
|
||||
# except ImportError:
|
||||
# from StringIO import StringIO
|
||||
from .models import User, LoginLog
|
||||
|
||||
|
||||
logger = logging.getLogger('jumpserver')
|
||||
@@ -172,3 +167,40 @@ def generate_token(request, user):
|
||||
return token
|
||||
|
||||
|
||||
def validate_ip(ip):
|
||||
try:
|
||||
ipaddress.ip_address(ip)
|
||||
return True
|
||||
except ValueError:
|
||||
pass
|
||||
return False
|
||||
|
||||
|
||||
def write_login_log(username, type='', ip='', user_agent=''):
|
||||
if not (ip and validate_ip(ip)):
|
||||
ip = '0.0.0.0'
|
||||
city = get_ip_city(ip)
|
||||
LoginLog.objects.create(
|
||||
username=username, type=type,
|
||||
ip=ip, city=city, user_agent=user_agent
|
||||
)
|
||||
|
||||
|
||||
def get_ip_city(ip, timeout=10):
|
||||
# Taobao ip api: http://ip.taobao.com//service/getIpInfo.php?ip=8.8.8.8
|
||||
# Sina ip api: http://int.dpool.sina.com.cn/iplookup/iplookup.php?ip=8.8.8.8&format=json
|
||||
|
||||
url = 'http://int.dpool.sina.com.cn/iplookup/iplookup.php?ip=%s&format=json' % ip
|
||||
try:
|
||||
r = requests.get(url, timeout=timeout)
|
||||
except requests.Timeout:
|
||||
r = None
|
||||
city = 'Unknown'
|
||||
if r and r.status_code == 200:
|
||||
try:
|
||||
data = r.json()
|
||||
if not isinstance(data, int) and data['ret'] == 1:
|
||||
city = data['country'] + ' ' + data['city']
|
||||
except ValueError:
|
||||
pass
|
||||
return city
|
||||
|
Reference in New Issue
Block a user