Fill out port 80 if it's available, otherwise port 3000

This commit is contained in:
Eugene Bujak 2019-02-01 18:18:08 +03:00
parent 34e14930de
commit d97c426646
2 changed files with 27 additions and 4 deletions

View File

@ -697,6 +697,7 @@ func handleSafeSearchStatus(w http.ResponseWriter, r *http.Request) {
type ipport struct {
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

View File

@ -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
// ---------------------