[agent-go] - 增加公网IP部分
This commit is contained in:
@@ -12,6 +12,11 @@ type AgentServerInfo struct {
|
||||
ServerIPInV4 string `json:"serverIpInV4" yaml:"serverIpInV4"`
|
||||
ServerIPPbV6 string `json:"serverIpPbV6" yaml:"serverIpPbV6"`
|
||||
ServerIPInV6 string `json:"serverIpInV6" yaml:"serverIpInV6"`
|
||||
City string `json:"city" yaml:"city"`
|
||||
Region string `json:"region" yaml:"region"`
|
||||
Country string `json:"country" yaml:"country"`
|
||||
Organization string `json:"organization" yaml:"organization"`
|
||||
TimeZone string `json:"timeZone" yaml:"timeZone"`
|
||||
Location string `json:"location" yaml:"location"`
|
||||
Provider string `json:"provider" yaml:"provider"`
|
||||
ManagePort string `json:"managePort" yaml:"managePort"`
|
||||
|
||||
@@ -41,6 +41,7 @@ func INIT(octopusAgentConfigFileName string) chan bool {
|
||||
agentInfo := a_status.ReportAgentInfo()
|
||||
refreshAgentInfoByStatusInfo(agentInfo, agentServerInfo)
|
||||
|
||||
// 如果从在本机文件,那么会使用手动写入的环境变量 进行覆盖
|
||||
if utils.FileExistAndNotNull(AgentServerInfoLocalFilePath) {
|
||||
|
||||
// 获取系统的环境变量
|
||||
@@ -293,11 +294,22 @@ func refreshAgentInfoByStatusInfo(agentInfo *a_status.AgentInfo, agentServerInfo
|
||||
|
||||
func refreshAgentNetworkInfo(agentInfo *a_status.AgentInfo, agentServerInfo *a_agent.AgentServerInfo) {
|
||||
|
||||
// 测试网卡名称
|
||||
//testCases := []string{"ens33", "eno1", "enp0s3", "enp1s2", "eth0", "enp2s5", "enx1234567890ab", "ens1234567890ab", "enp1234567890ab", "enp1234567890ab", "enp1", "lo","","docker0", "virbr0", "veth0",}
|
||||
//for _, tc := range testCases {
|
||||
// fmt.Printf("Network interface '%s' is %s\n", tc, fmt.Sprintf("%v", isNetworkInterface(tc)))
|
||||
//}
|
||||
// 获取Agent的公网服务信息
|
||||
publicNetworkInfo := a_status.PublicNetworkInfo{}
|
||||
publicNetworkInfo.GetPublicNetworkInfo()
|
||||
|
||||
marshal, _ := json.Marshal(publicNetworkInfo)
|
||||
log.InfoF("refreshAgentNetworkInfo - public network info is %s", marshal)
|
||||
|
||||
if publicNetworkInfo.IP != "" {
|
||||
agentServerInfo.ServerIPPbV4 = publicNetworkInfo.IP
|
||||
agentServerInfo.Region = publicNetworkInfo.Region
|
||||
agentServerInfo.City = publicNetworkInfo.City
|
||||
agentServerInfo.Country = publicNetworkInfo.Country
|
||||
agentServerInfo.Location = publicNetworkInfo.Loc
|
||||
agentServerInfo.Organization = publicNetworkInfo.Org
|
||||
agentServerInfo.TimeZone = publicNetworkInfo.Timezone
|
||||
}
|
||||
|
||||
// inner ip v4 v6
|
||||
for _, networkInfo := range agentInfo.NetworkInfo {
|
||||
|
||||
@@ -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