From 64744e218a50d8b3819115c5e66dbbab533cbe93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrian=20Z=C3=BCrcher?= Date: Sat, 17 Jan 2026 22:41:42 +0100 Subject: [PATCH] add feature check if ip is active --- handlers/infos.go | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/handlers/infos.go b/handlers/infos.go index abfaf96..0f6bcd8 100644 --- a/handlers/infos.go +++ b/handlers/infos.go @@ -5,32 +5,34 @@ import ( "net" "net/http" "slideshowApp/env" - "strings" ) // Helper to get the local network IP address -func getLocalIP() string { +func GetLocalIP() string { if env.Host.GetValue() != "0.0.0.0" && env.Host.GetValue() != "localhost" { return env.Host.GetValue() } - addrs, err := net.InterfaceAddrs() - if err != nil { - return "localhost" - } - for _, address := range addrs { - if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() { - if ipnet.IP.To4() != nil { - if env.Env.GetValue() == "development" && !strings.Contains(ipnet.IP.String(), "192.168") { - continue - } - return ipnet.IP.String() - } - } + if ip := getActiveIP(); ip != "" { + return ip } return "localhost" } +func getActiveIP() string { + conn, err := net.Dial("udp", "8.8.8.8:80") + if err != nil { + return "" + } + defer conn.Close() + + addr := conn.LocalAddr() + if udpAddr, ok := addr.(*net.UDPAddr); ok { + return udpAddr.IP.String() + } + return "" +} + func InfoHandler(w http.ResponseWriter, r *http.Request) { port := env.Port.GetValue() if port == "" { @@ -43,7 +45,7 @@ func InfoHandler(w http.ResponseWriter, r *http.Request) { } data := map[string]string{ - "ip": getLocalIP(), + "ip": GetLocalIP(), "port": port, "speed": speed, }