#!/bin/bash # ───────────────────────────────────────────────────────────── # 📁 PATH: start-openclaw.sh (root HF Space repo) # ───────────────────────────────────────────────────────────── set +e echo "===== OpenClaw Startup =====" # ── 1. Direktori ─────────────────────────────────────────────── mkdir -p /root/.openclaw/agents/main/sessions mkdir -p /root/.openclaw/credentials mkdir -p /root/.openclaw/sessions mkdir -p /root/.openclaw/browsers echo ">>> Directories ready." # ── 2. Restore backup ───────────────────────────────────────── python3 /app/sync.py restore echo ">>> Restore done." # ── 3. Fix DNS & Hosts (TELEGRAM FIX) ───────────────────────── echo "nameserver 1.1.1.1" >> /etc/resolv.conf echo "nameserver 8.8.8.8" >> /etc/resolv.conf echo "nameserver 8.8.4.4" >> /etc/resolv.conf echo "149.154.166.110 api.telegram.org" >> /etc/hosts echo ">>> DNS and Hosts fixed for Telegram." # ── 4. Chromium ─────────────────────────────────────────────── export PLAYWRIGHT_BROWSERS_PATH=/root/.openclaw/browsers CHROMIUM_PATH=$(find /root/.openclaw/browsers -name "chrome" -type f 2>/dev/null | head -1) if [ -z "$CHROMIUM_PATH" ]; then echo ">>> Installing Chromium..." OPENCLAW_NM=$(npm root -g 2>/dev/null)/openclaw/node_modules/playwright-core/cli.js if timeout 180 node "$OPENCLAW_NM" install chromium; then echo ">>> Chromium OK" else echo ">>> WARN: Chromium install failed" fi CHROMIUM_PATH=$(find /root/.openclaw/browsers -name "chrome" -type f 2>/dev/null | head -1) else echo ">>> Chromium found: $CHROMIUM_PATH" fi # ── 5. Bersihkan OPENAI_API_BASE ────────────────────────────── CLEAN_BASE=$(echo "$OPENAI_API_BASE" \ | sed "s|/chat/completions||g" \ | sed "s|/v1/|/v1|g" \ | sed "s|/v1$|/v1|g") # ── 6. Generate openclaw.json ───────────────────────────────── cat > /root/.openclaw/openclaw.json <>> openclaw.json generated with User ID Allowlist and Browser requirePairing: false." # ── 7. Node.js reverse proxy (ANTI-TIMEOUT TRICK) ───────────── node -e " const http = require('http'); const net = require('net'); process.on('uncaughtException', err => console.error('Proxy Exception:', err.message)); function proxyHttp(req, res, targetPort) { const opts = { hostname: '127.0.0.1', port: targetPort, path: req.url, method: req.method, headers: req.headers, }; const pr = http.request(opts, (r) => { res.writeHead(r.statusCode, r.headers); r.pipe(res, { end: true }); }); pr.on('error', (e) => { res.writeHead(200, { 'Content-Type': 'text/html' }); res.end('

🚀 OpenClaw is warming up...

Hugging Face is downloading dependencies. Please refresh in a few seconds.

'); }); req.pipe(pr, { end: true }); } function proxyWs(req, socket, head, targetPort) { const conn = net.connect(targetPort, '127.0.0.1', () => { conn.write( 'GET ' + req.url + ' HTTP/1.1\r\n' + Object.entries(req.headers).map(([k,v]) => k+': '+v).join('\r\n') + '\r\n\r\n' ); if (head && head.length) conn.write(head); socket.pipe(conn); conn.pipe(socket); }); conn.on('error', (e) => { socket.destroy(); }); socket.on('error', () => conn.destroy()); } const server = http.createServer((req, res) => { const port = req.url.startsWith('/telegram/webhook') ? 8787 : 7862; proxyHttp(req, res, port); }); server.on('upgrade', (req, socket, head) => { const port = req.url.startsWith('/telegram/webhook') ? 8787 : 7862; proxyWs(req, socket, head, port); }); server.listen(7860, '0.0.0.0', () => { console.log('Proxy (HTTP+WebSocket) on port 7860 (Anti-Timeout Active)'); }); " & echo ">>> Node.js WebSocket proxy started on port 7860." # ── 8. Backup otomatis setiap 1 jam ────────────────────────── (while true; do sleep 3600 echo ">>> Running scheduled backup..." python3 /app/sync.py backup done) & # ── 9. Jalankan OpenClaw ────────────────────────────────────── echo ">>> Running openclaw doctor --fix..." openclaw doctor --fix echo ">>> Starting OpenClaw on port 7862..." exec openclaw gateway run --port 7862