Skip to content

Commit

Permalink
add SecurityProtocolType.Tls13 on .Net 8 and refactor samples
Browse files Browse the repository at this point in the history
  • Loading branch information
bezzad committed Oct 31, 2024
1 parent 5113e7d commit ff9758f
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 122 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,9 @@ var downloadOpt = new DownloadConfiguration()
{
Accept = "*/*",
CookieContainer = cookies,
Headers = new WebHeaderCollection(), // { your custom headers }
Headers = ["Accept-Encoding: gzip, deflate, br"], // { your custom headers }
KeepAlive = true, // default value is false
ProtocolVersion = HttpVersion.Version11, // default value is HTTP 1.1
UseDefaultCredentials = false,
// your custom user agent or your_app_name/app_version.
UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64)",
Proxy = new WebProxy() {
Expand Down
7 changes: 6 additions & 1 deletion src/Downloader/AbstractDownloadService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,12 @@ protected AbstractDownloadService(DownloadConfiguration options)
// This property selects the version of the Secure Sockets Layer (SSL) or
// existing connections aren't changed.
ServicePointManager.SecurityProtocol =
SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
SecurityProtocolType.Tls | SecurityProtocolType.Tls11 |
SecurityProtocolType.Tls12;

#if NET8_0_OR_GREATER
ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls13;
#endif

// Accept the request for POST, PUT and PATCH verbs
ServicePointManager.Expect100Continue = false;
Expand Down
30 changes: 30 additions & 0 deletions src/Samples/Downloader.Sample/Helper.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;

namespace Downloader.Sample;

Expand Down Expand Up @@ -59,4 +63,30 @@ public static string UpdateTitleInfo(this DownloadProgressChangedEventArgs e, bo
$"[{usedMemory} memory] " +
(isPaused ? " - Paused" : "");
}

public static async Task HttpClientDownload(string url, string filename, string proxyAddress)
{
// Define the proxy address
var proxyUri = new Uri(proxyAddress);

// Create a WebProxy instance
var proxy = new WebProxy(proxyUri) {
// If your proxy requires credentials, set them here
// Credentials = new NetworkCredential("username", "password")
};

// Create an HttpClientHandler and set the proxy
var handler = new HttpClientHandler {
Proxy = proxy,
UseProxy = true
};

var client = new HttpClient(handler);
var request = new HttpRequestMessage(HttpMethod.Get, url);
var response = await client.SendAsync(request);
response.EnsureSuccessStatusCode();
var stream = await response.Content.ReadAsStreamAsync();
var fileStream = File.OpenWrite(filename);
await stream.CopyToAsync(fileStream);
}
}
14 changes: 4 additions & 10 deletions src/Samples/Downloader.Sample/Program.Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,13 @@ private static DownloadConfiguration GetDownloadConfiguration()
// config and customize request headers
Accept = "*/*",
CookieContainer = cookies,
Headers = new WebHeaderCollection(), // { your custom headers }
KeepAlive = true, // default value is false
Headers = ["Accept-Encoding: gzip, deflate, br"], // { your custom headers }
KeepAlive = true, // default value is false
ProtocolVersion = HttpVersion.Version11, // default value is HTTP 1.1
UseDefaultCredentials = false,
// your custom user agent or your_app_name/app_version.
UserAgent = $"DownloaderSample/{Assembly.GetExecutingAssembly().GetName().Version?.ToString(3)}"
// Proxy = new WebProxy(new Uri($"socks5://127.0.0.1:9050"))
// Proxy = new WebProxy() {
// Address = new Uri("http://YourProxyServer/proxy.pac"),
// UseDefaultCredentials = false,
// Credentials = System.Net.CredentialCache.DefaultNetworkCredentials,
// BypassProxyOnLocal = true
// }
UserAgent = $"DownloaderSample/{Assembly.GetExecutingAssembly().GetName().Version?.ToString(3)}",
Proxy = new WebProxy("socks5://127.0.0.1:12000")
}
};
}
Expand Down
24 changes: 2 additions & 22 deletions src/Samples/Downloader.Sample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,30 +160,10 @@ private static async Task DownloadFile(DownloadItem downloadItem)
Logger = FileLogger.Factory(downloadItem.FolderPath, Path.GetFileName(downloadItem.FileName));

