File size: 14,307 Bytes
1ebbb74
 
480076c
1ebbb74
 
 
 
 
 
b78171a
1ebbb74
8e6ac63
1ebbb74
33e2f36
 
 
 
 
 
9b14766
33e2f36
1ebbb74
 
 
 
 
 
8e6ac63
1ebbb74
8e6ac63
1ebbb74
 
8e6ac63
 
1ebbb74
 
8e6ac63
1ebbb74
1470245
 
 
 
9b14766
0e2d63a
1470245
8e6ac63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25fe1e3
8e6ac63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1ebbb74
480076c
8e6ac63
 
 
1470245
8e6ac63
1470245
8e6ac63
 
 
1470245
b053b86
 
 
 
 
8e6ac63
33e2f36
 
8e6ac63
eeef32f
b78171a
1470245
 
1ebbb74
1470245
8e6ac63
1ebbb74
 
9b14766
 
 
 
 
 
8e6ac63
1ebbb74
8e6ac63
 
 
1ebbb74
 
8e6ac63
1ebbb74
8e6ac63
 
 
1ebbb74
 
8e6ac63
1ebbb74
8e6ac63
 
1ebbb74
9b14766
8e6ac63
b78171a
1ebbb74
 
eeef32f
1ebbb74
33e2f36
9b14766
b78171a
eeef32f
1ebbb74
 
 
 
8e6ac63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1ebbb74
 
1470245
480076c
1470245
1ebbb74
 
 
 
1470245
1ebbb74
9b14766
1ebbb74
480076c
1ebbb74
 
 
 
1470245
9b14766
1470245
 
9b14766
 
1470245
 
 
9b14766
1470245
 
8e6ac63
1470245
 
1ebbb74
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0e2d63a
9b14766
8e6ac63
9b14766
0e2d63a
1ebbb74
 
 
 
 
8e6ac63
9b14766
1ebbb74
 
 
 
 
0e2d63a
8e6ac63
9b14766
8e6ac63
 
 
0e2d63a
1ebbb74
 
 
 
 
 
9b14766
1ebbb74
8e6ac63
1ebbb74
 
 
 
 
 
 
9b14766
1ebbb74
 
 
0e2d63a
8e6ac63
9b14766
1ebbb74
 
 
 
 
 
8e6ac63
9b14766
1ebbb74
 
 
 
 
8e6ac63
9b14766
8e6ac63
 
 
 
1ebbb74
8e6ac63
1ebbb74
 
 
8e6ac63
9b14766
1ebbb74
 
 
 
 
8e6ac63
9b14766
1ebbb74
 
 
 
 
8e6ac63
 
 
9b14766
8e6ac63
 
 
 
9b14766
8e6ac63
 
 
 
 
 
 
 
 
 
 
1ebbb74
 
9b14766
1ebbb74
 
 
9b14766
 
 
1ebbb74
 
 
9b14766
 
 
1ebbb74
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
import base64
import logging
from urllib.parse import urlparse

import httpx
from fastapi import Request, Response, HTTPException
from starlette.background import BackgroundTask

from .configs import settings
from .const import SUPPORTED_RESPONSE_HEADERS
from .mpd_processor import process_manifest, process_playlist, process_segment
from .schemas import HLSManifestParams, ProxyStreamParams, MPDManifestParams, MPDPlaylistParams, MPDSegmentParams
from .utils.cache_utils import get_cached_mpd, get_cached_init_segment
from .utils.http_utils import (
    Streamer,
    DownloadError,
    download_file_with_retry,
    request_with_retry,
    EnhancedStreamingResponse,
    ProxyRequestHeaders,
)
from .utils.m3u8_processor import M3U8Processor
from .utils.mpd_utils import pad_base64

logger = logging.getLogger(__name__)


