More refactoring.

This commit is contained in:
SergeantPanda 2025-03-19 16:45:47 -05:00
parent 8f1c233d64
commit 59245b1401
2 changed files with 14 additions and 9 deletions

View file

@ -34,4 +34,16 @@ def detect_stream_type(url):
return 'hls'
# Default to TS
return 'ts'
return 'ts'
def get_client_ip(request):
"""
Extract client IP address from request.
Handles cases where request is behind a proxy by checking X-Forwarded-For.
"""
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

View file

@ -10,6 +10,7 @@ from apps.proxy.config import TSConfig as Config
from . import proxy_server
from .channel_status import ChannelStatus
from .stream_generator import create_stream_generator
from .utils import get_client_ip
import logging
from apps.channels.models import Channel, Stream
from apps.m3u.models import M3UAccount, M3UAccountProfile
@ -21,14 +22,6 @@ from rest_framework.permissions import IsAuthenticated
logger = logging.getLogger("ts_proxy")
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
@api_view(['GET'])
def stream_ts(request, channel_id):
"""Stream TS data to client with immediate response and keep-alive packets during initialization"""