CurrentDownloadConfiguration = GetDownloadConfiguration();
if (downloadItem.Url.StartsWith("https://x.com/") ||
downloadItem.Url.StartsWith("https://www.youtube.com/") ||
downloadItem.Url.StartsWith("https://youtube.com/") ||
downloadItem.Url.StartsWith("https://youtu.be/"))
{
try
{
await Console.Out.WriteLineAsync("The url `" + downloadItem.Url + "` need to convert...");
VideoDownloaderHelper helper = new (CurrentDownloadConfiguration.RequestConfiguration.Proxy);
downloadItem.Url = await helper.GetCookedUrlAsync(downloadItem.Url);
await Console.Out.WriteLineAsync("Redirect: " + downloadItem.Url);
await SaveDownloadItems(DownloadList);
return;
}
catch (Exception e)
{
Console.WriteLine(e);
return;
}
}
if (downloadItem.Url.Contains(".m3u", StringComparison.OrdinalIgnoreCase))
{
VideoDownloaderHelper helper = new (CurrentDownloadConfiguration.RequestConfiguration.Proxy);
await helper.DownloadM3U8File(downloadItem.Url,
VideoDownloaderHelper helper = new(CurrentDownloadConfiguration.RequestConfiguration.Proxy);
await helper.DownloadM3U8File(downloadItem.Url,
downloadItem.FileName ?? Path.Combine(downloadItem.FolderPath, Path.GetRandomFileName(), ".mp4"));
return;
}
Expand Down
87 changes: 0 additions & 87 deletions src/Samples/Downloader.Sample/VideoDownloaderHelper.cs
Original file line number Diff line number Diff line change
@@ -1,45 +1,16 @@
using Newtonsoft.Json;
using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;

namespace Downloader.Sample;

/// <summary>
/// https://publer.io/
/// </summary>
public class VideoDownloaderHelper
{
private const string CreateJobUrl = "https://app.publer.io/hooks/media";
private const string JobStatusUrl = "https://app.publer.io/api/v1/job_status/{jobId}";
private static HttpClient Client;

class MediaResponse
{
[JsonProperty(PropertyName = "job_id")]
public string JobId { get; set; }
}

class JobStatusResponse
{
[JsonProperty(PropertyName = "payload")]
public List<Payload> Payloads { get; set; }

[JsonProperty(PropertyName = "status")]
public string Status { get; set; }
}

class Payload
{
[JsonProperty(PropertyName = "error")] public string Error { get; set; }
[JsonProperty(PropertyName = "path")] public string path { get; set; }
}

public VideoDownloaderHelper(IWebProxy proxy = null)
{
if (Client is not null)
Expand All @@ -56,67 +27,9 @@ public VideoDownloaderHelper(IWebProxy proxy = null)
Client = new HttpClient(httpClientHandler, disposeHandler: true);
}

Client.DefaultRequestHeaders.Add("Referer", "https://publer.io/");
Client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 Gecko/20100101 Firefox/131.0");
}

/// <summary>
/// Helper method to initiate the video processing job and wait for completion from https://publer.io/
/// </summary>
/// <param name="videoUrl"></param>
/// <returns></returns>
/// <exception cref="Exception">Link is invalid</exception>
/// <exception cref="WebException">The given link video was removed from server</exception>
public async Task<string> GetCookedUrlAsync(string videoUrl)
{
// 1. Initiate the video processing job
var jobRequestPayload = new { url = videoUrl };
StringContent requestBody = new(Newtonsoft.Json.JsonConvert.SerializeObject(jobRequestPayload),
Encoding.UTF8, "application/json");

HttpResponseMessage createJobResponse = await Client.PostAsync(CreateJobUrl, requestBody);
createJobResponse.EnsureSuccessStatusCode();

string resp = await createJobResponse.Content.ReadAsStringAsync();
MediaResponse media = JsonConvert.DeserializeObject<MediaResponse>(resp);

if (string.IsNullOrEmpty(media?.JobId))
{
throw new Exception("Failed to get job ID.");
}

// 2. Check the job status and wait for completion
string statusUrl = JobStatusUrl.Replace("{jobId}", media.JobId);

while (true)
{
HttpResponseMessage jobStatusResponse = await Client.GetAsync(statusUrl);
jobStatusResponse.EnsureSuccessStatusCode();

string jobStatusResponseBody = await jobStatusResponse.Content.ReadAsStringAsync();
JobStatusResponse jobStatus = JsonConvert.DeserializeObject<JobStatusResponse>(jobStatusResponseBody);

if (jobStatus?.Status?.Equals("complete", StringComparison.OrdinalIgnoreCase) == true)
{
// Extract the download URL from the payload
Payload data = jobStatus.Payloads?.FirstOrDefault();
if (!string.IsNullOrWhiteSpace(data?.Error))
{
throw new WebException(data.Error);
}

if (!string.IsNullOrEmpty(data?.path))
{
return data.path;
}

throw new Exception("Link is invalid: " + videoUrl);
}

// Wait for a few seconds before retrying
await Task.Delay(3000); // 3 seconds delay
}
}

public async Task DownloadM3U8File(string m3U8Url, string outputFilePath)
{
Expand Down

0 comments on commit ff9758f

Please sign in to comment.