async def setup_client_and_streamer(use_request_proxy: bool, verify_ssl: bool) -> tuple[httpx.AsyncClient, Streamer]:
    """
    Set up an HTTP client and a streamer.

    Args:
        use_request_proxy (bool): Whether to use a proxy for the request.
        verify_ssl (bool): Whether to verify SSL certificates.

    Returns:
        tuple: An httpx.AsyncClient instance and a Streamer instance.
    """
    client = httpx.AsyncClient(
        follow_redirects=True,
        timeout=httpx.Timeout(30.0),
        limits=httpx.Limits(max_keepalive_connections=10, max_connections=20),
        proxy=settings.proxy_url if use_request_proxy else None,
        verify=verify_ssl,
    )
    return client, Streamer(client)


def handle_exceptions(exception: Exception) -> Response:
    """
    Handle exceptions and return appropriate HTTP responses.

    Args:
        exception (Exception): The exception that was raised.

    Returns:
        Response: An HTTP response corresponding to the exception type.
    """
    if isinstance(exception, httpx.HTTPStatusError):
        logger.error(f"Upstream service error while handling request: {exception}")
        return Response(status_code=exception.response.status_code, content=f"Upstream service error: {exception}")
    elif isinstance(exception, DownloadError):
        logger.error(f"Error downloading content: {exception}")
        return Response(status_code=exception.status_code, content=str(exception))
    else:
        logger.exception(f"Internal server error while handling request: {exception}")
        return Response(status_code=502, content=f"Internal server error: {exception}")


async def handle_hls_stream_proxy(
    request: Request, hls_params: HLSManifestParams, proxy_headers: ProxyRequestHeaders
) -> Response:
    """
    Handle HLS stream proxy requests.

    This function processes HLS manifest files and streams content based on the request parameters.

    Args:
        request (Request): The incoming FastAPI request object.
        hls_params (HLSManifestParams): Parameters for the HLS manifest.
        proxy_headers (ProxyRequestHeaders): Headers to be used in the proxy request.

    Returns:
        Union[Response, EnhancedStreamingResponse]: Either a processed m3u8 playlist or a streaming response.
    """
    client, streamer = await setup_client_and_streamer(hls_params.use_request_proxy, hls_params.verify_ssl)

    try:
        if urlparse(hls_params.destination).path.endswith((".m3u", ".m3u8")):
            return await fetch_and_process_m3u8(
                streamer, hls_params.destination, proxy_headers, request, hls_params.key_url
            )

        response = await streamer.head(hls_params.destination, proxy_headers.request)
        if "mpegurl" in response.headers.get("content-type", "").lower():
            return await fetch_and_process_m3u8(
                streamer, hls_params.destination, proxy_headers, request, hls_params.key_url
            )

        content_range = proxy_headers.request.get("range", "bytes=0-")
        if "NaN" in content_range:
            # Handle invalid range requests "bytes=NaN-NaN"
            raise HTTPException(status_code=416, detail="Invalid Range Header")
        proxy_headers.request.update({"range": content_range})
        response_headers = prepare_response_headers(response.headers, proxy_headers.response)

        return EnhancedStreamingResponse(
            streamer.stream_content(hls_params.destination, proxy_headers.request),
            status_code=response.status_code,
            headers=response_headers,
            background=BackgroundTask(streamer.close),
        )
    except Exception as e:
        await client.aclose()
        return handle_exceptions(e)


async def handle_stream_request(
    method: str,
    video_url: str,
    proxy_headers: ProxyRequestHeaders,
    verify_ssl: bool = True,
    use_request_proxy: bool = True,
) -> Response:
    """
    Handle general stream requests.

    This function processes both HEAD and GET requests for video streams.

    Args:
        method (str): The HTTP method (e.g., 'GET' or 'HEAD').
        video_url (str): The URL of the video to stream.
        proxy_headers (ProxyRequestHeaders): Headers to be used in the proxy request.
        verify_ssl (bool, optional): Whether to verify SSL certificates. Defaults to True.
        use_request_proxy (bool, optional): Whether to use a proxy for the request. Defaults to True.

    Returns:
        Union[Response, EnhancedStreamingResponse]: Either a HEAD response or a streaming response.
    """
    client, streamer = await setup_client_and_streamer(use_request_proxy, verify_ssl)

    try:
        response = await streamer.head(video_url, proxy_headers.request)
        response_headers = prepare_response_headers(response.headers, proxy_headers.response)

        if method == "HEAD":
            await streamer.close()
            return Response(headers=response_headers, status_code=response.status_code)
        else:
            return EnhancedStreamingResponse(
                streamer.stream_content(video_url, proxy_headers.request),
                headers=response_headers,
                status_code=response.status_code,
                background=BackgroundTask(streamer.close),
            )
    except Exception as e:
        await client.aclose()
        return handle_exceptions(e)


