Procházet zdrojové kódy

feat(buff-sweep): 为扫货功能添加代理支持

- buff_sweep_runner.rs: http_post_form_steam 函数添加代理支持
- operate_confirmation 函数添加代理参数
- operate_confirmation_with_retry 函数添加代理支持,每次重试时重新构建代理

Made-with: Cursor
wuzilong před 6 měsíci
rodič
revize
e3316dc104

+ 23 - 10
client-tauri/src-tauri/src/commands/buff_sweep_runner.rs

@@ -1320,14 +1320,18 @@ async fn http_post_form_steam(
     }
     }
 
 
     for attempt in 0..=retry_times {
     for attempt in 0..=retry_times {
-        // POST 请求不使用代理,直接连接 Steam
+        // 使用全局共享代理池获取代理(本地5000端口Steam API必须走代理)
+        let proxy_url = get_proxy_from_global_pool(sweep_file).await?;
         let msg = format!(
         let msg = format!(
-            "[sweep:{}] Steam POST 直连(不使用代理)",
-            label
+            "[sweep:{}] Steam POST 使用代理 {}",
+            label,
+            proxy_endpoint_for_log(&proxy_url)
         );
         );
         info!("{}", msg);
         info!("{}", msg);
         sweep_log_diag(sweep_file, rid, "info", &msg).await;
         sweep_log_diag(sweep_file, rid, "info", &msg).await;
+        let proxy = reqwest::Proxy::all(&proxy_url).map_err(|e| e.to_string())?;
         let client = reqwest::Client::builder()
         let client = reqwest::Client::builder()
+            .proxy(proxy)
             .timeout(Duration::from_secs(25))
             .timeout(Duration::from_secs(25))
             .build()
             .build()
             .map_err(|e| e.to_string())?;
             .map_err(|e| e.to_string())?;
@@ -1465,6 +1469,7 @@ async fn operate_confirmation(
     acc: &mut LocalAccount,
     acc: &mut LocalAccount,
     confirmation_id: &str,
     confirmation_id: &str,
     _client: &reqwest::Client,
     _client: &reqwest::Client,
+    sweep_file: Option<&SweepShared>,
 ) -> Result<bool, String> {
 ) -> Result<bool, String> {
     let url = format!(
     let url = format!(
         "{}/trade-confirmations/operate",
         "{}/trade-confirmations/operate",
@@ -1482,8 +1487,11 @@ async fn operate_confirmation(
         "ConfirmationId": cid,
         "ConfirmationId": cid,
         "Operation": "accept",
         "Operation": "accept",
     });
     });
-    // 不使用代理,直接连接本地 steamAPI
+    // 使用代理连接本地 steamAPI
+    let proxy_url = get_proxy_from_global_pool(sweep_file).await?;
+    let proxy = reqwest::Proxy::all(&proxy_url).map_err(|e| e.to_string())?;
     let client = reqwest::Client::builder()
     let client = reqwest::Client::builder()
+        .proxy(proxy)
         .timeout(Duration::from_secs(25))
         .timeout(Duration::from_secs(25))
         .build()
         .build()
         .map_err(|e| format!("创建HTTP客户端失败: {}", e))?;
         .map_err(|e| format!("创建HTTP客户端失败: {}", e))?;
@@ -1522,6 +1530,7 @@ async fn operate_confirmation_with_retry(
     acc: &mut LocalAccount,
     acc: &mut LocalAccount,
     confirmation_id: &str,
     confirmation_id: &str,
     max_retries: u32,
     max_retries: u32,
+    sweep_file: Option<&SweepShared>,
 ) -> Result<bool, String> {
 ) -> Result<bool, String> {
     let url = format!(
     let url = format!(
         "{}/trade-confirmations/operate",
         "{}/trade-confirmations/operate",
@@ -1529,12 +1538,16 @@ async fn operate_confirmation_with_retry(
     );
     );
     let cid: i64 = confirmation_id.parse().map_err(|_| "confirmation_id 无效".to_string())?;
     let cid: i64 = confirmation_id.parse().map_err(|_| "confirmation_id 无效".to_string())?;
 
 
-    let client = reqwest::Client::builder()
-        .timeout(Duration::from_secs(25))
-        .build()
-        .map_err(|e| format!("创建HTTP客户端失败: {}", e))?;
-
     for attempt in 1..=max_retries {
     for attempt in 1..=max_retries {
+        // 使用代理连接本地 steamAPI
+        let proxy_url = get_proxy_from_global_pool(sweep_file).await?;
+        let proxy = reqwest::Proxy::all(&proxy_url).map_err(|e| e.to_string())?;
+        let client = reqwest::Client::builder()
+            .proxy(proxy)
+            .timeout(Duration::from_secs(25))
+            .build()
+            .map_err(|e| format!("创建HTTP客户端失败: {}", e))?;
+
         // 每次重试时重新构建 body(因为 session 可能已更新)
         // 每次重试时重新构建 body(因为 session 可能已更新)
         let body = json!({
         let body = json!({
             "MaFilePath": acc.mafile_path,
             "MaFilePath": acc.mafile_path,
@@ -1907,7 +1920,7 @@ async fn purchase_one_listing(
         }
         }
 
 
         // 第一步:使用幂等重试确认交易(最多10次)
         // 第一步:使用幂等重试确认交易(最多10次)
-        let confirm_result = operate_confirmation_with_retry(acc, &confirmation_id, 10).await;
+        let confirm_result = operate_confirmation_with_retry(acc, &confirmation_id, 10, sweep_file).await;
         let ok = match confirm_result {
         let ok = match confirm_result {
             Ok(result) => result,
             Ok(result) => result,
             Err(e) => {
             Err(e) => {