Skip to content
This repository has been archived by the owner on Feb 11, 2022. It is now read-only.

使用内部接口解决阿里云运行IP获取错误 #56

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 50 additions & 1 deletion agent/client/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ type Agent struct {
ctx context.Context
}

type EX_IP struct {
IP string // 外部IP地址
}

var httpClient = &http.Client{
Timeout: time.Second * 10,
Transport: &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}},
Expand Down Expand Up @@ -142,15 +146,60 @@ func (a Agent) getServerList() ([]string, error) {
}

func (a Agent) setLocalIP(ip string) {
var isAliyunECS bool
conn, err := net.Dial("tcp", ip)
if err != nil {
a.log("Net.Dial:", ip)
a.log("Error:", err)
panic(1)
}
defer conn.Close()
common.LocalIP = strings.Split(conn.LocalAddr().String(), ":")[0]
isAliyunECS=a.isAliYun()
if isAliyunECS {
common.LocalIP=a.getExternalIP()
} else {
common.LocalIP = strings.Split(conn.LocalAddr().String(), ":")[0]
}
//

}

func (a Agent) getExternalIP()(ip string) {
var url string
var response_json EX_IP
url = "http://" + a.ServerNetLoc + SERVER_API_IP
a.log("Web API:", url)
request, _ := http.NewRequest("GET", url, nil)
request.Close = true
resp, err := httpClient.Do(request)
if err != nil {
a.log("Error:", err)
panic(1)
}
defer resp.Body.Close()
result, err := ioutil.ReadAll(resp.Body)
if err != nil {
a.log("Error:", err)
panic(1)
}
err = json.Unmarshal([]byte(result), &response_json)
if err != nil {
a.log("Error:", err)
panic(1)
}
return strings.Split(response_json.IP,":")[0]

}


func (a Agent) isAliYun()(result bool) {
data, err := ioutil.ReadFile("/etc/motd")
if err != nil {
a.log("Error:", err)
}
return strings.Contains(string(data),"Alibaba")
}

func (a *Agent) configRefresh() {
ticker := time.NewTicker(time.Second * time.Duration(CONFIGR_REF_INTERVAL))
go func() {
Expand Down
1 change: 1 addition & 0 deletions agent/client/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ const (
CONFIGR_REF_INTERVAL int = 60
FAILMODE client.FailMode = client.Failtry
SERVER_API string = "/json/serverlist"
SERVER_API_IP string ="/json/myip"
TESTMODE bool = false
)
4 changes: 4 additions & 0 deletions web/controllers/agentapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ func (c *AgentApiController) Get() {
c.Data["json"] = GetAliveServerList()
}

if strings.Contains(currentURL, "myip") {
c.Data["json"] = bson.M{"ip": c.Ctx.Request.RemoteAddr}
}

if strings.Contains(currentURL, "dbinfo") {
esurl := beego.AppConfig.String("elastic_search::baseurl")
mgourl := beego.AppConfig.String("mongodb::url")
Expand Down
1 change: 1 addition & 0 deletions web/routers/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ func init() {
beego.NSRouter("/client", &controllers.ClientController{}),
beego.NSRouter("/download", &controllers.DloadController{}),
beego.NSRouter("/serverlist", &controllers.AgentApiController{}),
beego.NSRouter("/myip", &controllers.AgentApiController{}),
beego.NSRouter("/publickey", &controllers.AgentApiController{}),
beego.NSRouter("/dbinfo", &controllers.AgentApiController{}),
beego.NSRouter("/statistics", &controllers.StatisticsController{}),
Expand Down