badguardhome/dhcp.go

46 lines
1.1 KiB
Go
Raw Normal View History

2018-12-12 17:22:45 +00:00
package main
import (
"encoding/json"
"math/rand"
"net/http"
)
func handleDHCPStatus(w http.ResponseWriter, r *http.Request) {
status := map[string]interface{}{
2018-12-13 10:17:41 +00:00
"config": config.DHCP.Config,
2018-12-12 17:22:45 +00:00
"leases": config.DHCP.Leases,
}
w.Header().Set("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(status)
if err != nil {
httpError(w, http.StatusInternalServerError, "Unable to marshal DHCP status json: %s", err)
return
}
}
func handleDHCPSetConfig(w http.ResponseWriter, r *http.Request) {
newconfig := dhcpConfig{}
err := json.NewDecoder(r.Body).Decode(&newconfig)
if err != nil {
httpError(w, http.StatusBadRequest, "Failed to parse new DHCP config json: %s", err)
return
}
2018-12-13 10:17:41 +00:00
config.DHCP.Config = newconfig
2018-12-12 17:22:45 +00:00
}
// TODO: implement
func handleDHCPFindActiveServer(w http.ResponseWriter, r *http.Request) {
found := map[string]bool{
"found": rand.Intn(2) == 1,
}
w.Header().Set("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(found)
if err != nil {
httpError(w, http.StatusBadRequest, "Failed to marshal DHCP found json: %s", err)
return
}
}