false, 'error' => 'Method not allowed']); exit; } $host1 = trim($_POST['host1'] ?? ''); $host2 = trim($_POST['host2'] ?? ''); $mySlot = trim($_POST['my_slot'] ?? 'host1'); // 'host1', 'host2', 'host3', ... $myHostname = trim($_POST['my_hostname'] ?? ''); if (empty($host1)) { echo json_encode(['ok' => false, 'error' => 'HOST1 hostname is required']); exit; } // Validate slot — must be host\d+ if (!preg_match('/^host\d+$/', $mySlot)) { echo json_encode(['ok' => false, 'error' => 'Invalid slot']); exit; } // ── Write HOST1 / HOST2 into master.conf ───────────────────────────────────────────────────── $master = vv_read_conf_raw('master.conf'); if ($master === '') { echo json_encode(['ok' => false, 'error' => 'master.conf not found — check SCRIPTS_DIR in varaverk.cfg']); exit; } // Replace HOST1 and HOST2 lines (preserving indentation) $master = preg_replace( '/^(\s*HOST1\s*=\s*).*$/m', '${1}"' . addslashes($host1) . '"', $master ); $master = preg_replace( '/^(\s*HOST2\s*=\s*).*$/m', '${1}"' . addslashes($host2) . '"', $master ); // For partner slots beyond HOST2 — ensure the slot line exists in master.conf $slotNum = (int) preg_replace('/\D/', '', $mySlot); if ($slotNum > 2 && !empty($myHostname)) { $hostKey = 'HOST' . $slotNum; if (!preg_match('/^\s*' . $hostKey . '\s*=/m', $master)) { // Append after HOST2 line $master = preg_replace( '/^(\s*HOST2\s*=.*$)/m', '$1' . "\n {$hostKey}=\"" . addslashes($myHostname) . '"', $master ); } else { $master = preg_replace( '/^(\s*' . $hostKey . '\s*=\s*).*$/m', '${1}"' . addslashes($myHostname) . '"', $master ); } } if (!vv_write_conf_raw('master.conf', $master)) { echo json_encode(['ok' => false, 'error' => 'Failed to write master.conf']); exit; } // ── Create host*.conf from template ────────────────────────────────────────────────────────── $hostId = strtoupper($mySlot); // HOST1, HOST2, HOST3 ... $hostIdLow = strtolower($mySlot); // host1, host2, host3 ... $confFile = $hostIdLow . '.conf'; // Only create if it doesn't already exist — never overwrite an existing conf if (!file_exists(CONF_DIR . '/' . $confFile)) { $template = @file_get_contents(CONF_DIR . '/host.conf.template'); if ($template === false) { $template = @file_get_contents(SCRIPTS_DIR . '/Configurations/host.conf.template'); } if ($template !== false) { // Derive SSH key path from hostname convention // unRAID-MyServer → /root/.ssh/myserver_rsync_automation $sshOwner = strtolower(preg_replace('/^unraid-/i', '', $myHostname)); $sshKeyPath = '/root/.ssh/' . $sshOwner . '_rsync_automation'; $conf = str_replace('HOSTN', $hostId, $template); $conf = str_replace('hostn', $hostIdLow, $conf); // Pre-fill the SSH key path $conf = preg_replace( '/^(\s*' . $hostId . '_SSH_KEY\s*=\s*)""/m', '${1}"' . $sshKeyPath . '"', $conf ); if (!vv_write_conf_raw($confFile, $conf)) { echo json_encode(['ok' => false, 'error' => "Failed to write $confFile"]); exit; } } // Template missing is non-fatal — user can create the conf manually } echo json_encode(['ok' => true, 'host_id' => $hostId, 'conf_file' => $confFile]);