mirror of
https://github.com/zhom/donutbrowser.git
synced 2026-09-24 18:30:59 +02:00
refactor: launch all browsers via proxy
This commit is contained in:
@@ -137,7 +137,7 @@ impl BrowserRunner {
|
||||
app_handle: tauri::AppHandle,
|
||||
profile: &BrowserProfile,
|
||||
url: Option<String>,
|
||||
local_proxy_settings: Option<&ProxySettings>,
|
||||
_local_proxy_settings: Option<&ProxySettings>,
|
||||
) -> Result<BrowserProfile, Box<dyn std::error::Error + Send + Sync>> {
|
||||
// Check if browser is disabled due to ongoing update
|
||||
let auto_updater = crate::auto_updater::AutoUpdater::instance();
|
||||
@@ -154,60 +154,42 @@ impl BrowserRunner {
|
||||
// Handle camoufox profiles using nodecar launcher
|
||||
if profile.browser == "camoufox" {
|
||||
if let Some(mut camoufox_config) = profile.camoufox_config.clone() {
|
||||
// Handle proxy settings for camoufox
|
||||
if let Some(proxy_id) = &profile.proxy_id {
|
||||
if let Some(stored_proxy) = PROXY_MANAGER.get_proxy_settings_by_id(proxy_id) {
|
||||
println!("Starting proxy for Camoufox profile: {}", profile.name);
|
||||
// Always start a local proxy for Camoufox (for traffic monitoring and geoip support)
|
||||
let upstream_proxy = profile
|
||||
.proxy_id
|
||||
.as_ref()
|
||||
.and_then(|id| PROXY_MANAGER.get_proxy_settings_by_id(id));
|
||||
|
||||
// Start the proxy and get local proxy settings
|
||||
let local_proxy = PROXY_MANAGER
|
||||
.start_proxy(
|
||||
app_handle.clone(),
|
||||
&stored_proxy,
|
||||
0, // Use 0 as temporary PID, will be updated later
|
||||
Some(&profile.name),
|
||||
)
|
||||
.await
|
||||
.map_err(|e| format!("Failed to start proxy for Camoufox: {e}"))?;
|
||||
println!(
|
||||
"Starting local proxy for Camoufox profile: {} (upstream: {})",
|
||||
profile.name,
|
||||
upstream_proxy
|
||||
.as_ref()
|
||||
.map(|p| format!("{}:{}", p.host, p.port))
|
||||
.unwrap_or_else(|| "DIRECT".to_string())
|
||||
);
|
||||
|
||||
// Format proxy URL for camoufox
|
||||
let proxy_url = format!(
|
||||
"{}://{}:{}",
|
||||
if stored_proxy.proxy_type == "socks5" || stored_proxy.proxy_type == "socks4" {
|
||||
&stored_proxy.proxy_type
|
||||
} else {
|
||||
"http"
|
||||
},
|
||||
local_proxy.host,
|
||||
local_proxy.port
|
||||
);
|
||||
// Start the proxy and get local proxy settings
|
||||
let local_proxy = PROXY_MANAGER
|
||||
.start_proxy(
|
||||
app_handle.clone(),
|
||||
upstream_proxy.as_ref(),
|
||||
0, // Use 0 as temporary PID, will be updated later
|
||||
Some(&profile.name),
|
||||
)
|
||||
.await
|
||||
.map_err(|e| format!("Failed to start local proxy for Camoufox: {e}"))?;
|
||||
|
||||
// Add username and password if available
|
||||
let proxy_url = if let (Some(username), Some(password)) =
|
||||
(&stored_proxy.username, &stored_proxy.password)
|
||||
{
|
||||
format!(
|
||||
"{}://{}:{}@{}:{}",
|
||||
if stored_proxy.proxy_type == "socks5" || stored_proxy.proxy_type == "socks4" {
|
||||
&stored_proxy.proxy_type
|
||||
} else {
|
||||
"http"
|
||||
},
|
||||
username,
|
||||
password,
|
||||
local_proxy.host,
|
||||
local_proxy.port
|
||||
)
|
||||
} else {
|
||||
proxy_url
|
||||
};
|
||||
// Format proxy URL for camoufox - always use HTTP for the local proxy
|
||||
let proxy_url = format!("http://{}:{}", local_proxy.host, local_proxy.port);
|
||||
|
||||
// Set proxy in camoufox config
|
||||
camoufox_config.proxy = Some(proxy_url);
|
||||
// Set proxy in camoufox config
|
||||
camoufox_config.proxy = Some(proxy_url);
|
||||
|
||||
println!("Configured proxy for Camoufox: {:?}", camoufox_config.proxy);
|
||||
}
|
||||
}
|
||||
println!(
|
||||
"Configured local proxy for Camoufox: {:?}",
|
||||
camoufox_config.proxy
|
||||
);
|
||||
|
||||
// Use the existing config or create a test config if none exists
|
||||
let final_config = if camoufox_config.timezone.is_some()
|
||||
@@ -289,18 +271,14 @@ impl BrowserRunner {
|
||||
// Continue anyway, the error might not be critical
|
||||
}
|
||||
|
||||
// For Chromium browsers, use local proxy settings if available
|
||||
// For Firefox browsers, proxy settings are handled via PAC files
|
||||
let stored_proxy_settings = profile
|
||||
// Get stored proxy settings for later use (removed as we handle this in proxy startup)
|
||||
let _stored_proxy_settings = profile
|
||||
.proxy_id
|
||||
.as_ref()
|
||||
.and_then(|id| PROXY_MANAGER.get_proxy_settings_by_id(id));
|
||||
let proxy_for_launch_args = match browser_type {
|
||||
BrowserType::Chromium | BrowserType::Brave => {
|
||||
local_proxy_settings.or(stored_proxy_settings.as_ref())
|
||||
}
|
||||
_ => None, // Firefox browsers use PAC files, not launch args
|
||||
};
|
||||
|
||||
// For now, don't use proxy in launch args - we'll set it up after launch
|
||||
let proxy_for_launch_args: Option<&ProxySettings> = None;
|
||||
|
||||
// Get profile data path and launch arguments
|
||||
let profiles_dir = self.get_profiles_dir();
|
||||
@@ -399,24 +377,56 @@ impl BrowserRunner {
|
||||
// which is already handled in the profile creation process
|
||||
}
|
||||
|
||||
// Start proxy if configured and needed (for Chromium-based browsers)
|
||||
if let Some(proxy_id) = &profile.proxy_id {
|
||||
if let Some(stored_proxy) = PROXY_MANAGER.get_proxy_settings_by_id(proxy_id) {
|
||||
println!("Starting proxy for profile: {}", profile.name);
|
||||
// Always start a local proxy for traffic monitoring and potential upstream routing
|
||||
let upstream_proxy = profile
|
||||
.proxy_id
|
||||
.as_ref()
|
||||
.and_then(|id| PROXY_MANAGER.get_proxy_settings_by_id(id));
|
||||
|
||||
match PROXY_MANAGER
|
||||
.start_proxy(
|
||||
app_handle.clone(),
|
||||
&stored_proxy,
|
||||
actual_pid,
|
||||
Some(&profile.name),
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(_) => println!("Proxy started successfully for profile: {}", profile.name),
|
||||
Err(e) => println!("Warning: Failed to start proxy: {e}"),
|
||||
println!(
|
||||
"Starting local proxy for profile: {} (upstream: {})",
|
||||
profile.name,
|
||||
upstream_proxy
|
||||
.as_ref()
|
||||
.map(|p| format!("{}:{}", p.host, p.port))
|
||||
.unwrap_or_else(|| "DIRECT".to_string())
|
||||
);
|
||||
|
||||
match PROXY_MANAGER
|
||||
.start_proxy(
|
||||
app_handle.clone(),
|
||||
upstream_proxy.as_ref(),
|
||||
actual_pid,
|
||||
Some(&profile.name),
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(local_proxy) => {
|
||||
println!(
|
||||
"Local proxy started successfully for profile: {} on port: {}",
|
||||
profile.name, local_proxy.port
|
||||
);
|
||||
|
||||
// For Firefox-based browsers, update the PAC file with the local proxy
|
||||
if matches!(
|
||||
browser_type,
|
||||
BrowserType::Firefox
|
||||
| BrowserType::FirefoxDeveloper
|
||||
| BrowserType::Zen
|
||||
| BrowserType::TorBrowser
|
||||
| BrowserType::MullvadBrowser
|
||||
) {
|
||||
let profiles_dir = self.get_profiles_dir();
|
||||
let profile_path = profiles_dir
|
||||
.join(updated_profile.id.to_string())
|
||||
.join("profile");
|
||||
|
||||
if let Err(e) = self.apply_proxy_settings_to_profile(&profile_path, &local_proxy, None) {
|
||||
println!("Warning: Failed to update Firefox proxy settings: {e}");
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(e) => println!("Warning: Failed to start local proxy: {e}"),
|
||||
}
|
||||
|
||||
// Emit profile update event to frontend
|
||||
@@ -1351,7 +1361,12 @@ pub async fn launch_browser_profile(
|
||||
|
||||
// Start the proxy first
|
||||
match PROXY_MANAGER
|
||||
.start_proxy(app_handle.clone(), &proxy, temp_pid, Some(&profile.name))
|
||||
.start_proxy(
|
||||
app_handle.clone(),
|
||||
Some(&proxy),
|
||||
temp_pid,
|
||||
Some(&profile.name),
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(internal_proxy) => {
|
||||
|
||||
@@ -434,7 +434,12 @@ impl ProfileManager {
|
||||
// Browser is running and proxy is enabled, start new proxy
|
||||
if let Some(pid) = profile.process_id {
|
||||
match PROXY_MANAGER
|
||||
.start_proxy(app_handle.clone(), &proxy_settings, pid, Some(profile_name))
|
||||
.start_proxy(
|
||||
app_handle.clone(),
|
||||
Some(&proxy_settings),
|
||||
pid,
|
||||
Some(profile_name),
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(internal_proxy_settings) => {
|
||||
|
||||
@@ -249,10 +249,11 @@ impl ProxyManager {
|
||||
}
|
||||
|
||||
// Start a proxy for given proxy settings and associate it with a browser process ID
|
||||
// If proxy_settings is None, starts a direct proxy for traffic monitoring
|
||||
pub async fn start_proxy(
|
||||
&self,
|
||||
app_handle: tauri::AppHandle,
|
||||
proxy_settings: &ProxySettings,
|
||||
proxy_settings: Option<&ProxySettings>,
|
||||
browser_pid: u32,
|
||||
profile_name: Option<&str>,
|
||||
) -> Result<ProxySettings, String> {
|
||||
@@ -273,15 +274,19 @@ impl ProxyManager {
|
||||
// Check if we have a preferred port for this profile
|
||||
let preferred_port = if let Some(name) = profile_name {
|
||||
let profile_proxies = self.profile_proxies.lock().unwrap();
|
||||
profile_proxies.get(name).and_then(|settings| {
|
||||
profile_proxies.get(name).and_then(|_settings| {
|
||||
// Find existing proxy with same settings to reuse port
|
||||
let active_proxies = self.active_proxies.lock().unwrap();
|
||||
active_proxies
|
||||
.values()
|
||||
.find(|p| {
|
||||
p.upstream_host == settings.host
|
||||
&& p.upstream_port == settings.port
|
||||
&& p.upstream_type == settings.proxy_type
|
||||
if let Some(proxy_settings) = proxy_settings {
|
||||
p.upstream_host == proxy_settings.host
|
||||
&& p.upstream_port == proxy_settings.port
|
||||
&& p.upstream_type == proxy_settings.proxy_type
|
||||
} else {
|
||||
p.upstream_type == "DIRECT"
|
||||
}
|
||||
})
|
||||
.map(|p| p.local_port)
|
||||
})
|
||||
@@ -295,20 +300,25 @@ impl ProxyManager {
|
||||
.sidecar("nodecar")
|
||||
.map_err(|e| format!("Failed to create sidecar: {e}"))?
|
||||
.arg("proxy")
|
||||
.arg("start")
|
||||
.arg("--host")
|
||||
.arg(&proxy_settings.host)
|
||||
.arg("--proxy-port")
|
||||
.arg(proxy_settings.port.to_string())
|
||||
.arg("--type")
|
||||
.arg(&proxy_settings.proxy_type);
|
||||
.arg("start");
|
||||
|
||||
// Add credentials if provided
|
||||
if let Some(username) = &proxy_settings.username {
|
||||
nodecar = nodecar.arg("--username").arg(username);
|
||||
}
|
||||
if let Some(password) = &proxy_settings.password {
|
||||
nodecar = nodecar.arg("--password").arg(password);
|
||||
// Add upstream proxy settings if provided, otherwise create direct proxy
|
||||
if let Some(proxy_settings) = proxy_settings {
|
||||
nodecar = nodecar
|
||||
.arg("--host")
|
||||
.arg(&proxy_settings.host)
|
||||
.arg("--proxy-port")
|
||||
.arg(proxy_settings.port.to_string())
|
||||
.arg("--type")
|
||||
.arg(&proxy_settings.proxy_type);
|
||||
|
||||
// Add credentials if provided
|
||||
if let Some(username) = &proxy_settings.username {
|
||||
nodecar = nodecar.arg("--username").arg(username);
|
||||
}
|
||||
if let Some(password) = &proxy_settings.password {
|
||||
nodecar = nodecar.arg("--password").arg(password);
|
||||
}
|
||||
}
|
||||
|
||||
// If we have a preferred port, use it
|
||||
@@ -349,9 +359,13 @@ impl ProxyManager {
|
||||
let proxy_info = ProxyInfo {
|
||||
id: id.to_string(),
|
||||
local_url,
|
||||
upstream_host: proxy_settings.host.clone(),
|
||||
upstream_port: proxy_settings.port,
|
||||
upstream_type: proxy_settings.proxy_type.clone(),
|
||||
upstream_host: proxy_settings
|
||||
.map(|p| p.host.clone())
|
||||
.unwrap_or_else(|| "DIRECT".to_string()),
|
||||
upstream_port: proxy_settings.map(|p| p.port).unwrap_or(0),
|
||||
upstream_type: proxy_settings
|
||||
.map(|p| p.proxy_type.clone())
|
||||
.unwrap_or_else(|| "DIRECT".to_string()),
|
||||
local_port,
|
||||
};
|
||||
|
||||
@@ -363,8 +377,10 @@ impl ProxyManager {
|
||||
|
||||
// Store the profile proxy info for persistence
|
||||
if let Some(name) = profile_name {
|
||||
let mut profile_proxies = self.profile_proxies.lock().unwrap();
|
||||
profile_proxies.insert(name.to_string(), proxy_settings.clone());
|
||||
if let Some(proxy_settings) = proxy_settings {
|
||||
let mut profile_proxies = self.profile_proxies.lock().unwrap();
|
||||
profile_proxies.insert(name.to_string(), proxy_settings.clone());
|
||||
}
|
||||
}
|
||||
|
||||
// Return proxy settings for the browser
|
||||
|
||||
Reference in New Issue
Block a user