diff --git a/network/baseinfo/getip.go b/network/baseinfo/getip.go new file mode 100644 index 0000000..6542327 --- /dev/null +++ b/network/baseinfo/getip.go @@ -0,0 +1,43 @@ +package baseinfo + + +import ( + "fmt" + "net" +) + +func getip() { + interfaces, err := net.Interfaces() + if err != nil { + fmt.Println("Error getting network interfaces:", err) + return + } + + for _, iface := range interfaces { + addrs, err := iface.Addrs() + if err != nil { + fmt.Println("Error getting addresses for interface:", iface.Name, err) + continue + } + + fmt.Printf("Interface: %s\n", iface.Name) + 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 { + continue + } + + // Check if the IP is a global unicast address (indicating it's a public IP) + if ip.IsGlobalUnicast() { + fmt.Printf(" IP address: %s\n", ip.String()) + } + } + } +} diff --git a/network/baseinfo/getip_test.go b/network/baseinfo/getip_test.go new file mode 100644 index 0000000..27c70ba --- /dev/null +++ b/network/baseinfo/getip_test.go @@ -0,0 +1,7 @@ +package baseinfo + +import "testing" + +func TestGetIP(t *testing.T) { + getip() +} diff --git a/network/utils/utils.go b/network/utils/utils.go index 5417ae9..6eb8362 100644 --- a/network/utils/utils.go +++ b/network/utils/utils.go @@ -27,13 +27,13 @@ func FetchJsonFromURL(url, netType string, enableHeader bool, additionalHeader s // 创建 HTTP 客户端 client := req.C() - client.SetTimeout(6 * time.Second). + client.SetTimeout(7 * time.Second). SetDial(func(ctx context.Context, network, addr string) (net.Conn, error) { return (&net.Dialer{}).DialContext(ctx, netType, addr) }). - SetTLSHandshakeTimeout(3 * time.Second). - SetResponseHeaderTimeout(3 * time.Second). - SetExpectContinueTimeout(3 * time.Second) + SetTLSHandshakeTimeout(2 * time.Second). + SetResponseHeaderTimeout(2 * time.Second). + SetExpectContinueTimeout(2 * time.Second) client.R(). SetRetryCount(2). SetRetryBackoffInterval(1*time.Second, 2*time.Second). @@ -57,7 +57,7 @@ func FetchJsonFromURL(url, netType string, enableHeader bool, additionalHeader s return nil, fmt.Errorf("Error fetching %s info: %v", url, err) } // 检查响应状态码 - if !resp.IsSuccess() { + if !resp.IsSuccessState() { return nil, fmt.Errorf("Error fetching %s info: status code %d", url, resp.StatusCode) } // 解析 JSON 响应体