1414 /// <summary>
1515 /// Defines the <see cref="MediaManifestProxyController" />.
1616 /// </summary>
17- [ Authorize ]
1817 [ Route ( "api/[controller]" ) ]
1918 [ ApiController ]
2019 public class MediaManifestProxyController : ControllerBase
@@ -37,6 +36,7 @@ public MediaManifestProxyController(ILogger<MediaManifestProxyController> logger
3736 /// <param name="playBackUrl">The playBackUrl.</param>
3837 /// <param name="token">The token.</param>
3938 /// <returns>The MediaManifestProxy string.</returns>
39+ [ Authorize ]
4040 public string Get ( string playBackUrl , string token )
4141 {
4242 this . logger . LogDebug ( $ "playBackUrl={ playBackUrl } token={ token } ") ;
@@ -103,6 +103,80 @@ public string Get(string playBackUrl, string token)
103103 return null ;
104104 }
105105
106+ /// <summary>
107+ /// The LandingPageGet.
108+ /// </summary>
109+ /// <param name="playBackUrl">The playBackUrl.</param>
110+ /// <param name="token">The token.</param>
111+ /// <returns>The MediaManifestProxy string.</returns>
112+ [ HttpGet ]
113+ [ Route ( "LandingPageGet" ) ]
114+ public string LandingPageGet ( string playBackUrl , string token )
115+ {
116+ this . logger . LogDebug ( $ "playBackUrl={ playBackUrl } token={ token } ") ;
117+ var httpRequest = ( HttpWebRequest ) WebRequest . Create ( new Uri ( playBackUrl ) ) ;
118+ httpRequest . CachePolicy = new HttpRequestCachePolicy ( HttpRequestCacheLevel . NoCacheNoStore ) ;
119+ httpRequest . Timeout = 30000 ;
120+
121+ var httpResponse = httpRequest . GetResponse ( ) ;
122+
123+ try
124+ {
125+ this . logger . LogDebug ( $ "Calling httpResponse.GetResponseStream(): playBackUrl={ playBackUrl } ") ;
126+ var stream = httpResponse . GetResponseStream ( ) ;
127+ if ( stream != null )
128+ {
129+ using ( var reader = new StreamReader ( stream ) )
130+ {
131+ const string qualityLevelRegex = @"(|)([^""\s]+\.m3u8\(encryption=cbc\))" ;
132+ const string fragmentsRegex = @"(Fragments\([\w\d=-]+,[\w\d=-]+\))" ;
133+ const string urlRegex = @"(https?:\/\/[\da-z\.-]+\.[a-z\.]{2,6}[\/\w \.-]*\?[^,\s""]*)" ;
134+
135+ var baseUrl = playBackUrl . Substring ( 0 , playBackUrl . IndexOf ( ".ism" , System . StringComparison . OrdinalIgnoreCase ) ) + ".ism" ;
136+ this . logger . LogDebug ( $ "baseUrl={ baseUrl } ") ;
137+
138+ var content = reader . ReadToEnd ( ) ;
139+
140+ content = ReplaceUrisWithProxy ( content , baseUrl ) ;
141+ var newContent = Regex . Replace ( content , urlRegex , match =>
142+ {
143+ string baseUrlWithQuery = match . Groups [ 1 ] . Value ; // URL including the query string
144+
145+ // Append the token correctly without modifying surrounding characters
146+ string newUrl = baseUrlWithQuery . Contains ( "?" ) ?
147+ $ "{ baseUrlWithQuery } &token={ token } " :
148+ $ "{ baseUrlWithQuery } ?token={ token } ";
149+
150+ return newUrl ;
151+ } ) ;
152+
153+ this . logger . LogDebug ( $ "newContent={ newContent } ") ;
154+
155+ var match = Regex . Match ( playBackUrl , qualityLevelRegex ) ;
156+ if ( match . Success )
157+ {
158+ this . logger . LogDebug ( $ "match.Success") ;
159+ var qualityLevel = match . Groups [ 0 ] . Value ;
160+ newContent = Regex . Replace ( newContent , fragmentsRegex , m => string . Format ( CultureInfo . InvariantCulture , baseUrl + "/" + qualityLevel + "/" + m . Value ) ) ;
161+ this . logger . LogDebug ( $ "Updated newContent={ newContent } ") ;
162+ }
163+
164+ return newContent ;
165+ }
166+ }
167+ }
168+ catch ( Exception ex )
169+ {
170+ this . logger . LogError ( ex . Message ) ;
171+ }
172+ finally
173+ {
174+ httpResponse . Close ( ) ;
175+ }
176+
177+ return null ;
178+ }
179+
106180 private static string ReplaceUrisWithProxy ( string playlistContent , string proxyUrl )
107181 {
108182 // Split the playlist content into lines
0 commit comments