2018-12-12 20:22:45 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2018-12-21 15:52:59 +03:00
|
|
|
"net"
|
2018-12-12 20:22:45 +03:00
|
|
|
"net/http"
|
2018-12-28 17:17:51 +03:00
|
|
|
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/dhcpd"
|
2018-12-12 20:22:45 +03:00
|
|
|
)
|
|
|
|
|
2018-12-28 17:17:51 +03:00
|
|
|
var dhcpServer = dhcpd.Server{}
|
|
|
|
|
2018-12-12 20:22:45 +03:00
|
|
|
func handleDHCPStatus(w http.ResponseWriter, r *http.Request) {
|
|
|
|
status := map[string]interface{}{
|
2018-12-28 17:17:51 +03:00
|
|
|
"config": config.DHCP,
|
|
|
|
"leases": dhcpServer.Leases(),
|
2018-12-12 20:22:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2018-12-28 17:17:51 +03:00
|
|
|
newconfig := dhcpd.ServerConfig{}
|
2018-12-12 20:22:45 +03:00
|
|
|
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-28 17:17:51 +03:00
|
|
|
if newconfig.Enabled {
|
|
|
|
err := dhcpServer.Start(&newconfig)
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, http.StatusBadRequest, "Failed to start DHCP server: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !newconfig.Enabled {
|
|
|
|
dhcpServer.Stop()
|
|
|
|
}
|
|
|
|
config.DHCP = newconfig
|
2018-12-28 20:49:27 +03:00
|
|
|
httpUpdateConfigReloadDNSReturnOK(w, r)
|
2018-12-12 20:22:45 +03:00
|
|
|
}
|
|
|
|
|
2018-12-21 15:52:59 +03:00
|
|
|
func handleDHCPInterfaces(w http.ResponseWriter, r *http.Request) {
|
|
|
|
response := map[string]interface{}{}
|
|
|
|
|
|
|
|
ifaces, err := net.Interfaces()
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, http.StatusInternalServerError, "Couldn't get list of interfaces: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
type address struct {
|
|
|
|
IP string
|
|
|
|
Netmask string
|
|
|
|
}
|
|
|
|
|
|
|
|
type responseInterface struct {
|
2018-12-21 16:16:13 +03:00
|
|
|
Name string `json:"name"`
|
|
|
|
MTU int `json:"mtu"`
|
|
|
|
HardwareAddr string `json:"hardware_address"`
|
|
|
|
Addresses []string `json:"ip_addresses"`
|
2018-12-21 15:52:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := range ifaces {
|
|
|
|
if ifaces[i].Flags&net.FlagLoopback != 0 {
|
|
|
|
// it's a loopback, skip it
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if ifaces[i].Flags&net.FlagBroadcast == 0 {
|
|
|
|
// this interface doesn't support broadcast, skip it
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if ifaces[i].Flags&net.FlagPointToPoint != 0 {
|
|
|
|
// this interface is ppp, don't do dhcp over it
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
iface := responseInterface{
|
|
|
|
Name: ifaces[i].Name,
|
|
|
|
MTU: ifaces[i].MTU,
|
|
|
|
HardwareAddr: ifaces[i].HardwareAddr.String(),
|
|
|
|
}
|
|
|
|
addrs, err := ifaces[i].Addrs()
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, http.StatusInternalServerError, "Failed to get addresses for interface %v: %s", ifaces[i].Name, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, addr := range addrs {
|
|
|
|
iface.Addresses = append(iface.Addresses, addr.String())
|
|
|
|
}
|
|
|
|
if len(iface.Addresses) == 0 {
|
|
|
|
// this interface has no addresses, skip it
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
response[ifaces[i].Name] = iface
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.NewEncoder(w).Encode(response)
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, http.StatusInternalServerError, "Failed to marshal json with available interfaces: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-28 17:17:51 +03:00
|
|
|
// implement
|
2018-12-12 20:22:45 +03:00
|
|
|
func handleDHCPFindActiveServer(w http.ResponseWriter, r *http.Request) {
|
2018-12-28 17:17:51 +03:00
|
|
|
found, err := dhcpd.CheckIfOtherDHCPServersPresent(config.DHCP.InterfaceName)
|
2018-12-28 20:50:00 +03:00
|
|
|
result := map[string]interface{}{}
|
2018-12-28 17:17:51 +03:00
|
|
|
if err != nil {
|
2018-12-28 20:50:00 +03:00
|
|
|
result["error"] = err.Error()
|
|
|
|
} else {
|
|
|
|
result["found"] = found
|
2018-12-12 20:22:45 +03:00
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2018-12-28 17:17:51 +03:00
|
|
|
err = json.NewEncoder(w).Encode(result)
|
2018-12-12 20:22:45 +03:00
|
|
|
if err != nil {
|
2018-12-21 15:52:59 +03:00
|
|
|
httpError(w, http.StatusInternalServerError, "Failed to marshal DHCP found json: %s", err)
|
2018-12-12 20:22:45 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|