Skip to content
Open
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
8 changes: 8 additions & 0 deletions opencode-hub/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Jangan kirim runtime lokal, rahasia, dan riwayat git ke context build Docker.
.runtime/
.runtime-boot.log
.opencode-server-password
deploy/
node_modules/
.git/
*.log
7 changes: 7 additions & 0 deletions opencode-hub/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.runtime/
.opencode-server-password
node_modules/

# Hasil install.sh di VPS — berisi password dan hash.
deploy/.env
deploy/.env.bak.*
24 changes: 24 additions & 0 deletions opencode-hub/Dockerfile.hermes
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Hermes Agent (NousResearch) memakai installer resmi, non-interaktif.
# Setup wizard butuh TTY, jadi dijalankan terpisah:
# docker compose run --rm -it hermes hermes setup
FROM python:3.12-slim

ENV DEBIAN_FRONTEND=noninteractive \
HERMES_INSTALL_CHANNEL=stable \
HERMES_HOME=/root/.hermes

RUN apt-get update \
&& apt-get install -y --no-install-recommends \
git curl ca-certificates tini procps ripgrep \
&& rm -rf /var/lib/apt/lists/*

RUN curl -fsSL https://hermes-agent.nousresearch.com/install.sh \
| bash -s -- --non-interactive

# Installer menaruh binary di salah satu dari dua lokasi tergantung versi.
ENV PATH="/root/.hermes/hermes-agent/.hermes/bin:/root/.hermes/bin:${PATH}"

WORKDIR /workspace

ENTRYPOINT ["tini", "--"]
CMD ["hermes", "gateway", "start"]
14 changes: 14 additions & 0 deletions opencode-hub/Dockerfile.hub
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Image untuk server halaman (hub-server.mjs) dan server data (data-server.mjs).
# Keduanya memakai image yang sama, dibedakan oleh command di compose.
FROM node:22-alpine

WORKDIR /app

COPY opencode-hub/package.json ./
COPY opencode-hub/hub-server.mjs opencode-hub/data-server.mjs ./
COPY opencode-hub/data ./data
COPY opencode-hub/public ./public

ENV NODE_ENV=production

CMD ["node", "hub-server.mjs"]
17 changes: 17 additions & 0 deletions opencode-hub/Dockerfile.opencode
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Server agen opencode (headless). Port 4096, hanya di jaringan internal compose.
FROM node:22-bookworm-slim

RUN apt-get update \
&& apt-get install -y --no-install-recommends git ca-certificates curl ripgrep \
&& rm -rf /var/lib/apt/lists/*

# Paket npm resmi opencode adalah "opencode-ai" (bukan @opencode/cli).
RUN npm install -g opencode-ai@latest

WORKDIR /workspace

EXPOSE 4096

ENTRYPOINT ["opencode"]
# "web" = server agen + UI web asli opencode di akar origin (bukan hanya API).
CMD ["web", "--port", "4096", "--hostname", "0.0.0.0"]
149 changes: 149 additions & 0 deletions opencode-hub/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
# opencode-hub

Halaman server untuk [opencode](https://github.com/anomalyco/opencode) plus **server data yang
terpisah**. Dibuat di dalam repo Freebuff, memakai gaya visual gelap yang sama, tanpa menyentuh
kode Freebuff yang sudah ada.

Bukan produk resmi opencode maupun Freebuff, dan tidak berafiliasi dengan tim opencode.

## Tiga proses, tiga port

| Proses | Port | Bind | Peran |
| --- | --- | --- | --- |
| `opencode web` | 4096 | `0.0.0.0` | agen opencode + UI web di `/` (HTTP API & OpenAPI di `/doc`) |
| `data-server.mjs` | 8788 | `127.0.0.1` | menyajikan registry tautan/versi sebagai JSON |
| `hub-server.mjs` | 3000 | `0.0.0.0` | halaman dashboard + agregasi status |

Server data sengaja dipisah: ia tidak menyimpan sesi, tidak menyentuh filesystem proyek, dan
hanya membaca `data/resources.json`. Kalau server opencode mati, halaman tetap tampil dan
melaporkan statusnya.

## Menjalankan

```bash
cd opencode-hub
./start.sh # data + opencode + hub
# atau satu per satu:
node data-server.mjs
node hub-server.mjs
```

`start.sh` membuat password acak di `.opencode-server-password` (mode 600, di-gitignore) dan
memakainya untuk `OPENCODE_SERVER_PASSWORD`, jadi API opencode tidak terbuka tanpa autentikasi.
Log ada di `.runtime/*.log`; hentikan dengan `./stop.sh` (hanya memakai PID yang dicatat sendiri).

Butuh CLI opencode: `npm i -g @opencode/cli@latest`, atau lihat
<https://opencode.ai/docs>.

## Endpoint

Hub:

- `GET /api/state` — status opencode + data + hub
- `GET /api/resources` — proxy ke server data
- `GET /api/opencode/sessions` — 20 sesi terbaru (read-only)
- `GET /api/health`

Server data:

- `GET /health`
- `GET /api/resources` — semua grup
- `GET /api/resources?group=opencode` — filter grup

## Variabel lingkungan

| Nama | Default | Keterangan |
| --- | --- | --- |
| `HUB_PORT` / `HUB_HOST` | `3000` / `0.0.0.0` | halaman |
| `DATA_PORT` / `DATA_HOST` | `8788` / `127.0.0.1` | server data |
| `OPENCODE_URL` | `http://127.0.0.1:4096` | target server opencode |
| `OPENCODE_SERVER_PASSWORD` | — | password API opencode |

## Keamanan — baca sebelum mengekspos ke publik

`opencode serve` memberi kendali penuh atas sesi agen dan filesystem proyek. API-nya tidak
dirancang untuk internet terbuka. Repo
[nexusrootlab/incident](https://github.com/nexusrootlab/incident) mendokumentasikan kebocoran
data lintas-sesi pada OpenCode Zen (2026-04-02), jadi:

1. selalu set `OPENCODE_SERVER_PASSWORD` (mode `opencode web` menolak request tanpa password
dengan `401`, jadi UI-nya tetap terkunci); set `OPENCODE_HOST=127.0.0.1` kalau hanya mau lokal;
2. taruh reverse proxy dengan HTTPS + autentikasi di depannya;
3. hub ini hanya memanggil endpoint read-only (`/global/health`, `/session`) dan tidak
memproksikan API agen secara mentah.

## Menjalankan permanen dengan domain sendiri

Stack Docker Compose + Caddy (HTTPS otomatis) ada di [`deploy/`](deploy/README.md):
Caddy → hub → data, sementara `opencode web` dan Hermes hanya di jaringan internal.

Di VPS baru, satu baris lewat curl sudah cukup — skrip itu clone repo, memasang Docker,
mengisi `.env`, membuat login, lalu menjalankan semua container:

```bash
curl -fsSL https://raw.githubusercontent.com/cramelolo291-bot/freebuff/main/opencode-hub/deploy/bootstrap.sh \
| sudo bash -s -- --domain opencode.contoh.com --email anda@contoh.com
```

Atau dari repo yang sudah di-clone:

```bash
cd opencode-hub/deploy
sudo ./install.sh --domain opencode.contoh.com --email anda@contoh.com
```

Panduan lengkap (DNS, verifikasi, backup, troubleshooting) ada di
[`deploy/README.md`](deploy/README.md).

Proses di sandbox/VM sementara tidak bertahan setelah sesi berakhir. Untuk server yang benar-benar
hidup terus di mesin/VPS Anda tanpa Docker, jalankan dengan systemd:

Proses di sandbox/VM sementara tidak bertahan setelah sesi berakhir. Untuk server yang benar-benar
hidup terus di mesin/VPS Anda tanpa Docker, jalankan dengan process manager:

```ini
# /etc/systemd/system/opencode.service
[Unit]
Description=opencode headless server
After=network.target

[Service]
Environment=OPENCODE_SERVER_PASSWORD=GANTI_DI_SINI
ExecStart=/usr/local/bin/opencode serve --port 4096 --hostname 127.0.0.1
Restart=always

[Install]
WantedBy=multi-user.target
```

```bash
sudo systemctl enable --now opencode
```

Untuk hub + server data, unit serupa dengan `ExecStart=/usr/bin/node /path/opencode-hub/hub-server.mjs`
dan `data-server.mjs` (dua unit terpisah, `Restart=always`).

## Data

`data/resources.json` memuat tautan yang dipakai halaman. Field `verified` menandai entri yang
sumbernya benar-benar diperiksa saat pembuatan; entri `false` hanya tautan yang dikirim pengguna.

## URL publik sementara (tanpa domain sendiri)

```bash
./start.sh # opencode web :4096, data server :8788, hub :3000
./tunnel.sh # menerbitkan https://<acak>.app.lhr.life -> hub :3000
```

`cloudflared` quick tunnel **tidak bisa** dipakai di VM ini: edge Cloudflare hanya lewat
UDP/TCP 7844 dan port itu diblokir (hasil precheck: `hard_fail=true`, halaman jadi error 1033).
`localhost.run` lewat SSH port 22 berhasil, jadi itu yang dipakai.

Batasannya: URL acak dan bisa berputar, tunnel mati kalau proses SSH-nya mati, dan hub tidak
memasang login sendiri — siapa pun yang memegang URL bisa memakai opencode. Untuk alamat tetap
(`opencode.lo`) pakai stack di `deploy/` (Docker + Caddy) di VPS dengan domain yang DNS-nya kamu
kendalikan.

Tunnel yang dipakai sekarang: **serveo.net** (`./tunnel.sh`, fallback `localhost.run`).
localhost.run membatasi bandwidth free tier (permintaan ditahan sampai puluhan detik), jadi hanya
dipakai kalau serveo mati.
97 changes: 97 additions & 0 deletions opencode-hub/data-server.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#!/usr/bin/env node
/**
* opencode-hub — server data (TERPISAH dari server opencode).
*
* Tugasnya hanya menyajikan registry data (tautan referensi, versi, catatan)
* sebagai JSON. Port default 8788 supaya jelas berbeda dari opencode (4096)
* dan dari halaman hub (3000).
*/
import { createServer } from "node:http";
import { readFile } from "node:fs/promises";
import { fileURLToPath } from "node:url";
import { dirname, join } from "node:path";