def prepare_response_headers(original_headers, proxy_response_headers) -> dict:
    """
    Prepare response headers for the proxy response.

    This function filters the original headers, ensures proper transfer encoding,
    and merges them with the proxy response headers.

    Args:
        original_headers (httpx.Headers): The original headers from the upstream response.
        proxy_response_headers (dict): Additional headers to be included in the proxy response.

    Returns:
        dict: The prepared headers for the proxy response.
    """
    response_headers = {k: v for k, v in original_headers.multi_items() if k in SUPPORTED_RESPONSE_HEADERS}
    response_headers.update(proxy_response_headers)
    return response_headers


async def proxy_stream(method: str, stream_params: ProxyStreamParams, proxy_headers: ProxyRequestHeaders):
    """
    Proxies the stream request to the given video URL.

    Args:
        method (str): The HTTP method (e.g., GET, HEAD).
        stream_params (ProxyStreamParams): The parameters for the stream request.
        proxy_headers (ProxyRequestHeaders): The headers to include in the request.

    Returns:
        Response: The HTTP response with the streamed content.
    """
    return await handle_stream_request(
        method, stream_params.destination, proxy_headers, stream_params.verify_ssl, stream_params.use_request_proxy
    )


async def fetch_and_process_m3u8(
    streamer: Streamer, url: str, proxy_headers: ProxyRequestHeaders, request: Request, key_url: str = None
):
    """
    Fetches and processes the m3u8 playlist, converting it to an HLS playlist.

    Args:
        streamer (Streamer): The HTTP client to use for streaming.
        url (str): The URL of the m3u8 playlist.
        proxy_headers (ProxyRequestHeaders): The headers to include in the request.
        request (Request): The incoming HTTP request.
        key_url (str, optional): The HLS Key URL to replace the original key URL. Defaults to None.

    Returns:
        Response: The HTTP response with the processed m3u8 playlist.
    """
    try:
        content = await streamer.get_text(url, proxy_headers.request)
        processor = M3U8Processor(request, key_url)
        processed_content = await processor.process_m3u8(content, str(streamer.response.url))
        response_headers = {"Content-Disposition": "inline", "Accept-Ranges": "none"}
        response_headers.update(proxy_headers.response)
        return Response(
            content=processed_content,
            media_type="application/vnd.apple.mpegurl",
            headers=response_headers,
        )
    except Exception as e:
        return handle_exceptions(e)
    finally:
        await streamer.close()


async def handle_drm_key_data(key_id, key, drm_info):
    """
    Handles the DRM key data, retrieving the key ID and key from the DRM info if not provided.

    Args:
        key_id (str): The DRM key ID.
        key (str): The DRM key.
        drm_info (dict): The DRM information from the MPD manifest.

    Returns:
        tuple: The key ID and key.
    """
    if drm_info and not drm_info.get("isDrmProtected"):
        return None, None

    if not key_id or not key:
        if "keyId" in drm_info and "key" in drm_info:
            key_id = drm_info["keyId"]
            key = drm_info["key"]
        elif "laUrl" in drm_info and "keyId" in drm_info:
            raise HTTPException(status_code=400, detail="LA URL is not supported yet")
        else:
            raise HTTPException(
                status_code=400, detail="Unable to determine key_id and key, and they were not provided"
            )

    return key_id, key


