49 lines
1.2 KiB
TypeScript
49 lines
1.2 KiB
TypeScript
import { Router } from 'express'
|
|
import axios from 'axios'
|
|
|
|
const router = Router()
|
|
|
|
router.get('/nodes', async (_req, res) => {
|
|
try {
|
|
const host = process.env.HEADSCALE_HOST
|
|
const key = process.env.HEADSCALE_API_KEY
|
|
if (!host || !key) {
|
|
res.status(503).json({ error: 'HEADSCALE_HOST / HEADSCALE_API_KEY not configured' })
|
|
return
|
|
}
|
|
|
|
const response = await axios.get(`${host}/api/v1/node`, {
|
|
headers: { Authorization: `Bearer ${key.trim()}` },
|
|
})
|
|
|
|
type HsNode = {
|
|
id: string
|
|
name: string
|
|
ipAddresses: string[]
|
|
online: boolean
|
|
lastSeen: string
|
|
user?: { name: string }
|
|
}
|
|
|
|
const nodes: HsNode[] = response.data?.nodes ?? []
|
|
|
|
res.json({
|
|
total: nodes.length,
|
|
online: nodes.filter(n => n.online).length,
|
|
nodes: nodes.map(n => ({
|
|
id: n.id,
|
|
name: n.name,
|
|
ip: n.ipAddresses?.[0] ?? '',
|
|
online: n.online,
|
|
lastSeen: n.lastSeen,
|
|
user: n.user?.name ?? '',
|
|
})),
|
|
})
|
|
} catch (err: unknown) {
|
|
const msg = err instanceof Error ? err.message : 'Unknown error'
|
|
res.status(500).json({ error: msg })
|
|
}
|
|
})
|
|
|
|
export default router
|