diff --git a/client/src/__locales/en.json b/client/src/__locales/en.json index a4f8279c..74ac7021 100644 --- a/client/src/__locales/en.json +++ b/client/src/__locales/en.json @@ -468,5 +468,6 @@ "set_static_ip": "Set a static IP address", "install_static_ok": "Good news! The static IP address is already configured", "install_static_error": "AdGuard Home cannot configure it automatically for this network interface. Please look for an instruction on how to do this manually.", - "install_static_configure": "We have detected that a dynamic IP address is used — <0>{{ip}}. Do you want to use it as your static address?" + "install_static_configure": "We have detected that a dynamic IP address is used — <0>{{ip}}. Do you want to use it as your static address?", + "confirm_static_ip": "AdGuard Home will configure {{ip}} to be your static IP address. Do you want to proceed?" } diff --git a/client/src/install/Setup/Settings.js b/client/src/install/Setup/Settings.js index 1c4da3fa..876aa05b 100644 --- a/client/src/install/Setup/Settings.js +++ b/client/src/install/Setup/Settings.js @@ -89,7 +89,7 @@ class Settings extends Component { @@ -133,7 +133,7 @@ class Settings extends Component { handleFix(web, dns, set_static_ip); }; - handleStaticIp = () => { + handleStaticIp = (ip) => { const { webIp, webPort, @@ -146,7 +146,9 @@ class Settings extends Component { const dns = { ip: dnsIp, port: dnsPort, autofix: false }; const set_static_ip = true; - handleFix(web, dns, set_static_ip); + if (window.confirm(this.props.t('confirm_static_ip', { ip }))) { + handleFix(web, dns, set_static_ip); + } }; render() { diff --git a/dhcpd/network_utils.go b/dhcpd/network_utils.go index 8407e031..79c13285 100644 --- a/dhcpd/network_utils.go +++ b/dhcpd/network_utils.go @@ -43,8 +43,7 @@ func SetStaticIP(ifaceName string) error { } if runtime.GOOS == "darwin" { - return fmt.Errorf("cannot do that") - // return setStaticIPDarwin(ifaceName) + return setStaticIPDarwin(ifaceName) } return fmt.Errorf("Cannot set static IP on %s", runtime.GOOS) @@ -190,7 +189,7 @@ func setStaticIPDarwin(ifaceName string) error { } args := make([]string, 0) - args = append(args, "-setdnsservers") + args = append(args, "-setdnsservers", portInfo.name) args = append(args, dnsAddrs...) // Setting DNS servers is necessary when configuring a static IP diff --git a/home/control_install.go b/home/control_install.go index 50d6e7c1..17ffafa9 100644 --- a/home/control_install.go +++ b/home/control_install.go @@ -67,14 +67,14 @@ func handleInstallGetAddresses(w http.ResponseWriter, r *http.Request) { } type checkConfigReqEnt struct { - Port int `json:"port"` - IP string `json:"ip"` - Autofix bool `json:"autofix"` - SetStaticIP bool `json:"set_static_ip"` + Port int `json:"port"` + IP string `json:"ip"` + Autofix bool `json:"autofix"` } type checkConfigReq struct { - Web checkConfigReqEnt `json:"web"` - DNS checkConfigReqEnt `json:"dns"` + Web checkConfigReqEnt `json:"web"` + DNS checkConfigReqEnt `json:"dns"` + SetStaticIP bool `json:"set_static_ip"` } type checkConfigRespEnt struct { @@ -135,28 +135,7 @@ func handleInstallCheckConfig(w http.ResponseWriter, r *http.Request) { if err != nil { respData.DNS.Status = fmt.Sprintf("%v", err) } else { - interfaceName := util.GetInterfaceByIP(reqData.DNS.IP) - staticIPStatus := "yes" - - if len(interfaceName) == 0 { - staticIPStatus = "error" - respData.StaticIP.Error = fmt.Sprintf("Couldn't find network interface by IP %s", reqData.DNS.IP) - } else if reqData.DNS.SetStaticIP { - err = dhcpd.SetStaticIP(interfaceName) - staticIPStatus = "error" - respData.StaticIP.Error = err.Error() - } else { - // check if we have a static IP - isStaticIP, err := dhcpd.HasStaticIP(interfaceName) - if err != nil { - staticIPStatus = "error" - respData.StaticIP.Error = err.Error() - } else if !isStaticIP { - staticIPStatus = "no" - respData.StaticIP.IP = util.GetSubnet(interfaceName) - } - } - respData.StaticIP.Static = staticIPStatus + respData.StaticIP = handleStaticIP(reqData.DNS.IP, reqData.SetStaticIP) } } @@ -168,6 +147,46 @@ func handleInstallCheckConfig(w http.ResponseWriter, r *http.Request) { } } +// handleStaticIP - handles static IP request +// It either checks if we have a static IP +// Or if set=true, it tries to set it +func handleStaticIP(ip string, set bool) staticIPJSON { + resp := staticIPJSON{} + + interfaceName := util.GetInterfaceByIP(ip) + resp.Static = "no" + + if len(interfaceName) == 0 { + resp.Static = "error" + resp.Error = fmt.Sprintf("Couldn't find network interface by IP %s", ip) + return resp + } + + if set { + // Try to set static IP for the specified interface + err := dhcpd.SetStaticIP(interfaceName) + if err != nil { + resp.Static = "error" + resp.Error = err.Error() + return resp + } + } + + // Fallthrough here even if we set static IP + // Check if we have a static IP and return the details + isStaticIP, err := dhcpd.HasStaticIP(interfaceName) + if err != nil { + resp.Static = "error" + resp.Error = err.Error() + } else { + if isStaticIP { + resp.Static = "yes" + } + resp.IP = util.GetSubnet(interfaceName) + } + return resp +} + // Check if DNSStubListener is active func checkDNSStubListener() bool { if runtime.GOOS != "linux" { diff --git a/util/helpers.go b/util/helpers.go index 730e0a2c..c50c940d 100644 --- a/util/helpers.go +++ b/util/helpers.go @@ -27,7 +27,7 @@ func RunCommand(command string, arguments ...string) (int, string, error) { cmd := exec.Command(command, arguments...) out, err := cmd.Output() if err != nil { - return 1, "", fmt.Errorf("exec.Command(%s) failed: %s", command, err) + return 1, "", fmt.Errorf("exec.Command(%s) failed: %v: %s", command, err, string(out)) } return cmd.ProcessState.ExitCode(), string(out), nil