2018-12-12 20:22:45 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2019-04-03 17:45:39 +03:00
|
|
|
"errors"
|
2018-12-29 16:40:29 +03:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2019-02-07 18:07:38 +03:00
|
|
|
"net"
|
2018-12-12 20:22:45 +03:00
|
|
|
"net/http"
|
2019-04-03 17:45:39 +03:00
|
|
|
"os/exec"
|
|
|
|
"runtime"
|
2018-12-29 16:40:29 +03:00
|
|
|
"strings"
|
2018-12-28 21:01:31 +03:00
|
|
|
"time"
|
2018-12-28 17:17:51 +03:00
|
|
|
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/dhcpd"
|
2019-04-03 19:36:20 +03:00
|
|
|
"github.com/AdguardTeam/golibs/file"
|
2019-02-25 16:44:22 +03:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
2018-12-28 21:01:16 +03:00
|
|
|
"github.com/joomcode/errorx"
|
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) {
|
2019-03-11 16:03:34 +03:00
|
|
|
log.Tracef("%s %v", r.Method, r.URL)
|
2018-12-28 21:01:31 +03:00
|
|
|
rawLeases := dhcpServer.Leases()
|
|
|
|
leases := []map[string]string{}
|
|
|
|
for i := range rawLeases {
|
|
|
|
lease := map[string]string{
|
2018-12-29 17:04:40 +03:00
|
|
|
"mac": rawLeases[i].HWAddr.String(),
|
|
|
|
"ip": rawLeases[i].IP.String(),
|
|
|
|
"hostname": rawLeases[i].Hostname,
|
|
|
|
"expires": rawLeases[i].Expiry.Format(time.RFC3339),
|
2018-12-28 21:01:31 +03:00
|
|
|
}
|
|
|
|
leases = append(leases, lease)
|
|
|
|
|
|
|
|
}
|
2018-12-12 20:22:45 +03:00
|
|
|
status := map[string]interface{}{
|
2018-12-28 17:17:51 +03:00
|
|
|
"config": config.DHCP,
|
2018-12-28 21:01:31 +03:00
|
|
|
"leases": 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-14 13:02:04 +03:00
|
|
|
type leaseJSON struct {
|
|
|
|
HWAddr string `json:"mac"`
|
|
|
|
IP string `json:"ip"`
|
|
|
|
Hostname string `json:"hostname"`
|
|
|
|
}
|
|
|
|
|
2019-05-14 12:59:59 +03:00
|
|
|
type dhcpServerConfigJSON struct {
|
|
|
|
dhcpd.ServerConfig `json:",inline"`
|
2019-05-14 13:02:04 +03:00
|
|
|
StaticLeases []leaseJSON `json:"static_leases"`
|
2019-05-14 12:59:59 +03:00
|
|
|
}
|
|
|
|
|
2018-12-12 20:22:45 +03:00
|
|
|
func handleDHCPSetConfig(w http.ResponseWriter, r *http.Request) {
|
2019-03-11 16:03:34 +03:00
|
|
|
log.Tracef("%s %v", r.Method, r.URL)
|
2019-05-14 12:59:59 +03:00
|
|
|
newconfig := dhcpServerConfigJSON{}
|
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
|
|
|
|
}
|
|
|
|
|
2019-05-14 16:59:21 +03:00
|
|
|
err = dhcpServer.CheckConfig(newconfig.ServerConfig)
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, http.StatusBadRequest, "Invalid DHCP configuration: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-03-05 18:04:49 +03:00
|
|
|
err = dhcpServer.Stop()
|
|
|
|
if err != nil {
|
|
|
|
log.Error("failed to stop the DHCP server: %s", err)
|
|
|
|
}
|
|
|
|
|
2019-05-14 16:59:21 +03:00
|
|
|
err = dhcpServer.Init(newconfig.ServerConfig)
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, http.StatusBadRequest, "Invalid DHCP configuration: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-12-28 17:17:51 +03:00
|
|
|
if newconfig.Enabled {
|
2019-04-03 19:36:20 +03:00
|
|
|
|
|
|
|
staticIP, err := hasStaticIP(newconfig.InterfaceName)
|
|
|
|
if !staticIP && err == nil {
|
|
|
|
err = setStaticIP(newconfig.InterfaceName)
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, http.StatusInternalServerError, "Failed to configure static IP: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-14 16:59:21 +03:00
|
|
|
err = dhcpServer.Start()
|
2018-12-28 17:17:51 +03:00
|
|
|
if err != nil {
|
|
|
|
httpError(w, http.StatusBadRequest, "Failed to start DHCP server: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2019-03-05 18:04:49 +03:00
|
|
|
|
2019-05-14 12:59:59 +03:00
|
|
|
config.DHCP = newconfig.ServerConfig
|
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) {
|
2019-03-11 16:03:34 +03:00
|
|
|
log.Tracef("%s %v", r.Method, r.URL)
|
2018-12-21 15:52:59 +03:00
|
|
|
response := map[string]interface{}{}
|
|
|
|
|
2019-01-29 20:41:57 +03:00
|
|
|
ifaces, err := getValidNetInterfaces()
|
2018-12-21 15:52:59 +03:00
|
|
|
if err != nil {
|
2019-01-29 20:41:57 +03:00
|
|
|
httpError(w, http.StatusInternalServerError, "Couldn't get interfaces: %s", err)
|
2018-12-21 15:52:59 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-02-07 18:07:38 +03:00
|
|
|
for _, iface := range ifaces {
|
|
|
|
if iface.Flags&net.FlagLoopback != 0 {
|
|
|
|
// it's a loopback, skip it
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if iface.Flags&net.FlagBroadcast == 0 {
|
|
|
|
// this interface doesn't support broadcast, skip it
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
addrs, err := iface.Addrs()
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, http.StatusInternalServerError, "Failed to get addresses for interface %s: %s", iface.Name, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonIface := netInterface{
|
|
|
|
Name: iface.Name,
|
|
|
|
MTU: iface.MTU,
|
|
|
|
HardwareAddr: iface.HardwareAddr.String(),
|
|
|
|
}
|
|
|
|
|
|
|
|
if iface.Flags != 0 {
|
|
|
|
jsonIface.Flags = iface.Flags.String()
|
|
|
|
}
|
|
|
|
// we don't want link-local addresses in json, so skip them
|
|
|
|
for _, addr := range addrs {
|
|
|
|
ipnet, ok := addr.(*net.IPNet)
|
|
|
|
if !ok {
|
|
|
|
// not an IPNet, should not happen
|
|
|
|
httpError(w, http.StatusInternalServerError, "SHOULD NOT HAPPEN: got iface.Addrs() element %s that is not net.IPNet, it is %T", addr, addr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// ignore link-local
|
|
|
|
if ipnet.IP.IsLinkLocalUnicast() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
jsonIface.Addresses = append(jsonIface.Addresses, ipnet.IP.String())
|
|
|
|
}
|
|
|
|
if len(jsonIface.Addresses) != 0 {
|
|
|
|
response[iface.Name] = jsonIface
|
|
|
|
}
|
|
|
|
|
2018-12-21 15:52:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
err = json.NewEncoder(w).Encode(response)
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, http.StatusInternalServerError, "Failed to marshal json with available interfaces: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-03 17:45:39 +03:00
|
|
|
// Perform the following tasks:
|
|
|
|
// . Search for another DHCP server running
|
|
|
|
// . Check if a static IP is configured for the network interface
|
|
|
|
// Respond with results
|
2018-12-12 20:22:45 +03:00
|
|
|
func handleDHCPFindActiveServer(w http.ResponseWriter, r *http.Request) {
|
2019-03-11 16:03:34 +03:00
|
|
|
log.Tracef("%s %v", r.Method, r.URL)
|
2018-12-29 16:39:29 +03:00
|
|
|
body, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
errorText := fmt.Sprintf("failed to read request body: %s", err)
|
2019-02-25 16:44:22 +03:00
|
|
|
log.Error(errorText)
|
2018-12-29 16:39:29 +03:00
|
|
|
http.Error(w, errorText, http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
interfaceName := strings.TrimSpace(string(body))
|
|
|
|
if interfaceName == "" {
|
|
|
|
errorText := fmt.Sprintf("empty interface name specified")
|
2019-02-25 16:44:22 +03:00
|
|
|
log.Error(errorText)
|
2018-12-29 16:39:29 +03:00
|
|
|
http.Error(w, errorText, http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2019-04-03 15:49:55 +03:00
|
|
|
|
2018-12-29 16:39:29 +03:00
|
|
|
found, err := dhcpd.CheckIfOtherDHCPServersPresent(interfaceName)
|
2019-04-03 15:49:55 +03:00
|
|
|
|
|
|
|
othSrv := map[string]interface{}{}
|
|
|
|
foundVal := "no"
|
|
|
|
if found {
|
|
|
|
foundVal = "yes"
|
|
|
|
} else if err != nil {
|
|
|
|
foundVal = "error"
|
|
|
|
othSrv["error"] = err.Error()
|
2018-12-12 20:22:45 +03:00
|
|
|
}
|
2019-04-03 15:49:55 +03:00
|
|
|
othSrv["found"] = foundVal
|
|
|
|
|
2019-04-03 17:45:39 +03:00
|
|
|
staticIP := map[string]interface{}{}
|
|
|
|
isStaticIP, err := hasStaticIP(interfaceName)
|
|
|
|
staticIPStatus := "yes"
|
|
|
|
if err != nil {
|
|
|
|
staticIPStatus = "error"
|
|
|
|
staticIP["error"] = err.Error()
|
|
|
|
} else if !isStaticIP {
|
|
|
|
staticIPStatus = "no"
|
|
|
|
staticIP["ip"] = getFullIP(interfaceName)
|
|
|
|
}
|
|
|
|
staticIP["static"] = staticIPStatus
|
|
|
|
|
2019-04-03 15:49:55 +03:00
|
|
|
result := map[string]interface{}{}
|
2019-04-03 17:45:39 +03:00
|
|
|
result["other_server"] = othSrv
|
|
|
|
result["static_ip"] = staticIP
|
2019-04-03 15:49:55 +03:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2018-12-28 21:01:16 +03:00
|
|
|
|
2019-04-03 17:45:39 +03:00
|
|
|
// Check if network interface has a static IP configured
|
|
|
|
func hasStaticIP(ifaceName string) (bool, error) {
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
return false, errors.New("Can't detect static IP: not supported on Windows")
|
|
|
|
}
|
|
|
|
|
|
|
|
body, err := ioutil.ReadFile("/etc/dhcpcd.conf")
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
lines := strings.Split(string(body), "\n")
|
|
|
|
nameLine := fmt.Sprintf("interface %s", ifaceName)
|
2019-04-05 12:47:26 +03:00
|
|
|
withinInterfaceCtx := false
|
2019-04-03 17:45:39 +03:00
|
|
|
|
|
|
|
for _, line := range lines {
|
|
|
|
line = strings.TrimSpace(line)
|
|
|
|
|
2019-04-05 12:47:26 +03:00
|
|
|
if withinInterfaceCtx && len(line) == 0 {
|
2019-04-03 17:45:39 +03:00
|
|
|
// an empty line resets our state
|
2019-04-05 12:47:26 +03:00
|
|
|
withinInterfaceCtx = false
|
2019-04-03 17:45:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(line) == 0 || line[0] == '#' {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
line = strings.TrimSpace(line)
|
|
|
|
|
2019-04-05 12:47:26 +03:00
|
|
|
if !withinInterfaceCtx {
|
2019-04-03 17:45:39 +03:00
|
|
|
if line == nameLine {
|
2019-04-05 12:47:26 +03:00
|
|
|
// we found our interface
|
|
|
|
withinInterfaceCtx = true
|
2019-04-03 17:45:39 +03:00
|
|
|
}
|
|
|
|
|
2019-04-05 12:47:26 +03:00
|
|
|
} else {
|
2019-04-03 17:45:39 +03:00
|
|
|
if strings.HasPrefix(line, "interface ") {
|
2019-04-05 12:47:26 +03:00
|
|
|
// we found another interface - reset our state
|
|
|
|
withinInterfaceCtx = false
|
2019-04-03 17:45:39 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(line, "static ip_address=") {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get IP address with netmask
|
|
|
|
func getFullIP(ifaceName string) string {
|
|
|
|
cmd := exec.Command("ip", "-oneline", "-family", "inet", "address", "show", ifaceName)
|
|
|
|
log.Tracef("executing %s %v", cmd.Path, cmd.Args)
|
|
|
|
d, err := cmd.Output()
|
|
|
|
if err != nil || cmd.ProcessState.ExitCode() != 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
fields := strings.Fields(string(d))
|
|
|
|
if len(fields) < 4 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
_, _, err = net.ParseCIDR(fields[3])
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return fields[3]
|
|
|
|
}
|
|
|
|
|
2019-04-03 19:36:20 +03:00
|
|
|
// Get gateway IP address
|
|
|
|
func getGatewayIP(ifaceName string) string {
|
|
|
|
cmd := exec.Command("ip", "route", "show", "dev", ifaceName)
|
|
|
|
log.Tracef("executing %s %v", cmd.Path, cmd.Args)
|
|
|
|
d, err := cmd.Output()
|
|
|
|
if err != nil || cmd.ProcessState.ExitCode() != 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
fields := strings.Fields(string(d))
|
|
|
|
if len(fields) < 3 || fields[0] != "default" {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
ip := net.ParseIP(fields[2])
|
|
|
|
if ip == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return fields[2]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set a static IP for network interface
|
|
|
|
func setStaticIP(ifaceName string) error {
|
|
|
|
ip := getFullIP(ifaceName)
|
|
|
|
if len(ip) == 0 {
|
|
|
|
return errors.New("Can't get IP address")
|
|
|
|
}
|
|
|
|
|
|
|
|
body, err := ioutil.ReadFile("/etc/dhcpcd.conf")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ip4, _, err := net.ParseCIDR(ip)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
add := fmt.Sprintf("\ninterface %s\nstatic ip_address=%s\n",
|
|
|
|
ifaceName, ip)
|
|
|
|
body = append(body, []byte(add)...)
|
|
|
|
|
|
|
|
gatewayIP := getGatewayIP(ifaceName)
|
|
|
|
if len(gatewayIP) != 0 {
|
|
|
|
add = fmt.Sprintf("static routers=%s\n",
|
|
|
|
gatewayIP)
|
|
|
|
body = append(body, []byte(add)...)
|
|
|
|
}
|
|
|
|
|
|
|
|
add = fmt.Sprintf("static domain_name_servers=%s\n\n",
|
|
|
|
ip4)
|
|
|
|
body = append(body, []byte(add)...)
|
|
|
|
|
|
|
|
err = file.SafeWrite("/etc/dhcpcd.conf", body)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-05-14 13:02:04 +03:00
|
|
|
func handleDHCPAddStaticLease(w http.ResponseWriter, r *http.Request) {
|
|
|
|
log.Tracef("%s %v", r.Method, r.URL)
|
|
|
|
|
|
|
|
lj := leaseJSON{}
|
|
|
|
err := json.NewDecoder(r.Body).Decode(&lj)
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, http.StatusBadRequest, "json.Decode: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ip := parseIPv4(lj.IP)
|
|
|
|
if ip == nil {
|
|
|
|
httpError(w, http.StatusBadRequest, "invalid IP")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
mac, _ := net.ParseMAC(lj.HWAddr)
|
|
|
|
|
|
|
|
lease := dhcpd.Lease{
|
|
|
|
IP: ip,
|
|
|
|
HWAddr: mac,
|
|
|
|
Hostname: lj.Hostname,
|
|
|
|
}
|
|
|
|
err = dhcpServer.AddStaticLease(lease)
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, http.StatusBadRequest, "%s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
returnOK(w)
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleDHCPRemoveStaticLease(w http.ResponseWriter, r *http.Request) {
|
|
|
|
log.Tracef("%s %v", r.Method, r.URL)
|
|
|
|
|
|
|
|
lj := leaseJSON{}
|
|
|
|
err := json.NewDecoder(r.Body).Decode(&lj)
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, http.StatusBadRequest, "json.Decode: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ip := parseIPv4(lj.IP)
|
|
|
|
if ip == nil {
|
|
|
|
httpError(w, http.StatusBadRequest, "invalid IP")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
mac, _ := net.ParseMAC(lj.HWAddr)
|
|
|
|
|
|
|
|
lease := dhcpd.Lease{
|
|
|
|
IP: ip,
|
|
|
|
HWAddr: mac,
|
|
|
|
Hostname: lj.Hostname,
|
|
|
|
}
|
|
|
|
err = dhcpServer.RemoveStaticLease(lease)
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, http.StatusBadRequest, "%s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
returnOK(w)
|
|
|
|
}
|
|
|
|
|
2018-12-28 21:01:16 +03:00
|
|
|
func startDHCPServer() error {
|
2019-01-25 16:01:27 +03:00
|
|
|
if !config.DHCP.Enabled {
|
2018-12-29 15:04:14 +03:00
|
|
|
// not enabled, don't do anything
|
|
|
|
return nil
|
|
|
|
}
|
2019-05-14 16:59:21 +03:00
|
|
|
|
|
|
|
err := dhcpServer.Init(config.DHCP)
|
|
|
|
if err != nil {
|
|
|
|
return errorx.Decorate(err, "Couldn't init DHCP server")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = dhcpServer.Start()
|
2018-12-28 21:01:16 +03:00
|
|
|
if err != nil {
|
|
|
|
return errorx.Decorate(err, "Couldn't start DHCP server")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2019-02-04 13:54:53 +03:00
|
|
|
|
|
|
|
func stopDHCPServer() error {
|
|
|
|
if !config.DHCP.Enabled {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if !dhcpServer.Enabled {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
err := dhcpServer.Stop()
|
|
|
|
if err != nil {
|
|
|
|
return errorx.Decorate(err, "Couldn't stop DHCP server")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|