feat: add reverse ssh sandbox targets

A sandbox behind NAT could not reach the room. A reverse target dials a tunnel-only sshd on the server, forwards localhost:22 to a loopback port, and the worker connects there.

The server assigns the port and writes one authorized_keys line per tunnel target. The key carries permitlisten for that port; the sshd allows remote forwarding only, with no shell or local forwarding. The tunnel private key is shown once and never stored; rotating it rewrites the file. The target list and setup dialog carry the command, and the docs include the sshd config and sidecar requirements.
This commit is contained in:
vhaudiquet committed 2026-11-29 19:25:14 +01:00
1 parent fc684115b5
commit d85f68a176
20 files changed
+893 -38

No files matched your search

+161 -12
View File
@@ -9,6 +9,7 @@ import type {
LoginFlowStatus,
ProviderConfig,
ProviderListResponse,
SandboxKind,
SandboxListResponse,
SandboxTarget,
StartLoginResponse,
@@ -93,6 +94,7 @@ const flowInstruction = computed(() => {
}
})
const sandboxKind = ref<SandboxKind>('direct')
const sandboxName = ref('')
const sandboxHost = ref('')
const sandboxPort = ref<number | string>(22)
@@ -101,6 +103,10 @@ const sandboxRoot = ref('/root')
const sandboxKey = ref('')
const sandboxBusy = ref(false)
const sandboxError = ref('')
const tunnelSetup = ref<{ id: string; name: string; privateKey: string; command: string } | null>(null)
const tunnelBusy = ref(false)
const tunnelError = ref('')
const copiedTunnel = ref<string | null>(null)
const deletingProvider = ref<string | null>(null)
const deletingTarget = ref<string | null>(null)
@@ -306,6 +312,7 @@ function openSandboxDialog() {
function closeSandboxDialog() {
sandboxDialog.value = false
sandboxKind.value = 'direct'
sandboxKey.value = ''
sandboxError.value = ''
}
@@ -339,21 +346,33 @@ async function addSandbox() {
sandboxBusy.value = true
sandboxError.value = ''
const body: CreateSandboxBody = {
kind: sandboxKind.value,
name: sandboxName.value.trim(),
host: sandboxHost.value.trim(),
port: Number(sandboxPort.value) || 22,
user_name: sandboxUser.value.trim() || 'root',
workspace_root: sandboxRoot.value.trim() || '/root',
}
if (sandboxKind.value === 'direct') {
body.host = sandboxHost.value.trim()
body.port = Number(sandboxPort.value) || 22
}
if (sandboxKey.value.trim()) body.private_key = sandboxKey.value.trim()
try {
await api.post<CreateSandboxResponse>('/sandbox-targets', body)
const result = await api.post<CreateSandboxResponse>('/sandbox-targets', body)
if (result.kind === 'tunnel') {
tunnelSetup.value = {
id: result.id,
name: sandboxName.value.trim(),
privateKey: result.tunnel_private_key ?? '',
command: result.tunnel_command ?? '',
}
}
sandboxName.value = ''
sandboxHost.value = ''
sandboxPort.value = 22
sandboxUser.value = 'root'
sandboxRoot.value = '/root'
sandboxKey.value = ''
sandboxKind.value = 'direct'
await loadLists()
sandboxDialog.value = false
} catch (err) {
@@ -363,6 +382,47 @@ async function addSandbox() {
}
}
function openTunnelSetup(target: SandboxTarget) {
tunnelError.value = ''
copiedTunnel.value = null
tunnelSetup.value = {
id: target.id,
name: target.name,
privateKey: '',
command: target.tunnel_command ?? '',
}
}
async function regenerateTunnelKey() {
const setup = tunnelSetup.value
if (!setup || tunnelBusy.value) return
tunnelBusy.value = true
tunnelError.value = ''
try {
const result = await api.post<CreateSandboxResponse>(`/sandbox-targets/${setup.id}/tunnel-key`)
setup.privateKey = result.tunnel_private_key ?? ''
setup.command = result.tunnel_command ?? setup.command
copiedTunnel.value = null
await loadLists()
} catch (err) {
tunnelError.value = apiErrorMessage(err)
} finally {
tunnelBusy.value = false
}
}
async function copyTunnelValue(value: string, key: string) {
try {
await navigator.clipboard.writeText(value)
copiedTunnel.value = key
window.setTimeout(() => {
if (copiedTunnel.value === key) copiedTunnel.value = null
}, 1800)
} catch {
tunnelError.value = 'The clipboard is not available. Select the text and copy it by hand.'
}
}
async function removeProvider(provider: ProviderConfig) {
if (deleteBusy.value) return
deleteBusy.value = true
@@ -591,7 +651,7 @@ async function copyKey(target: SandboxTarget) {
Add sandbox
</button>
</div>
<p class="hint">A sandbox is an ssh machine the room works on. The public key belongs in its authorized_keys file.</p>
<p class="hint">A sandbox is an ssh machine the room works on. Direct targets are reached at their address. Reverse SSH targets dial this server, so they can sit behind NAT.</p>
<div v-if="!loading && targets.length === 0" class="empty">No sandboxes yet. Use Add sandbox to connect one.</div>
@@ -599,7 +659,10 @@ async function copyKey(target: SandboxTarget) {
<div v-for="target in targets" :key="target.id" class="settings-row">
<div class="settings-row-main">
<div class="settings-row-title">{{ target.name }}</div>
<div class="settings-row-detail">{{ target.user_name }}@{{ target.host }}:{{ target.port }}</div>
<div v-if="target.kind === 'tunnel'" class="settings-row-detail">
Tunnel port {{ target.port }} on this server, sandbox {{ target.user_name }}@localhost:22
</div>
<div v-else class="settings-row-detail">{{ target.user_name }}@{{ target.host }}:{{ target.port }}</div>
<div class="settings-row-detail">Workspace root: {{ target.workspace_root }}</div>
<div class="key-line">
<span class="key-text">{{ target.public_key }}</span>
@@ -623,6 +686,14 @@ async function copyKey(target: SandboxTarget) {
</div>
</div>
<div class="settings-row-actions">
<button
v-if="target.kind === 'tunnel'"
type="button"
class="btn btn-sm"
@click="openTunnelSetup(target)"
>
Setup
</button>
<button
type="button"
class="btn btn-sm"
@@ -669,20 +740,34 @@ async function copyKey(target: SandboxTarget) {
<input id="sandbox-name" v-model="sandboxName" class="input" type="text" maxlength="60" autofocus required>
</div>
<div class="field">
<label class="field-label" for="sandbox-host">Host</label>
<input id="sandbox-host" v-model="sandboxHost" class="input" type="text" spellcheck="false" required>
<label class="field-label" for="sandbox-kind">Connection</label>
<select id="sandbox-kind" v-model="sandboxKind" class="select">
<option value="direct">Direct SSH</option>
<option value="tunnel">Reverse SSH</option>
</select>
</div>
</div>
<div class="form-grid">
<div v-if="sandboxKind === 'direct'" class="form-grid">
<div class="field">
<label class="field-label" for="sandbox-host">Host</label>
<input id="sandbox-host" v-model="sandboxHost" class="input" type="text" spellcheck="false" required>
</div>
<div class="field">
<label class="field-label" for="sandbox-port">Port</label>
<input id="sandbox-port" v-model.number="sandboxPort" class="input" type="number" min="1" max="65535" required>
</div>
<div class="field">
<label class="field-label" for="sandbox-user">SSH user</label>
<input id="sandbox-user" v-model="sandboxUser" class="input" type="text" spellcheck="false">
</div>
</div>
<div v-else class="field">
<span class="hint">
The server assigns a loopback port. After adding, run the tunnel command on the sandbox and add this target's public key to its authorized_keys.
</span>
</div>
<div class="field">
<label class="field-label" for="sandbox-user">SSH user</label>
<input id="sandbox-user" v-model="sandboxUser" class="input" type="text" spellcheck="false">
</div>
<div class="field">
@@ -713,6 +798,70 @@ async function copyKey(target: SandboxTarget) {
</div>
</form>
</BaseDialog>
<BaseDialog
v-if="tunnelSetup"
title="Reverse SSH setup"
:description="`Run the tunnel on ${tunnelSetup.name}, then test the target.`"
@close="tunnelSetup = null"
>
<div class="form">
<p class="hint">
Add the target's public key to the sandbox's authorized_keys. The tunnel sshd reads the authorized_keys file this server writes.
</p>
<div class="field">
<label class="field-label">Tunnel command</label>
<pre class="command-block">{{ tunnelSetup.command }}</pre>
<button
type="button"
class="btn btn-sm btn-quiet"
@click="copyTunnelValue(tunnelSetup.command, 'setup-command')"
>
<component :is="copiedTunnel === 'setup-command' ? Check : Copy" :size="14" aria-hidden="true" />
{{ copiedTunnel === 'setup-command' ? 'Copied' : 'Copy' }}
</button>
</div>
<div class="field">
<label class="field-label">
Tunnel private key
<span v-if="tunnelSetup.privateKey" class="field-optional">shown once</span>
</label>
<textarea
v-if="tunnelSetup.privateKey"
:value="tunnelSetup.privateKey"
class="textarea"
rows="5"
readonly
></textarea>
<p v-else class="hint">
The key was shown when the target was added. Regenerate it if it was lost; the old key stops working.
</p>
<button
v-if="tunnelSetup.privateKey"
type="button"
class="btn btn-sm btn-quiet"
@click="copyTunnelValue(tunnelSetup.privateKey, 'setup-key')"
>
<component :is="copiedTunnel === 'setup-key' ? Check : Copy" :size="14" aria-hidden="true" />
{{ copiedTunnel === 'setup-key' ? 'Copied' : 'Copy key' }}
</button>
</div>
<p class="hint">
Save the key as <code>rhema-tunnel-{{ tunnelSetup.id }}</code> in the directory where you run the command, then run <code>chmod 600</code> on it.
</p>
<p v-if="tunnelError" class="form-error" role="alert">{{ tunnelError }}</p>
<div class="dialog-actions">
<button type="button" class="btn" @click="tunnelSetup = null">Close</button>
<button type="button" class="btn" :disabled="tunnelBusy" @click="regenerateTunnelKey">
{{ tunnelBusy ? 'Regenerating' : 'Regenerate key' }}
</button>
</div>
</div>
</BaseDialog>
</section>
</div>
</template>