Skip to content

armgit / muxin

Public Imported from GitHub
0

All your machines, one app. For you and your AI agents: agent sessions, desktops, files, logs and GitHub across your Macs, Linux and Windows machines, with an MCP server.

Windows: narrow an older pairing's firewall rule when the agent updates

Adib Hanna64c05909adaf
diff --git a/agent/src/direct.rs b/agent/src/direct.rs
index 6bd99cf..b99f993 100644
--- a/agent/src/direct.rs
+++ b/agent/src/direct.rs
@@ -387,6 +387,7 @@ fn is_tailnet(ip: std::net::IpAddr) -> bool {
/// is brought up to date.
pub fn migrate() -> i32 {
if cfg!(windows) {
+ narrow_firewall_rule();
return 0;
}
let home = crate::sys::user_home();
@@ -467,6 +468,24 @@ fn systemd_unit() -> String {
)
}
+/// Before 0.5.7 the firewall rule let the agent in from any address: one made
+/// then is narrowed to FIREWALL_REMOTE. Only where pairing made one; changing
+/// it takes an administrator, as making it did, and otherwise it stays as it was.
+#[cfg(windows)]
+fn narrow_firewall_rule() {
+ if !home_dir().join("direct").is_dir() {
+ return;
+ }
+ let _ = crate::win::ps(&format!(
+ "try {{ Get-NetFirewallRule -DisplayName {task} -ErrorAction Stop | Where-Object {{ ($_ | Get-NetFirewallAddressFilter).RemoteAddress -contains 'Any' }} | Set-NetFirewallRule -RemoteAddress {remote} -ErrorAction Stop }} catch {{ }}",
+ task = crate::win::quote(TASK),
+ remote = FIREWALL_REMOTE
+ ));
+}
+
+#[cfg(not(windows))]
+fn narrow_firewall_rule() {}
+
/// Brings an installed systemd unit up to date (without restarting it), so the
/// restart after an update already leaves sessions alone.
fn refresh_unit() {
diff --git a/src/fleet/sessions/agent.rs b/src/fleet/sessions/agent.rs
index 4efc9e2..1446b3c 100644
--- a/src/fleet/sessions/agent.rs
+++ b/src/fleet/sessions/agent.rs
@@ -221,7 +221,8 @@ async fn upload(spec: &MachineSpec, bytes: Vec<u8>) -> Result<String, String> {
/// Puts an uploaded build (`muxen-agent.upload.exe`) in place on Windows and
/// prints its hash. A running agent holds its file, but Windows lets it be
/// renamed: the old one moves aside and goes once nothing runs it; the
-/// desktop helper, running the old build, goes now.
+/// desktop helper, running the old build, goes now. Then `migrate` brings up
+/// to date what an older build set up (its firewall rule).
const SWAP_PS1: &str = "$ErrorActionPreference = 'Stop'\n\
$d = Join-Path $env:USERPROFILE '.muxen\\bin'\n\
$t = Join-Path $d 'muxen-agent.exe'\n\
@@ -229,6 +230,7 @@ const SWAP_PS1: &str = "$ErrorActionPreference = 'Stop'\n\
Move-Item -Force (Join-Path $d 'muxen-agent.upload.exe') $t\n\
Get-CimInstance Win32_Process -Filter \"Name='muxen-agent.exe'\" | Where-Object { $_.CommandLine -match ' gui-serve' } | ForEach-Object { Stop-Process -Id $_.ProcessId -Force -ErrorAction SilentlyContinue }\n\
Get-ChildItem $d -Filter 'muxen-agent.old.*.exe' | ForEach-Object { Remove-Item -Force $_.FullName -ErrorAction SilentlyContinue }\n\
+ & $t migrate | Out-Null\n\
& $t hash\n";
/// Copies the build into place on Windows and prints its hash: over sftp