windows: install into Program Files, seed components, log as a service

install copies the release into %ProgramFiles%\ProxySuite (upgrading in
place when the service exists), registers it there, and puts it on the
system PATH: a copy in Downloads could be replaced by any user, and the
service runs it as SYSTEM. uninstall removes the service, the PATH
entry and the program files, and leaves the state.

At every start the service copies the release's components into bin\
when they are newer, staged and verified file by file, and only from a
service running in Program Files. Under the Service Control Manager it
logs to logs\service.log, reports StartPending until the API listens,
and a failed start as a service-specific error.

zapret refuses to start while Flowseal's, the win bundle's or
GoodbyeDPI's service holds WinDivert, and says which to remove.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
faumarayandClaude Opus 5.5 committed 2026-10-27 00:10:26 +03:00
1 parent d1bce9db94
commit 28cc7b2cbe
5 files changed
+338 -36

No files matched your search

+39 -7
View File
@@ -68,10 +68,23 @@ fn address(arg: Option<String>) -> Address {
}
}
/// Everything the service does, until `shutdown` resolves.
pub async fn run(state_dir: Option<PathBuf>, socket: Option<String>, shutdown: impl std::future::Future<Output = ()>) -> Result<()> {
/// Everything the service does, until `shutdown` resolves. `ready` is called once the API
/// listens.
pub async fn run(
state_dir: Option<PathBuf>,
socket: Option<String>,
ready: impl FnOnce(),
shutdown: impl std::future::Future<Output = ()>,
) -> Result<()> {
let _ = rustls::crypto::ring::default_provider().install_default();
let paths = state_dir.map(paths::Paths::new).unwrap_or_else(paths::Paths::platform_default);
paths.create_all().with_context(|| format!("creating {}", paths.root.display()))?;
// The components the release carries, next to the program, into bin\ when they are newer.
if let Some(dir) = install::bundled_components_dir() {
for note in install::seed(&dir, &paths.bin()) {
tracing::info!("{note}");
}
}
let svc = service::Service::new(paths.clone()).with_context(|| format!("opening {}", paths.root.display()))?;
let interactive = svc.cfg().user_control.enable;
let shared = Arc::new(Mutex::new(svc));
@@ -79,6 +92,7 @@ pub async fn run(state_dir: Option<PathBuf>, socket: Option<String>, shutdown: i
let addr = address(socket);
let server = Server::bind(&addr, interactive).with_context(|| format!("listening on {addr:?}"))?;
tracing::info!("listening on {addr:?}, state in {}", paths.root.display());
ready();
if let Err(e) = shared.lock().await.apply().await {
tracing::warn!("not starting the proxy yet: {e}");
@@ -109,12 +123,30 @@ pub async fn run(state_dir: Option<PathBuf>, socket: Option<String>, shutdown: i
Ok(())
}
/// `logs\service.log`, started over (one old one kept) past 5 MiB.
fn service_log(dir: &std::path::Path) -> std::io::Result<std::fs::File> {
let path = dir.join("service.log");
if std::fs::metadata(&path).is_ok_and(|m| m.len() > 5 * 1024 * 1024) {
let _ = std::fs::rename(&path, dir.join("service.log.1"));
}
std::fs::OpenOptions::new().create(true).append(true).open(path)
}
fn main() -> Result<()> {
let args = Args::parse();
tracing_subscriber::fmt()
.with_env_filter(tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else(|_| "info".into()))
.with_writer(std::io::stderr)
.init();
let filter = || tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else(|_| "info".into());
let as_service = cfg!(windows) && !args.console && args.command.is_none();
if as_service {
// Under the Service Control Manager nothing reads stderr: the log is a file.
let paths = args.state_dir.clone().map(paths::Paths::new).unwrap_or_else(paths::Paths::platform_default);
let file = paths.create_all().ok().and_then(|()| service_log(&paths.logs()).ok());
match file {
Some(f) => tracing_subscriber::fmt().with_env_filter(filter()).with_ansi(false).with_writer(std::sync::Mutex::new(f)).init(),
None => tracing_subscriber::fmt().with_env_filter(filter()).with_writer(std::io::stderr).init(),
}
} else {
tracing_subscriber::fmt().with_env_filter(filter()).with_writer(std::io::stderr).init();
}
if let Some(Command::StageComponents { to, cache, only }) = &args.command {
let _ = rustls::crypto::ring::default_provider().install_default();
let rt = tokio::runtime::Runtime::new()?;
@@ -132,7 +164,7 @@ fn main() -> Result<()> {
}
if args.console || !cfg!(windows) {
let rt = tokio::runtime::Runtime::new()?;
return rt.block_on(run(args.state_dir, args.socket, async {
return rt.block_on(run(args.state_dir, args.socket, || {}, async {
let _ = tokio::signal::ctrl_c().await;
}));
}