File size: 7,153 Bytes
78e7c64
 
 
 
 
 
a99ac07
78e7c64
a99ac07
 
 
78e7c64
 
 
 
a99ac07
78e7c64
a99ac07
78e7c64
a99ac07
78e7c64
dc78112
256baa7
78e7c64
 
dc78112
 
78e7c64
a744254
a99ac07
78e7c64
 
 
a99ac07
 
 
a744254
a99ac07
 
 
78e7c64
 
a99ac07
78e7c64
 
a744254
78e7c64
 
 
 
 
a744254
78e7c64
 
 
 
eab571d
8ec3da7
 
 
 
eab571d
8ec3da7
 
78e7c64
 
 
 
 
 
 
 
 
 
 
 
8ec3da7
eab571d
 
8ec3da7
a99ac07
78e7c64
a99ac07
78e7c64
 
5495142
78e7c64
 
 
 
a99ac07
78e7c64
 
a99ac07
 
78e7c64
 
07525c5
a744254
 
 
 
1bcb0d8
f5ad965
07525c5
a744254
 
 
faffa9c
eab571d
a744254
 
78e7c64
 
 
faffa9c
78e7c64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5495142
a744254
1012339
faffa9c
 
 
 
1012339
 
faffa9c
 
 
 
 
 
 
 
 
 
1012339
 
 
 
 
faffa9c
 
 
 
 
 
 
 
 
 
 
 
 
 
1012339
faffa9c
 
 
 
 
 
 
 
 
 
 
 
 
 
1012339
faffa9c
 
 
 
 
a744254
 
 
 
 
78e7c64
faffa9c
a99ac07
78e7c64
 
faffa9c
f5ad965
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#!/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