fix ip header

This commit is contained in:
virusdefender 2018-01-06 23:53:41 +08:00
parent d8221ca8f5
commit 0e83a920b2
5 changed files with 11 additions and 5 deletions

View File

@ -1,3 +1,4 @@
from django.conf import settings
from django.db import connection from django.db import connection
from django.utils.timezone import now from django.utils.timezone import now
from django.utils.deprecation import MiddlewareMixin from django.utils.deprecation import MiddlewareMixin
@ -19,10 +20,11 @@ class APITokenAuthMiddleware(MiddlewareMixin):
class SessionRecordMiddleware(MiddlewareMixin): class SessionRecordMiddleware(MiddlewareMixin):
def process_request(self, request): def process_request(self, request):
request.ip = request.META.get(settings.IP_HEADER, request.META.get("REMOTE_ADDR"))
if request.user.is_authenticated(): if request.user.is_authenticated():
session = request.session session = request.session
session["user_agent"] = request.META.get("HTTP_USER_AGENT", "") session["user_agent"] = request.META.get("HTTP_USER_AGENT", "")
session["ip"] = request.META.get("HTTP_X_REAL_IP", request.META.get("REMOTE_ADDR")) session["ip"] = request.ip
session["last_activity"] = now() session["last_activity"] = now()
user_sessions = request.user.session_keys user_sessions = request.user.session_keys
if session.session_key not in user_sessions: if session.session_key not in user_sessions:

View File

@ -1,6 +1,7 @@
import hashlib import hashlib
from unittest import mock from unittest import mock
from django.conf import settings
from django.utils import timezone from django.utils import timezone
from options.options import SysOptions from options.options import SysOptions
@ -87,7 +88,7 @@ class JudgeServerHeartbeatTest(APITestCase):
self.token = "test" self.token = "test"
self.hashed_token = hashlib.sha256(self.token.encode("utf-8")).hexdigest() self.hashed_token = hashlib.sha256(self.token.encode("utf-8")).hexdigest()
SysOptions.judge_server_token = self.token SysOptions.judge_server_token = self.token
self.headers = {"HTTP_X_JUDGE_SERVER_TOKEN": self.hashed_token, "HTTP_X_REAL_IP": "1.2.3.4"} self.headers = {"HTTP_X_JUDGE_SERVER_TOKEN": self.hashed_token, settings.IP_HEADER: "1.2.3.4"}
def test_new_heartbeat(self): def test_new_heartbeat(self):
resp = self.client.post(self.url, data=self.data, **self.headers) resp = self.client.post(self.url, data=self.data, **self.headers)

View File

@ -136,7 +136,7 @@ class JudgeServerHeartbeatAPI(CSRFExemptAPIView):
server.memory_usage = data["memory"] server.memory_usage = data["memory"]
server.cpu_usage = data["cpu"] server.cpu_usage = data["cpu"]
server.service_url = data["service_url"] server.service_url = data["service_url"]
server.ip = request.META["HTTP_X_REAL_IP"] server.ip = request.ip
server.last_heartbeat = timezone.now() server.last_heartbeat = timezone.now()
server.save() server.save()
except JudgeServer.DoesNotExist: except JudgeServer.DoesNotExist:

View File

@ -1,5 +1,6 @@
proxy_pass http://backend; proxy_pass http://backend;
proxy_set_header X-Real-IP __IP_HEADER__; proxy_set_header X-Real-IP __IP_HEADER__;
proxy_set_header Host $http_host;client_max_body_size 200M; proxy_set_header Host $http_host;
client_max_body_size 200M;
proxy_http_version 1.1; proxy_http_version 1.1;
proxy_set_header Connection ''; proxy_set_header Connection '';

View File

@ -207,4 +207,6 @@ CELERY_ACCEPT_CONTENT = ["json"]
CELERY_TASK_SERIALIZER = "json" CELERY_TASK_SERIALIZER = "json"
RAVEN_CONFIG = { RAVEN_CONFIG = {
'dsn': 'https://b200023b8aed4d708fb593c5e0a6ad3d:1fddaba168f84fcf97e0d549faaeaff0@sentry.io/263057' 'dsn': 'https://b200023b8aed4d708fb593c5e0a6ad3d:1fddaba168f84fcf97e0d549faaeaff0@sentry.io/263057'
} }
IP_HEADER = "HTTP_X_REAL_IP"