Skip to content

Commit 6b74131

Browse files
authored
Merge pull request #28 from techartdev/tty-configurable-port
Add configurable terminal port support and update translations
2 parents e6d6f62 + f9c8053 commit 6b74131

8 files changed

Lines changed: 66 additions & 8 deletions

File tree

DOCS.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,13 @@ Control how the OpenClaw gateway binds to the network:
170170
These settings are applied automatically on add-on startup. No need to run `openclaw config` commands manually.
171171

172172
### Terminal
173-
- `enable_terminal` (bool, default **true**)
173+
- **`enable_terminal`** (bool, default **true**)
174+
- Enable or disable the web terminal button inside Home Assistant
175+
176+
- **`terminal_port`** (int, default **7681**)
177+
- Port number for the web terminal (ttyd) to listen on
178+
- Change this if port 7681 conflicts with another service on your system
179+
- Valid range: 1024-65535
174180

175181
Security note: the terminal gives shell access inside the add-on container.
176182

openclaw_assistant/config.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ options:
2828
# Enable web terminal inside Home Assistant (Ingress) via ttyd
2929
enable_terminal: true
3030

31+
# Terminal port (change if 7681 conflicts with another service)
32+
terminal_port: 7681
33+
3134
# Public base URL for opening the Gateway Web UI in a new tab (not embedded).
3235
# Recommended: NO trailing slash.
3336
# Example: "https://example.duckdns.org:12345" or "http://192.168.1.10:18789"
@@ -65,6 +68,7 @@ options:
6568
schema:
6669
timezone: str
6770
enable_terminal: bool?
71+
terminal_port: int(1024,65535)?
6872
gateway_public_url: str?
6973
homeassistant_token: str?
7074

