Add a proxy pool with rotation, liveness checks and a management page
The parser will need to move between proxies, so this adds the module it will sit on: pluggable sources, a pool that hands proxies out and learns from the outcome, three rotation strategies, and a page to drive it. Sources are IProxySource implementations. The public proxifly/free-proxy-list feed is fetched as the combined all/data.json through jsDelivr and filtered locally — one conditional request beats four per-protocol ones that can disagree mid-publish — and cached for the five minutes upstream takes to regenerate. A feed that is down keeps serving its last payload rather than emptying the pool. The user's own list lives in proxies.custom.json beside the settings, takes a pasted blob, and names the lines it could not parse instead of quietly dropping them. Both knobs the pool exposes are settings, as asked: rotation is Sticky (default, the only one that keeps site sessions coherent), RoundRobin or WeightedRandom; liveness is either a parallel sweep of the whole pool or a probe at hand-out time. Free lists are a few percent alive, so skipping verification entirely means mostly waiting on timeouts. Two invariants worth keeping, both of which cost a bug to find: Availability is decided by the quarantine, never by Health. Excluding everything that has ever failed made the quarantine window dead code and discarded proxies permanently on their first hiccup, which is exactly wrong for addresses that flap constantly. Health only orders the candidates now. A probe verdict does not touch the success/failure counters. Those are about real requests, and letting a sweep over a few thousand proxies rewrite them would drown the evidence weighted selection reads. SOCKS needs no extra package — .NET resolves socks4/socks4a/socks5 in WebProxy — but a proxifly record with "protocol": "https" is still an HTTP proxy reached over http:// with CONNECT, not an https:// scheme. 115 new tests. Also fixes a pre-existing flake: a command gated on another command's IsExecuting cannot be driven straight after its Execute() completes, because IsExecuting is published on the output scheduler. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5
parent
aeafe0af36
commit
9bf2ea5532
@@ -0,0 +1,106 @@
|
||||
using AvParser.Core.Proxies;
|
||||
|
||||
namespace AvParser.Core.Tests.Proxies;
|
||||
|
||||
public class ProxyEndpointTests
|
||||
{
|
||||
[Theory]
|
||||
[InlineData("1.2.3.4:8080", ProxyProtocol.Http, "1.2.3.4", 8080)]
|
||||
[InlineData("http://1.2.3.4:8080", ProxyProtocol.Http, "1.2.3.4", 8080)]
|
||||
[InlineData("https://proxy.example.com:3128", ProxyProtocol.Https, "proxy.example.com", 3128)]
|
||||
[InlineData("socks4://1.2.3.4:1080", ProxyProtocol.Socks4, "1.2.3.4", 1080)]
|
||||
[InlineData("socks5://1.2.3.4:1080", ProxyProtocol.Socks5, "1.2.3.4", 1080)]
|
||||
[InlineData(" socks5h://1.2.3.4:1080 ", ProxyProtocol.Socks5, "1.2.3.4", 1080)]
|
||||
public void Parses_the_forms_public_lists_actually_use(string text, ProxyProtocol protocol, string host, int port)
|
||||
{
|
||||
ProxyEndpoint.TryParse(text, out var endpoint).ShouldBeTrue();
|
||||
|
||||
endpoint!.Protocol.ShouldBe(protocol);
|
||||
endpoint.Host.ShouldBe(host);
|
||||
endpoint.Port.ShouldBe(port);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Parses_credentials()
|
||||
{
|
||||
ProxyEndpoint.TryParse("http://alice:s3cret@1.2.3.4:8080", out var endpoint).ShouldBeTrue();
|
||||
|
||||
endpoint!.Username.ShouldBe("alice");
|
||||
endpoint.Password.ShouldBe("s3cret");
|
||||
endpoint.HasCredentials.ShouldBeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Takes_the_last_at_sign_so_a_password_may_contain_one()
|
||||
{
|
||||
ProxyEndpoint.TryParse("alice:p@ss@1.2.3.4:8080", out var endpoint).ShouldBeTrue();
|
||||
|
||||
endpoint!.Username.ShouldBe("alice");
|
||||
endpoint.Password.ShouldBe("p@ss");
|
||||
endpoint.Host.ShouldBe("1.2.3.4");
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(null)]
|
||||
[InlineData("")]
|
||||
[InlineData(" ")]
|
||||
[InlineData("1.2.3.4")]
|
||||
[InlineData("1.2.3.4:")]
|
||||
[InlineData("1.2.3.4:0")]
|
||||
[InlineData("1.2.3.4:70000")]
|
||||
[InlineData("1.2.3.4:notaport")]
|
||||
[InlineData(":8080")]
|
||||
[InlineData("gopher://1.2.3.4:8080")]
|
||||
[InlineData("@1.2.3.4:8080")]
|
||||
public void Rejects_malformed_input(string? text) => ProxyEndpoint.TryParse(text, out _).ShouldBeFalse();
|
||||
|
||||
[Fact]
|
||||
public void Key_is_case_insensitive_and_identifies_the_address()
|
||||
{
|
||||
ProxyEndpoint.TryParse("SOCKS5://Proxy.Example.COM:1080", out var upper).ShouldBeTrue();
|
||||
ProxyEndpoint.TryParse("socks5://proxy.example.com:1080", out var lower).ShouldBeTrue();
|
||||
|
||||
upper!.Key.ShouldBe(lower!.Key);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Key_separates_the_same_host_on_different_protocols()
|
||||
{
|
||||
ProxyEndpoint.TryParse("http://1.2.3.4:1080", out var http).ShouldBeTrue();
|
||||
ProxyEndpoint.TryParse("socks5://1.2.3.4:1080", out var socks).ShouldBeTrue();
|
||||
|
||||
http!.Key.ShouldNotBe(socks!.Key);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(ProxyProtocol.Http, "http")]
|
||||
[InlineData(ProxyProtocol.Https, "http")]
|
||||
[InlineData(ProxyProtocol.Socks4, "socks4")]
|
||||
[InlineData(ProxyProtocol.Socks5, "socks5")]
|
||||
public void Https_proxies_are_still_reached_over_the_http_scheme(ProxyProtocol protocol, string scheme)
|
||||
{
|
||||
// .NET has no "https" proxy scheme — an HTTPS-capable proxy tunnels TLS with CONNECT
|
||||
// over a plain http:// proxy URI. Getting this wrong makes every such proxy unusable.
|
||||
new ProxyEndpoint(protocol, "1.2.3.4", 8080).Scheme.ShouldBe(scheme);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Round_trips_through_its_own_string_form()
|
||||
{
|
||||
var original = new ProxyEndpoint(ProxyProtocol.Socks5, "1.2.3.4", 1080);
|
||||
|
||||
ProxyEndpoint.TryParse(original.ToString(), out var parsed).ShouldBeTrue();
|
||||
|
||||
parsed!.Key.ShouldBe(original.Key);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("transparent", ProxyAnonymity.Transparent)]
|
||||
[InlineData("anonymous", ProxyAnonymity.Anonymous)]
|
||||
[InlineData("elite", ProxyAnonymity.Elite)]
|
||||
[InlineData("high", ProxyAnonymity.Elite)]
|
||||
[InlineData("nonsense", ProxyAnonymity.Unknown)]
|
||||
[InlineData(null, ProxyAnonymity.Unknown)]
|
||||
public void Parses_anonymity(string? text, ProxyAnonymity expected) =>
|
||||
ProxyEndpoint.ParseAnonymity(text).ShouldBe(expected);
|
||||
}
|
||||
Reference in New Issue
Block a user