On avance...

This commit is contained in:
2025-09-06 17:45:28 +02:00
parent 83a0b62dd3
commit d1132e00cd
81 changed files with 6656 additions and 475 deletions

17
netutils/ip_detect.go Normal file
View File

@@ -0,0 +1,17 @@
package netutils
import (
"net"
)
// Fonction helper (remplace votre netutils.GuessLocalIP)
func GuessLocalIP() (string, error) {
conn, err := net.Dial("udp", "8.8.8.8:80")
if err != nil {
return "127.0.0.1", nil
}
defer conn.Close()
localAddr := conn.LocalAddr().(*net.UDPAddr)
return localAddr.IP.String(), nil
}

49
netutils/list_all_ip.go Normal file
View File

@@ -0,0 +1,49 @@
package netutils
import (
"net"
)
// ListAllIPs returns a map of interface names to their associated IPv4 addresses.
func ListAllIPs() map[string][]string {
result := make(map[string][]string)
ifaces, err := net.Interfaces()
if err != nil {
result["error"] = []string{err.Error()}
return result
}
for _, iface := range ifaces {
if iface.Flags&net.FlagUp == 0 {
continue // Ignore down interfaces
}
addrs, err := iface.Addrs()
if err != nil {
continue
}
var ips []string
for _, addr := range addrs {
var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
}
if ip == nil || ip.To4() == nil || ip.IsLoopback() {
continue
}
ips = append(ips, ip.String())
}
if len(ips) > 0 {
result[iface.Name] = ips
}
}
return result
}