Skip to content

Commit

Permalink
解决反代时IP不正确的情况
Browse files Browse the repository at this point in the history
  • Loading branch information
kingmo888 committed Sep 9, 2024
1 parent 7b8951d commit 175e116
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
13 changes: 11 additions & 2 deletions api/views_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@
from django.utils.translation import gettext as _


def get_client_ip(request):
x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
if x_forwarded_for:
ip = x_forwarded_for.split(',')[0]
else:
ip = request.META.get('REMOTE_ADDR')
return ip


def login(request):
result = {}
if request.method == 'GET':
Expand Down Expand Up @@ -226,7 +235,7 @@ def sysinfo(request):
if request.method == 'GET':
result['error'] = _('错误的提交方式!')
return JsonResponse(result)
client_ip = request.META.get('REMOTE_ADDR')
client_ip = get_client_ip(request)
postdata = json.loads(request.body)
device = RustDesDevice.objects.filter(Q(rid=postdata['id']) & Q(uuid=postdata['uuid'])).first()
if not device:
Expand Down Expand Up @@ -255,7 +264,7 @@ def heartbeat(request):
postdata = json.loads(request.body)
device = RustDesDevice.objects.filter(Q(rid=postdata['id']) & Q(uuid=postdata['uuid'])).first()
if device:
client_ip = request.META.get('REMOTE_ADDR')
client_ip = get_client_ip(request)
device.ip_address = client_ip
device.save()
# token保活
Expand Down
4 changes: 2 additions & 2 deletions api/views_front.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ def share(request):
sharelinks = ShareLink.objects.filter(Q(uid=request.user.id) & Q(is_used=False) & Q(is_expired=False))

# 省资源:处理已过期请求,不主动定时任务轮询请求,在任意地方请求时,检查是否过期,过期则保存。
now = datetime.datetime.now()
# now = datetime.datetime.now()
for sl in sharelinks:
check_sharelink_expired(sl)
sharelinks = ShareLink.objects.filter(Q(uid=request.user.id) & Q(is_used=False) & Q(is_expired=False))
Expand Down Expand Up @@ -327,7 +327,7 @@ def share(request):
# 自己的peers若重叠,需要跳过
peers_self_ids = [x.rid for x in RustDeskPeer.objects.filter(Q(uid=request.user.id))]
peers_share = RustDeskPeer.objects.filter(Q(rid__in=peers) & Q(uid=sharelink.uid))
peers_share_ids = [x.rid for x in peers_share]
# peers_share_ids = [x.rid for x in peers_share]

for peer in peers_share:
if peer.rid in peers_self_ids:
Expand Down

0 comments on commit 175e116

Please sign in to comment.