add feature check if ip is active
This commit is contained in:
@@ -5,30 +5,32 @@ 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 != "" {
|
||||||
|
return ip
|
||||||
|
}
|
||||||
|
return "localhost"
|
||||||
|
}
|
||||||
|
|
||||||
|
func getActiveIP() string {
|
||||||
|
conn, err := net.Dial("udp", "8.8.8.8:80")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "localhost"
|
return ""
|
||||||
}
|
}
|
||||||
for _, address := range addrs {
|
defer conn.Close()
|
||||||
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
|
|
||||||
if ipnet.IP.To4() != nil {
|
addr := conn.LocalAddr()
|
||||||
if env.Env.GetValue() == "development" && !strings.Contains(ipnet.IP.String(), "192.168") {
|
if udpAddr, ok := addr.(*net.UDPAddr); ok {
|
||||||
continue
|
return udpAddr.IP.String()
|
||||||
}
|
}
|
||||||
return ipnet.IP.String()
|
return ""
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return "localhost"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func InfoHandler(w http.ResponseWriter, r *http.Request) {
|
func InfoHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
@@ -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,
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user