add feature check if ip is active
This commit is contained in:
@@ -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,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user