Files
WebScraper/examples/test_vpn_setup.rs
2025-12-09 14:57:18 +01:00

188 lines
6.7 KiB
Rust
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// examples/test_vpn_setup.rs
//! Quick VPN Setup Test
//!
//! Testet nur die VPN-Verbindung und IP-Überprüfung ohne Scraping-Tasks
//!
//! Usage:
//! ENABLE_VPN_ROTATION=true VPN_SERVERS=US cargo run --example test_vpn_setup
//!
//! Or with debug logging:
//! RUST_LOG=debug ENABLE_VPN_ROTATION=true VPN_SERVERS=US cargo run --example test_vpn_setup
use anyhow::Result;
use std::sync::Arc;
// Import von main crate
use event_backtest_engine::config::Config;
use event_backtest_engine::scraper::vpn_integration::VpnIntegration;
use event_backtest_engine::scraper::webdriver::ChromeDriverPool;
#[tokio::main]
async fn main() -> Result<()> {
// Initialize logging
tracing_subscriber::fmt()
.with_max_level(tracing::Level::INFO)
.with_target(false)
.init();
println!("\n═══════════════════════════════════════════════════════════");
println!(" 🔧 VPN Setup Test - Quick Validation");
println!("═══════════════════════════════════════════════════════════\n");
// 1. Load config
println!("1⃣ Loading configuration...");
let config = match Config::load() {
Ok(cfg) => {
println!(" ✓ Config loaded successfully");
cfg
}
Err(e) => {
println!(" ❌ Failed to load config: {}", e);
return Err(e);
}
};
// 2. Display VPN settings
println!("\n2⃣ VPN Configuration:");
println!(
" - VPN Rotation: {}",
if config.enable_vpn_rotation {
"✅ ENABLED"
} else {
"⚠️ DISABLED"
}
);
if config.enable_vpn_rotation {
let servers = config.get_vpn_servers();
if servers.is_empty() {
println!(" - Servers: ❌ NO SERVERS CONFIGURED");
println!("\n❌ Error: VPN rotation enabled but no servers configured!");
println!(" Please set VPN_SERVERS in .env (e.g., VPN_SERVERS=US,UK,JP)");
return Ok(());
}
println!(" - Servers: {:?}", servers);
println!(" - Tasks per Session: {}", config.tasks_per_vpn_session);
println!(" - Extension ID: {}", config.protonvpn_extension_id);
} else {
println!(" VPN rotation is disabled. Test with:");
println!(
" ENABLE_VPN_ROTATION=true VPN_SERVERS=US cargo run --example test_vpn_setup"
);
return Ok(());
}
// 3. Create VPN Integration
println!("\n3⃣ Initializing VPN Integration...");
let vpn = match VpnIntegration::from_config(&config) {
Ok(v) => {
println!(" ✓ VPN Integration created");
v
}
Err(e) => {
println!(" ❌ Failed to initialize VPN: {}", e);
return Err(e);
}
};
if !vpn.enabled {
println!(" ⚠️ VPN is not enabled in config");
return Ok(());
}
// 4. Create ChromeDriver Pool (single instance for testing)
println!("\n4⃣ Creating ChromeDriver Pool (1 instance for testing)...");
let pool: Arc<ChromeDriverPool> = match ChromeDriverPool::new(1).await {
Ok(p) => {
println!(" ✓ ChromeDriver pool created");
Arc::new(p)
}
Err(e) => {
println!(" ❌ Failed to create ChromeDriver pool: {}", e);
println!(" Make sure chromedriver-win64/chromedriver.exe exists");
return Err(e);
}
};
println!(" - Instances: {}", pool.get_number_of_instances());
// 5. Initialize first VPN session
println!("\n5⃣ Creating VPN Session...");
match vpn.initialize_session().await {
Ok(session_id) => {
println!(" ✓ VPN session created: {}", session_id);
}
Err(e) => {
println!(" ❌ Failed to create VPN session: {}", e);
return Err(e);
}
}
// 6. Get current session info
println!("\n6⃣ VPN Session Info:");
if let Some(session) = vpn.get_current_session_id().await {
println!(" - Session ID: {}", session);
}
// 7. Test WebDriver basic navigation
println!("\n7⃣ Testing WebDriver Navigation...");
match test_webdriver_navigation(&pool).await {
Ok(_) => {
println!(" ✓ WebDriver navigation successful");
}
Err(e) => {
println!(" ⚠️ WebDriver test had issues: {}", e);
println!(" This might be normal if extension UI differs");
}
}
// Summary
println!("\n═══════════════════════════════════════════════════════════");
println!(" ✅ VPN Setup Test Complete!");
println!("═══════════════════════════════════════════════════════════");
println!("\nNext steps:");
println!(" 1. Check if VPN connection is established in Chrome");
println!(" 2. Verify IP address changed (should be from VPN server)");
println!(" 3. If all looks good, you can run the full scraper:");
println!(" cargo run");
Ok(())
}
/// Test basic WebDriver navigation to extension
async fn test_webdriver_navigation(pool: &Arc<ChromeDriverPool>) -> Result<()> {
println!(" Navigating to IP check site...");
// Simple test: navigate to whatismyipaddress.com
match pool
.execute("https://whatismyipaddress.com/".to_string(), |client| {
async move {
let source = client.source().await?;
// Try to extract IP
if let Some(start) = source.find("IPv4") {
let section = &source[start..];
if let Some(ip_start) = section.find(|c: char| c.is_numeric()) {
if let Some(ip_end) =
section[ip_start..].find(|c: char| !c.is_numeric() && c != '.')
{
let ip = &section[ip_start..ip_start + ip_end];
println!(" - Detected IP: {}", ip);
return Ok(ip.to_string());
}
}
}
Ok("IP extraction attempted".to_string())
}
})
.await
{
Ok(result) => {
println!(" Result: {}", result);
Ok(())
}
Err(e) => Err(e),
}
}