From e46a032ed5a6fdced44cd917415b1ce206a88150 Mon Sep 17 00:00:00 2001 From: lay295 Date: Sun, 7 Jan 2024 00:56:08 -0600 Subject: [PATCH] Reverse proxy for legacy windows users --- TwitchDownloaderCore/TwitchHelper.cs | 44 ++++++++++++++++++++++++---- 1 file changed, 39 insertions(+), 5 deletions(-) diff --git a/TwitchDownloaderCore/TwitchHelper.cs b/TwitchDownloaderCore/TwitchHelper.cs index 5b69db50..3b53f25b 100644 --- a/TwitchDownloaderCore/TwitchHelper.cs +++ b/TwitchDownloaderCore/TwitchHelper.cs @@ -57,17 +57,51 @@ public static async Task GetVideoToken(int videoId, strin public static async Task GetVideoPlaylist(int videoId, string token, string sig) { - var request = new HttpRequestMessage() + HttpRequestMessage request; + HttpResponseMessage response; + try { - RequestUri = new Uri($"https://usher.ttvnw.net/vod/{videoId}.m3u8?sig={sig}&token={token}&allow_source=true&allow_audio_only=true&platform=web&player_backend=mediaplayer&playlist_include_framerate=true&supported_codecs=av1,h264"), - Method = HttpMethod.Get - }; - var response = await httpClient.SendAsync(request); + request = new HttpRequestMessage() + { + RequestUri = new Uri($"https://usher.ttvnw.net/vod/{videoId}.m3u8?sig={sig}&token={token}&allow_source=true&allow_audio_only=true&platform=web&player_backend=mediaplayer&playlist_include_framerate=true&supported_codecs=av1,h264"), + Method = HttpMethod.Get + }; + response = await httpClient.SendAsync(request); + } + catch (Exception ex) + { + if (IsAuthException(ex)) + { + request = new HttpRequestMessage() + { + RequestUri = new Uri($"https://twitch-downloader-proxy.twitcharchives.workers.dev/{videoId}.m3u8?sig={sig}&token={token}&allow_source=true&allow_audio_only=true&platform=web&player_backend=mediaplayer&playlist_include_framerate=true&supported_codecs=av1,h264"), + Method = HttpMethod.Get + }; + response = await httpClient.SendAsync(request); + } + else + { + throw; + } + } response.EnsureSuccessStatusCode(); return await response.Content.ReadAsStringAsync(); } + static bool IsAuthException(Exception ex) + { + while (ex != null) + { + if (ex is System.Security.Authentication.AuthenticationException) + { + return true; + } + ex = ex.InnerException; + } + return false; + } + public static async Task GetClipInfo(object clipId) { var request = new HttpRequestMessage()