Download utils/network.go from bisheng13/nofx13: direct link, hf CLI and curl.
- Browser
- Download file 4.81 kB
-
https://huggingface.co/spaces/bisheng13/nofx13/resolve/main/utils/network.go
- Command line
-
hf download hf://spaces/bisheng13/nofx13/utils/network.go
-
curl -L -o network.go https://huggingface.co/spaces/bisheng13/nofx13/resolve/main/utils/network.go
4.81 kB
| package utils | |
| import ( | |
| "encoding/json" | |
| "fmt" | |
| "io" | |
| "net" | |
| "net/http" | |
| "strings" | |
| "time" | |
| ) | |
| // NetworkInfo 网络信息结构体 | |
| type NetworkInfo struct { | |
| InternalIPv4 string `json:"internal_ipv4"` | |
| ExternalIPv4 string `json:"external_ipv4"` | |
| Hostname string `json:"hostname"` | |
| Interfaces []string `json:"interfaces"` | |
| Region string `json:"region,omitempty"` | |
| Platform string `json:"platform"` | |
| } | |
| // GetNetworkInfo 获取完整的网络信息 | |
| func GetNetworkInfo() *NetworkInfo { | |
| info := &NetworkInfo{ | |
| Platform: "huggingface-space", | |
| } | |
| // 获取内部 IP | |
| info.InternalIPv4 = getInternalIP() | |
| // 获取外部 IP | |
| info.ExternalIPv4 = getExternalIP() | |
| // 获取主机名 | |
| info.Hostname = getHostname() | |
| // 获取网络接口 | |
| info.Interfaces = getNetworkInterfaces() | |
| // 检测运行平台 | |
| info.Platform = detectPlatform() | |
| return info | |
| } | |
| // getInternalIP 获取内部 IPv4 地址 | |
| func getInternalIP() string { | |
| interfaces, err := net.Interfaces() | |
| if err != nil { | |
| return "unknown" | |
| } | |
| for _, iface := range interfaces { | |
| // 跳过回环接口和未启用的接口 | |
| if iface.Flags&net.FlagUp == 0 || iface.Flags&net.FlagLoopback != 0 { | |
| continue | |
| } | |
| addrs, err := iface.Addrs() | |
| if err != nil { | |
| continue | |
| } | |
| for _, addr := range addrs { | |
| var ip net.IP | |
| switch v := addr.(type) { | |
| case *net.IPNet: | |
| ip = v.IP | |
| case *net.IPAddr: | |
| ip = v.IP | |
| } | |
| // 只返回 IPv4 地址 | |
| if ip != nil && ip.To4() != nil && !ip.IsLoopback() { | |
| return ip.String() | |
| } | |
| } | |
| } | |
| return "unknown" | |
| } | |
| // getExternalIP 获取外部 IPv4 地址 | |
| func getExternalIP() string { | |
| // 使用多个服务提高可靠性 | |
| services := []string{ | |
| "https://api.ipify.org", | |
| "https://checkip.amazonaws.com", | |
| "https://icanhazip.com", | |
| "https://ident.me", | |
| } | |
| client := &http.Client{Timeout: 5 * time.Second} | |
| for _, service := range services { | |
| resp, err := client.Get(service) | |
| if err != nil { | |
| continue | |
| } | |
| defer resp.Body.Close() | |
| if resp.StatusCode == http.StatusOK { | |
| body, err := io.ReadAll(resp.Body) | |
| if err == nil { | |
| ip := strings.TrimSpace(string(body)) | |
| // 验证是否是有效的 IPv4 地址 | |
| if net.ParseIP(ip) != nil && ip.To4() != nil { | |
| return ip | |
| } | |
| } | |
| } | |
| } | |
| return "unknown" | |
| } | |
| // getHostname 获取主机名 | |
| func getHostname() string { | |
| hostname, err := os.Hostname() | |
| if err != nil { | |
| return "unknown" | |
| } | |
| return hostname | |
| } | |
| // getNetworkInterfaces 获取网络接口列表 | |
| func getNetworkInterfaces() []string { | |
| interfaces, err := net.Interfaces() | |
| if err != nil { | |
| return []string{"unknown"} | |
| } | |
| var ifaces []string | |
| for _, iface := range interfaces { | |
| if iface.Flags&net.FlagUp != 0 { | |
| ifaces = append(ifaces, iface.Name) | |
| } | |
| } | |
| if len(ifaces) == 0 { | |
| return []string{"unknown"} | |
| } | |
| return ifaces | |
| } | |
| // detectPlatform 检测运行平台 | |
| func detectPlatform() string { | |
| // 检查 Hugging Face Space 环境变量 | |
| if os.Getenv("HUGGINGFACE_SPACE") == "true" { | |
| return "huggingface-space" | |
| } | |
| // 检查其他云平台标识 | |
| if os.Getenv("SPACE_ID") != "" { | |
| return "huggingface-space" | |
| } | |
| // 检查 Docker 环境 | |
| if _, err := os.Stat("/.dockerenv"); err == nil { | |
| return "docker-container" | |
| } | |
| // 检查 Kubernetes | |
| if os.Getenv("KUBERNETES_SERVICE_HOST") != "" { | |
| return "kubernetes" | |
| } | |
| return "unknown" | |
| } | |
| // GetNetworkInfoJSON 获取网络信息的 JSON 格式 | |
| func GetNetworkInfoJSON() string { | |
| info := GetNetworkInfo() | |
| data, err := json.MarshalIndent(info, "", " ") | |
| if err != nil { | |
| return fmt.Sprintf(`{"error": "%s"}`, err.Error()) | |
| } | |
| return string(data) | |
| } | |
| // PrintNetworkInfo 打印网络信息(用于启动时显示) | |
| func PrintNetworkInfo() { | |
| info := GetNetworkInfo() | |
| fmt.Println("🌐 运行环境网络信息:") | |
| fmt.Printf(" 平台: %s\n", info.Platform) | |
| fmt.Printf(" 内部 IPv4: %s\n", info.InternalIPv4) | |
| fmt.Printf(" 外部 IPv4: %s\n", info.ExternalIPv4) | |
| fmt.Printf(" 主机名: %s\n", info.Hostname) | |
| fmt.Printf(" 网络接口: %s\n", strings.Join(info.Interfaces, ", ")) | |
| fmt.Println() | |
| } |