proxy_fetch 0.1.2

A smart proxy fetcher for Rust that dynamically manages and scores a pool of proxies to ensure high-availability requests. / 一个智能的 Rust 代理抓取器,可动态管理代理池并对其评分,以确保高可用的网络请求。
Documentation
use anyhow::Result;
use log::{error, info};
use percent_encoding::percent_decode_str;
use regex::Regex;
use reqwest_middleware::{ClientBuilder, ClientWithMiddleware};
use reqwest_ss_proxy::{SsConnector, SsMiddleware};

#[static_init::constructor(0)]
extern "C" fn _log_init() {
  log_init::init();
}

#[tokio::test]
async fn test_proxy_connection() -> Result<()> {
  let urls = match std::fs::read_to_string("tests/ss") {
    Ok(s) => s,
    Err(_) => {
      error!(
        "未找到 tests/ss 文件,跳过代理连接测试
Could not find the tests/ss file, skipping proxy connection test"
      );
      return Ok(());
    }
  };

  for url in urls.lines() {
    let url = url.trim();
    if url.is_empty() {
      continue;
    }
    info!("{}", hide_token_ss_url(url));

    // 1. Create the SsConnector.
    let connector = match SsConnector::new(url) {
      Ok(c) => c,
      Err(e) => {
        error!("{}", e);
        continue;
      }
    };

    // 2. Create the SsMiddleware from the connector.
    let ss_middleware = SsMiddleware::new(connector);

    // 3. Build the reqwest client with the middleware.
    let client = reqwest::Client::builder().no_proxy().build()?;
    let client: ClientWithMiddleware = ClientBuilder::new(client).with(ss_middleware).build();

    // 4. Send the request using the reqwest API.
    for test_url in ["http://ifconfig.me/ip", "https://ifconfig.me/ip"] {
      match client.get(test_url).send().await {
        Ok(res) => {
          let status = res.status();
          let ip = res.text().await?;
          info!("\t{test_url} {} {}", status, ip);
        }
        Err(e) => {
          error!("\t{test_url} ❌ {e}");
        }
      }
    }
  }

  Ok(())
}