Android app for starting/stopping services via Termux: - Service cards for OpenClaw Gateway, SmallClawViewer, Watchdog - Status dots (green=running, red=stopped, grey=unknown) - Termux:API intent integration to run shortcut scripts - Auto-refresh every 15 seconds - Open button for SmallClawViewer WebView - Dark theme, Material3
121 lines
3.5 KiB
JavaScript
121 lines
3.5 KiB
JavaScript
import { createServer } from 'http'
|
|
import { readFile } from 'fs/promises'
|
|
import { join, extname } from 'path'
|
|
import { fileURLToPath } from 'url'
|
|
import { dirname } from 'path'
|
|
import { exec } from 'child_process'
|
|
import { promisify } from 'util'
|
|
|
|
const execAsync = promisify(exec)
|
|
const __dirname = dirname(fileURLToPath(import.meta.url))
|
|
const ROOT = '/data/data/com.termux/files/home/.openclaw/workspace/ClawDash'
|
|
const PORT = 3002
|
|
|
|
const SERVICES = {
|
|
'OpenClaw Gateway': {
|
|
id: 'gateway',
|
|
checkPort: 18789,
|
|
startCmd: 'node /data/data/com.termux/files/usr/bin/openclaw gateway 2>&1 &',
|
|
stopCmd: '',
|
|
description: 'OpenClaw main daemon'
|
|
},
|
|
'SmallClawViewer': {
|
|
id: 'smallclaw',
|
|
checkPort: 3001,
|
|
startCmd: 'node /data/data/com.termux/files/home/.openclaw/workspace/SmallClawViewer/server/index.js 2>&1 &',
|
|
checkProcess: 'SmallClawViewer',
|
|
description: 'Mobile workspace viewer'
|
|
},
|
|
'ClawDash': {
|
|
id: 'clawdash',
|
|
checkPort: 3002,
|
|
description: 'Service dashboard (this)'
|
|
}
|
|
}
|
|
|
|
async function isPortOpen(port) {
|
|
try {
|
|
const { stdout } = await execAsync(`ss -tlnp 2>/dev/null | grep -c ":${port} " || echo 0`)
|
|
return parseInt(stdout.trim()) > 0
|
|
} catch {
|
|
return false
|
|
}
|
|
}
|
|
|
|
async function isProcessRunning(name) {
|
|
try {
|
|
const { stdout } = await execAsync(`ps -ef 2>/dev/null | grep "${name}" | grep -v grep | grep -c "" || echo 0`)
|
|
return parseInt(stdout.trim()) > 0
|
|
} catch {
|
|
return false
|
|
}
|
|
}
|
|
|
|
const server = createServer(async (req, res) => {
|
|
const url = req.url.split('?')[0]
|
|
|
|
// API: get status of all services
|
|
if (url === '/api/status' && req.method === 'GET') {
|
|
const statuses = []
|
|
for (const [name, svc] of Object.entries(SERVICES)) {
|
|
let running = false
|
|
if (svc.checkPort) {
|
|
running = await isPortOpen(svc.checkPort)
|
|
}
|
|
if (!running && svc.checkProcess) {
|
|
running = await isProcessRunning(svc.checkProcess)
|
|
}
|
|
statuses.push({ name, id: svc.id, running, description: svc.description })
|
|
}
|
|
res.setHeader('Content-Type', 'application/json')
|
|
res.end(JSON.stringify(statuses))
|
|
return
|
|
}
|
|
|
|
// API: start a service
|
|
if (url.startsWith('/api/start/')) {
|
|
const id = url.split('/api/start/')[1].split('?')[0]
|
|
res.setHeader('Content-Type', 'application/json')
|
|
try {
|
|
const svc = Object.values(SERVICES).find(s => s.id === id)
|
|
if (!svc || !svc.startCmd) {
|
|
res.statusCode = 400
|
|
res.end(JSON.stringify({ ok: false, error: 'No start command for service' }))
|
|
return
|
|
}
|
|
const { stdout, stderr } = await execAsync(svc.startCmd, { timeout: 10000 })
|
|
res.end(JSON.stringify({ ok: true, stdout, stderr }))
|
|
} catch (e) {
|
|
res.end(JSON.stringify({ ok: true, message: e.message }))
|
|
}
|
|
return
|
|
}
|
|
|
|
// Serve static files
|
|
let filePath = url === '/' ? 'index.html' : url
|
|
filePath = join(ROOT, filePath)
|
|
const ext = extname(filePath)
|
|
const MIME = { '.html': 'text/html', '.js': 'application/javascript', '.css': 'text/css' }
|
|
const contentType = MIME[ext] || 'text/plain'
|
|
|
|
try {
|
|
const content = await readFile(filePath)
|
|
res.setHeader('Content-Type', contentType)
|
|
res.end(content)
|
|
} catch {
|
|
res.statusCode = 404
|
|
res.end('Not found')
|
|
}
|
|
})
|
|
|
|
server.on('error', (e) => {
|
|
if (e.code === 'EADDRINUSE') {
|
|
console.error(`Port ${PORT} is in use, waiting 5s before retry...`)
|
|
setTimeout(() => server.listen(PORT, '127.0.0.1'), 5000)
|
|
}
|
|
})
|
|
|
|
server.listen(PORT, '127.0.0.1', () => {
|
|
console.log(`ClawDash running at http://127.0.0.1:${PORT}`)
|
|
})
|