use crate::parse::find_addrs_global;
use crate::types::Proto;
use crate::utils::canonicalize_ip;
use regex::Regex;
use serde::Deserialize;
use std::collections::BTreeSet;
#[derive(Debug, Clone, Deserialize)]
pub struct ProviderSpec {
pub url: String,
#[serde(default)]
pub protocols: Vec<Proto>,
#[serde(default)]
pub pattern: Option<String>,
#[serde(default = "default_timeout")]
pub timeout: u64,
#[serde(default, rename = "type")]
pub kind: Option<String>,
}
fn default_timeout() -> u64 {
20
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct Candidate {
pub host: String,
pub port: u16,
pub protocols: BTreeSet<Proto>,
}
impl ProviderSpec {
pub fn new(url: &str, protocols: &[Proto]) -> Self {
ProviderSpec {
url: url.to_owned(),
protocols: protocols.to_vec(),
pattern: None,
timeout: default_timeout(),
kind: None,
}
}
pub fn extract(&self, body: &str) -> Vec<Candidate> {
let protocols: BTreeSet<Proto> = self.protocols.iter().copied().collect();
let pairs = match &self.pattern {
Some(pat) => extract_with_pattern(pat, body),
None => find_addrs_global(body),
};
let mut seen = BTreeSet::new();
let mut out = Vec::new();
for (raw_host, raw_port) in pairs {
let Ok(port) = raw_port.parse::<u16>() else {
continue;
};
if port == 0 {
continue;
}
let Some(host) = canonicalize_ip(&raw_host) else {
continue;
};
if host
.parse::<std::net::IpAddr>()
.is_ok_and(|ip| ip.is_unspecified())
{
continue;
}
let cand = Candidate {
host,
port,
protocols: protocols.clone(),
};
if seen.insert((cand.host.clone(), cand.port)) {
out.push(cand);
}
}
out
}
}
pub fn bundled_registry() -> Vec<ProviderSpec> {
const YAML: &str = include_str!("../data/providers.yaml");
serde_yaml_ng::from_str(YAML).expect("bundled providers.yaml is valid")
}
pub fn load_provider_dir(dir: &std::path::Path) -> Vec<ProviderSpec> {
if !dir.is_dir() {
tracing::warn!(dir = %dir.display(), "provider directory does not exist");
return Vec::new();
}
let mut paths: Vec<std::path::PathBuf> = match std::fs::read_dir(dir) {
Ok(entries) => entries
.filter_map(|e| e.ok().map(|e| e.path()))
.filter(|p| {
let ext_ok = matches!(
p.extension().and_then(|e| e.to_str()),
Some("yaml" | "yml" | "json")
);
let named = p
.file_name()
.and_then(|n| n.to_str())
.is_some_and(|n| !n.starts_with('_'));
ext_ok && named
})
.collect(),
Err(e) => {
tracing::warn!(dir = %dir.display(), error = %e, "cannot read provider directory");
return Vec::new();
}
};
paths.sort();
paths
.into_iter()
.filter_map(|p| match std::fs::read_to_string(&p) {
Ok(text) => match serde_yaml_ng::from_str::<ProviderSpec>(&text) {
Ok(spec) => match spec.kind.as_deref() {
None | Some("simple") => Some(spec),
Some(other) => {
tracing::warn!(
path = %p.display(),
kind = other,
"unsupported provider type (only 'simple' is supported) — skipping"
);
None
}
},
Err(e) => {
tracing::warn!(path = %p.display(), error = %e, "skipping invalid provider config");
None
}
},
Err(e) => {
tracing::warn!(path = %p.display(), error = %e, "cannot read provider config");
None
}
})
.collect()
}
pub fn config_template() -> &'static str {
"# proxybroker provider config — one provider per file.\n\
# Drop this in a directory and pass it with --provider-dir.\n\
# A filename starting with `_` is skipped (rename to disable).\n\
url: \"http://example.com/proxy-list.txt\"\n\
# Protocols proxies from this source may support (empty = check all):\n\
protocols: [HTTP, HTTPS]\n\
# Optional 2-group (host, port) regex; omit to use the default IP:port scanner:\n\
# pattern: \"(\\\\d+\\\\.\\\\d+\\\\.\\\\d+\\\\.\\\\d+):(\\\\d+)\"\n\
timeout: 20\n"
}
pub async fn fetch(spec: &ProviderSpec, client: &reqwest::Client) -> Vec<Candidate> {
match fetch_body(spec, client).await {
Ok(body) => spec.extract(&body),
Err(e) => {
tracing::debug!(url = %spec.url, error = %e, "provider fetch failed");
Vec::new()
}
}
}
async fn fetch_body(spec: &ProviderSpec, client: &reqwest::Client) -> reqwest::Result<String> {
client
.get(&spec.url)
.timeout(std::time::Duration::from_secs(spec.timeout))
.send()
.await?
.error_for_status()?
.text()
.await
}
fn extract_with_pattern(pattern: &str, body: &str) -> Vec<(String, String)> {
let Ok(re) = Regex::new(pattern) else {
return Vec::new();
};
let mut out = Vec::new();
for caps in re.captures_iter(body) {
match (caps.get(1), caps.get(2)) {
(Some(h), Some(p)) => out.push((h.as_str().to_owned(), p.as_str().to_owned())),
_ => {
let whole = caps.get(0).map(|m| m.as_str()).unwrap_or("");
if let Some((h, p)) = whole.rsplit_once(':') {
out.push((h.to_owned(), p.to_owned()));
}
}
}
}
out
}
#[cfg(test)]
mod tests {
use super::*;
fn spec(protocols: &[Proto]) -> ProviderSpec {
ProviderSpec::new("http://example/", protocols)
}
#[test]
fn extracts_from_plain_text_list() {
let body = "8.8.8.8:8080\n1.1.1.1:3128\n";
let got = spec(&[Proto::Http]).extract(body);
assert_eq!(got.len(), 2);
assert_eq!(got[0].host, "8.8.8.8");
assert_eq!(got[0].port, 8080);
assert!(got[0].protocols.contains(&Proto::Http));
}
#[test]
fn extracts_from_html_table() {
let body = "<tr><td>66.55.44.33</td><td>8888</td></tr>\
<tr><td>22.33.44.55</td><td>9999</td></tr>";
let got = spec(&[]).extract(body);
assert_eq!(got.len(), 2);
assert_eq!(got[0].host, "66.55.44.33");
assert_eq!(got[1].port, 9999);
}
#[test]
fn matches_python_pipeline_on_messy_input() {
let body = "999.1.1.1:80 5.5.5.5:0 010.1.1.1:80 7.7.7.7:1234";
let got = spec(&[]).extract(body);
assert_eq!(got.len(), 2, "{got:?}");
assert_eq!(got[0].host, "99.1.1.1");
assert_eq!(got[0].port, 80);
assert_eq!(got[1].host, "7.7.7.7");
assert_eq!(got[1].port, 1234);
}
#[test]
fn deduplicates_repeated_addresses() {
let body = "9.9.9.9:53\n9.9.9.9:53\n9.9.9.9:53";
assert_eq!(spec(&[]).extract(body).len(), 1);
}
#[test]
fn drops_the_unspecified_sentinel() {
let body = "0.0.0.0:80\n8.8.8.8:8080\n1.1.1.1:3128";
let got = spec(&[Proto::Http]).extract(body);
assert!(
got.iter().all(|c| c.host != "0.0.0.0"),
"unspecified address leaked into candidates: {got:?}"
);
assert_eq!(got.len(), 2, "both real rows must be kept: {got:?}");
assert!(got.iter().any(|c| c.host == "8.8.8.8" && c.port == 8080));
}
#[test]
fn custom_pattern_with_two_groups() {
let body = "IP=1.2.3.4 PORT=8080; IP=5.6.7.8 PORT=3128";
let mut s = spec(&[Proto::Socks5]);
s.pattern = Some(r"IP=(\d+\.\d+\.\d+\.\d+) PORT=(\d+)".to_owned());
let got = s.extract(body);
assert_eq!(got.len(), 2);
assert_eq!(got[0].host, "1.2.3.4");
assert_eq!(got[1].port, 3128);
assert!(got[1].protocols.contains(&Proto::Socks5));
}
#[test]
fn spec_deserializes_from_yaml() {
let yaml = "url: https://example.com/list\nprotocols: [HTTP, SOCKS5]\ntimeout: 15";
let s: ProviderSpec = serde_yaml_ng::from_str(yaml).unwrap();
assert_eq!(s.url, "https://example.com/list");
assert_eq!(s.protocols, vec![Proto::Http, Proto::Socks5]);
assert_eq!(s.timeout, 15);
assert!(s.pattern.is_none());
}
#[test]
fn python_config_extra_fields_are_ignored() {
let yaml = "name: My List\ntype: simple\nformat: text\nurl: http://x/list\n\
protocols: [HTTP]\nmax_connections: 4\ntimeout: 30";
let s: ProviderSpec = serde_yaml_ng::from_str(yaml).unwrap();
assert_eq!(s.url, "http://x/list");
assert_eq!(s.timeout, 30);
}
#[test]
fn config_template_is_a_valid_provider_spec() {
let s: ProviderSpec = serde_yaml_ng::from_str(config_template()).unwrap();
assert_eq!(s.protocols, vec![Proto::Http, Proto::Https]);
}
#[test]
fn load_provider_dir_reads_sorted_skips_underscore_and_bad() {
let dir = std::env::temp_dir().join(format!("pb_prov_{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
std::fs::write(dir.join("b.yaml"), "url: http://b/\nprotocols: [SOCKS5]").unwrap();
std::fs::write(
dir.join("a.json"),
r#"{"url":"http://a/","protocols":["HTTP"]}"#,
)
.unwrap();
std::fs::write(dir.join("_disabled.yaml"), "url: http://skip/").unwrap();
std::fs::write(dir.join("broken.yaml"), "url: [not a string").unwrap();
std::fs::write(dir.join("notconfig.txt"), "ignored").unwrap();
let specs = load_provider_dir(&dir);
assert_eq!(specs.len(), 2, "{specs:?}");
assert_eq!(specs[0].url, "http://a/");
assert_eq!(specs[0].protocols, vec![Proto::Http]);
assert_eq!(specs[1].url, "http://b/");
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn load_provider_dir_missing_dir_is_empty() {
let dir = std::env::temp_dir().join("pb_prov_definitely_missing_xyz");
assert!(load_provider_dir(&dir).is_empty());
}
#[test]
fn unsupported_provider_types_are_skipped_not_loaded_broken() {
let dir = std::env::temp_dir().join(format!("pb_prov_kind_{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
std::fs::write(
dir.join("ok.yaml"),
"type: simple\nurl: http://ok/\nprotocols: [HTTP]",
)
.unwrap();
std::fs::write(
dir.join("paginated.yaml"),
"type: paginated\nurl: http://p/?page={}",
)
.unwrap();
std::fs::write(
dir.join("api.yaml"),
"type: api\nurl: http://a/\napi_key: secret",
)
.unwrap();
std::fs::write(
dir.join("typeless.yaml"),
"url: http://t/\nprotocols: [HTTP]",
)
.unwrap();
let specs = load_provider_dir(&dir);
let urls: Vec<_> = specs.iter().map(|s| s.url.as_str()).collect();
assert_eq!(urls, vec!["http://ok/", "http://t/"], "{specs:?}");
let _ = std::fs::remove_dir_all(&dir);
}
}