Pretty-format leases so it shows human readable MAC address.

This commit is contained in:
Eugene Bujak 2018-12-28 21:01:31 +03:00
parent 03effab345
commit 8fc5aebf12

14
dhcp.go
View File

@ -4,6 +4,7 @@ import (
"encoding/json" "encoding/json"
"net" "net"
"net/http" "net/http"
"time"
"github.com/AdguardTeam/AdGuardHome/dhcpd" "github.com/AdguardTeam/AdGuardHome/dhcpd"
"github.com/joomcode/errorx" "github.com/joomcode/errorx"
@ -12,9 +13,20 @@ import (
var dhcpServer = dhcpd.Server{} var dhcpServer = dhcpd.Server{}
func handleDHCPStatus(w http.ResponseWriter, r *http.Request) { func handleDHCPStatus(w http.ResponseWriter, r *http.Request) {
rawLeases := dhcpServer.Leases()
leases := []map[string]string{}
for i := range rawLeases {
lease := map[string]string{
"mac": rawLeases[i].HWAddr.String(),
"ip": rawLeases[i].IP.String(),
"expires": rawLeases[i].Expiry.Format(time.RFC3339),
}
leases = append(leases, lease)
}
status := map[string]interface{}{ status := map[string]interface{}{
"config": config.DHCP, "config": config.DHCP,
"leases": dhcpServer.Leases(), "leases": leases,
} }
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")