From d97c4266464c7183de000f82fda0c6937f570bee Mon Sep 17 00:00:00 2001 From: Eugene Bujak Date: Fri, 1 Feb 2019 18:18:08 +0300 Subject: [PATCH] Fill out port 80 if it's available, otherwise port 3000 --- control.go | 21 +++++++++++++++++---- helpers.go | 10 ++++++++++ 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/control.go b/control.go index 7acc58ae..e174c622 100644 --- a/control.go +++ b/control.go @@ -695,8 +695,9 @@ func handleSafeSearchStatus(w http.ResponseWriter, r *http.Request) { } type ipport struct { - IP string `json:"ip"` - Port int `json:"port"` + IP string `json:"ip"` + Port int `json:"port"` + Warning string `json:"warning"` } type firstRunData struct { @@ -720,8 +721,20 @@ func handleGetDefaultAddresses(w http.ResponseWriter, r *http.Request) { } // fill out the fields - data.Web.Port = 3000 // TODO: find out if port 80 is available -- if not, fall back to 3000 - data.DNS.Port = 53 // TODO: find out if port 53 is available -- if not, show a big warning + + // find out if port 80 is available -- if not, fall back to 3000 + if checkPortAvailable(80) { + data.Web.Port = 80 + } else { + data.Web.Port = 3000 + } + + // find out if port 53 is available -- if not, show a big warning + data.DNS.Port = 53 + if !checkPortAvailable(53) { + data.DNS.Warning = "Port 53 is not available for binding -- this will make DNS clients unable to contact AdGuard Home." + } + data.Interfaces = make(map[string]interface{}) for _, iface := range ifaces { data.Interfaces[iface.Name] = iface diff --git a/helpers.go b/helpers.go index 53e6f32d..f83b9821 100644 --- a/helpers.go +++ b/helpers.go @@ -258,6 +258,16 @@ func findIPv4IfaceAddr(ifaces []netInterface) string { return "" } +// checkPortAvailable is not a cheap test to see if the port is bindable, because it's actually doing the bind momentarily +func checkPortAvailable(port int) bool { + ln, err := net.Listen("tcp", fmt.Sprintf(":%d", port)) + if err != nil { + return false + } + ln.Close() + return true +} + // --------------------- // debug logging helpers // ---------------------