fix loopback check

This commit is contained in:
hardy 2023-06-09 20:23:24 +08:00
parent a29e969eed
commit 9df2cf7bfe
No known key found for this signature in database
GPG Key ID: D1ED7F7A9ED520C3
1 changed files with 38 additions and 40 deletions

View File

@ -7,6 +7,11 @@ package api
import ( import (
"context" "context"
"fmt" "fmt"
"net"
"net/http"
"strconv"
"time"
log "github.com/cihub/seelog" log "github.com/cihub/seelog"
"infini.sh/console/modules/agent/client" "infini.sh/console/modules/agent/client"
common2 "infini.sh/console/modules/agent/common" common2 "infini.sh/console/modules/agent/common"
@ -21,10 +26,6 @@ import (
"infini.sh/framework/core/util" "infini.sh/framework/core/util"
elastic2 "infini.sh/framework/modules/elastic" elastic2 "infini.sh/framework/modules/elastic"
"infini.sh/framework/modules/elastic/common" "infini.sh/framework/modules/elastic/common"
"net"
"net/http"
"strconv"
"time"
) )
type APIHandler struct { type APIHandler struct {
@ -45,7 +46,7 @@ func (h *APIHandler) createInstance(w http.ResponseWriter, req *http.Request, ps
if v, ok := tokens.Load(token); !ok { if v, ok := tokens.Load(token); !ok {
h.WriteError(w, "token is invalid", http.StatusUnauthorized) h.WriteError(w, "token is invalid", http.StatusUnauthorized)
return return
}else{ } else {
if t, ok := v.(*Token); !ok || t.CreatedAt.Add(ExpiredIn).Before(time.Now()) { if t, ok := v.(*Token); !ok || t.CreatedAt.Add(ExpiredIn).Before(time.Now()) {
tokens.Delete(token) tokens.Delete(token)
h.WriteError(w, "token was expired", http.StatusUnauthorized) h.WriteError(w, "token was expired", http.StatusUnauthorized)
@ -67,16 +68,16 @@ func (h *APIHandler) createInstance(w http.ResponseWriter, req *http.Request, ps
res, err := client.GetClient().GetInstanceBasicInfo(context.Background(), obj.GetEndpoint()) res, err := client.GetClient().GetInstanceBasicInfo(context.Background(), obj.GetEndpoint())
if err != nil { if err != nil {
errStr := fmt.Sprintf("get agent instance basic info error: %s", err.Error()) errStr := fmt.Sprintf("get agent instance basic info error: %s", err.Error())
h.WriteError(w,errStr , http.StatusInternalServerError) h.WriteError(w, errStr, http.StatusInternalServerError)
log.Error(errStr) log.Error(errStr)
return return
} }
if res.ID == "" { if res.ID == "" {
errStr :=fmt.Sprintf("got unexpected response of agent instance basic info: %s", util.MustToJSON(res)) errStr := fmt.Sprintf("got unexpected response of agent instance basic info: %s", util.MustToJSON(res))
h.WriteError(w, errStr , http.StatusInternalServerError) h.WriteError(w, errStr, http.StatusInternalServerError)
log.Error(errStr) log.Error(errStr)
return return
}else{ } else {
obj.ID = res.ID obj.ID = res.ID
obj.Version = res.Version obj.Version = res.Version
obj.MajorIP = res.MajorIP obj.MajorIP = res.MajorIP
@ -122,7 +123,6 @@ func (h *APIHandler) createInstance(w http.ResponseWriter, req *http.Request, ps
} }
func (h *APIHandler) getInstance(w http.ResponseWriter, req *http.Request, ps httprouter.Params) { func (h *APIHandler) getInstance(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
id := ps.MustGetParameter("instance_id") id := ps.MustGetParameter("instance_id")
@ -275,7 +275,7 @@ func (h *APIHandler) getInstanceStats(w http.ResponseWriter, req *http.Request,
if itemV, ok := item.(map[string]interface{}); ok { if itemV, ok := item.(map[string]interface{}); ok {
if agentID, ok := util.GetMapValueByKeys([]string{"agent", "id"}, itemV); ok { if agentID, ok := util.GetMapValueByKeys([]string{"agent", "id"}, itemV); ok {
if v, ok := agentID.(string); ok { if v, ok := agentID.(string); ok {
if ab, ok := util.GetMapValueByKeys([]string{"payload","instance", "system"}, itemV); ok{ if ab, ok := util.GetMapValueByKeys([]string{"payload", "instance", "system"}, itemV); ok {
if abV, ok := ab.(map[string]interface{}); ok { if abV, ok := ab.(map[string]interface{}); ok {
result[v] = util.MapStr{ result[v] = util.MapStr{
"timestamp": itemV["timestamp"], "timestamp": itemV["timestamp"],
@ -342,7 +342,6 @@ func (h *APIHandler) updateInstance(w http.ResponseWriter, req *http.Request, ps
}, 200) }, 200)
} }
func (h *APIHandler) searchInstance(w http.ResponseWriter, req *http.Request, ps httprouter.Params) { func (h *APIHandler) searchInstance(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
var ( var (
@ -423,7 +422,7 @@ func (h *APIHandler) getESNodesInfo(w http.ResponseWriter, req *http.Request, ps
h.WriteJSON(w, nodes, http.StatusOK) h.WriteJSON(w, nodes, http.StatusOK)
} }
func (h *APIHandler) refreshESNodesInfo(w http.ResponseWriter, req *http.Request, ps httprouter.Params){ func (h *APIHandler) refreshESNodesInfo(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
id := ps.MustGetParameter("instance_id") id := ps.MustGetParameter("instance_id")
obj := agent.Instance{} obj := agent.Instance{}
obj.ID = id obj.ID = id
@ -498,7 +497,7 @@ func (h *APIHandler) authESNode(w http.ResponseWriter, req *http.Request, ps htt
h.WriteError(w, err.Error(), http.StatusInternalServerError) h.WriteError(w, err.Error(), http.StatusInternalServerError)
return return
} }
if !util.StringInArray(inst.IPS, host) { if !util.StringInArray(inst.IPS, host) && !net.ParseIP(host).IsLoopback() {
h.WriteError(w, fmt.Sprintf("got node host %s not match any ip of %v", host, inst.IPS), http.StatusInternalServerError) h.WriteError(w, fmt.Sprintf("got node host %s not match any ip of %v", host, inst.IPS), http.StatusInternalServerError)
return return
} }
@ -671,7 +670,7 @@ func refreshNodesInfo(inst *agent.Instance) ([]agent.ESNodeInfo, error) {
node = *oldNode node = *oldNode
} }
oldPids[oldNode.ProcessInfo.PID] = struct{}{} oldPids[oldNode.ProcessInfo.PID] = struct{}{}
}else{ } else {
node.ID = util.GetUUID() node.ID = util.GetUUID()
} }
if node.ClusterUuid != "" { if node.ClusterUuid != "" {
@ -709,7 +708,7 @@ func refreshNodesInfo(inst *agent.Instance) ([]agent.ESNodeInfo, error) {
return resultNodes, nil return resultNodes, nil
} }
func getNodeByPidOrUUID(nodes map[int]*agent.ESNodeInfo, pid int, uuid string) *agent.ESNodeInfo{ func getNodeByPidOrUUID(nodes map[int]*agent.ESNodeInfo, pid int, uuid string) *agent.ESNodeInfo {
if nodes[pid] != nil { if nodes[pid] != nil {
return nodes[pid] return nodes[pid]
} }
@ -721,7 +720,7 @@ func getNodeByPidOrUUID(nodes map[int]*agent.ESNodeInfo, pid int, uuid string) *
return nil return nil
} }
func getNodesInfoFromES(agentID string) (map[int]*agent.ESNodeInfo, error){ func getNodesInfoFromES(agentID string) (map[int]*agent.ESNodeInfo, error) {
query := util.MapStr{ query := util.MapStr{
"size": 100, "size": 100,
"query": util.MapStr{ "query": util.MapStr{
@ -731,7 +730,6 @@ func getNodesInfoFromES(agentID string) (map[int]*agent.ESNodeInfo, error){
}, },
}, },
}, },
} }
q := orm.Query{ q := orm.Query{
RawQuery: util.MustToJSONBytes(query), RawQuery: util.MustToJSONBytes(query),
@ -760,13 +758,13 @@ func pickAgentSettings(settings []agent.Setting, nodeInfo agent.ESNodeInfo) *age
return nil return nil
} }
func getAgentTaskSetting(agentID string, node agent.ESNodeInfo) (*agent.Setting, error){ func getAgentTaskSetting(agentID string, node agent.ESNodeInfo) (*agent.Setting, error) {
taskSetting, err := getSettingsByClusterID(node.ClusterID) taskSetting, err := getSettingsByClusterID(node.ClusterID)
if err != nil { if err != nil {
return nil, err return nil, err
} }
taskSetting.Logs = &model.LogsTask{ taskSetting.Logs = &model.LogsTask{
Enabled:true, Enabled: true,
LogsPath: node.Path.Logs, LogsPath: node.Path.Logs,
} }
return &agent.Setting{ return &agent.Setting{