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
import std/[json, options, tables, strutils, sequtils, parseutils]
import std/[marshal, cgi, os, httpclient, times]
import mustache
import types, utils
const
templateOverview = staticRead "templates/overview.tpl"
proc main() =
var leaseFile = "./dnsmasq.lease"
if getEnv("LEASE_PATH") != "":
leaseFile = getEnv("LEASE_PATH")
if not fileExists(leaseFile):
echo "Lease file not found!"
quit()
let (duid, leases) = parseLeaseFile(leaseFile)
case getRequestURI()
of "":
stdout.write("Content-type: text/plain\n\n")
stdout.write("Not called from cgi?")
of "/":
var tplLeases = newSeq[Table[string, string]]()
var wifiConnections : Table[string, JsonNode]
if getEnv("MIKROTIK_HOST") != "" and getEnv("MIKROTIK_USER") != "" and getEnv("MIKROTIK_PASSWORD") != "":
wifiConnections = getWifiConnections(getEnv("MIKROTIK_HOST"), getEnv("MIKROTIK_USER"), getEnv("MIKROTIK_PASSWORD"))
for lease in leases:
var element : Table[string, string];
if not lease.hostname.isSome:
element["HOSTNAME"] = "-"
else:
element["HOSTNAME"] = lease.hostname.get
if lease.type == v4:
element["MAC_ADDRESS"] = lease.macAddress
element["IP4_ADDRESS"] = lease.address
element["IP6_ADDRESS"] = "-"
if wifiConnections.hasKey(lease.macAddress):
element["WIFI_SSID"] = wifiConnections[lease.macAddress]["ssid"].getStr
element["WIFI_SIGNAL"] = wifiConnections[lease.macAddress]["signal"].getStr
element["WIFI_RX_RATE"] = $(wifiConnections[lease.macAddress]["rx-rate"].getStr.parseInt / 1000000)
element["WIFI_TX_RATE"] = $(wifiConnections[lease.macAddress]["tx-rate"].getStr.parseInt / 1000000)
else:
element["MAC_ADDRESS"] = "-"
element["IP4_ADDRESS"] = "-"
element["IP6_ADDRESS"] = lease.address
let time = (lease.expiry - now()).toParts
element["EXPIRY_TIME"] = $time[Days] & "d " & $time[Hours] & "h " & $time[Minutes] & "m"
tplLeases.add(element)
let context = newContext()
context["PAGE"] = "Overview"
context["LEASES"] = tplLeases
stdout.write("Content-type: text/html\n\n")
stdout.write(templateOverview.render(context))
of "/json":
stdout.write("Content-type: application/json\n\n")
stdout.write($$leases)
main()