Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ gives them their `-app.<N>` heading.

## Not yet numbered

- Fresh RouterOS `/app` installs seed the host address in an encrypted
Centrs address book using a mounted CDB key. Startup preserves existing
address books. Router credentials can be imported into the host record
after startup (#47).

## 0.1.11-app.221 — 2026-11-25

`sha-aa5a7a9`.
Expand Down
6 changes: 6 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ ENV T3APP_PROVIDERS="claude codex"
# DEFAULT_SKILLS, #46): GitHub sources in the skills CLI's syntax, refreshed
# in the background. Operators add tokens, or set `none`.
ENV T3APP_SKILLS="tikoci/routeros-skills"
# RouterOS expands the host marker and mounts the key file on new /app
# installs. A standalone container gets the unexpanded marker.
ENV T3APP_ROUTEROS_HOST="[networkRouterIP]"
# This is the mount path. RouterOS supplies the key at runtime.
# hadolint ignore=DL3064
ENV CENTRS_CDB_PASSWORD_FILE=/run/secrets/t3code_cdb_key
# uv uses the image's Python 3 and never fetches a managed Python by itself;
# `uv python install` still works when asked for explicitly.
ENV UV_PYTHON_DOWNLOADS=manual
Expand Down
58 changes: 57 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ app disk, so logins, the T3 database, and `~/projects` survive reboots.

### Preview or edit the YAML

[See in /app YAML Editor](https://tikoci.dev/restraml/tikapp.html?version=latest&yaml=%23+yaml-language-server%3A+%24schema%3Dhttps%3A%2F%2Ftikoci.dev%2Frestraml%2Frouteros-app-yaml-schema.latest.json%0A%23+tikoci%2Fapp-t3code+%E2%80%94+RouterOS+%2Fapp+definition+for+the+T3+Code+server.%0A%23%0A%23+Install+on+the+router+%28production%2C+HTTPS+via+%2Fip%2Fcloud+or+your+own+domain%29%3A%0A%23+++%2Fapp%2Fadd+yaml%3D%3Ccontents-of-this-file%3E+use-https%3Dyes%0A%23+On+CHR+%28including+tikoci%2Fquickchr+labs%29%2C+use+use-https%3Dno+instead.%0A%23%0A%23+Image+tags+track+upstream+T3+Code+stable+releases+only+%28no+nightlies%29%3A%0A%23+ghcr.io%2Ftikoci%2Fapp-t3code%3A%3CT3CODE_VER%3E+with+%3A0.1+%2F+%3A0+%2F+%3Alatest+as%0A%23+stability+aliases.+Pin+the+full+version+in+production%3B+%3A0.1+follows%0A%23+patch+releases.%0A%0Aname%3A+t3code%0Adescr%3A+T3+Code+server+%E2%80%94+agentic+coding+in+the+browser+%28GHCR+build+for+RouterOS%29%0Apage%3A+https%3A%2F%2Fgithub.com%2Ftikoci%2Fapp-t3code%0Acategory%3A+development%0Aurl-path%3A+%2F%0Aservices%3A%0A++t3code%3A%0A++++image%3A+ghcr.io%2Ftikoci%2Fapp-t3code%3A0.1%0A++++ports%3A%0A++++++-+%223773%3A3773%3Aweb%3Atcp%22%0A++++environment%3A%0A++++++HOME%3A+%2Fdata%0A++++++T3CODE_HOME%3A+%2Fdata%2F.t3%0A++++++T3CODE_HOST%3A+0.0.0.0%0A++++++T3CODE_PORT%3A+%223773%22%0A++++++T3CODE_TELEMETRY_ENABLED%3A+%22false%22%0A++++++%23+Optional+tool+groups%2C+installed+into+%2Fdata+in+the+background+after%0A++++++%23+start.+Remove+names+to+skip+them%3B+%22none%22+is+core+only.+On+an+installed%0A++++++%23+app+change+this+with+%2Fapp%2Fset+environment%3D+%28README%29%2C+never+yaml%3D.%0A++++++T3APP_TOOL_GROUPS%3A+%22capture+labs+vpn+routing+dev%22%0A++++++%23+Provider+CLIs+to+install+into+%2Fdata+in+the+background%3A+claude+codex%0A++++++%23+cursor+grok+opencode+antigravity+pi+muse%2C+or+%22none%22.+Same+rules.%0A++++++T3APP_PROVIDERS%3A+%22claude+codex%22%0A++++++%23+Agent+skills+to+import+into+%2Fdata+at+each+start+%28GitHub+owner%2Frepo%2C%0A++++++%23+owner%2Frepo%40skill%2C+owner%2Frepo%23ref%2C+space-separated%29%2C+or+%22none%22.%0A++++++T3APP_SKILLS%3A+%22tikoci%2Frouteros-skills%22%0A++++volumes%3A%0A++++++%23+Keep+this+name+and+destination+when+upgrading%3A+the+whole+home%2C%0A++++++%23+including+.t3%2Fuserdata%2C+projects%2C+worktrees%2C+and+provider+logins.%0A++++++-+t3code-data%3A%2Fdata%0A++++restart%3A+unless-stopped%0Avolumes%3A%0A++t3code-data%3A+%7B%7D%0A) to inspect, validate, or customize
[See in /app YAML Editor](https://tikoci.dev/restraml/tikapp.html?version=latest&yaml=%23+yaml-language-server%3A+%24schema%3Dhttps%3A%2F%2Ftikoci.dev%2Frestraml%2Frouteros-app-yaml-schema.latest.json%0A%23+tikoci%2Fapp-t3code+%E2%80%94+RouterOS+%2Fapp+definition+for+the+T3+Code+server.%0A%23%0A%23+Install+on+the+router+%28production%2C+HTTPS+via+%2Fip%2Fcloud+or+your+own+domain%29%3A%0A%23+++%2Fapp%2Fadd+yaml%3D%3Ccontents-of-this-file%3E+use-https%3Dyes%0A%23+On+CHR+%28including+tikoci%2Fquickchr+labs%29%2C+use+use-https%3Dno+instead.%0A%23%0A%23+Image+tags+track+upstream+T3+Code+stable+releases+only+%28no+nightlies%29%3A%0A%23+ghcr.io%2Ftikoci%2Fapp-t3code%3A%3CT3CODE_VER%3E+with+%3A0.1+%2F+%3A0+%2F+%3Alatest+as%0A%23+stability+aliases.+Pin+the+full+version+in+production%3B+%3A0.1+follows%0A%23+patch+releases.%0A%0Aname%3A+t3code%0Adescr%3A+T3+Code+server+%E2%80%94+agentic+coding+in+the+browser+%28GHCR+build+for+RouterOS%29%0Apage%3A+https%3A%2F%2Fgithub.com%2Ftikoci%2Fapp-t3code%0Acategory%3A+development%0Aurl-path%3A+%2F%0Aservices%3A%0A++t3code%3A%0A++++image%3A+ghcr.io%2Ftikoci%2Fapp-t3code%3A0.1%0A++++ports%3A%0A++++++-+%223773%3A3773%3Aweb%3Atcp%22%0A++++environment%3A%0A++++++HOME%3A+%2Fdata%0A++++++T3CODE_HOME%3A+%2Fdata%2F.t3%0A++++++T3CODE_HOST%3A+0.0.0.0%0A++++++T3CODE_PORT%3A+%223773%22%0A++++++T3CODE_TELEMETRY_ENABLED%3A+%22false%22%0A++++++%23+Optional+tool+groups%2C+installed+into+%2Fdata+in+the+background+after%0A++++++%23+start.+Remove+names+to+skip+them%3B+%22none%22+is+core+only.+On+an+installed%0A++++++%23+app+change+this+with+%2Fapp%2Fset+environment%3D+%28README%29%2C+never+yaml%3D.%0A++++++T3APP_TOOL_GROUPS%3A+%22capture+labs+vpn+routing+dev%22%0A++++++%23+Provider+CLIs+to+install+into+%2Fdata+in+the+background%3A+claude+codex%0A++++++%23+cursor+grok+opencode+antigravity+pi+muse%2C+or+%22none%22.+Same+rules.%0A++++++T3APP_PROVIDERS%3A+%22claude+codex%22%0A++++++%23+Agent+skills+to+import+into+%2Fdata+at+each+start+%28GitHub+owner%2Frepo%2C%0A++++++%23+owner%2Frepo%40skill%2C+owner%2Frepo%23ref%2C+space-separated%29%2C+or+%22none%22.%0A++++++T3APP_SKILLS%3A+%22tikoci%2Frouteros-skills%22%0A++++++%23+The+host+address+and+key+path+are+ordinary+settings.+RouterOS%0A++++++%23+mounts+the+generated+key+separately+as+a+file.%0A++++++T3APP_ROUTEROS_HOST%3A+%22%5BnetworkRouterIP%5D%22%0A++++++CENTRS_CDB_PASSWORD_FILE%3A+%22%2Frun%2Fsecrets%2Ft3code_cdb_key%22%0A++++secrets%3A%0A++++++-+t3code_cdb_key%0A++++volumes%3A%0A++++++%23+Keep+this+name+and+destination+when+upgrading%3A+the+whole+home%2C%0A++++++%23+including+.t3%2Fuserdata%2C+projects%2C+worktrees%2C+and+provider+logins.%0A++++++-+t3code-data%3A%2Fdata%0A++++restart%3A+unless-stopped%0Asecrets%3A%0A++t3code_cdb_key%3A%0Avolumes%3A%0A++t3code-data%3A+%7B%7D%0A) to inspect, validate, or customize
the single-app definition in our sister project, [tikoci/restraml](https://github.com/tikoci/restraml).
The share link embeds the YAML as a snapshot; the downloads below track the
current published files. Both YAML files include schema comments for editor validation.
Expand Down Expand Up @@ -169,6 +169,62 @@ The container serves plain HTTP on **3773** (`T3CODE_HOST=0.0.0.0`,
`T3CODE_PORT=3773`). Put HTTPS in front for anything beyond localhost —
the hosted web/mobile T3 apps require it.

### Reach the RouterOS host with Centrs

On a fresh RouterOS `/app` install, startup creates an encrypted Centrs
address book at `/data/.config/tikoci/winbox.cdb`. It adds the host's
`[networkRouterIP]` address to the `host` group. The generated CDB key
is mounted at `/run/secrets/t3code_cdb_key`. Use `centrs devices list`
to find the address. Pass it as the positional target in
`centrs retrieve <address> /system/identity`. The appropriate RouterOS
management service must be reachable from the container.

Startup seeds the address without credentials by default. In the
container terminal, import the password into that host record from a
private file. You can use a file delivered by a T3 secret request.
Replace the example address, account and file path.

```sh
centrs devices set 172.18.0.1 --user t3-agent --password-stdin < /path/to/router-password
```

Remove the temporary password file after a successful import. Keep the
account password separate from the CDB key. For a disposable quickchr lab
with passwordless `admin`, set `T3APP_ROUTEROS_LAB_ADMIN=yes` before
first start, or use `centrs devices set 172.18.0.1 --user admin` after
startup, with the actual host address. Preserve
`T3APP_ROUTEROS_HOST=[networkRouterIP]` if replacing the full app
environment list. An existing address book stays in place. Edit it with
Centrs when the host address or account changes.

An image-only `/app/update` keeps existing installations usable. To add
automatic seeding to an install with the older YAML, follow the
[offline backup and volume restore procedure](#what-survives-an-image-replacement)
before migrating the YAML. For CDBs created by this app, back up
`<disk>/apps/t3code/secret-t3code_cdb_key` separately from `/data`.
On RouterOS 7.25.2, applying the YAML removed all of `<disk>/apps/t3code`,
including the volume and the key file, and nothing came back until the app
was enabled. Keep the app disabled, recreate the volume's source directory,
and restore it with its ownership and mount mapping intact.

If the restore includes an existing CDB, set its original key before the
first enable, in a private RouterOS admin terminal. RouterOS then writes the
saved value to the key file instead of generating a new one. The same
command recovers a key that has already changed. It updates the mount and
restarts an enabled app while keeping its volumes; a disabled app stays
disabled. Include any other app secret entries if you customized the YAML.

```routeros
/app/set [find where name="t3code"] secrets="t3code_cdb_key:<saved-key>"
```

Plain Docker and Podman start without a host seed. T3 startup honors an
existing `CENTRS_CDB_PASSWORD` and clears the file setting in its own
environment. Direct sessions such as `/container/shell` and `docker exec`
get the container's configured environment. When those sessions already
have a value-based key, or when setting `CENTRS_CDB_PASSWORD` for one
command, also set `CENTRS_CDB_PASSWORD_FILE=` so Centrs has one key source.

### Pairing with the web UI

The browser needs a one-time token to connect to this container. The pairing
Expand Down
15 changes: 15 additions & 0 deletions SECURITY.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,21 @@ The supported way to pair is still `t3app pairing` (see
[Pairing with the web UI](README.md#pairing-with-the-web-ui)), which needs
`/container/shell`, so it needs write access.

### Centrs address book and RouterOS account

On fresh RouterOS installs, the app generates a CDB key and supplies it
through `/run/secrets/t3code_cdb_key`. The Centrs environment setting holds
the path. Processes in the app can read the mount. The router's backing
file, `<disk>/apps/t3code/secret-t3code_cdb_key`, is readable by anyone
with access to that file or a backup that includes it. Keep that key
backup apart from the encrypted CDB.

A router password imported on stdin is stored in the host's encrypted
CDB record. RouterOS account permissions control what
the agent can do. Other targets use their own records. A global Centrs
target password or a password stored in `__default__` can apply to other
routers, including targets outside the address book.

### Software installed at runtime

The image installs some software into `/data` after it starts, so its
Expand Down
8 changes: 8 additions & 0 deletions app/t3code.tikapp.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,18 @@ services:
# Agent skills to import into /data at each start (GitHub owner/repo,
# owner/repo@skill, owner/repo#ref, space-separated), or "none".
T3APP_SKILLS: "tikoci/routeros-skills"
# The host address and key path are ordinary settings. RouterOS
# mounts the generated key separately as a file.
T3APP_ROUTEROS_HOST: "[networkRouterIP]"
CENTRS_CDB_PASSWORD_FILE: "/run/secrets/t3code_cdb_key"
secrets:
- t3code_cdb_key
volumes:
# Keep this name and destination when upgrading: the whole home,
# including .t3/userdata, projects, worktrees, and provider logins.
- t3code-data:/data
restart: unless-stopped
secrets:
t3code_cdb_key:
volumes:
t3code-data: {}
6 changes: 6 additions & 0 deletions app/t3code.tikappstore.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,14 @@
T3APP_TOOL_GROUPS: "capture labs vpn routing dev"
T3APP_PROVIDERS: "claude codex"
T3APP_SKILLS: "tikoci/routeros-skills"
T3APP_ROUTEROS_HOST: "[networkRouterIP]"
CENTRS_CDB_PASSWORD_FILE: "/run/secrets/t3code_cdb_key"
secrets:
- t3code_cdb_key
volumes:
- t3code-data:/data
restart: unless-stopped
secrets:
t3code_cdb_key:
volumes:
t3code-data: {}
2 changes: 1 addition & 1 deletion bin/t3app
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
# lib/t3app.ts, which calls each area's module in-process. --no-install:
# lib/ imports only what the image installed beside it (frozen lockfile),
# never a package Bun would fetch at runtime.
exec bun --no-install /usr/local/lib/t3app/t3app.ts "$@"
exec bun --no-env-file --no-install /usr/local/lib/t3app/t3app.ts "$@"
13 changes: 11 additions & 2 deletions docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ umask 022
mkdir -p "${HOME}/projects"
cd "${HOME}"

# Preserve the existing value-based Centrs setup. Without a RouterOS host
# or a mounted image-default key, keep the default source unset for agents.
if [[ -v CENTRS_CDB_PASSWORD ]] ||
{ [[ ${CENTRS_CDB_PASSWORD_FILE:-} == /run/secrets/t3code_cdb_key && ! -e $CENTRS_CDB_PASSWORD_FILE ]] &&
[[ -z ${T3APP_ROUTEROS_HOST:-} || $T3APP_ROUTEROS_HOST == '[networkRouterIP]' ]]; }; then
export CENTRS_CDB_PASSWORD_FILE=
fi

# The image PATH carries the default tool-group shim dir; a relocated payload
# root (T3APP_TOOLS_HOME) needs its own, resolved here at runtime so T3 and
# every agent it starts inherit it. Same slot as profile.d: after the user's
Expand Down Expand Up @@ -50,8 +58,9 @@ background() {
# `install --keep-trying` are its entrypoint-only forms.
# init: name any ignored legacy T3CODE_* keys, report both selections
# (warnings go to the container log), relink tool-group payloads already in
# the home (offline, fast) and write the runtime status JSON. Bounded and
# never fatal: T3 must start whatever the selections say.
# the home (offline, fast), write the runtime status JSON and seed the
# RouterOS host in a new Centrs address book. Bounded and never fatal:
# T3 must start whatever the selections say.
# Then the installs run in the background; T3 does not wait. Tool-group
# payloads install while missing; provider CLIs (#13) install while missing
# from PATH (npm into /data/.local, the vendor's installer, or
Expand Down
100 changes: 100 additions & 0 deletions lib/router-credentials.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import { closeSync, linkSync, lstatSync, mkdirSync, mkdtempSync, openSync, readdirSync, rmSync, statSync } from "node:fs";
import { isIP } from "node:net";
import { join } from "node:path";
import { kernelFlock, LOCK_EX, LOCK_NB } from "./runtime.ts";

const ROUTER_IP = "T3APP_ROUTEROS_HOST";
const LAB_ADMIN = "T3APP_ROUTEROS_LAB_ADMIN";
const CDB_KEY_FILE = "CENTRS_CDB_PASSWORD_FILE";
const BAKED_CENTRS = ["/usr/local/bin/bun", "--no-env-file", "--no-install", "/opt/bun/bin/centrs"];

function pathInfo(path: string) {
try {
return lstatSync(path);
} catch (error) {
if ((error as NodeJS.ErrnoException).code === "ENOENT") return undefined;
throw error;
}
}

/** Only create the default CDB on a RouterOS start with a resolved host and a file-based key. */
export function initHostCdb(env: NodeJS.ProcessEnv = process.env, centrs: readonly string[] = BAKED_CENTRS): boolean {
const home = env["HOME"];
const host = env[ROUTER_IP];
const key = env[CDB_KEY_FILE];
if (!home || !host || !isIP(host)) return true; // The image alone has an unexpanded RouterOS placeholder.

const dir = join(home, ".config", "tikoci");
const cdb = join(dir, "winbox.cdb");
const config = join(env["XDG_CONFIG_HOME"] || join(home, ".config"), "tikoci", "centrs.env");

function run(args: readonly string[]): Buffer {
const result = Bun.spawnSync([...centrs, ...args, "--json"], { cwd: home, env, stdin: "ignore", stderr: "pipe", timeout: 8_000 });
if (!result.success) {
// Centrs sends JSON errors to stderr. Copy only the error code to the container log.
let code: unknown;
try {
code = JSON.parse(result.stderr.toString()).error?.code;
} catch {
// A timeout or launch failure need not have a centrs envelope.
}
const detail = typeof code === "string" && /^[a-z0-9-]+\/[a-z0-9-]+$/.test(code) ? ` (${code})` : "";
throw new Error(`centrs ${args.slice(0, 2).join(" ")} failed${detail}`);
}
return result.stdout;
}

try {
// Keep operator-owned locations and value-based keys outside the bootstrap.
if (env["CENTRS_CDB_FILE"] || env["CENTRS_CDB_PASSWORD"] !== undefined) return true;
// Centrs settings get reads the file even when ordinary commands skip it.
const skipConfig = /^(1|true|yes|on)$/i.test(env["CENTRS_SKIP_ENV_FILE"]?.trim() ?? "");
if (!skipConfig && pathInfo(config) && JSON.parse(run(["settings", "get", "CENTRS_CDB_FILE"]).toString()).data?.value) return true;
if (!pathInfo(cdb) && (!key || !pathInfo(key))) throw new Error(`provide the app key file via ${CDB_KEY_FILE}`);

mkdirSync(dir, { recursive: true, mode: 0o700 });
// Serialize our startup attempts so the next boot can remove abandoned staging directories.
const lock = openSync(dir, "r");
try {
if (!kernelFlock(lock, LOCK_EX | LOCK_NB)) return true;
for (const entry of readdirSync(dir, { withFileTypes: true })) {
if (entry.isDirectory() && /^\.t3app-cdb-[a-zA-Z0-9]{6}$/.test(entry.name)) rmSync(join(dir, entry.name), { recursive: true, force: true });
}

const existing = pathInfo(cdb);
if (existing) {
const target = existing.isSymbolicLink() ? statSync(cdb, { throwIfNoEntry: false }) : existing;
if (!target) throw new Error("winbox.cdb symlink points to a missing file");
if (!target.isFile()) throw new Error("winbox.cdb must point to a regular file");
// Explicitly naming the file keeps centrs list from creating an open CDB if it disappears.
run(["devices", "list", "--cdb-file", cdb]);
return true;
}
if (!key) throw new Error(`provide the app key file via ${CDB_KEY_FILE}`);
const staging = mkdtempSync(join(dir, ".t3app-cdb-"));
try {
const stagedCdb = join(staging, "winbox.cdb");
const flags = ["--cdb-file", stagedCdb, "--cdb-password-file", key];
run(["devices", "init", "--encrypt", ...flags]);
run(["devices", "add", host, "--group", "host", ...flags, ...(env[LAB_ADMIN] === "yes" ? ["--user", "admin"] : [])]);
// Centrs has its own writer lock. The no-replace link lets an existing
// file win, and centrs detects a changed revision before it publishes.
try {
linkSync(stagedCdb, cdb);
} catch (error) {
if ((error as NodeJS.ErrnoException).code !== "EEXIST") throw error;
}
} finally {
rmSync(staging, { recursive: true, force: true });
}
} finally {
closeSync(lock);
}
return true;
} catch (error) {
const code = (error as NodeJS.ErrnoException).code;
const message = code ? `file operation failed (${code})` : error instanceof Error ? error.message : "initialization failed";
console.error(`t3app: RouterOS CDB: ${message}`);
return false;
}
}
Loading