From 62eb067c5ddceba398b2df4fc7e2d09dee0ceb65 Mon Sep 17 00:00:00 2001 From: zeaslity Date: Mon, 29 Apr 2024 10:03:58 +0800 Subject: [PATCH] =?UTF-8?q?[agent-go]=20-=20=E5=A2=9E=E5=8A=A0=E5=85=AC?= =?UTF-8?q?=E7=BD=91IP=E9=83=A8=E5=88=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- agent-go/a_agent/AgentServerInfo.go | 5 ++ agent-go/a_init/AgentInitialization.go | 22 +++++++-- agent-go/a_status/Network.go | 49 +++++++++++++++++++ agent-go/a_status/Network_test.go | 6 +++ ...itional-spring-configuration-metadata.json | 9 ++++ server/src/main/resources/application.yml | 5 ++ 6 files changed, 91 insertions(+), 5 deletions(-) create mode 100644 server/src/main/resources/META-INF/additional-spring-configuration-metadata.json diff --git a/agent-go/a_agent/AgentServerInfo.go b/agent-go/a_agent/AgentServerInfo.go index cf92421..e30c599 100644 --- a/agent-go/a_agent/AgentServerInfo.go +++ b/agent-go/a_agent/AgentServerInfo.go @@ -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"` diff --git a/agent-go/a_init/AgentInitialization.go b/agent-go/a_init/AgentInitialization.go index c28553f..773f389 100644 --- a/agent-go/a_init/AgentInitialization.go +++ b/agent-go/a_init/AgentInitialization.go @@ -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 { diff --git a/agent-go/a_status/Network.go b/agent-go/a_status/Network.go index 2ca05ca..dd1961d 100644 --- a/agent-go/a_status/Network.go +++ b/agent-go/a_status/Network.go @@ -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 diff --git a/agent-go/a_status/Network_test.go b/agent-go/a_status/Network_test.go index 15ba992..f80005b 100644 --- a/agent-go/a_status/Network_test.go +++ b/agent-go/a_status/Network_test.go @@ -42,3 +42,9 @@ func TestGetNetworkInfo(t *testing.T) { } } + +func TestPublicNetworkInfo_GetPublicNetworkInfo(t *testing.T) { + p := &PublicNetworkInfo{} + + p.GetPublicNetworkInfo() +} diff --git a/server/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/server/src/main/resources/META-INF/additional-spring-configuration-metadata.json new file mode 100644 index 0000000..e8cc669 --- /dev/null +++ b/server/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -0,0 +1,9 @@ +{ + "properties": [ + { + "name": "octopus.notify.pusher_url", + "type": "java.lang.String", + "description": "Description for octopus.notify.pusher_url." + } + ] +} \ No newline at end of file diff --git a/server/src/main/resources/application.yml b/server/src/main/resources/application.yml index 780e185..9013606 100644 --- a/server/src/main/resources/application.yml +++ b/server/src/main/resources/application.yml @@ -93,6 +93,11 @@ mybatis-plus: # mapper-locations: classpath*:/real-mappers/**/*.xml octopus: + notify: + # 消息通知的URL地址 + pusher_url: http://192.168.35.71:8080 + # 发送消息的密钥 + access_token: 123456 message: # agent boot up default common exchange init_exchange: InitExchange