Legend017/mediaflow-proxy
0
1from typing import Annotated2 3from fastapi import Request, Depends, APIRouter, Query, HTTPException4 5from .handlers import handle_hls_stream_proxy, proxy_stream, get_manifest, get_playlist, get_segment, get_public_ip6from .schemas import MPDSegmentParams, MPDPlaylistParams, HLSManifestParams, ProxyStreamParams, MPDManifestParams7from .utils.http_utils import get_proxy_headers, ProxyRequestHeaders8 9proxy_router = APIRouter()10 11 12@proxy_router.head("/hls/manifest.m3u8")13@proxy_router.get("/hls/manifest.m3u8")14async def hls_stream_proxy(15 request: Request,16 hls_params: Annotated[HLSManifestParams, Query()],17 proxy_headers: Annotated[ProxyRequestHeaders, Depends(get_proxy_headers)],18):19 """20 Proxify HLS stream requests, fetching and processing the m3u8 playlist or streaming the content.21 22 Args:23 request (Request): The incoming HTTP request.24 hls_params (HLSPlaylistParams): The parameters for the HLS stream request.25 proxy_headers (ProxyRequestHeaders): The headers to include in the request.26 27 Returns:28 Response: The HTTP response with the processed m3u8 playlist or streamed content.29 """30 return await handle_hls_stream_proxy(request, hls_params, proxy_headers)31 32 33@proxy_router.head("/stream")34@proxy_router.get("/stream")35async def proxy_stream_endpoint(36 request: Request,37 stream_params: Annotated[ProxyStreamParams, Query()],38 proxy_headers: Annotated[ProxyRequestHeaders, Depends(get_proxy_headers)],39):40 """41 Proxies stream requests to the given video URL.42 43 Args:44 request (Request): The incoming HTTP request.45 stream_params (ProxyStreamParams): The parameters for the stream request.46 proxy_headers (ProxyRequestHeaders): The headers to include in the request.47 48 Returns:49 Response: The HTTP response with the streamed content.50 """51 content_range = proxy_headers.request.get("range", "bytes=0-")52 if "nan" in content_range.casefold():53 # Handle invalid range requests "bytes=NaN-NaN"54 raise HTTPException(status_code=416, detail="Invalid Range Header")55 proxy_headers.request.update({"range": content_range})56 return await proxy_stream(request.method, stream_params, proxy_headers)57 58 59@proxy_router.get("/mpd/manifest.m3u8")60async def manifest_endpoint(61 request: Request,62 manifest_params: Annotated[MPDManifestParams, Query()],63 proxy_headers: Annotated[ProxyRequestHeaders, Depends(get_proxy_headers)],64):65 """66 Retrieves and processes the MPD manifest, converting it to an HLS manifest.67 68 Args:69 request (Request): The incoming HTTP request.70 manifest_params (MPDManifestParams): The parameters for the manifest request.71 proxy_headers (ProxyRequestHeaders): The headers to include in the request.72 73 Returns:74 Response: The HTTP response with the HLS manifest.75 """76 return await get_manifest(request, manifest_params, proxy_headers)77 78 79@proxy_router.get("/mpd/playlist.m3u8")80async def playlist_endpoint(81 request: Request,82 playlist_params: Annotated[MPDPlaylistParams, Query()],83 proxy_headers: Annotated[ProxyRequestHeaders, Depends(get_proxy_headers)],84):85 """86 Retrieves and processes the MPD manifest, converting it to an HLS playlist for a specific profile.87 88 Args:89 request (Request): The incoming HTTP request.90 playlist_params (MPDPlaylistParams): The parameters for the playlist request.91 proxy_headers (ProxyRequestHeaders): The headers to include in the request.92 93 Returns:94 Response: The HTTP response with the HLS playlist.95 """96 return await get_playlist(request, playlist_params, proxy_headers)97 98 99@proxy_router.get("/mpd/segment.mp4")100async def segment_endpoint(101 segment_params: Annotated[MPDSegmentParams, Query()],102 proxy_headers: Annotated[ProxyRequestHeaders, Depends(get_proxy_headers)],103):104 """105 Retrieves and processes a media segment, decrypting it if necessary.106 107 Args:108 segment_params (MPDSegmentParams): The parameters for the segment request.109 proxy_headers (ProxyRequestHeaders): The headers to include in the request.110 111 Returns:112 Response: The HTTP response with the processed segment.113 """114 return await get_segment(segment_params, proxy_headers)115 116 117@proxy_router.get("/ip")118async def get_mediaflow_proxy_public_ip(119 use_request_proxy: bool = True,120):121 """122 Retrieves the public IP address of the MediaFlow proxy server.123 124 Returns:125 Response: The HTTP response with the public IP address in the form of a JSON object. {"ip": "xxx.xxx.xxx.xxx"}126 """127 return await get_public_ip(use_request_proxy)128 