mirror of
https://github.com/oneclickvirt/basics.git
synced 2025-10-08 10:00:29 +08:00
update
This commit is contained in:
43
network/baseinfo/getip.go
Normal file
43
network/baseinfo/getip.go
Normal 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())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
7
network/baseinfo/getip_test.go
Normal file
7
network/baseinfo/getip_test.go
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
package baseinfo
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestGetIP(t *testing.T) {
|
||||||
|
getip()
|
||||||
|
}
|
@@ -27,13 +27,13 @@ func FetchJsonFromURL(url, netType string, enableHeader bool, additionalHeader s
|
|||||||
|
|
||||||
// 创建 HTTP 客户端
|
// 创建 HTTP 客户端
|
||||||
client := req.C()
|
client := req.C()
|
||||||
client.SetTimeout(6 * time.Second).
|
client.SetTimeout(7 * time.Second).
|
||||||
SetDial(func(ctx context.Context, network, addr string) (net.Conn, error) {
|
SetDial(func(ctx context.Context, network, addr string) (net.Conn, error) {
|
||||||
return (&net.Dialer{}).DialContext(ctx, netType, addr)
|
return (&net.Dialer{}).DialContext(ctx, netType, addr)
|
||||||
}).
|
}).
|
||||||
SetTLSHandshakeTimeout(3 * time.Second).
|
SetTLSHandshakeTimeout(2 * time.Second).
|
||||||
SetResponseHeaderTimeout(3 * time.Second).
|
SetResponseHeaderTimeout(2 * time.Second).
|
||||||
SetExpectContinueTimeout(3 * time.Second)
|
SetExpectContinueTimeout(2 * time.Second)
|
||||||
client.R().
|
client.R().
|
||||||
SetRetryCount(2).
|
SetRetryCount(2).
|
||||||
SetRetryBackoffInterval(1*time.Second, 2*time.Second).
|
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)
|
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)
|
return nil, fmt.Errorf("Error fetching %s info: status code %d", url, resp.StatusCode)
|
||||||
}
|
}
|
||||||
// 解析 JSON 响应体
|
// 解析 JSON 响应体
|
||||||
|
Reference in New Issue
Block a user