add feature check if ip is active

This commit is contained in:
Adrian Zürcher
2026-01-17 22:41:42 +01:00
parent 7d3db6b485
commit 64744e218a

View File

@@ -5,32 +5,34 @@ import (
"net" "net"
"net/http" "net/http"
"slideshowApp/env" "slideshowApp/env"
"strings"
) )
// Helper to get the local network IP address // 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" { if env.Host.GetValue() != "0.0.0.0" && env.Host.GetValue() != "localhost" {
return env.Host.GetValue() return env.Host.GetValue()
} }
addrs, err := net.InterfaceAddrs() if ip := getActiveIP(); ip != "" {
if err != nil { return ip
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()
}
}
} }
return "localhost" 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) { func InfoHandler(w http.ResponseWriter, r *http.Request) {
port := env.Port.GetValue() port := env.Port.GetValue()
if port == "" { if port == "" {
@@ -43,7 +45,7 @@ func InfoHandler(w http.ResponseWriter, r *http.Request) {
} }
data := map[string]string{ data := map[string]string{
"ip": getLocalIP(), "ip": GetLocalIP(),
"port": port, "port": port,
"speed": speed, "speed": speed,
} }