This commit is contained in:
TsMask
2023-08-18 09:53:27 +08:00
parent c9a39a348b
commit de86011436

View File

@@ -1,7 +1,6 @@
package monitor
import (
"fmt"
"net/http"
"sort"
"strings"
@@ -42,21 +41,21 @@ var (
// IPAddr IP地址
func IPAddr(w http.ResponseWriter, r *http.Request) {
ipAddrs := make(map[string]string)
ipAddrs := []map[string]string{}
interfaces, err := net.Interfaces()
if err == nil {
for _, iface := range interfaces {
var addrs []string
addrs := map[string]string{}
for _, v := range iface.Addrs {
prefix := strings.Split(v.Addr, "/")[0]
if strings.Contains(prefix, "::") {
addrs = append(addrs, fmt.Sprintf("IPv6 %s", prefix))
addrs["IPv6"] = prefix
}
if strings.Contains(prefix, ".") {
addrs = append(addrs, fmt.Sprintf("IPv4 %s", prefix))
addrs["IPv4"] = prefix
}
}
ipAddrs[iface.Name] = strings.Join(addrs, " / ")
ipAddrs = append(ipAddrs, addrs)
}
}
services.ResponseWithJson(w, 200, ipAddrs)