mirror of https://gitee.com/godoos/godoos.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
67 lines
1.4 KiB
67 lines
1.4 KiB
package localchat
|
|
|
|
import (
|
|
"fmt"
|
|
"net"
|
|
"os"
|
|
)
|
|
|
|
// 获取自己的IP地址和主机名
|
|
func GetMyIPAndHostname() (string, string, error) {
|
|
hostname, err := os.Hostname()
|
|
if err != nil {
|
|
return "", "", fmt.Errorf("failed to get hostname: %w", err)
|
|
}
|
|
preferredIP, err := GetMyIp()
|
|
if err != nil {
|
|
return "", "", fmt.Errorf("failed to get IP address: %w", err)
|
|
}
|
|
|
|
return preferredIP, hostname, nil
|
|
}
|
|
func GetMyIp() (string, error) {
|
|
addrs, err := net.Interfaces()
|
|
if err != nil {
|
|
return "", fmt.Errorf("failed to get network interfaces: %w", err)
|
|
}
|
|
|
|
var preferredIP net.IP
|
|
for _, iface := range addrs {
|
|
if iface.Flags&net.FlagUp == 0 {
|
|
// Skip interfaces that are not up
|
|
continue
|
|
}
|
|
ifAddrs, err := iface.Addrs()
|
|
if err != nil {
|
|
continue // Ignore this interface if we can't get its addresses
|
|
}
|
|
for _, addr := range ifAddrs {
|
|
var ip net.IP
|
|
switch v := addr.(type) {
|
|
case *net.IPNet:
|
|
ip = v.IP
|
|
case *net.IPAddr:
|
|
ip = v.IP
|
|
default:
|
|
continue
|
|
}
|
|
if ip.IsLoopback() {
|
|
continue // Skip loopback addresses
|
|
}
|
|
if ip.To4() != nil && (ip.IsPrivate() || ip.IsGlobalUnicast()) {
|
|
// Prefer global unicast or private addresses over link-local
|
|
preferredIP = ip
|
|
break
|
|
}
|
|
}
|
|
if preferredIP != nil {
|
|
// Found a preferred IP, break out of the loop
|
|
break
|
|
}
|
|
}
|
|
|
|
if preferredIP == nil {
|
|
return "", fmt.Errorf("no preferred non-loopback IPv4 address found")
|
|
}
|
|
return preferredIP.String(), nil
|
|
}
|
|
|