ctucx.git: dnsmasq-lease-overview

web-overview for dhcp-leases from dnsmasq

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 

import std/[httpclient, strutils, tables, json, options, times, base64]
import types

proc parseLeaseFile* (file: string): (string, seq[Lease]) = 
  var
    mode   : Type = v4
    leases : seq[Lease]
    duid   : string

  for line in file.lines:
    if line == "": continue

    let fields = line.split(" ")

    if fields[0] != "duid":
      if mode != v6:
        leases.add(Lease(
          type       : v4,
          expiry     : fields[0].parseInt.fromUnix.inZone(local()),
          macAddress : fields[1],
          address    : fields[2],
          hostname   : if fields[3] != "*": some(fields[3]) else: none(string),
          clientId   : if fields[4] != "*": some(fields[4]) else: none(string)
        ))

      else:
        leases.add(Lease(
          type      : v6,
          expiry    : fields[0].parseInt.fromUnix.inZone(local()),
          iaid      : fields[1],
          address   : fields[2],
          hostname  : if fields[3] != "*": some(fields[3]) else: none(string),
          clientId  : if fields[4] != "*": some(fields[4]) else: none(string)
        ))

    else:
      duid = fields[1]
      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