const __dirname = dirname(fileURLToPath(import.meta.url));
const PORT = Number(process.env.DATA_PORT ?? 8788);
const HOST = process.env.DATA_HOST ?? "127.0.0.1";
const STARTED_AT = new Date();

const registry = JSON.parse(
await readFile(join(__dirname, "data", "resources.json"), "utf8"),
);

let hits = 0;

function json(res, status, body) {
const payload = JSON.stringify(body, null, 2);
res.writeHead(status, {
"content-type": "application/json; charset=utf-8",
"content-length": Buffer.byteLength(payload),
"cache-control": "no-store",
"access-control-allow-origin": process.env.DATA_ALLOW_ORIGIN ?? "http://127.0.0.1:3000",
});
res.end(payload);
}

const server = createServer(async (req, res) => {
hits += 1;
const url = new URL(req.url ?? "/", `http://${req.headers.host ?? "localhost"}`);

if (req.method === "OPTIONS") {
res.writeHead(204, {
"access-control-allow-origin": process.env.DATA_ALLOW_ORIGIN ?? "http://127.0.0.1:3000",
"access-control-allow-methods": "GET, HEAD, OPTIONS",
});
return res.end();
}

if (req.method !== "GET" && req.method !== "HEAD") {
return json(res, 405, { error: "method_not_allowed" });
}

if (url.pathname === "/health") {
return json(res, 200, {
service: "opencode-hub-data",
ok: true,
uptimeSeconds: Math.round(process.uptime()),
startedAt: STARTED_AT.toISOString(),
pid: process.pid,
hits,
groups: registry.groups.length,
resources: registry.groups.reduce((n, g) => n + g.items.length, 0),
});
}

if (url.pathname === "/api/resources") {
const group = url.searchParams.get("group");
if (!group) return json(res, 200, registry);

const found = registry.groups.find((g) => g.id === group);
if (!found) return json(res, 404, { error: "unknown_group", group });
return json(res, 200, { updated: registry.updated, groups: [found] });
}

if (url.pathname === "/") {
return json(res, 200, {
service: "opencode-hub-data",
endpoints: {
"/health": "status server data",
"/api/resources": "semua grup tautan",
"/api/resources?group=opencode": "filter satu grup",
},
});
}

return json(res, 404, { error: "not_found", path: url.pathname });
});

server.listen(PORT, HOST, () => {
console.log(`[data] listening on http://${HOST}:${PORT} (pid ${process.pid})`);
});

for (const signal of ["SIGINT", "SIGTERM"]) {
process.on(signal, () => {
console.log(`[data] ${signal} -> shutdown`);
server.close(() => process.exit(0));
});
}
Loading
Loading