[agent-go] - 增加公网IP部分

This commit is contained in:
zeaslity
2024-04-29 10:03:58 +08:00
parent f124972b84
commit 62eb067c5d
6 changed files with 91 additions and 5 deletions

View File

@@ -12,6 +12,11 @@ type AgentServerInfo struct {
ServerIPInV4 string `json:"serverIpInV4" yaml:"serverIpInV4"` ServerIPInV4 string `json:"serverIpInV4" yaml:"serverIpInV4"`
ServerIPPbV6 string `json:"serverIpPbV6" yaml:"serverIpPbV6"` ServerIPPbV6 string `json:"serverIpPbV6" yaml:"serverIpPbV6"`
ServerIPInV6 string `json:"serverIpInV6" yaml:"serverIpInV6"` 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"` Location string `json:"location" yaml:"location"`
Provider string `json:"provider" yaml:"provider"` Provider string `json:"provider" yaml:"provider"`
ManagePort string `json:"managePort" yaml:"managePort"` ManagePort string `json:"managePort" yaml:"managePort"`

View File

@@ -41,6 +41,7 @@ func INIT(octopusAgentConfigFileName string) chan bool {
agentInfo := a_status.ReportAgentInfo() agentInfo := a_status.ReportAgentInfo()
refreshAgentInfoByStatusInfo(agentInfo, agentServerInfo) refreshAgentInfoByStatusInfo(agentInfo, agentServerInfo)
// 如果从在本机文件,那么会使用手动写入的环境变量 进行覆盖
if utils.FileExistAndNotNull(AgentServerInfoLocalFilePath) { 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) { func refreshAgentNetworkInfo(agentInfo *a_status.AgentInfo, agentServerInfo *a_agent.AgentServerInfo) {
// 测试网卡名称 // 获取Agent的公网服务信息
//testCases := []string{"ens33", "eno1", "enp0s3", "enp1s2", "eth0", "enp2s5", "enx1234567890ab", "ens1234567890ab", "enp1234567890ab", "enp1234567890ab", "enp1", "lo","","docker0", "virbr0", "veth0",} publicNetworkInfo := a_status.PublicNetworkInfo{}
//for _, tc := range testCases { publicNetworkInfo.GetPublicNetworkInfo()
// fmt.Printf("Network interface '%s' is %s\n", tc, fmt.Sprintf("%v", isNetworkInterface(tc)))
//} 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 // inner ip v4 v6
for _, networkInfo := range agentInfo.NetworkInfo { for _, networkInfo := range agentInfo.NetworkInfo {

View File

@@ -5,7 +5,9 @@ import (
"errors" "errors"
"fmt" "fmt"
"github.com/shirou/gopsutil/v3/net" "github.com/shirou/gopsutil/v3/net"
"io"
net2 "net" net2 "net"
"net/http"
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
@@ -20,6 +22,53 @@ type NetworkMetric struct {
RecvSpeed float64 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 { type NetworkInfo struct {
Name string `json:"name"` // e.g., "en0", "lo0", "eth0.100" Name string `json:"name"` // e.g., "en0", "lo0", "eth0.100"
MTU int `json:"mtu"` // maximum transmission unit MTU int `json:"mtu"` // maximum transmission unit

View File

@@ -42,3 +42,9 @@ func TestGetNetworkInfo(t *testing.T) {
} }
} }
func TestPublicNetworkInfo_GetPublicNetworkInfo(t *testing.T) {
p := &PublicNetworkInfo{}
p.GetPublicNetworkInfo()
}

View File

@@ -0,0 +1,9 @@
{
"properties": [
{
"name": "octopus.notify.pusher_url",
"type": "java.lang.String",
"description": "Description for octopus.notify.pusher_url."
}
]
}

View File

@@ -93,6 +93,11 @@ mybatis-plus:
# mapper-locations: classpath*:/real-mappers/**/*.xml # mapper-locations: classpath*:/real-mappers/**/*.xml
octopus: octopus:
notify:
# 消息通知的URL地址
pusher_url: http://192.168.35.71:8080
# 发送消息的密钥
access_token: 123456
message: message:
# agent boot up default common exchange # agent boot up default common exchange
init_exchange: InitExchange init_exchange: InitExchange