async def get_manifest(
    request: Request,
    manifest_params: MPDManifestParams,
    proxy_headers: ProxyRequestHeaders,
):
    """
    Retrieves and processes the MPD manifest, converting it to an HLS manifest.

    Args:
        request (Request): The incoming HTTP request.
        manifest_params (MPDManifestParams): The parameters for the manifest request.
        proxy_headers (ProxyRequestHeaders): The headers to include in the request.

    Returns:
        Response: The HTTP response with the HLS manifest.
    """
    try:
        mpd_dict = await get_cached_mpd(
            manifest_params.destination,
            headers=proxy_headers.request,
            parse_drm=not manifest_params.key_id and not manifest_params.key,
            verify_ssl=manifest_params.verify_ssl,
            use_request_proxy=manifest_params.use_request_proxy,
        )
    except DownloadError as e:
        raise HTTPException(status_code=e.status_code, detail=f"Failed to download MPD: {e.message}")
    drm_info = mpd_dict.get("drmInfo", {})

    if drm_info and not drm_info.get("isDrmProtected"):
        # For non-DRM protected MPD, we still create an HLS manifest
        return await process_manifest(request, mpd_dict, proxy_headers, None, None)

    key_id, key = await handle_drm_key_data(manifest_params.key_id, manifest_params.key, drm_info)

    # check if the provided key_id and key are valid
    if key_id and len(key_id) != 32:
        key_id = base64.urlsafe_b64decode(pad_base64(key_id)).hex()
    if key and len(key) != 32:
        key = base64.urlsafe_b64decode(pad_base64(key)).hex()

    return await process_manifest(request, mpd_dict, proxy_headers, key_id, key)


async def get_playlist(
    request: Request,
    playlist_params: MPDPlaylistParams,
    proxy_headers: ProxyRequestHeaders,
):
    """
    Retrieves and processes the MPD manifest, converting it to an HLS playlist for a specific profile.

    Args:
        request (Request): The incoming HTTP request.
        playlist_params (MPDPlaylistParams): The parameters for the playlist request.
        proxy_headers (ProxyRequestHeaders): The headers to include in the request.

    Returns:
        Response: The HTTP response with the HLS playlist.
    """
    mpd_dict = await get_cached_mpd(
        playlist_params.destination,
        headers=proxy_headers.request,
        parse_drm=not playlist_params.key_id and not playlist_params.key,
        parse_segment_profile_id=playlist_params.profile_id,
        verify_ssl=playlist_params.verify_ssl,
        use_request_proxy=playlist_params.use_request_proxy,
    )
    return await process_playlist(request, mpd_dict, playlist_params.profile_id, proxy_headers)


async def get_segment(
    segment_params: MPDSegmentParams,
    proxy_headers: ProxyRequestHeaders,
):
    """
    Retrieves and processes a media segment, decrypting it if necessary.

    Args:
        segment_params (MPDSegmentParams): The parameters for the segment request.
        proxy_headers (ProxyRequestHeaders): The headers to include in the request.

    Returns:
        Response: The HTTP response with the processed segment.
    """
    try:
        init_content = await get_cached_init_segment(
            segment_params.init_url, proxy_headers.request, segment_params.verify_ssl, segment_params.use_request_proxy
        )
        segment_content = await download_file_with_retry(
            segment_params.segment_url,
            proxy_headers.request,
            verify_ssl=segment_params.verify_ssl,
            use_request_proxy=segment_params.use_request_proxy,
        )
    except Exception as e:
        return handle_exceptions(e)

    return await process_segment(
        init_content,
        segment_content,
        segment_params.mime_type,
        proxy_headers,
        segment_params.key_id,
        segment_params.key,
    )


async def get_public_ip(use_request_proxy: bool = True):
    """
    Retrieves the public IP address of the MediaFlow proxy.

    Args:
        use_request_proxy (bool, optional): Whether to use the proxy configuration from the user's MediaFlow config. Defaults to True.

    Returns:
        Response: The HTTP response with the public IP address.
    """
    ip_address_data = await request_with_retry(
        "GET", "https://api.ipify.org?format=json", {}, use_request_proxy=use_request_proxy
    )
    return ip_address_data.json()