perf: 修改其它获取ip函数

pull/11976/head
wangruidong 2023-10-26 15:44:11 +08:00 committed by 老广
parent cc656a8a97
commit e5e3289dc1
9 changed files with 24 additions and 29 deletions

View File

@ -1,7 +1,7 @@
from django.db import models
from django.utils.translation import ugettext_lazy as _
from .base import BaseACL, BaseACLQuerySet
from common.utils import get_request_ip, get_ip_city
from common.utils import get_request_ip_or_data, get_ip_city
from common.utils.ip import contains_ip
from common.utils.time_period import contains_time_period
from common.utils.timezone import local_now_display
@ -73,7 +73,7 @@ class LoginACL(BaseACL):
from tickets.models import ApplyLoginTicket
from orgs.models import Organization
title = _('Login confirm') + ' {}'.format(self.user)
login_ip = get_request_ip(request) if request else ''
login_ip = get_request_ip_or_data(request) if request else ''
login_ip = login_ip or '0.0.0.0'
login_city = get_ip_city(login_ip)
login_datetime = local_now_display()

View File

@ -4,7 +4,7 @@ from django.db import transaction
from django.core.cache import cache
from django.utils.translation import ugettext_lazy as _
from common.utils import get_request_ip, get_logger
from common.utils import get_request_ip_or_data, get_logger
from common.utils.timezone import as_current_tz
from common.utils.encode import Singleton
from common.local import encrypted_field_set
@ -15,7 +15,6 @@ from orgs.utils import get_current_org_id
from .backends import get_operate_log_storage
logger = get_logger(__name__)
@ -156,7 +155,7 @@ class OperatorLogHandler(metaclass=Singleton):
if not user or not user.is_authenticated:
return
remote_addr = get_request_ip(current_request)
remote_addr = get_request_ip_or_data(current_request)
resource_display = self.get_resource_display(resource)
before, after = self.data_processing(before, after)
if not force and not any([before, after]):

View File

@ -38,10 +38,9 @@ from .const import MODELS_NEED_RECORD
from terminal.backends.command.serializers import SessionCommandSerializer
from terminal.serializers import SessionSerializer
from common.const.signals import POST_ADD, POST_REMOVE, POST_CLEAR, SKIP_SIGNAL
from common.utils import get_request_ip, get_logger, get_syslogger
from common.utils import get_request_ip_or_data, get_logger, get_syslogger
from common.utils.encode import data_to_json
logger = get_logger(__name__)
sys_logger = get_syslogger(__name__)
json_render = JSONRenderer()
@ -198,7 +197,7 @@ def on_user_change_password(sender, user=None, **kwargs):
remote_addr = '127.0.0.1'
change_by = 'System'
else:
remote_addr = get_request_ip(current_request)
remote_addr = get_request_ip_or_data(current_request)
if not current_request.user.is_authenticated:
change_by = str(user)
else:
@ -250,7 +249,7 @@ def get_login_backend(request):
def generate_data(username, request, login_type=None):
user_agent = request.META.get('HTTP_USER_AGENT', '')
login_ip = get_request_ip(request) or '0.0.0.0'
login_ip = get_request_ip_or_data(request) or '0.0.0.0'
if login_type is None and isinstance(request, Request):
login_type = request.META.get('HTTP_X_JMS_LOGIN_TYPE', 'U')

View File

@ -9,7 +9,7 @@ from django.contrib.auth import logout as auth_logout
from apps.authentication import mixins
from common.utils import gen_key_pair
from common.utils import get_request_ip
from common.utils import get_request_ip_or_data
from .signals import post_auth_failed
@ -73,7 +73,7 @@ class ThirdPartyLoginMiddleware(mixins.AuthMixin):
if request.path.find(url) > -1:
return response
ip = get_request_ip(request)
ip = get_request_ip_or_data(request)
try:
self.request = request
self._check_login_acl(request.user, ip)

View File

@ -6,7 +6,7 @@ from urllib.parse import urljoin, urlparse
from django.conf import settings
from django.utils.translation import ugettext_lazy as _
from common.utils import validate_ip, get_ip_city, get_request_ip
from common.utils import validate_ip, get_ip_city, get_request_ip_or_data
from common.utils import get_logger
from audits.models import UserLoginLog
from audits.const import DEFAULT_CITY
@ -19,7 +19,7 @@ def check_different_city_login_if_need(user, request):
if not settings.SECURITY_CHECK_DIFFERENT_CITY_LOGIN:
return
ip = get_request_ip(request) or '0.0.0.0'
ip = get_request_ip_or_data(request) or '0.0.0.0'
if not (ip and validate_ip(ip)):
city = DEFAULT_CITY
else:

View File