openclaw_assistant/nginx.conf.tpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ http {
3131
# Proxy everything under /terminal/ (including websocket /terminal/ws)
3232
location ^~ /terminal/ {
3333
# IMPORTANT: no trailing slash in proxy_pass so nginx preserves the full URI
34-
proxy_pass http://127.0.0.1:7681;
34+
proxy_pass http://127.0.0.1:__TERMINAL_PORT__;
3535
proxy_http_version 1.1;
3636
proxy_set_header Upgrade $http_upgrade;
3737
proxy_set_header Connection "upgrade";

openclaw_assistant/run.sh

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,19 @@ TZNAME=$(jq -r '.timezone // "Europe/Sofia"' "$OPTIONS_FILE")
2121
GW_PUBLIC_URL=$(jq -r '.gateway_public_url // empty' "$OPTIONS_FILE")
2222
HA_TOKEN=$(jq -r '.homeassistant_token // empty' "$OPTIONS_FILE")
2323
ENABLE_TERMINAL=$(jq -r '.enable_terminal // true' "$OPTIONS_FILE")
24+
TERMINAL_PORT_RAW=$(jq -r '.terminal_port // 7681' "$OPTIONS_FILE")
25+
26+
# SECURITY: Validate TERMINAL_PORT to prevent nginx config injection
27+
# Only allow numeric values in valid port range (1024-65535)
28+
if [[ "$TERMINAL_PORT_RAW" =~ ^[0-9]+$ ]] && [ "$TERMINAL_PORT_RAW" -ge 1024 ] && [ "$TERMINAL_PORT_RAW" -le 65535 ]; then
29+
TERMINAL_PORT="$TERMINAL_PORT_RAW"
30+
else
31+
echo "ERROR: Invalid terminal_port '$TERMINAL_PORT_RAW'. Must be numeric 1024-65535. Using default 7681."
32+
TERMINAL_PORT="7681"
33+
fi
34+
35+
echo "DEBUG: enable_terminal config value: '$ENABLE_TERMINAL'"
36+
echo "DEBUG: terminal_port config value: '$TERMINAL_PORT' (validated)"
2437

2538
# Generic router SSH settings
2639
ROUTER_HOST=$(jq -r '.router_ssh_host // empty' "$OPTIONS_FILE")
@@ -239,12 +252,29 @@ openclaw gateway run &
239252
GW_PID=$!
240253

241254
# Start web terminal (optional)
242-
if [ "$ENABLE_TERMINAL" = "true" ]; then
243-
echo "Starting web terminal (ttyd) on 127.0.0.1:7681 ..."
244-
ttyd -W -i 127.0.0.1 -p 7681 -b /terminal bash &
255+
TTYD_PID_FILE="/var/run/openclaw-ttyd.pid"
256+
257+
# Clean up stale ttyd process from previous run using PID file
258+
if [ -f "$TTYD_PID_FILE" ]; then
259+
OLD_PID=$(cat "$TTYD_PID_FILE" 2>/dev/null || echo "")
260+
if [ -n "$OLD_PID" ] && kill -0 "$OLD_PID" 2>/dev/null; then
261+
echo "Stopping previous ttyd process (PID $OLD_PID)..."
262+
kill "$OLD_PID" 2>/dev/null || true
263+
sleep 1
264+
# Force kill if still running
265+
kill -9 "$OLD_PID" 2>/dev/null || true
266+
fi
267+
rm -f "$TTYD_PID_FILE"
268+
fi
269+
270+
if [ "$ENABLE_TERMINAL" = "true" ] || [ "$ENABLE_TERMINAL" = "1" ]; then
271+
echo "Starting web terminal (ttyd) on 127.0.0.1:${TERMINAL_PORT} ..."
272+
ttyd -W -i 127.0.0.1 -p "${TERMINAL_PORT}" -b /terminal bash &
245273
TTYD_PID=$!
274+
echo "$TTYD_PID" > "$TTYD_PID_FILE"
275+
echo "ttyd started with PID $TTYD_PID"
246276
else
247-
echo "Terminal disabled (enable_terminal=false)"
277+
echo "Terminal disabled (enable_terminal=$ENABLE_TERMINAL)"
248278
fi
249279

250280
# Start ingress reverse proxy (nginx). This provides the add-on UI inside HA.
@@ -254,20 +284,22 @@ fi
254284
# The gateway token is NOT managed by the add-on; OpenClaw will generate/store it.
255285
# Best-effort: query it via CLI (works even if openclaw.json is JSON5). If unknown, we hide the button.
256286
GW_TOKEN="$(timeout 2s openclaw config get gateway.auth.token 2>/dev/null | tr -d '\n' || true)"
257-
GW_PUBLIC_URL="$GW_PUBLIC_URL" GW_TOKEN="$GW_TOKEN" python3 - <<'PY'
287+
GW_PUBLIC_URL="$GW_PUBLIC_URL" GW_TOKEN="$GW_TOKEN" TERMINAL_PORT="$TERMINAL_PORT" python3 - <<'PY'
258288
import os
259289
from pathlib import Path
260290
261291
tpl = Path('/etc/nginx/nginx.conf.tpl').read_text()
262292
landing_tpl = Path('/etc/nginx/landing.html.tpl').read_text()
263293
public_url = os.environ.get('GW_PUBLIC_URL','')
294+
terminal_port = os.environ.get('TERMINAL_PORT', '7681')
264295
265296
# Token comes from environment (best-effort CLI query in run.sh)
266297
token = os.environ.get('GW_TOKEN','')
267298
268299
gw_path = '' if public_url.endswith('/') else '/'
269300
270-
conf = tpl
301+
# Replace terminal port placeholder in nginx config
302+
conf = tpl.replace('__TERMINAL_PORT__', terminal_port)
271303
Path('/etc/nginx/nginx.conf').write_text(conf)
272304
273305
landing = landing_tpl.replace('__GATEWAY_TOKEN__', token)

openclaw_assistant/translations/bg.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ configuration:
77
name: Активиране на уеб терминал
88
description: Активиране на уеб терминал бутона в Home Assistant (Ingress) чрез ttyd
99

10+
terminal_port:
11+
name: Порт на терминал
12+
description: Номер на порт за уеб терминала (по подразбиране - 7681). Променете, ако този порт е в конфликт с друга услуга.
13+
1014
gateway_public_url:
1115
name: Публичен URL на Gateway
1216
description: Публичен базов URL за отваряне на Gateway уеб интерфейса в нов таб (не вграден). Пример - https://example.duckdns.org:12345 или http://192.168.1.10:18789

openclaw_assistant/translations/de.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ configuration:
77
name: Web-Terminal aktivieren
88
description: Web-Terminal-Schaltfläche in Home Assistant (Ingress) über ttyd aktivieren
99

10+
terminal_port:
11+
name: Terminal-Port
12+
description: Portnummer für das Web-Terminal (Standard - 7681). Ändern Sie diese, wenn dieser Port mit einem anderen Dienst in Konflikt steht.
13+
1014
gateway_public_url:
1115
name: Öffentliche Gateway-URL
1216
description: Öffentliche Basis-URL zum Öffnen der Gateway-Weboberfläche in einem neuen Tab (nicht eingebettet). Beispiel - https://example.duckdns.org:12345 oder http://192.168.1.10:18789

openclaw_assistant/translations/en.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ configuration:
77
name: Enable Web Terminal
88
description: Enable web terminal Button inside Home Assistant (Ingress) via ttyd
99

10+
terminal_port:
11+
name: Terminal Port
12+
description: Port number for the web terminal (default - 7681). Change if this port conflicts with another service.
13+
1014
gateway_public_url:
1115
name: Gateway Public URL
1216
description: Public base URL for opening the Gateway Web UI in a new tab (not embedded). Example - https://example.duckdns.org:12345 or http://192.168.1.10:18789

openclaw_assistant/translations/es.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ configuration:
77
name: Activar terminal web
88
description: Activar botón de terminal web dentro de Home Assistant (Ingress) mediante ttyd
99

10+
terminal_port:
11+
name: Puerto del terminal
12+
description: Número de puerto para el terminal web (predeterminado - 7681). Cambie si este puerto entra en conflicto con otro servicio.
13+
1014
gateway_public_url:
1115
name: URL pública del Gateway
1216
description: URL base pública para abrir la interfaz web del Gateway en una nueva pestaña (no integrada). Ejemplo - https://example.duckdns.org:12345 o http://192.168.1.10:18789

0 commit comments

Comments
 (0)