48 lines
1.4 KiB
TypeScript
48 lines
1.4 KiB
TypeScript
import { Router } from 'express'
|
|
import axios from 'axios'
|
|
|
|
const router = Router()
|
|
|
|
router.get('/stats', async (_req, res) => {
|
|
try {
|
|
const host = process.env.ADGUARD_HOST
|
|
const user = process.env.ADGUARD_USER
|
|
const pass = process.env.ADGUARD_PASSWORD
|
|
if (!host) {
|
|
res.status(503).json({ error: 'ADGUARD_HOST not configured' })
|
|
return
|
|
}
|
|
|
|
const u = user?.trim() ?? ''
|
|
const p = pass?.trim() ?? ''
|
|
const b64 = Buffer.from(`${u}:${p}`).toString('base64')
|
|
const statsRes = await axios.get(`${host}/control/stats`, {
|
|
headers: {
|
|
Authorization: `Basic ${b64}`,
|
|
'User-Agent': 'Mozilla/5.0',
|
|
},
|
|
maxRedirects: 0,
|
|
})
|
|
|
|
const d = statsRes.data
|
|
const queries: number[] = d.dns_queries ?? []
|
|
const blocked: number[] = d.blocked_filtering ?? []
|
|
const total: number = d.num_dns_queries ?? 0
|
|
const totalBlocked: number = d.num_blocked_filtering ?? 0
|
|
|
|
const timeSlots = queries.map((q, i) => ({ queries: q, blocked: blocked[i] ?? 0 }))
|
|
|
|
res.json({
|
|
totalQueries: total,
|
|
blockedQueries: totalBlocked,
|
|
blockedPercent: total > 0 ? ((totalBlocked / total) * 100).toFixed(1) : '0.0',
|
|
timeSlots,
|
|
})
|
|
} catch (err: unknown) {
|
|
const msg = err instanceof Error ? err.message : 'Unknown error'
|
|
res.status(500).json({ error: msg })
|
|
}
|
|
})
|
|
|
|
export default router
|