Skip to content

YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context

fix(mdns): make LAN device discovery opt-in (off by default) (#3937)

Louis Beaumont17b6f8dc6b83
diff --git a/.github/pr-media/mdns_optin_flow.png b/.github/pr-media/mdns_optin_flow.png
new file mode 100644
index 000000000..a96c2ebd1
Binary files /dev/null and b/.github/pr-media/mdns_optin_flow.png differ
diff --git a/apps/screenpipe-app-tauri/src-tauri/src/server_core.rs b/apps/screenpipe-app-tauri/src-tauri/src/server_core.rs
index 65da17403..dae956041 100644
--- a/apps/screenpipe-app-tauri/src-tauri/src/server_core.rs
+++ b/apps/screenpipe-app-tauri/src-tauri/src/server_core.rs
@@ -602,7 +602,13 @@ impl ServerCore {
info!("Server core started successfully");
crate::health::set_boot_phase("ready", None);
- // mDNS
+ // mDNS LAN discovery is opt-in (off by default) so the app doesn't
+ // trigger the macOS "Local Network" permission prompt unless the user
+ // wants multi-device discovery. Enable with SCREENPIPE_ENABLE_MDNS=1.
+ let mdns_enabled = std::env::var("SCREENPIPE_ENABLE_MDNS")
+ .map(|v| matches!(v.trim(), "1" | "true" | "TRUE"))
+ .unwrap_or(false);
+ screenpipe_connect::mdns::set_enabled(mdns_enabled);
if let Err(e) = screenpipe_connect::mdns::advertise(config.port) {
warn!("mdns advertisement failed (non-fatal): {}", e);
}
diff --git a/crates/screenpipe-connect/src/mdns.rs b/crates/screenpipe-connect/src/mdns.rs
index 5168eaa09..fa31ffbf7 100644
--- a/crates/screenpipe-connect/src/mdns.rs
+++ b/crates/screenpipe-connect/src/mdns.rs
@@ -11,6 +11,7 @@
//! screenpipe continues to work. Discovery falls back to SSH/Tailscale.
use mdns_sd::{ServiceDaemon, ServiceEvent, ServiceInfo};
+use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::OnceLock;
use std::time::Duration;
use tracing::{debug, info, warn};
@@ -38,6 +39,25 @@ const BROWSE_TIMEOUT: Duration = Duration::from_secs(3);
/// service stays advertised. Dropping the daemon would unregister the service.
static DAEMON: OnceLock<Option<ServiceDaemon>> = OnceLock::new();
+/// Whether mDNS LAN peer discovery is enabled. Off by default: both advertising
+/// and browsing open a multicast socket, which makes macOS prompt for "Local
+/// Network" access. Discovery is opt-in (CLI `--enable-mdns` /
+/// `SCREENPIPE_ENABLE_MDNS`), so screenpipe stays silent on the network unless
+/// the user wants multi-device discovery / remote sync.
+static ENABLED: AtomicBool = AtomicBool::new(false);
+
+/// Enable or disable mDNS advertise + browse. Call once at startup from config.
+/// While disabled, `advertise` and `browse` are no-ops that never touch the
+/// network (no daemon, no multicast, no permission prompt).
+pub fn set_enabled(enabled: bool) {
+ ENABLED.store(enabled, Ordering::Relaxed);
+}
+
+/// Whether mDNS discovery is currently enabled.
+pub fn is_enabled() -> bool {
+ ENABLED.load(Ordering::Relaxed)
+}
+
fn get_or_init_daemon() -> Option<&'static ServiceDaemon> {
DAEMON
.get_or_init(|| match ServiceDaemon::new() {
@@ -55,6 +75,11 @@ fn get_or_init_daemon() -> Option<&'static ServiceDaemon> {
/// Call once at startup. The advertisement persists until the process exits.
/// Returns `Ok(())` on success or silently succeeds if mDNS is unavailable.
pub fn advertise(port: u16) -> Result<(), String> {
+ if !is_enabled() {
+ debug!("mdns: discovery disabled, not advertising");
+ return Ok(());
+ }
+
let daemon = match get_or_init_daemon() {
Some(d) => d,
None => return Ok(()), // mDNS unavailable, skip silently
@@ -108,6 +133,9 @@ pub fn advertise(port: u16) -> Result<(), String> {
/// Returns a list of `(host, port)` pairs. Best-effort: returns an empty
/// vec if mDNS is unavailable or no instances are found within the timeout.
pub async fn browse() -> Vec<(String, u16)> {
+ if !is_enabled() {
+ return Vec::new();
+ }
// Run the blocking browse on a dedicated thread
tokio::task::spawn_blocking(browse_blocking)
.await
diff --git a/crates/screenpipe-engine/src/bin/screenpipe-engine.rs b/crates/screenpipe-engine/src/bin/screenpipe-engine.rs
index add456bab..eb8cc9d41 100644
--- a/crates/screenpipe-engine/src/bin/screenpipe-engine.rs
+++ b/crates/screenpipe-engine/src/bin/screenpipe-engine.rs
@@ -451,6 +451,10 @@ async fn main() -> anyhow::Result<()> {
.into_recording_config(local_data_dir.clone(), &record_arg_sources)
.await?;
+ // mDNS LAN discovery is opt-in (off by default) so we don't trigger the
+ // macOS "Local Network" permission prompt unless the user wants it.
+ screenpipe_connect::mdns::set_enabled(record_args.enable_mdns);
+
// Store the guard in a variable that lives for the entire main function
let _log_guard = Some(setup_logging(
&local_data_dir,
diff --git a/crates/screenpipe-engine/src/cli/mod.rs b/crates/screenpipe-engine/src/cli/mod.rs
index 7c0032132..5a1ce3542 100644
--- a/crates/screenpipe-engine/src/cli/mod.rs
+++ b/crates/screenpipe-engine/src/cli/mod.rs
@@ -567,6 +567,12 @@ pub struct RecordArgs {
#[arg(long, default_value_t = false)]
pub enable_sync: bool,
+ /// Enable mDNS LAN discovery (advertise this instance + browse for peers).
+ /// Off by default: it opens a multicast socket, which triggers the macOS
+ /// "Local Network" permission prompt. Opt in for multi-device sync.
+ #[arg(long, env = "SCREENPIPE_ENABLE_MDNS", default_value_t = false)]
+ pub enable_mdns: bool,
+
/// API token for cloud sync
#[arg(long, env = "SCREENPIPE_SYNC_TOKEN")]
pub sync_token: Option<String>,