From eae6f0b818f196c41ee6e0cc689e1382f1f42dbb Mon Sep 17 00:00:00 2001 From: vapao Date: Fri, 18 Feb 2022 09:28:46 +0800 Subject: [PATCH] =?UTF-8?q?F=20=E4=BF=AE=E5=A4=8Dwebsocket=E8=BF=9E?= =?UTF-8?q?=E6=8E=A5=E8=AE=A4=E8=AF=81=E6=9C=AA=E5=BA=94=E7=94=A8=E5=AE=89?= =?UTF-8?q?=E5=85=A8=E8=AE=BE=E7=BD=AE=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spug_api/consumer/middleware.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/spug_api/consumer/middleware.py b/spug_api/consumer/middleware.py index d118bad..bba5d58 100644 --- a/spug_api/consumer/middleware.py +++ b/spug_api/consumer/middleware.py @@ -4,6 +4,7 @@ from django.db import close_old_connections from channels.security.websocket import WebsocketDenier from apps.account.models import User +from apps.setting.utils import AppSetting from libs.utils import get_request_real_ip from urllib.parse import parse_qs import time @@ -41,8 +42,9 @@ class AuthMiddleware: token = parse_qs(query_string).get('x-token', [''])[0] if token and len(token) == 32: user = User.objects.filter(access_token=token).first() - if user and x_real_ip == user.last_ip and user.token_expired >= time.time() and user.is_active: - scope['user'] = user - return True, None - return False, f'Verify failed: {x_real_ip} <> {user.last_ip if user else None}' + if user and user.token_expired >= time.time() and user.is_active: + if x_real_ip == user.last_ip or AppSetting.get_default('bind_ip') is False: + scope['user'] = user + return True, None + return False, f'Verify failed: {x_real_ip} <> {user.last_ip if user else None}' return False, 'Token is invalid'