This commit is contained in:
spiritlhl
2024-07-29 03:05:23 +00:00
parent 5246749704
commit c06aaee987
3 changed files with 55 additions and 5 deletions

43
network/baseinfo/getip.go Normal file
View File

@@ -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())
}
}
}
}

View File

@@ -0,0 +1,7 @@
package baseinfo
import "testing"
func TestGetIP(t *testing.T) {
getip()
}

View File

@@ -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 响应体