Skip to content

Commit 803eac2

Browse files
committed
misc: no system proxy by default
1 parent 3f2cc56 commit 803eac2

File tree

3 files changed

+42
-17
lines changed

3 files changed

+42
-17
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "msstore_cdn"
3-
version = "1.0.0"
3+
version = "1.0.1"
44
edition = "2024"
55

66
[dependencies]

src/main.rs

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::net::IpAddr;
66
use std::path::{Path, PathBuf};
77
use std::sync::{Arc, Mutex};
88
use std::time::{Duration, Instant};
9-
use tracing::{debug, error, info, instrument, warn};
9+
use tracing::{debug, error, info, instrument};
1010

1111
#[cfg(target_os = "windows")]
1212
use native_windows_derive::NwgUi;
@@ -115,6 +115,10 @@ struct Args {
115115
/// Automatically select the fastest source
116116
#[arg(long, default_value_t = true)]
117117
auto: bool,
118+
119+
/// Enable system proxy usage (Default: false)
120+
#[arg(long, default_value_t = false)]
121+
use_system_proxy: bool,
118122
}
119123

120124
fn main() -> Result<()> {
@@ -167,7 +171,8 @@ fn init_logging(debug_mode: bool) {
167171

168172
async fn async_main(mut args: Args, gui_logger: Option<Arc<Mutex<String>>>) -> Result<()> {
169173
let selected_source_domain = if args.auto {
170-
match run_auto_selection(gui_logger.clone()).await {
174+
// Pass the proxy flag to the selection logic
175+
match run_auto_selection(gui_logger.clone(), args.use_system_proxy).await {
171176
Ok(domain) => {
172177
info!("Auto-selection winner: {}", domain);
173178
domain
@@ -198,7 +203,10 @@ async fn async_main(mut args: Args, gui_logger: Option<Arc<Mutex<String>>>) -> R
198203
}
199204

200205
/// 自动选择逻辑
201-
async fn run_auto_selection(gui_logger: Option<Arc<Mutex<String>>>) -> Result<String> {
206+
async fn run_auto_selection(
207+
gui_logger: Option<Arc<Mutex<String>>>,
208+
use_system_proxy: bool,
209+
) -> Result<String> {
202210
macro_rules! log_msg {
203211
($($arg:tt)*) => {{
204212
let msg = format!($($arg)*);
@@ -213,15 +221,26 @@ async fn run_auto_selection(gui_logger: Option<Arc<Mutex<String>>>) -> Result<St
213221
}
214222

215223
log_msg!("Starting auto-selection...");
224+
if use_system_proxy {
225+
log_msg!("Network: Using System Proxy");
226+
} else {
227+
log_msg!("Network: Direct Connection (No Proxy)");
228+
}
216229
log_msg!("Fetching file links from API...");
217230

218-
// 1. Fetch Links
219-
let client = reqwest::Client::builder()
231+
// 1. Configure Client
232+
let mut client_builder = reqwest::Client::builder()
220233
.user_agent("Mozilla/5.0 (Windows NT 10.0; Win64; x64)")
221-
// 增加超时设置,防止 API 卡死
222-
.timeout(Duration::from_secs(10))
223-
.build()?;
234+
.timeout(Duration::from_secs(10));
235+
236+
// Disable proxy if not requested
237+
if !use_system_proxy {
238+
client_builder = client_builder.no_proxy();
239+
}
224240

241+
let client = client_builder.build()?;
242+
243+
// 2. Fetch Links
225244
let payload = ApiRequest {
226245
productInput: "9WZDNCRD29V9".to_string(),
227246
market: "US".to_string(),
@@ -241,7 +260,7 @@ async fn run_auto_selection(gui_logger: Option<Arc<Mutex<String>>>) -> Result<St
241260

242261
let api_data: ApiResponse = resp.json().await.context("Failed to parse API response")?;
243262

244-
// 2. Select a test file
263+
// 3. Select a test file
245264
let test_package = api_data
246265
.appx_packages
247266
.iter()
@@ -261,7 +280,7 @@ async fn run_auto_selection(gui_logger: Option<Arc<Mutex<String>>>) -> Result<St
261280
format!("{}?{}", path, query)
262281
};
263282

264-
// 3. Benchmark in parallel
283+
// 4. Benchmark in parallel
265284
log_msg!("Benchmarking {} sources...", DNS_SOURCES.len());
266285

267286
let futures = DNS_SOURCES.iter().map(|source| {
@@ -326,7 +345,6 @@ async fn run_auto_selection(gui_logger: Option<Arc<Mutex<String>>>) -> Result<St
326345
let size = bytes.len() as u64;
327346

328347
// Check size consistency
329-
// 如果 API 返回的 Content-Length 存在,则校验
330348
if size == 0 {
331349
debug!("[{}] Downloaded 0 bytes.", source_name);
332350
return (source_name, source_domain, None, 0);
@@ -369,7 +387,7 @@ async fn run_auto_selection(gui_logger: Option<Arc<Mutex<String>>>) -> Result<St
369387
.collect()
370388
.await;
371389

372-
// 4. Analyze results
390+
// 5. Analyze results
373391
let mut valid_results: Vec<_> = results
374392
.into_iter()
375393
.filter_map(|(name, domain, duration, size)| {
@@ -673,16 +691,20 @@ pub struct HostsApp {
673691
combo_source: nwg::ComboBox<String>,
674692

675693
// Row 2
694+
// Layout: [Dry Run] [Auto Select] [Use Proxy]
676695
#[nwg_control(text: "Dry Run", check_state: nwg::CheckBoxState::Unchecked)]
677-
#[nwg_layout_item(layout: layout, col: 1, row: 2)]
696+
#[nwg_layout_item(layout: layout, col: 0, row: 2)]
678697
check_dry: nwg::CheckBox,
679698

680-
// Auto Select Checkbox
681699
#[nwg_control(text: "Auto Select", check_state: nwg::CheckBoxState::Checked)]
682-
#[nwg_layout_item(layout: layout, col: 2, row: 2)]
700+
#[nwg_layout_item(layout: layout, col: 1, row: 2)]
683701
#[nwg_events( OnButtonClick: [HostsApp::on_auto_check] )]
684702
check_auto: nwg::CheckBox,
685703

704+
#[nwg_control(text: "Use Proxy", check_state: nwg::CheckBoxState::Unchecked)]
705+
#[nwg_layout_item(layout: layout, col: 2, row: 2)]
706+
check_proxy: nwg::CheckBox,
707+
686708
// Row 3
687709
#[nwg_control(text: "Update Hosts File", flags: "VISIBLE")]
688710
#[nwg_layout_item(layout: layout, col: 1, row: 3, col_span: 2)]
@@ -710,6 +732,7 @@ impl HostsApp {
710732
let names: Vec<String> = DNS_SOURCES.iter().map(|s| s.name.to_string()).collect();
711733
self.combo_source.set_collection(names);
712734
self.combo_source.set_selection(Some(0));
735+
self.on_auto_check(); // Set initial state
713736
}
714737

715738
fn on_auto_check(&self) {
@@ -725,6 +748,7 @@ impl HostsApp {
725748
// UI Parameters
726749
let is_auto = self.check_auto.check_state() == nwg::CheckBoxState::Checked;
727750
let dry_run = self.check_dry.check_state() == nwg::CheckBoxState::Checked;
751+
let use_proxy = self.check_proxy.check_state() == nwg::CheckBoxState::Checked;
728752

729753
let source_idx = self.combo_source.selection().unwrap_or(0);
730754

@@ -749,6 +773,7 @@ impl HostsApp {
749773
output: None,
750774
source_index: source_idx,
751775
auto: is_auto,
776+
use_system_proxy: use_proxy,
752777
};
753778

754779
let res = rt.block_on(async_main(args, Some(logs_handle.clone())));

0 commit comments

Comments
 (0)