ctucx.git: dnsmasq-lease-overview

web-overview for dhcp-leases from dnsmasq

commit c222a434dd94cb7246c0730c16ce79a96dbf376b
parent ed758e4b7a298c8ee513c9ebeb9a39ab4e4b5b30
Author: Leah (ctucx) <git@ctu.cx>
Date: Fri, 28 Jun 2024 20:08:06 +0200

implement wifi-information for clients
3 files changed, 30 insertions(+), 3 deletions(-)
M
src/overview.nim
|
14
++++++++++++--
M
src/templates/overview.tpl
|
7
+++++++
M
src/utils.nim
|
12
+++++++++++-
diff --git a/src/overview.nim b/src/overview.nim
@@ -1,4 +1,4 @@
-import std/[json, options, tables, sequtils, parseutils]
+import std/[json, options, tables, strutils, sequtils, parseutils]
 import std/[marshal, cgi, os, httpclient, times]
 
 import mustache

@@ -26,7 +26,11 @@ proc main() =
       stdout.write("Not called from cgi?")
 
     of "/":
-      var tplLeases = newSeq[Table[string, string]]()
+      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];

@@ -41,6 +45,12 @@ proc main() =
           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"] = "-"
diff --git a/src/templates/overview.tpl b/src/templates/overview.tpl
@@ -35,6 +35,7 @@
 						<th>IPv4 address</th>
 						<th>IPv6 address</th>
 						<th>MAC address</th>
+						<th>WIFI info</th>
 						<th>Expires in</th>
 					</tr>
 				</thead>

@@ -50,6 +51,12 @@
 						<td>{{IP4_ADDRESS}}</td>
 						<td>{{IP6_ADDRESS}}</td>
 						<td>{{MAC_ADDRESS}}</td>
+						{{#WIFI_SSID}}
+						<td>{{WIFI_SIGNAL}} dBm - TX: {{WIFI_TX_RATE}} Mbit/s / RX: {{WIFI_RX_RATE}} Mbit/s</td>
+						{{/WIFI_SSID}}
+						{{^WIFI_SSID}}
+						<td>-</td>
+						{{/WIFI_SSID}}
 						<td>{{EXPIRY_TIME}}</td>
 					</tr>
 					{{/LEASES}}
diff --git a/src/utils.nim b/src/utils.nim
@@ -1,5 +1,5 @@
 
-import std/[strutils, options, times]
+import std/[httpclient, strutils, tables, json, options, times, base64]
 import types
 
 proc parseLeaseFile* (file: string): (string, seq[Lease]) = 

@@ -39,3 +39,13 @@ proc parseLeaseFile* (file: string): (string, seq[Lease]) =
       mode = v6
 
   return (duid, leases)
+
+proc getWifiConnections* (host: string, user: string, password: string): Table[string, JsonNode] =
+  var client = newHttpClient()
+  client.headers["Authorization"] = "Basic " & base64.encode(user & ":" & password)
+
+  let response = client.getContent(host & "/rest/interface/wifiwave2/registration-table").parseJson()
+  client.close()
+
+  for element in response:
+    result[element["mac-address"].getStr.toLowerAscii()] = element