Skip to content

Commit

Permalink
Use newest CloudProxy API endpoint request.cookies to make it work …
Browse files Browse the repository at this point in the history
…on every page (including file downloads)
  • Loading branch information
JBou committed Dec 8, 2020
1 parent f84d117 commit a112345
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 29 deletions.
10 changes: 5 additions & 5 deletions src/CloudProxySharp/ClearanceHandler.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Linq;
using System.Linq;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -16,7 +16,7 @@ namespace CloudProxySharp
/// </summary>
public class ClearanceHandler : DelegatingHandler
{
private readonly CloudProxy _cloudProxy;
private readonly CloudProxySolver _cloudProxySolver;

/// <summary>
/// The User-Agent which will be used accross this session (null means default CloudProxy User-Agent).
Expand All @@ -40,7 +40,7 @@ public ClearanceHandler(string cloudProxyApiUrl)
{
if (!string.IsNullOrWhiteSpace(cloudProxyApiUrl))
{
_cloudProxy = new CloudProxy(cloudProxyApiUrl)
_cloudProxySolver = new CloudProxySolver(cloudProxyApiUrl)
{
MaxTimeout = MaxTimeout
};
Expand All @@ -64,11 +64,11 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
// Detect if there is a challenge in the response
if (ChallengeDetector.IsClearanceRequired(response))
{
if (_cloudProxy == null)
if (_cloudProxySolver == null)
throw new CloudProxyException("Challenge detected but CloudProxy is not configured");

// Resolve the challenge using CloudProxy API
var cloudProxyResponse = await _cloudProxy.Solve(request);
var cloudProxyResponse = await _cloudProxySolver.Solve(request);

// Change the cookies in the original request with the cookies provided by CloudProxy
InjectCookies(request, cloudProxyResponse);
Expand Down
2 changes: 1 addition & 1 deletion src/CloudProxySharp/CloudProxySharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>netstandard1.3</TargetFramework>
<PackageId>CloudProxySharp</PackageId>
<RootNamespace>CloudProxySharp</RootNamespace>
<Version>1.0.0</Version>
<Version>1.0.1</Version>
<AssemblyVersion>1.0.0</AssemblyVersion>
<FileVersion>1.0.0</FileVersion>
<Authors>Gabriel Patzleiner (JBou)</Authors>
Expand Down
9 changes: 2 additions & 7 deletions src/CloudProxySharp/Solvers/CloudProxySolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@

namespace CloudProxySharp.Solvers
{
public class CloudProxy
public class CloudProxySolver
{
private static readonly SemaphoreLocker Locker = new SemaphoreLocker();
private HttpClient _httpClient;
private readonly Uri _cloudProxyUri;

public int MaxTimeout = 60000;

public CloudProxy(string cloudProxyApiUrl)
public CloudProxySolver(string cloudProxyApiUrl)
{
var apiUrl = cloudProxyApiUrl;
if (!apiUrl.EndsWith("/"))
Expand All @@ -29,19 +29,14 @@ public CloudProxy(string cloudProxyApiUrl)
public async Task<CloudProxyResponse> Solve(HttpRequestMessage request)
{
CloudProxyResponse result = null;
Uri originalUri = request.RequestUri;

await Locker.LockAsync(async () =>
{
HttpResponseMessage response;
try
{
_httpClient = new HttpClient();
var builder = new UriBuilder(originalUri) {Path = string.Empty};
request.RequestUri = builder.Uri;
response = await _httpClient.PostAsync(_cloudProxyUri, GenerateCloudProxyRequest(request));
request.RequestUri = originalUri;
}
catch (HttpRequestException e)
{
Expand Down
2 changes: 1 addition & 1 deletion src/CloudProxySharp/Types/CloudProxyRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace CloudProxySharp.Types
public class CloudProxyRequest
{
[JsonProperty("cmd")]
public string Command = "request.get";
public string Command = "request.cookies";

[JsonProperty("url")]
public string Url;
Expand Down
9 changes: 9 additions & 0 deletions src/CloudProxySharp/Types/CloudProxyRequestGet.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Newtonsoft.Json;

namespace CloudProxySharp.Types
{
public class CloudProxyRequestGet : CloudProxyRequest
{
[JsonProperty("cmd")] public new string Command = "request.get";
}
}
5 changes: 2 additions & 3 deletions src/CloudProxySharp/Types/CloudProxyResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@ public class Solution
public string Url;
public int Status;
public Dictionary<string, string> Headers;
public string Response;
public Cookie[] Cookies;
public string UserAgent;
}

public class Cookie
{
public string Name;
Expand Down Expand Up @@ -53,4 +51,5 @@ public System.Net.Cookie ToCookieObj()
return cookie;
}
}

}
16 changes: 16 additions & 0 deletions src/CloudProxySharp/Types/CloudProxyResponseGet.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// ReSharper disable UnassignedField.Global
// ReSharper disable ClassNeverInstantiated.Global

namespace CloudProxySharp.Types
{
public class CloudProxyResponseGet : CloudProxyResponse
{
public new SolutionGet Solution;
}

public class SolutionGet : Solution
{
public string Response;
public string UserAgent;
}
}
29 changes: 24 additions & 5 deletions test/CloudProxySharp.Tests/ClearanceHandlerTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Net.Sockets;
using System.Threading.Tasks;
using CloudProxySharp.Exceptions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
Expand All @@ -11,8 +13,9 @@ namespace CloudProxySharp.Tests
[TestClass]
public class ClearanceHandlerTests
{
private readonly Uri _protectedUri = new Uri("https://dailyiptvlist.com/");
private readonly Uri _protectedDownloadUri = new Uri("https://dailyiptvlist.com/dl/de-m3uplaylist-2020-10-23-1.m3u");
private readonly Uri _protectedUri = new Uri("https://iptvm3ulist.com/");
private readonly Uri _protectedDownloadUri = new Uri("https://iptvm3ulist.com/m3u/de01_iptvm3ulist_com_211120.m3u");
private readonly Uri _protectedDownloadUri2 = new Uri("https://www.spigotmc.org/resources/hubkick.2/download?version=203285");

[TestMethod]
public async Task SolveOk()
Expand Down Expand Up @@ -54,7 +57,21 @@ public async Task SolveOkCloudflareDownload()

var client = new HttpClient(handler);
var response = await client.GetAsync(_protectedDownloadUri);
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
Assert.AreEqual(MediaTypeHeaderValue.Parse("audio/x-mpegurl"), response.Content.Headers.ContentType);
}

[TestMethod]
public async Task SolveOkCloudflareDownload2()
{
var handler = new ClearanceHandler(Settings.CloudProxyApiUrl)
{
UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36",
MaxTimeout = 60000,
};

var client = new HttpClient(handler);
var response = await client.GetAsync(_protectedDownloadUri2);
Assert.AreEqual(MediaTypeHeaderValue.Parse("application/octet-stream"), response.Content.Headers.ContentType);
}

[TestMethod]
Expand All @@ -75,7 +92,9 @@ public async Task SolveError()
}
catch (HttpRequestException e)
{
Assert.IsTrue(e.Message.Contains("Name or service not know"));
Assert.IsNotNull(e.InnerException);
Assert.IsInstanceOfType(e.InnerException, typeof(SocketException));
Assert.AreEqual(SocketError.HostNotFound, ((SocketException)e.InnerException).SocketErrorCode);
}
catch (Exception e)
{
Expand Down
14 changes: 7 additions & 7 deletions test/CloudProxySharp.Tests/CloudProxyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class CloudProxyTests
public async Task SolveOk()
{
var uri = new Uri("https://www.google.com/");
var cloudProxy = new CloudProxy(Settings.CloudProxyApiUrl);
var cloudProxy = new CloudProxySolver(Settings.CloudProxyApiUrl);
var request = new HttpRequestMessage(HttpMethod.Get, uri);

var cloudProxyResponse = await cloudProxy.Solve(request);
Expand All @@ -29,10 +29,10 @@ public async Task SolveOk()

Assert.AreEqual("https://www.google.com/", cloudProxyResponse.Solution.Url);
Assert.AreEqual(cloudProxyResponse.Solution.Status, HttpStatusCode.OK);
Assert.IsTrue(cloudProxyResponse.Solution.Response.Contains("<title>Google</title>"));
//Assert.IsTrue(cloudProxyResponse.Solution.Response.Contains("<title>Google</title>"));
Assert.IsTrue(cloudProxyResponse.Solution.Cookies.Any());
Assert.IsTrue(cloudProxyResponse.Solution.Headers.Any());
Assert.IsTrue(cloudProxyResponse.Solution.UserAgent.Contains(" Firefox/"));
//Assert.IsTrue(cloudProxyResponse.Solution.UserAgent.Contains(" Firefox/"));

var firstCookie = cloudProxyResponse.Solution.Cookies.First();
Assert.IsTrue(!string.IsNullOrWhiteSpace(firstCookie.Name));
Expand All @@ -44,20 +44,20 @@ public async Task SolveOkUserAgent()
{
const string userAgent = "Mozilla/5.0 (X11; Linux i686; rv:77.0) Gecko/20100101 Firefox/77.0";
var uri = new Uri("https://www.google.com/");
var cloudProxy = new CloudProxy(Settings.CloudProxyApiUrl);
var cloudProxy = new CloudProxySolver(Settings.CloudProxyApiUrl);
var request = new HttpRequestMessage(HttpMethod.Get, uri);
request.Headers.Add(HttpHeaders.UserAgent, userAgent);

var cloudProxyResponse = await cloudProxy.Solve(request);
Assert.AreEqual("ok", cloudProxyResponse.Status);
Assert.AreEqual(userAgent, cloudProxyResponse.Solution.UserAgent);
//Assert.AreEqual(userAgent, cloudProxyResponse.Solution.UserAgent);
}

[TestMethod]
public async Task SolveError()
{
var uri = new Uri("https://www.google.bad1/");
var cloudProxy = new CloudProxy(Settings.CloudProxyApiUrl);
var cloudProxy = new CloudProxySolver(Settings.CloudProxyApiUrl);
var request = new HttpRequestMessage(HttpMethod.Get, uri);

try
Expand All @@ -79,7 +79,7 @@ public async Task SolveError()
public async Task SolveErrorConfig()
{
var uri = new Uri("https://www.google.com/");
var cloudProxy = new CloudProxy("http://localhost:44445");
var cloudProxy = new CloudProxySolver("http://localhost:44445");
var request = new HttpRequestMessage(HttpMethod.Get, uri);

try
Expand Down

0 comments on commit a112345

Please sign in to comment.