@@ -33,6 +33,7 @@ const SSH_GUEST_DIR: &str = "/root/.vibebox";
33const DEFAULT_SSH_USER: &str = "vibebox";
33const DEFAULT_SSH_USER: &str = "vibebox";
34const SSH_CONNECT_RETRIES: usize = 20;
34const SSH_CONNECT_RETRIES: usize = 20;
35const SSH_CONNECT_DELAY_MS: u64 = 500;
35const SSH_CONNECT_DELAY_MS: u64 = 500;
36+const SSH_SETUP_SCRIPT: &str = include_str!("ssh.sh");
37#[derive(Debug, Clone, Serialize, Deserialize)]
38#[derive(Debug, Clone, Serialize, Deserialize)]
38struct InstanceConfig {
39struct InstanceConfig {
@@ -61,7 +62,12 @@ pub fn run_with_ssh(
61 let instance_dir = ensure_instance_dir(&project_root)?;
62 let instance_dir = ensure_instance_dir(&project_root)?;
62 let (ssh_key, _ssh_pub) = ensure_ssh_keypair(&instance_dir)?;
63 let (ssh_key, _ssh_pub) = ensure_ssh_keypair(&instance_dir)?;
64− let config = load_or_create_instance_config(&instance_dir)?;
65+ let mut config = load_or_create_instance_config(&instance_dir)?;
66+ // Clear cached IP to avoid reusing a stale address on startup.
67+ if config.vm_ipv4.is_some() {
68+ config.vm_ipv4 = None;
69+ write_instance_config(&instance_dir.join(INSTANCE_TOML), &config)?;
65 let config = Arc::new(Mutex::new(config));
71 let config = Arc::new(Mutex::new(config));
67 let extra_shares = vec![DirectoryShare::new(
73 let extra_shares = vec![DirectoryShare::new(
@@ -244,40 +250,15 @@ fn build_ssh_login_actions(
245 let key_path = format!("{guest_dir}/{key_name}.pub");
251 let key_path = format!("{guest_dir}/{key_name}.pub");
247− let mask_cache = format!(
253+ let setup_script = SSH_SETUP_SCRIPT
248− "if [ -d /root/{project_name}/.vibebox ]; then mount -t tmpfs tmpfs /root/{project_name}/.vibebox; fi"
254+ .replace("__SSH_USER__", &ssh_user)
249− );
255+ .replace("__SUDO_PASSWORD__", &sudo_password)
256+ .replace("__PROJECT_NAME__", project_name)
257+ .replace("__KEY_PATH__", &key_path);
258+ let setup = vm::script_command_from_content("ssh_setup", &setup_script)
259+ .expect("ssh setup script contained invalid marker");
252− "if ! command -v sshd >/dev/null 2>&1; then apt-get update && apt-get install -y openssh-server sudo; fi",
253− "systemctl enable ssh >/dev/null 2>&1 || true",
254− &format!("id -u {ssh_user} >/dev/null 2>&1 || useradd -m -s /bin/bash {ssh_user}"),
255− &format!("echo \"{ssh_user}:{sudo_password}\" | chpasswd"),
256− &format!("usermod -aG sudo {ssh_user}"),
257− &format!("install -d -m 700 /home/{ssh_user}/.ssh"),
258− &format!("install -m 600 {key_path} /home/{ssh_user}/.ssh/authorized_keys"),
259− &format!("chown -R {ssh_user}:{ssh_user} /home/{ssh_user}/.ssh"),
260− "mkdir -p /etc/ssh/sshd_config.d",
261− "cat >/etc/ssh/sshd_config.d/vibebox.conf <<'VIBEBOX_SSHD'",
262− "PasswordAuthentication no",
263− "KbdInteractiveAuthentication no",
264− "ChallengeResponseAuthentication no",
265− "PubkeyAuthentication yes",
266− "PermitRootLogin no",
267− &format!("AllowUsers {ssh_user}"),
269− "systemctl restart ssh",
270− "echo VIBEBOX_SSH_READY",
272− let setup = setup_lines.join("\n");
274− let ip_probe = "while true; do ip=$(ip -4 -o addr show scope global | awk '{print $4}' | cut -d/ -f1 | head -n 1); if [ -n \"$ip\" ]; then echo VIBEBOX_IPV4=$ip; break; fi; sleep 1; done";
277− LoginAction::Send(mask_cache),
278− LoginAction::Send(setup),
261+ vec![LoginAction::Send(setup)]
279− LoginAction::Send(ip_probe.to_string()),
283fn spawn_ssh_io(
264fn spawn_ssh_io(