[agent-go] - 增加公网IP部分
This commit is contained in:
@@ -5,7 +5,9 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/shirou/gopsutil/v3/net"
|
||||
"io"
|
||||
net2 "net"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -20,6 +22,53 @@ type NetworkMetric struct {
|
||||
RecvSpeed float64
|
||||
}
|
||||
|
||||
type PublicNetworkInfo struct {
|
||||
IP string `json:"ip,omitempty"`
|
||||
City string `json:"city,omitempty"`
|
||||
Region string `json:"region,omitempty"`
|
||||
Country string `json:"country,omitempty"`
|
||||
Loc string `json:"loc,omitempty"`
|
||||
Org string `json:"org,omitempty"`
|
||||
Postal string `json:"postal,omitempty"`
|
||||
Timezone string `json:"timezone,omitempty"`
|
||||
}
|
||||
|
||||
func (pn *PublicNetworkInfo) GetPublicNetworkInfo() {
|
||||
|
||||
url := "https://ipinfo.io"
|
||||
req, err := http.NewRequest("GET", url, nil)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
req.Header.Set("Authorization", "Bearer 6ecb0db9bd8f19")
|
||||
client := &http.Client{
|
||||
Timeout: 5 * time.Second, // set timeout to 10 seconds
|
||||
}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
err = json.Unmarshal(body, &pn)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
//utils.BeautifulPrint(pn)
|
||||
|
||||
}
|
||||
|
||||
type NetworkInfo struct {
|
||||
Name string `json:"name"` // e.g., "en0", "lo0", "eth0.100"
|
||||
MTU int `json:"mtu"` // maximum transmission unit
|
||||
|
||||
@@ -42,3 +42,9 @@ func TestGetNetworkInfo(t *testing.T) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestPublicNetworkInfo_GetPublicNetworkInfo(t *testing.T) {
|
||||
p := &PublicNetworkInfo{}
|
||||
|
||||
p.GetPublicNetworkInfo()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user