Testclaw / start-openclaw.sh
Elysiadev11's picture
Update start-openclaw.sh
5495142 verified
#!/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 <<EOF
{
"models": {
"providers": {
"kilo_gateway": {
"baseUrl": "https://api.kilo.ai/api/gateway",
"apiKey": "anonymous",
"api": "openai-completions",
"models": [
{ "id": "kilo-auto/free", "name": "Kilo Auto Free", "contextWindow": 128000, "maxTokens": 16000 }
]
},
"nvidia": {
"baseUrl": "$CLEAN_BASE",
"apiKey": "$OPENAI_API_KEY",
"api": "openai-completions",
"models": [
{ "id": "$MODEL", "name": "$MODEL", "contextWindow": 128000 }
]
}
}
},
"agents": {
"defaults": {
"model": {
"primary": "kilo_gateway/kilo-auto/free",
"fallbacks": ["nvidia/$MODEL"]
}
}
},
"commands": { "restart": true },
"browser": {
"enabled": true,
"requirePairing": false,
"headless": true,
"noSandbox": true,
"executablePath": "$CHROMIUM_PATH",
"defaultProfile": "openclaw",
"ssrfPolicy": { "dangerouslyAllowPrivateNetwork": true },
"profiles": {
"openclaw": {
"cdpPort": 18800,
"color": "0088FF"
}
}
},
"channels": {
"telegram": {
"enabled": true,
"botToken": "$TELEGRAM_BOT_TOKEN",
"dmPolicy": "allowlist",
"allowFrom": ["$TELEGRAM_ALLOW_ID"],
"webhookUrl": "https://elysiadev11-testclaw.hf.space/telegram/webhook",
"webhookSecret": "$OPENCLAW_GATEWAY_PASSWORD",
"webhookPath": "/telegram/webhook",
"webhookHost": "0.0.0.0",
"webhookPort": 8787,
"streaming": "off"
}
},
"gateway": {
"mode": "local",
"bind": "lan",
"port": 7862,
"trustedProxies": ["0.0.0.0/0"],
"auth": { "mode": "token", "token": "$OPENCLAW_GATEWAY_PASSWORD" },
"http": {
"endpoints": {
"chatCompletions": { "enabled": true }
}
},
"controlUi": {
"enabled": true,
"allowInsecureAuth": true,
"dangerouslyDisableDeviceAuth": true,
"dangerouslyAllowHostHeaderOriginFallback": true
}
}
}
EOF
echo ">>> 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('<html style=\"background:#1e1e2e; color:#cdd6f4; font-family:sans-serif; text-align:center; padding-top:20%;\"><body><h2>πŸš€ OpenClaw is warming up...</h2><p>Hugging Face is downloading dependencies. Please refresh in a few seconds.</p></body></html>');
});
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