@ -20,13 +20,12 @@ from authentication import errors
from authentication.mixins import AuthMixin
from authentication.const import ConfirmType
from common.sdk.im.dingtalk import DingTalk
from common.utils.common import get_request_ip
from common.utils.common import get_request_ip_or_data
from authentication.notifications import OAuthBindMessage
from .mixins import METAMixin
logger = get_logger(__file__)
DINGTALK_STATE_SESSION_KEY = '_dingtalk_state'
@ -175,7 +174,7 @@ class DingTalkQRBindCallbackView(DingTalkQRMixin, View):
return response
raise e
ip = get_request_ip(request)
ip = get_request_ip_or_data(request)
OAuthBindMessage(user, ip, _('DingTalk'), user_id).publish_async()
msg = _('Binding DingTalk successfully')
response = self.get_success_response(redirect_url, msg, msg)
@ -200,7 +199,7 @@ class DingTalkEnableStartView(UserVerifyPasswordView):
class DingTalkQRLoginView(DingTalkQRMixin, METAMixin, View):
permission_classes = (AllowAny,)
def get(self, request: HttpRequest):
def get(self, request: HttpRequest):
redirect_url = request.GET.get('redirect_url') or reverse('index')
next_url = self.get_next_url_from_meta() or reverse('index')
@ -258,7 +257,7 @@ class DingTalkQRLoginCallbackView(AuthMixin, DingTalkQRMixin, View):
class DingTalkOAuthLoginView(DingTalkOAuthMixin, View):
permission_classes = (AllowAny,)
def get(self, request: HttpRequest):
def get(self, request: HttpRequest):
redirect_url = request.GET.get('redirect_url')
redirect_uri = reverse('authentication:dingtalk-oauth-login-callback', external=True)

View File

@ -16,7 +16,7 @@ from common.utils.django import reverse, get_object_or_none
from common.mixins.views import UserConfirmRequiredExceptionMixin, PermissionsMixin
from common.permissions import UserConfirmation
from common.sdk.im.feishu import FeiShu, URL
from common.utils.common import get_request_ip
from common.utils.common import get_request_ip_or_data
from authentication import errors
from authentication.const import ConfirmType
from authentication.mixins import AuthMixin
@ -24,7 +24,6 @@ from authentication.notifications import OAuthBindMessage
logger = get_logger(__file__)
FEISHU_STATE_SESSION_KEY = '_feishu_state'
@ -140,7 +139,7 @@ class FeiShuQRBindCallbackView(FeiShuQRMixin, View):
return response
raise e
ip = get_request_ip(request)
ip = get_request_ip_or_data(request)
OAuthBindMessage(user, ip, _('FeiShu'), user_id).publish_async()
msg = _('Binding FeiShu successfully')
response = self.get_success_response(redirect_url, msg, msg)
@ -165,7 +164,7 @@ class FeiShuEnableStartView(UserVerifyPasswordView):
class FeiShuQRLoginView(FeiShuQRMixin, View):
permission_classes = (AllowAny,)
def get(self, request: HttpRequest):
def get(self, request: HttpRequest):
redirect_url = request.GET.get('redirect_url') or reverse('index')
redirect_uri = reverse('authentication:feishu-qr-login-callback', external=True)
redirect_uri += '?' + urlencode({

View File

@ -16,7 +16,7 @@ from common.utils.django import reverse, get_object_or_none
from common.sdk.im.wecom import URL
from common.sdk.im.wecom import WeCom
from common.mixins.views import UserConfirmRequiredExceptionMixin, PermissionsMixin
from common.utils.common import get_request_ip
from common.utils.common import get_request_ip_or_data
from common.permissions import UserConfirmation
from authentication import errors
from authentication.mixins import AuthMixin
@ -26,7 +26,6 @@ from .mixins import METAMixin
logger = get_logger(__file__)
WECOM_STATE_SESSION_KEY = '_wecom_state'
@ -174,7 +173,7 @@ class WeComQRBindCallbackView(WeComQRMixin, View):
return response
raise e
ip = get_request_ip(request)
ip = get_request_ip_or_data(request)
OAuthBindMessage(user, ip, _('WeCom'), wecom_userid).publish_async()
msg = _('Binding WeCom successfully')
response = self.get_success_response(redirect_url, msg, msg)
@ -196,7 +195,7 @@ class WeComEnableStartView(UserVerifyPasswordView):
class WeComQRLoginView(WeComQRMixin, METAMixin, View):
permission_classes = (AllowAny,)
def get(self, request: HttpRequest):
def get(self, request: HttpRequest):
redirect_url = request.GET.get('redirect_url') or reverse('index')
next_url = self.get_next_url_from_meta() or reverse('index')
redirect_uri = reverse('authentication:wecom-qr-login-callback', external=True)
@ -253,7 +252,7 @@ class WeComQRLoginCallbackView(AuthMixin, WeComQRMixin, View):
class WeComOAuthLoginView(WeComOAuthMixin, View):
permission_classes = (AllowAny,)
def get(self, request: HttpRequest):
def get(self, request: HttpRequest):
redirect_url = request.GET.get('redirect_url')
redirect_uri = reverse('authentication:wecom-oauth-login-callback', external=True)

View File

@ -4,7 +4,7 @@ from django.utils.translation import ugettext_lazy as _
from common.drf.serializers import BulkModelSerializer, AdaptedBulkListSerializer
from common.utils import is_uuid
from users.serializers import ServiceAccountSerializer
from common.utils import get_request_ip, pretty_string
from common.utils import get_request_ip_or_data, pretty_string
from .. import const
from ..models import (
@ -129,7 +129,7 @@ class TerminalRegistrationSerializer(serializers.ModelSerializer):
request = self.context.get('request')
instance.is_accepted = True
if request:
instance.remote_addr = get_request_ip(request)
instance.remote_addr = get_request_ip_or_data(request)
sa = self.service_account.create(validated_data)
sa.system_roles.add_role_system_component()
instance.user = sa