From 59557150be29d0d572b5e06b87b0ccc30111488a Mon Sep 17 00:00:00 2001 From: Ilya Speranskiy Date: Wed, 25 Nov 2026 02:22:32 +0300 Subject: [PATCH 1/3] fix(vpn): keep the tunnel up when Windows reserves its ports The Stats API inbound used a hardcoded port 10085. After a reboot Hyper-V, Docker or WSL can reserve that range for themselves, the core died on bind with WSAEACCES, and the VPN stayed off until the user noticed. - take the Stats API port from the OS (ephemeral bind) and keep it out of the SOCKS/HTTP and provider inbound numbers; - retry the start once when the core reports a bind failure; - explain an unavailable SOCKS/HTTP port as a Windows reservation instead of a program the user has to close; - restart the core after an unexpected exit with the shared backoff policy and give up after five attempts; a manual stop still stops it; - log failed starts to xray.log instead of keeping them in memory only; - move RestartPolicy to supervisor.rs so the Telegram proxy and the VPN share one implementation. --- CHANGELOG.md | 11 + README.md | 2 + src-tauri/src/lib.rs | 1 + src-tauri/src/supervisor.rs | 160 ++++++++++++++ src-tauri/src/tgws.rs | 147 +------------ src-tauri/src/xray.rs | 424 ++++++++++++++++++++++++++++++------ 6 files changed, 538 insertions(+), 207 deletions(-) create mode 100644 src-tauri/src/supervisor.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index a62285e..e726fda 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,17 @@ Все заметные изменения проекта. Формат — [Keep a Changelog](https://keepachangelog.com/ru/1.1.0/), версии — [SemVer](https://semver.org/lang/ru/). +## [2.3.2] — 2026-11-25 + +VPN больше не остаётся выключенным после перезагрузки, если системе помешал нужный ему порт. + +### Исправлено + +- VPN снова подключается, даже если порт, который нужен ядру, зарезервировала Windows: свободный + порт выбирается заново при каждом запуске. +- Если VPN всё-таки оборвался, приложение само поднимает его обратно (до пяти попыток), а не ждёт, + пока это заметит пользователь. Ручное выключение по-прежнему остаётся выключением. + ## [2.3.1] — 2026-11-22 Страница Zapret выглядит как остальные разделы, а «Главная» открывается целиком. diff --git a/README.md b/README.md index 9992413..3833302 100644 --- a/README.md +++ b/README.md @@ -159,6 +159,8 @@ VPN и Telegram настраиваются отдельно и не нужны число ошибок и SLO выбранной локации за час. - **Автопереключение по желанию**: если туннель не отвечает 5 минут, приложение выберет живую локацию с лучшим пингом. По умолчанию выключено. +- **Переподключение без участия**: если ядро завершится само, приложение поднимет его + обратно (до пяти попыток подряд). Ручное выключение остаётся выключением. - **Остаток трафика и срок подписки**, если провайдер их сообщает, и ссылка в личный кабинет. - **Подписка обновляется сама**: приложение раз в час проверяет, не прошло ли 12 часов с прошлой загрузки, и обновляет список. Проверка идёт только при открытом окне. Если после обновления diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 872528a..bb68598 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -15,6 +15,7 @@ mod runtime; mod signing; mod strategy; mod subscription; +mod supervisor; mod sys; mod tgws; mod tray; diff --git a/src-tauri/src/supervisor.rs b/src-tauri/src/supervisor.rs new file mode 100644 index 0000000..efe7a8f --- /dev/null +++ b/src-tauri/src/supervisor.rs @@ -0,0 +1,160 @@ +//! Restart budget shared by the background supervisors. +//! +//! The Telegram proxy and the VPN core both die on their own now and then, and +//! both are brought back by the same arithmetic: a short list of growing delays +//! and a window that forgives a service which stayed alive for a while. The +//! policy is pure — its only inputs are the current time and the restarts +//! already made — which is what makes it testable without any processes. + +use std::collections::VecDeque; +use std::time::Duration; + +/// Delays before each automatic restart, indexed by the restarts already made. +pub const RESTART_DELAYS_SECS: [u64; 5] = [2, 5, 15, 30, 60]; + +/// Restart budget window: at most [`RESTART_DELAYS_SECS`] restarts inside it. +pub const RESTART_WINDOW_SECS: u64 = 600; + +/// What the supervisor does after the service died or stopped answering. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum RestartDecision { + /// Wait `delay`, then start the service again. `attempt` is 1-based. + Retry { attempt: u32, delay: Duration }, + /// The budget for the current window is spent: report `failed` and stop + /// instead of looping forever. + GiveUp { attempts: u32 }, +} + +/// Restart budget of the supervisor. +#[derive(Debug, Default)] +pub struct RestartPolicy { + /// Unix time of each restart in the current window, oldest first. + history: VecDeque, +} + +impl RestartPolicy { + pub fn new() -> Self { + Self::default() + } + + /// A manual start or stop begins a fresh cycle. + pub fn reset(&mut self) { + self.history.clear(); + } + + /// Decides what to do about an unexpected exit observed at `now`. + /// + /// Delays grow as 2, 5, 15, 30 and 60 seconds; a sixth failure inside ten + /// minutes gives up. + pub fn next_attempt(&mut self, now: u64) -> RestartDecision { + self.forget_outside_window(now); + let index = self.history.len(); + let Some(delay) = RESTART_DELAYS_SECS.get(index) else { + return RestartDecision::GiveUp { + attempts: index as u32, + }; + }; + self.history.push_back(now); + RestartDecision::Retry { + attempt: index as u32 + 1, + delay: Duration::from_secs(*delay), + } + } + + /// Drops restarts older than the window: a service that ran for ten minutes + /// is healthy again, so it earns a full budget. + fn forget_outside_window(&mut self, now: u64) { + while let Some(at) = self.history.front() { + if now.saturating_sub(*at) < RESTART_WINDOW_SECS { + break; + } + self.history.pop_front(); + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn restart_policy_backs_off_then_gives_up() { + let mut policy = RestartPolicy::new(); + let expected = [2u64, 5, 15, 30, 60]; + let mut now = 1_000; + for (index, seconds) in expected.iter().enumerate() { + assert_eq!( + policy.next_attempt(now), + RestartDecision::Retry { + attempt: index as u32 + 1, + delay: Duration::from_secs(*seconds), + } + ); + // The service dies again right away. + now += 1; + } + // Five attempts spent: no sixth restart, ever. + assert_eq!( + policy.next_attempt(now), + RestartDecision::GiveUp { attempts: 5 } + ); + assert_eq!( + policy.next_attempt(now + 1), + RestartDecision::GiveUp { attempts: 5 } + ); + } + + #[test] + fn restart_policy_forgets_attempts_outside_the_window() { + let mut policy = RestartPolicy::new(); + for step in 0..5u64 { + assert!(matches!( + policy.next_attempt(1_000 + step), + RestartDecision::Retry { .. } + )); + } + assert!(matches!( + policy.next_attempt(1_004), + RestartDecision::GiveUp { .. } + )); + + // The window slides one entry at a time: at t=1600 only the first + // restart is old enough to forget, so the fifth delay is next. + assert_eq!( + policy.next_attempt(1_000 + RESTART_WINDOW_SECS), + RestartDecision::Retry { + attempt: 5, + delay: Duration::from_secs(60), + } + ); + // Ten seconds later the whole old history is outside the window and + // only the restart from t=1600 still counts. + assert_eq!( + policy.next_attempt(1_010 + RESTART_WINDOW_SECS), + RestartDecision::Retry { + attempt: 2, + delay: Duration::from_secs(5), + } + ); + } + + #[test] + fn manual_start_and_stop_restore_the_budget() { + let mut policy = RestartPolicy::new(); + for step in 0..5u64 { + let _ = policy.next_attempt(2_000 + step); + } + assert!(matches!( + policy.next_attempt(2_010), + RestartDecision::GiveUp { .. } + )); + policy.reset(); + assert_eq!( + policy.next_attempt(2_020), + RestartDecision::Retry { + attempt: 1, + delay: Duration::from_secs(2), + } + ); + } +} diff --git a/src-tauri/src/tgws.rs b/src-tauri/src/tgws.rs index ace8a57..be6caaa 100644 --- a/src-tauri/src/tgws.rs +++ b/src-tauri/src/tgws.rs @@ -21,6 +21,7 @@ use crate::config; use crate::lock_config; +use crate::supervisor::{RestartDecision, RestartPolicy, RESTART_DELAYS_SECS}; use crate::sys; use crate::AppState; use serde::{Deserialize, Serialize}; @@ -103,10 +104,6 @@ const START_GRACE: Duration = Duration::from_millis(700); /// event, so a light poll is the only portable option; one `try_wait` per /// half-second costs nothing measurable. const WATCH_INTERVAL: Duration = Duration::from_millis(500); -/// Delays before each automatic restart, indexed by the restarts already made. -const RESTART_DELAYS_SECS: [u64; 5] = [2, 5, 15, 30, 60]; -/// Restart budget window: at most [`RESTART_DELAYS_SECS`] restarts inside it. -const RESTART_WINDOW_SECS: u64 = 600; /// Step of the cancellable wait before a restart. const RESTART_POLL: Duration = Duration::from_millis(250); /// At most one "proxy restarted" toast per this window, so a flapping proxy @@ -243,67 +240,6 @@ pub struct TgwsStatus { pub rust_binary_present: bool, } -/// What the supervisor does after the proxy died or stopped answering. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub enum RestartDecision { - /// Wait `delay`, then start the proxy again. `attempt` is 1-based. - Retry { attempt: u32, delay: Duration }, - /// The budget for the current window is spent: report `failed` and stop - /// instead of looping forever. - GiveUp { attempts: u32 }, -} - -/// Restart budget of the supervisor. -/// -/// Pure by design: the only inputs are the current time and the restarts -/// already made, so the whole policy is unit-testable without processes. -#[derive(Debug, Default)] -pub struct RestartPolicy { - /// Unix time of each restart in the current window, oldest first. - history: VecDeque, -} - -impl RestartPolicy { - pub fn new() -> Self { - Self::default() - } - - /// A manual start or stop begins a fresh cycle. - pub fn reset(&mut self) { - self.history.clear(); - } - - /// Decides what to do about an unexpected exit observed at `now`. - /// - /// Delays grow as 2, 5, 15, 30 and 60 seconds; a sixth failure inside ten - /// minutes gives up. - pub fn next_attempt(&mut self, now: u64) -> RestartDecision { - self.forget_outside_window(now); - let index = self.history.len(); - let Some(delay) = RESTART_DELAYS_SECS.get(index) else { - return RestartDecision::GiveUp { - attempts: index as u32, - }; - }; - self.history.push_back(now); - RestartDecision::Retry { - attempt: index as u32 + 1, - delay: Duration::from_secs(*delay), - } - } - - /// Drops restarts older than the window: a proxy that ran for ten minutes - /// is healthy again, so it earns a full budget. - fn forget_outside_window(&mut self, now: u64) { - while let Some(at) = self.history.front() { - if now.saturating_sub(*at) < RESTART_WINDOW_SECS { - break; - } - self.history.pop_front(); - } - } -} - /// Runtime state of the supervisor, stored in [`AppState`]. pub struct Tgws { child: Mutex>, @@ -3448,87 +3384,6 @@ mod tests { ); } - #[test] - fn restart_policy_backs_off_then_gives_up() { - let mut policy = RestartPolicy::new(); - let expected = [2u64, 5, 15, 30, 60]; - let mut now = 1_000; - for (index, seconds) in expected.iter().enumerate() { - assert_eq!( - policy.next_attempt(now), - RestartDecision::Retry { - attempt: index as u32 + 1, - delay: Duration::from_secs(*seconds), - } - ); - // The proxy dies again right away. - now += 1; - } - // Five attempts spent: no sixth restart, ever. - assert_eq!( - policy.next_attempt(now), - RestartDecision::GiveUp { attempts: 5 } - ); - assert_eq!( - policy.next_attempt(now + 1), - RestartDecision::GiveUp { attempts: 5 } - ); - } - - #[test] - fn restart_policy_forgets_attempts_outside_the_window() { - let mut policy = RestartPolicy::new(); - for step in 0..5u64 { - assert!(matches!( - policy.next_attempt(1_000 + step), - RestartDecision::Retry { .. } - )); - } - assert!(matches!( - policy.next_attempt(1_004), - RestartDecision::GiveUp { .. } - )); - - // The window slides one entry at a time: at t=1600 only the first - // restart is old enough to forget, so the fifth delay is next. - assert_eq!( - policy.next_attempt(1_000 + RESTART_WINDOW_SECS), - RestartDecision::Retry { - attempt: 5, - delay: Duration::from_secs(60), - } - ); - // Ten seconds later the whole old history is outside the window and - // only the restart from t=1600 still counts. - assert_eq!( - policy.next_attempt(1_010 + RESTART_WINDOW_SECS), - RestartDecision::Retry { - attempt: 2, - delay: Duration::from_secs(5), - } - ); - } - - #[test] - fn manual_start_and_stop_restore_the_budget() { - let mut policy = RestartPolicy::new(); - for step in 0..5u64 { - let _ = policy.next_attempt(2_000 + step); - } - assert!(matches!( - policy.next_attempt(2_010), - RestartDecision::GiveUp { .. } - )); - policy.reset(); - assert_eq!( - policy.next_attempt(2_020), - RestartDecision::Retry { - attempt: 1, - delay: Duration::from_secs(2), - } - ); - } - #[test] fn liveness_probe_only_touches_the_local_listener() { let listener = TcpListener::bind(("127.0.0.1", 0)).expect("listener"); diff --git a/src-tauri/src/xray.rs b/src-tauri/src/xray.rs index 21cb956..3cc2576 100644 --- a/src-tauri/src/xray.rs +++ b/src-tauri/src/xray.rs @@ -12,6 +12,7 @@ use crate::config; use crate::geo; use crate::lock_config; use crate::registry; +use crate::supervisor::{RestartDecision, RestartPolicy, RESTART_DELAYS_SECS}; use crate::sys; use crate::AppState; use serde::{Deserialize, Serialize}; @@ -22,7 +23,7 @@ use std::io::{BufRead, BufReader, Read, Write}; use std::os::windows::process::CommandExt; use std::path::{Path, PathBuf}; use std::process::{Child, Command, Stdio}; -use std::sync::atomic::{AtomicBool, AtomicU16, Ordering}; +use std::sync::atomic::{AtomicBool, AtomicU16, AtomicU64, Ordering}; use std::sync::{Mutex, MutexGuard}; use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH}; use tauri::{AppHandle, Emitter, Manager}; @@ -49,6 +50,9 @@ const BASE_SOURCES: &[(&str, &str)] = &[ const LOG_LINES: usize = 300; const LOG_MAX_BYTES: u64 = 5 * 1024 * 1024; const START_GRACE: Duration = Duration::from_millis(900); +/// How many times the core is started before the failure is reported. The +/// second attempt exists only for a port lost between the probe and `listen`. +const START_ATTEMPTS: u8 = 2; #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)] #[serde(rename_all = "snake_case")] @@ -96,11 +100,16 @@ pub struct TrafficStats { pub down: u64, } -/// Port of the local Stats API inbound; a provider config may already use it. -static STATS_PORT: AtomicU16 = AtomicU16::new(10085); +/// Port of the local Stats API inbound. Chosen from the OS on every start and +/// `0` until then: a fixed number can fall inside a range Windows reserved for +/// Hyper-V, Docker or WSL, and the core then dies on `bind` with `WSAEACCES`. +static STATS_PORT: AtomicU16 = AtomicU16::new(0); -/// One proxy guard thread at a time. -static PROXY_GUARD: AtomicBool = AtomicBool::new(false); +/// Generation of the proxy guard: every start bumps it, and a guard thread +/// whose number no longer matches retires. Without it a guard that was asleep +/// during a restart woke up, saw a newer flag and kept working next to the new +/// one, so either could switch the other off on exit. +static PROXY_GUARD: AtomicU64 = AtomicU64::new(0); /// One entry of the subscription, as shown in the UI. #[derive(Debug, Clone, Serialize)] @@ -123,6 +132,13 @@ pub struct Xray { /// Last downloaded subscription; kept in memory and on disk so the UI works /// before (and without) a fresh fetch. profiles: Mutex>, + /// The user wants the tunnel up. An unexpected exit is only restarted while + /// this is set; a manual stop clears it. + desired: AtomicBool, + /// One recovery loop at a time: the watcher can notice the same exit twice. + recovering: AtomicBool, + /// Restart budget of the current episode, shared with its recovery loop. + restarts: Mutex, } impl Default for Xray { @@ -158,6 +174,9 @@ impl Xray { logs: Mutex::new(VecDeque::new()), op: Mutex::new(()), profiles: Mutex::new(Vec::new()), + desired: AtomicBool::new(false), + recovering: AtomicBool::new(false), + restarts: Mutex::new(RestartPolicy::new()), } } } @@ -288,6 +307,10 @@ pub fn traffic(app: &AppHandle) -> Result { return Ok(TrafficStats::default()); } let port = STATS_PORT.load(Ordering::Relaxed); + if port == 0 { + // Nothing has been started yet: there is no API to ask. + return Ok(TrafficStats::default()); + } let output = Command::new(binary_path()) .args(["api", "statsquery", &format!("--server=127.0.0.1:{port}")]) .creation_flags(crate::sys::CREATE_NO_WINDOW) @@ -1382,6 +1405,41 @@ fn direct_tag(value: &Value) -> Option { .map(str::to_string) } +/// Ports the config already listens on, so the Stats API never takes one of them. +fn stats_reserved_ports(value: &Value) -> Vec { + value + .get("inbounds") + .and_then(Value::as_array) + .map(|inbounds| { + inbounds + .iter() + .filter_map(|inbound| inbound.get("port").and_then(Value::as_u64)) + .filter_map(|port| u16::try_from(port).ok()) + .collect() + }) + .unwrap_or_default() +} + +/// A free loopback port for the local Stats API. +/// +/// The number comes from the OS, not from this code: Windows reserves whole port +/// ranges for Hyper-V, Docker and WSL (`netsh interface ipv4 show +/// excludedportrange`), and a listener inside such a range fails with +/// `WSAEACCES` — that is what killed the core right after start. An ephemeral +/// bind never lands there, and the probe also rules out a number that another +/// application is already listening on. +fn pick_stats_port(reserved: &[u16]) -> Option { + for _ in 0..8 { + let listener = std::net::TcpListener::bind(("127.0.0.1", 0)).ok()?; + let port = listener.local_addr().ok()?.port(); + drop(listener); + if port >= 1024 && !reserved.contains(&port) { + return Some(port); + } + } + None +} + fn write_config(app: &AppHandle, index: usize) -> Result { let list = ensure_profiles(app)?; let cfg = list @@ -1420,16 +1478,13 @@ fn write_config(app: &AppHandle, index: usize) -> Result { ); } - // Stats API: live counters for the UI. The provider may already define an - // `api` inbound or use the default port, so both are detected and relocated. - let mut stats_port = 10085u16; - if let Some(inbounds) = value.get("inbounds").and_then(Value::as_array) { - while inbounds.iter().any(|inbound| { - inbound.get("port").and_then(Value::as_u64) == Some(u64::from(stats_port)) - }) { - stats_port += 1; - } - } + // Stats API: live counters for the UI. The port is taken from the OS on + // every start, because a fixed number can sit inside a range Windows has + // reserved for Hyper-V/Docker/WSL and the core then cannot bind at all. + let reserved = stats_reserved_ports(&value); + let Some(stats_port) = pick_stats_port(&reserved) else { + return Err("Не удалось подобрать свободный порт для служебного API статистики.".into()); + }; value["stats"] = json!({}); if !value.get("policy").map(Value::is_object).unwrap_or(false) { value["policy"] = json!({}); @@ -1556,8 +1611,45 @@ fn log_tail(app: &AppHandle, count: usize) -> String { // ---------- process ---------- -fn port_available(port: u16) -> bool { - std::net::TcpListener::bind(("127.0.0.1", port)).is_ok() +/// What happened to a loopback port when we tried to take it. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum PortState { + Free, + /// Another socket holds it. + Busy, + /// Windows does not allow the bind at all: the port sits inside a range + /// reserved for Hyper-V, Docker or WSL (`WSAEACCES`, 10013). + Reserved, +} + +fn port_state(port: u16) -> PortState { + match std::net::TcpListener::bind(("127.0.0.1", port)) { + Ok(_) => PortState::Free, + Err(error) => match error.raw_os_error() { + // WSAEACCES: the bind is forbidden by policy, not by a listener. + Some(10013) => PortState::Reserved, + _ => PortState::Busy, + }, + } +} + +/// Readable reason for a port the tunnel cannot have. +fn port_hint(port: u16, state: PortState) -> String { + if state == PortState::Reserved { + return format!( + "Порт {port} недоступен: его зарезервировала Windows (Hyper-V, Docker или WSL). \ + Выберите другой порт в настройках VPN." + ); + } + match port_owner(port) { + // Never kill by image name: another client's xray.exe may be running. + Some(pid) => format!( + "Порт {port} занят программой (PID {pid}). Закройте её или выберите другой порт." + ), + None => { + format!("Порт {port} занят другой программой. Закройте её или выберите другой порт.") + } + } } pub fn validate_port(port: u16) -> Result { @@ -1582,18 +1674,22 @@ fn port_owner(port: u16) -> Option { pub fn start(app: &AppHandle) -> Result<(), String> { let state = app.state::(); let _guard = lock(&state.xray.op); + // A manual start begins a fresh restart budget. + lock(&state.xray.restarts).reset(); start_locked(app) } pub fn stop(app: &AppHandle) -> Result<(), String> { let state = app.state::(); let _guard = lock(&state.xray.op); + lock(&state.xray.restarts).reset(); stop_locked(app) } pub fn restart(app: &AppHandle) -> Result<(), String> { let state = app.state::(); let _guard = lock(&state.xray.op); + lock(&state.xray.restarts).reset(); stop_locked(app)?; std::thread::sleep(Duration::from_millis(300)); start_locked(app) @@ -1609,25 +1705,18 @@ fn start_locked(app: &AppHandle) -> Result<(), String> { return Ok(()); } } - ensure_core_locked(app)?; + ensure_core_locked(app).map_err(|error| fail_start(app, error))?; let cfg = load_cfg(app); - if !port_available(cfg.socks_port) || !port_available(cfg.http_port) { - let busy = if !port_available(cfg.socks_port) { - cfg.socks_port + let socks = port_state(cfg.socks_port); + let http = port_state(cfg.http_port); + if socks != PortState::Free || http != PortState::Free { + let (busy, state) = if socks != PortState::Free { + (cfg.socks_port, socks) } else { - cfg.http_port + (cfg.http_port, http) }; - // Never kill by image name: another client's xray.exe may be running. - let owner = port_owner(busy) - .map(|pid| format!(" (PID {pid})")) - .unwrap_or_default(); - return Err(mark_failed( - app, - format!( - "Порт {busy} занят{owner}. Закройте программу, которая его держит, или выберите другой порт." - ), - )); + return Err(fail_start(app, port_hint(busy, state))); } set_status(app, |s| { @@ -1636,11 +1725,57 @@ fn start_locked(app: &AppHandle) -> Result<(), String> { s.last_error = None; }); - let config_file = write_config(app, cfg.selected)?; - let binary = binary_path(); - let mut cmd = Command::new(&binary); + // The OS reservation can change between the probe above and the core's own + // `listen`, and another process can grab the Stats port in that window. One + // retry with a freshly chosen number is cheaper than sending the user to + // look at ports, so the start is attempted twice. + let mut message = String::new(); + for attempt in 1..=START_ATTEMPTS { + let config_file = match write_config(app, cfg.selected) { + Ok(path) => path, + Err(error) => return Err(fail_start(app, error)), + }; + let (pid, exited) = match launch_core(app, &config_file) { + Ok(result) => result, + Err(error) => return Err(fail_start(app, error)), + }; + let Some(reason) = exited else { + finish_start(app, &cfg, pid); + return Ok(()); + }; + // The stderr reader is another thread: give it a moment so a bind error + // is in the tail before the retry decision is made. + std::thread::sleep(Duration::from_millis(150)); + let tail = log_tail(app, 3); + message = if tail.is_empty() { + format!("Xray завершился сразу после запуска ({reason}).") + } else { + format!("Xray завершился сразу после запуска ({reason}). {tail}") + }; + if attempt < START_ATTEMPTS && mentions_bind_failure(&tail) { + push_log(app, format!("Служебный порт занят — пробую другой. {tail}")); + continue; + } + break; + } + Err(fail_start(app, message)) +} + +/// Reports a start failure: status for the page, line for the log on disk. +fn fail_start(app: &AppHandle, message: impl Into) -> String { + let message = message.into(); + push_log(app, message.clone()); + mark_failed(app, message) +} + +/// Starts the core on an already written config and waits out the grace period. +/// +/// Returns the pid and the exit reason when the process died within +/// [`START_GRACE`]: that is how a bind failure or a broken config looks. +fn launch_core(app: &AppHandle, config_file: &Path) -> Result<(u32, Option), String> { + let mut cmd = Command::new(binary_path()); cmd.arg("-c") - .arg(&config_file) + .arg(config_file) .current_dir(dir()) // Xray looks for geosite.dat next to its executable; pin the resource // directory to ours so a moved binary still finds the lists. @@ -1650,10 +1785,9 @@ fn start_locked(app: &AppHandle) -> Result<(), String> { .stderr(Stdio::piped()); sys::hidden(&mut cmd); - let mut child = match cmd.spawn() { - Ok(child) => child, - Err(e) => return Err(mark_failed(app, format!("Не удалось запустить Xray: {e}"))), - }; + let mut child = cmd + .spawn() + .map_err(|e| format!("Не удалось запустить Xray: {e}"))?; let pid = child.id(); crate::job::adopt(&child); if let Some(stdout) = child.stdout.take() { @@ -1679,18 +1813,22 @@ fn start_locked(app: &AppHandle) -> Result<(), String> { None => Some("процесс не запустился".to_string()), } }; - if let Some(reason) = exited { + if exited.is_some() { let state = app.state::(); *lock(&state.xray.child) = None; - let tail = log_tail(app, 3); - let message = if tail.is_empty() { - format!("Xray завершился сразу после запуска ({reason}).") - } else { - format!("Xray завершился сразу после запуска ({reason}). {tail}") - }; - return Err(mark_failed(app, message)); } + Ok((pid, exited)) +} +/// Whether a short exit looks like a port the core was not allowed to bind. +fn mentions_bind_failure(tail: &str) -> bool { + tail.contains("failed to listen") || tail.contains("bind:") +} + +/// Marks a successful start: status, remembered session, system proxy, watcher. +fn finish_start(app: &AppHandle, cfg: &config::XrayConfig, pid: u32) { + let state = app.state::(); + state.xray.desired.store(true, Ordering::SeqCst); set_status(app, |s| { s.state = XrayState::Running; s.pid = Some(pid); @@ -1706,7 +1844,6 @@ fn start_locked(app: &AppHandle) -> Result<(), String> { } spawn_watcher(app.clone(), pid); spawn_proxy_guard(app.clone()); - Ok(()) } fn spawn_watcher(app: AppHandle, pid: u32) { @@ -1736,30 +1873,162 @@ fn spawn_watcher(app: AppHandle, pid: u32) { format!("Xray завершился неожиданно ({code}). {tail}") }; // The inbound is gone: a leftover WinINET proxy would break every browser. - PROXY_GUARD.store(false, Ordering::SeqCst); + // Bumping the generation retires the guard of this tunnel. + PROXY_GUARD.fetch_add(1, Ordering::SeqCst); restore_system_proxy(); set_status(&app, |s| s.system_proxy = false); - mark_failed(&app, message); + push_log(&app, format!("supervisor: {message}")); + recover(&app, &message); return; }); } +/// Brings the core back after an unexpected exit, following [`RestartPolicy`]. +/// +/// A VPN that stays silently off until the user notices is the worst failure +/// mode of this app, so the watcher does not stop at "failed": it waits out the +/// backoff and starts the core again. A manual stop, the end of the Windows +/// session or a spent budget ends the loop. +fn recover(app: &AppHandle, reason: &str) { + let state = app.state::(); + // Another watch may be finishing its own recovery right now; its last act is + // clearing the flag, so a death seen in that gap must not be dropped: wait + // for the slot to free up and take over. + if !take_recovery(&state) { + return; + } + + let mut cause = reason.to_string(); + loop { + if sys::session_ending() { + mark_failed( + app, + "Windows завершает сеанс — VPN запустится при следующем входе.", + ); + break; + } + // A manual stop during the backoff cancels the restart. + if !state.xray.desired.load(Ordering::SeqCst) { + break; + } + + // The guard is dropped before the decision is acted upon: `give_up` + // and `start_locked` touch other locks. + let decision = lock(&state.xray.restarts).next_attempt(now_secs()); + let (attempt, delay) = match decision { + RestartDecision::GiveUp { attempts } => { + give_up(app, attempts, &cause); + break; + } + RestartDecision::Retry { attempt, delay } => (attempt, delay), + }; + + mark_restarting(app, attempt, delay, &cause); + if !sleep_unless_stopped(app, delay) { + break; + } + + let running = { + let _guard = lock(&state.xray.op); + if !state.xray.desired.load(Ordering::SeqCst) || lock(&state.xray.child).is_some() { + break; + } + match start_locked(app) { + Ok(()) => true, + Err(error) => { + cause = error; + false + } + } + }; + + if running { + push_log( + app, + format!( + "supervisor: VPN снова работает (попытка {attempt} из {}).", + RESTART_DELAYS_SECS.len() + ), + ); + break; + } + } + + state.xray.recovering.store(false, Ordering::SeqCst); +} + +/// Status while the supervisor waits out the backoff: `starting`, never +/// `error`, so the page shows a reconnect instead of a dead switch. +fn mark_restarting(app: &AppHandle, attempt: u32, delay: Duration, cause: &str) { + let message = format!( + "VPN перезапускается (попытка {attempt} из {}), через {} с. {cause}", + RESTART_DELAYS_SECS.len(), + delay.as_secs() + ); + set_status(app, |s| { + s.state = XrayState::Starting; + s.pid = None; + s.last_error = Some(message.clone()); + }); + push_log(app, format!("supervisor: {message}")); +} + +/// The budget is spent: say why in plain language and stop trying. +/// +/// `session_running` stays set on purpose: the port reservation may change +/// after a reboot, so the next app start still tries to bring the tunnel back. +fn give_up(app: &AppHandle, attempts: u32, cause: &str) { + let state = app.state::(); + state.xray.desired.store(false, Ordering::SeqCst); + let message = format!("VPN не запускается после {attempts} попыток. {cause}"); + push_log(app, format!("supervisor: {message}")); + mark_failed(app, message); +} + +/// Waits `delay` in small steps so a manual stop during the backoff is instant. +fn sleep_unless_stopped(app: &AppHandle, delay: Duration) -> bool { + let state = app.state::(); + let step = Duration::from_millis(200); + let mut left = delay; + while left > Duration::ZERO { + let slice = left.min(step); + std::thread::sleep(slice); + left -= slice; + if !state.xray.desired.load(Ordering::SeqCst) || sys::session_ending() { + return false; + } + } + true +} + +/// Takes the single recovery slot, waiting out a loop that is just finishing. +fn take_recovery(state: &AppState) -> bool { + for _ in 0..20 { + if !state.xray.recovering.swap(true, Ordering::SeqCst) { + return true; + } + std::thread::sleep(Duration::from_millis(100)); + } + false +} + /// Keeps the Windows system proxy pointed at us while the tunnel is up. /// /// Some tools rewrite the WinINET settings on boot or on update; the browsers /// then silently stop using the tunnel. The guard re-applies our proxy once per /// half a minute and gives up after three failures in a row. fn spawn_proxy_guard(app: AppHandle) { - if PROXY_GUARD.swap(true, Ordering::SeqCst) { - return; - } + // A new guard retires the previous one: it checks its generation after + // every sleep and leaves without touching the shared value. + let generation = PROXY_GUARD.fetch_add(1, Ordering::SeqCst) + 1; std::thread::spawn(move || { + let alive = || PROXY_GUARD.load(Ordering::SeqCst) == generation; let mut failures = 0u8; let mut restored = false; - while PROXY_GUARD.load(Ordering::SeqCst) { + while alive() { std::thread::sleep(Duration::from_secs(30)); - if !PROXY_GUARD.load(Ordering::SeqCst) { - break; + if !alive() { + return; } let running = { let state = app.state::(); @@ -1788,20 +2057,24 @@ fn spawn_proxy_guard(app: AppHandle) { } failures += 1; if failures >= 3 { - PROXY_GUARD.store(false, Ordering::SeqCst); let _ = app.emit( "proxy-guard", "Системный прокси снят другим приложением. Включите «VPN для сайтов» заново.", ); - break; + return; } } - PROXY_GUARD.store(false, Ordering::SeqCst); }); } fn stop_locked(app: &AppHandle) -> Result<(), String> { - PROXY_GUARD.store(false, Ordering::SeqCst); + // Retire the guard: a pending re-apply must not outlive the tunnel. + PROXY_GUARD.fetch_add(1, Ordering::SeqCst); + { + // The user asked for "off": a pending recovery must not bring it back. + let state = app.state::(); + state.xray.desired.store(false, Ordering::SeqCst); + } let _ = save_cfg(app, |cfg| cfg.session_running = false); let taken = { let state = app.state::(); @@ -1849,6 +2122,7 @@ pub fn stop_on_exit(app: &AppHandle) { restore_system_proxy(); let taken = { let state = app.state::(); + state.xray.desired.store(false, Ordering::SeqCst); let mut guard = lock(&state.xray.child); guard.take() }; @@ -2585,4 +2859,32 @@ mod tests { assert!(validate_port(17808).is_ok()); assert!(validate_port(80).is_err()); } + + #[test] + fn stats_port_is_taken_from_the_os() { + // The OS does not hand out a port that is already listened on, so this + // test really covers the exclusion list: a number reserved by the + // caller (SOCKS/HTTP/inbound) must come back different. + let listener = std::net::TcpListener::bind(("127.0.0.1", 0)).expect("listener"); + let busy = listener.local_addr().expect("addr").port(); + let port = pick_stats_port(&[busy, 17808, 17809]).expect("свободный порт"); + assert_ne!(port, busy); + assert!(![17808u16, 17809].contains(&port)); + assert!( + std::net::TcpListener::bind(("127.0.0.1", port)).is_ok(), + "выбранный порт должен приниматься системой" + ); + } + + #[test] + fn reserved_ports_come_from_the_inbounds() { + let value = json!({ + "inbounds": [ + { "port": 17808, "protocol": "socks" }, + { "port": 17809, "protocol": "http" }, + { "port": 443, "protocol": "vless" } + ] + }); + assert_eq!(stats_reserved_ports(&value), vec![17808, 17809, 443]); + } } From cfc117d68912c582f006d247ebf014c91af72772 Mon Sep 17 00:00:00 2001 From: Ilya Speranskiy Date: Wed, 25 Nov 2026 10:14:22 +0300 Subject: [PATCH 2/3] chore(release): 2.3.2 --- package-lock.json | 4 ++-- package.json | 2 +- src-tauri/Cargo.lock | 2 +- src-tauri/Cargo.toml | 2 +- src-tauri/tauri.conf.json | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/package-lock.json b/package-lock.json index 514d3bc..a23b0fb 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "splitline", - "version": "2.3.1", + "version": "2.3.2", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "splitline", - "version": "2.3.1", + "version": "2.3.2", "license": "MIT", "dependencies": { "@fontsource-variable/inter": "^5.3.0", diff --git a/package.json b/package.json index 4d05d44..c023072 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "splitline", "private": true, - "version": "2.3.1", + "version": "2.3.2", "description": "Windows 10/11 GUI manager for flowseal-zapret (zapret-discord-youtube by Flowseal)", "author": "Dezuron", "license": "MIT", diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 0034a5d..5aa5cf4 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -5477,7 +5477,7 @@ dependencies = [ [[package]] name = "zapret-ui" -version = "2.3.1" +version = "2.3.2" dependencies = [ "base64 0.22.1", "dirs 5.0.1", diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 815918d..79a5d73 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "zapret-ui" -version = "2.3.1" +version = "2.3.2" description = "Windows 10/11 GUI manager for flowseal-zapret (zapret-discord-youtube by Flowseal)" authors = ["Dezuron"] license = "MIT" diff --git a/src-tauri/tauri.conf.json b/src-tauri/tauri.conf.json index c7026ab..4bf4e2c 100644 --- a/src-tauri/tauri.conf.json +++ b/src-tauri/tauri.conf.json @@ -1,7 +1,7 @@ { "$schema": "https://schema.tauri.app/config/2", "productName": "Splitline", - "version": "2.3.1", + "version": "2.3.2", "identifier": "com.zapret.ui", "mainBinaryName": "Splitline", "build": { From 53c8121c73b8110fed35c6de08f3f06ff6529137 Mon Sep 17 00:00:00 2001 From: Ilya Speranskiy Date: Wed, 25 Nov 2026 10:22:27 +0300 Subject: [PATCH 3/3] chore(deps): bump cookie and url in the lockfile RUSTSEC-2026-0594 and RUSTSEC-2026-0592 were published after the last release, so `cargo audit` turned the 2.3.2 pull request red without any dependency change on our side. Both fixes are patch releases: - cookie 0.18.2 -> 0.18.3 - url 2.5.8 -> 2.5.9 (with form_urlencoded 1.2.2 -> 1.2.3) --- src-tauri/Cargo.lock | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 5aa5cf4..7f62b70 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -559,9 +559,9 @@ dependencies = [ [[package]] name = "cookie" -version = "0.18.2" +version = "0.18.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1a373e3602691c3cdea496d2f0ee5935151e6168fe87739483c463db1b2f2f87" +checksum = "d55f2d6c30eeec7d5c6f98bd4395d4abb1895629eab97ee6522d340623615f8a" dependencies = [ "time", "version_check", @@ -701,7 +701,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d045de693cb712d0b22c6a64be5b953f67b3ce00ab5ad3dd5d8b441886ab8e1a" dependencies = [ "quote", - "syn 3.0.10", + "syn 2.0.119", ] [[package]] @@ -1147,9 +1147,9 @@ checksum = "aa9a19cbb55df58761df49b23516a86d432839add4af60fc256da840f66ed35b" [[package]] name = "form_urlencoded" -version = "1.2.2" +version = "1.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cb4cb245038516f5f85277875cdaa4f7d2c9a0fa0468de06ed190163b1581fcf" +checksum = "afa489199eaffb1211795df1c6236ce1ad0d41a6fa45250add73e3b02420f06e" dependencies = [ "percent-encoding", ] @@ -4644,9 +4644,9 @@ checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1" [[package]] name = "url" -version = "2.5.8" +version = "2.5.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ff67a8a4397373c3ef660812acab3268222035010ab8680ec4215f38ba3d0eed" +checksum = "f00cd2407bb42c0ef17be88d4cf020f5cceffd121665bac9b8b9493a369b3b1a" dependencies = [ "form_urlencoded", "idna",