*(home): fixed issues with setting static IP on Mac

This commit is contained in:
Andrey Meshkov 2020-02-13 16:47:30 +03:00
commit b4649a6b27
5 changed files with 57 additions and 36 deletions

View File

@ -468,5 +468,6 @@
"set_static_ip": "Set a static IP address", "set_static_ip": "Set a static IP address",
"install_static_ok": "Good news! The static IP address is already configured", "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_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}}</0>. 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}}</0>. 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?"
} }

View File

@ -89,7 +89,7 @@ class Settings extends Component {
<button <button
type="button" type="button"
className="btn btn-outline-primary btn-sm" className="btn btn-outline-primary btn-sm"
onClick={this.handleStaticIp} onClick={() => this.handleStaticIp(ip)}
> >
<Trans>set_static_ip</Trans> <Trans>set_static_ip</Trans>
</button> </button>
@ -133,7 +133,7 @@ class Settings extends Component {
handleFix(web, dns, set_static_ip); handleFix(web, dns, set_static_ip);
}; };
handleStaticIp = () => { handleStaticIp = (ip) => {
const { const {
webIp, webIp,
webPort, webPort,
@ -146,7 +146,9 @@ class Settings extends Component {
const dns = { ip: dnsIp, port: dnsPort, autofix: false }; const dns = { ip: dnsIp, port: dnsPort, autofix: false };
const set_static_ip = true; const set_static_ip = true;
if (window.confirm(this.props.t('confirm_static_ip', { ip }))) {
handleFix(web, dns, set_static_ip); handleFix(web, dns, set_static_ip);
}
}; };
render() { render() {

View File

@ -43,8 +43,7 @@ func SetStaticIP(ifaceName string) error {
} }
if runtime.GOOS == "darwin" { 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) 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 := make([]string, 0)
args = append(args, "-setdnsservers") args = append(args, "-setdnsservers", portInfo.name)
args = append(args, dnsAddrs...) args = append(args, dnsAddrs...)
// Setting DNS servers is necessary when configuring a static IP // Setting DNS servers is necessary when configuring a static IP

View File

@ -70,11 +70,11 @@ type checkConfigReqEnt struct {
Port int `json:"port"` Port int `json:"port"`
IP string `json:"ip"` IP string `json:"ip"`
Autofix bool `json:"autofix"` Autofix bool `json:"autofix"`
SetStaticIP bool `json:"set_static_ip"`
} }
type checkConfigReq struct { type checkConfigReq struct {
Web checkConfigReqEnt `json:"web"` Web checkConfigReqEnt `json:"web"`
DNS checkConfigReqEnt `json:"dns"` DNS checkConfigReqEnt `json:"dns"`
SetStaticIP bool `json:"set_static_ip"`
} }
type checkConfigRespEnt struct { type checkConfigRespEnt struct {
@ -135,28 +135,7 @@ func handleInstallCheckConfig(w http.ResponseWriter, r *http.Request) {
if err != nil { if err != nil {
respData.DNS.Status = fmt.Sprintf("%v", err) respData.DNS.Status = fmt.Sprintf("%v", err)
} else { } else {
interfaceName := util.GetInterfaceByIP(reqData.DNS.IP) respData.StaticIP = handleStaticIP(reqData.DNS.IP, reqData.SetStaticIP)
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
} }
} }
@ -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 // Check if DNSStubListener is active
func checkDNSStubListener() bool { func checkDNSStubListener() bool {
if runtime.GOOS != "linux" { if runtime.GOOS != "linux" {

View File

@ -27,7 +27,7 @@ func RunCommand(command string, arguments ...string) (int, string, error) {
cmd := exec.Command(command, arguments...) cmd := exec.Command(command, arguments...)
out, err := cmd.Output() out, err := cmd.Output()
if err != nil { 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 return cmd.ProcessState.ExitCode(), string(out), nil