Files
Leonid PershinandClaude Opus 5 9bf2ea5532 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>
2026-08-13 17:22:30 +03:00

116 lines
3.4 KiB
C#

using AvParser.Core.Proxies;
using AvParser.Infrastructure.Proxies;
namespace AvParser.Infrastructure.Tests;
public class ProxiflyFeedTests
{
/// <summary>Captured verbatim from the live feed, so the schema is pinned by a real payload.</summary>
private const string Sample = """
[
{
"proxy": "socks5://208.102.51.6:58208",
"protocol": "socks5",
"ip": "208.102.51.6",
"port": 58208,
"https": false,
"anonymity": "transparent",
"score": 1,
"geolocation": { "country": "US", "city": "Unknown" }
},
{
"proxy": "http://45.61.98.1:3128",
"protocol": "http",
"ip": "45.61.98.1",
"port": 3128,
"https": true,
"anonymity": "elite",
"score": 4,
"geolocation": { "country": "DE", "city": "Berlin" }
}
]
""";
[Fact]
public void Parses_the_published_schema()
{
var endpoints = ProxiflyProxySource.ParseFeed(Sample);
endpoints.Count.ShouldBe(2);
var socks = endpoints[0];
socks.Protocol.ShouldBe(ProxyProtocol.Socks5);
socks.Host.ShouldBe("208.102.51.6");
socks.Port.ShouldBe(58208);
socks.Country.ShouldBe("US");
socks.Anonymity.ShouldBe(ProxyAnonymity.Transparent);
socks.Score.ShouldBe(1);
}
[Fact]
public void Keeps_a_real_city_and_drops_the_Unknown_placeholder()
{
var endpoints = ProxiflyProxySource.ParseFeed(Sample);
// The feed writes the literal string "Unknown" rather than omitting the field; showing
// that in the UI would be worse than showing nothing.
endpoints[0].City.ShouldBeNull();
endpoints[1].City.ShouldBe("Berlin");
}
[Fact]
public void An_empty_feed_is_not_an_error() => ProxiflyProxySource.ParseFeed("[]").ShouldBeEmpty();
[Fact]
public void Falls_back_to_the_proxy_field_when_the_parts_are_missing()
{
var endpoint = ProxiflyProxySource.ToEndpoint(
new ProxiflyRecord { Proxy = "socks4://9.9.9.9:1080", Protocol = "socks4" }
);
endpoint.ShouldNotBeNull();
endpoint.Protocol.ShouldBe(ProxyProtocol.Socks4);
endpoint.Port.ShouldBe(1080);
}
[Fact]
public void Skips_a_record_it_cannot_make_sense_of() =>
ProxiflyProxySource
.ToEndpoint(
new ProxiflyRecord
{
Protocol = "gopher",
Ip = "1.2.3.4",
Port = 80,
}
)
.ShouldBeNull();
[Fact]
public void Skips_a_record_with_an_impossible_port() =>
ProxiflyProxySource
.ToEndpoint(
new ProxiflyRecord
{
Protocol = "http",
Ip = "1.2.3.4",
Port = 0,
}
)
.ShouldBeNull();
[Fact]
public void A_malformed_row_does_not_take_the_whole_feed_down()
{
const string mixed = """
[
{ "protocol": "http", "ip": "1.2.3.4", "port": 8080 },
{ "protocol": "nonsense", "ip": "5.6.7.8", "port": 9 },
{ "protocol": "socks5", "ip": "9.9.9.9", "port": 1080 }
]
""";
ProxiflyProxySource.ParseFeed(mixed).Count.ShouldBe(2);
}
}