增加公网 IP 采集和网卡 IP 展示
This commit is contained in:
1 parent
6fa90252df
commit
be54ef2d31
28 files changed
+956
-59
No files matched your search
@@ -21,7 +21,8 @@ func (h *AgentHandler) Paging(c echo.Context) error {
|
||||
PageRequest(pr).
|
||||
Contains("name", c.QueryParam("name")).
|
||||
Contains("hostname", c.QueryParam("hostname")).
|
||||
Contains("ip", c.QueryParam("ip"))
|
||||
Contains("ipv4", c.QueryParam("ipv4")).
|
||||
Contains("ipv6", c.QueryParam("ipv6"))
|
||||
|
||||
// 处理状态筛选
|
||||
if status == "online" {
|
||||
|
||||
@@ -17,13 +17,15 @@ type AgentHandler struct {
|
||||
ddnsService *service.DDNSService
|
||||
sshLoginService *service.SSHLoginService
|
||||
apiKeyService *service.ApiKeyService
|
||||
propertyService *service.PropertyService
|
||||
wsManager *ws.Manager
|
||||
upgrader websocket.Upgrader
|
||||
}
|
||||
|
||||
func NewAgentHandler(logger *zap.Logger, agentService *service.AgentService, trafficService *service.TrafficService,
|
||||
metricService *service.MetricService, monitorService *service.MonitorService, tamperService *service.TamperService,
|
||||
ddnsService *service.DDNSService, sshLoginService *service.SSHLoginService, apiKeyService *service.ApiKeyService, wsManager *ws.Manager) *AgentHandler {
|
||||
ddnsService *service.DDNSService, sshLoginService *service.SSHLoginService, apiKeyService *service.ApiKeyService,
|
||||
propertyService *service.PropertyService, wsManager *ws.Manager) *AgentHandler {
|
||||
|
||||
h := &AgentHandler{
|
||||
logger: logger,
|
||||
@@ -35,6 +37,7 @@ func NewAgentHandler(logger *zap.Logger, agentService *service.AgentService, tra
|
||||
ddnsService: ddnsService,
|
||||
sshLoginService: sshLoginService,
|
||||
apiKeyService: apiKeyService,
|
||||
propertyService: propertyService,
|
||||
wsManager: wsManager,
|
||||
}
|
||||
|
||||
|
||||
@@ -134,7 +134,8 @@ func (h *AgentHandler) GetLatestMetrics(c echo.Context) error {
|
||||
ctx := c.Request().Context()
|
||||
|
||||
// 验证探针访问权限
|
||||
if _, err := h.agentService.GetAgentByAuth(ctx, id, utils.IsAuthenticated(c)); err != nil {
|
||||
isAuthenticated := utils.IsAuthenticated(c)
|
||||
if _, err := h.agentService.GetAgentByAuth(ctx, id, isAuthenticated); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -142,8 +143,11 @@ func (h *AgentHandler) GetLatestMetrics(c echo.Context) error {
|
||||
if !ok {
|
||||
return orz.NewError(404, "探针最新指标不存在")
|
||||
}
|
||||
// 清空网络接口指标,避免返回敏感信息
|
||||
metrics.NetworkInterfaces = nil
|
||||
if !isAuthenticated {
|
||||
sanitized := *metrics
|
||||
sanitized.NetworkInterfaces = nil
|
||||
return orz.Ok(c, &sanitized)
|
||||
}
|
||||
|
||||
return orz.Ok(c, metrics)
|
||||
}
|
||||
|
||||
@@ -29,7 +29,8 @@ func (h *AgentHandler) Get(c echo.Context) error {
|
||||
|
||||
// 未登录时隐藏敏感信息
|
||||
if !isAuthenticated {
|
||||
agent.IP = ""
|
||||
agent.IPv4 = ""
|
||||
agent.IPv6 = ""
|
||||
agent.Hostname = ""
|
||||
}
|
||||
|
||||
@@ -41,7 +42,8 @@ func (h *AgentHandler) GetAgents(c echo.Context) error {
|
||||
ctx := c.Request().Context()
|
||||
|
||||
// 根据认证状态返回相应的探针列表
|
||||
agents, err := h.agentService.ListByAuth(ctx, utils.IsAuthenticated(c))
|
||||
isAuthenticated := utils.IsAuthenticated(c)
|
||||
agents, err := h.agentService.ListByAuth(ctx, isAuthenticated)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -61,7 +63,7 @@ func (h *AgentHandler) GetAgents(c echo.Context) error {
|
||||
|
||||
result := make([]map[string]interface{}, 0, len(agents))
|
||||
for _, agent := range agents {
|
||||
result = append(result, h.buildAgentListItem(agent))
|
||||
result = append(result, h.buildAgentListItem(agent, isAuthenticated))
|
||||
}
|
||||
|
||||
return orz.Ok(c, orz.Map{
|
||||
@@ -70,7 +72,7 @@ func (h *AgentHandler) GetAgents(c echo.Context) error {
|
||||
})
|
||||
}
|
||||
|
||||
func (h *AgentHandler) buildAgentListItem(agent models.Agent) map[string]interface{} {
|
||||
func (h *AgentHandler) buildAgentListItem(agent models.Agent, isAuthenticated bool) map[string]interface{} {
|
||||
item := map[string]any{
|
||||
"id": agent.ID,
|
||||
"name": agent.Name,
|
||||
@@ -100,7 +102,13 @@ func (h *AgentHandler) buildAgentListItem(agent models.Agent) map[string]interfa
|
||||
|
||||
metrics, ok := h.metricService.GetLatestMetrics(agent.ID)
|
||||
if ok {
|
||||
item["metrics"] = metrics
|
||||
if !isAuthenticated && metrics != nil {
|
||||
sanitized := *metrics
|
||||
sanitized.NetworkInterfaces = nil
|
||||
item["metrics"] = &sanitized
|
||||
} else {
|
||||
item["metrics"] = metrics
|
||||
}
|
||||
}
|
||||
|
||||
return item
|
||||
|
||||
@@ -57,6 +57,11 @@ func (h *AgentHandler) HandleWebSocket(c echo.Context) error {
|
||||
h.logger.Error("failed to send ssh login config", zap.Error(err))
|
||||
// 配置下发失败不中断连接,只记录日志
|
||||
}
|
||||
// 下发公网 IP 采集配置
|
||||
if err := h.sendPublicIPConfig(conn, agent.ID); err != nil {
|
||||
h.logger.Error("failed to send public ip config", zap.Error(err))
|
||||
// 配置下发失败不中断连接,只记录日志
|
||||
}
|
||||
|
||||
// 创建客户端并注册到管理器
|
||||
client := h.newClient(agent.ID, conn)
|
||||
@@ -87,6 +92,9 @@ func (h *AgentHandler) handleWebSocketMessage(ctx context.Context, agentID strin
|
||||
case protocol.MessageTypeDDNSIPReport:
|
||||
return h.handleDDNSIPReportMessage(ctx, agentID, data)
|
||||
|
||||
case protocol.MessageTypePublicIPReport:
|
||||
return h.handlePublicIPReportMessage(ctx, agentID, data)
|
||||
|
||||
case protocol.MessageTypeSSHLoginEvent:
|
||||
return h.handleSSHLoginEventMessage(ctx, agentID, data)
|
||||
|
||||
@@ -188,6 +196,15 @@ func (h *AgentHandler) handleDDNSIPReportMessage(ctx context.Context, agentID st
|
||||
return h.ddnsService.HandleIPReport(ctx, agentID, &ipReport)
|
||||
}
|
||||
|
||||
func (h *AgentHandler) handlePublicIPReportMessage(ctx context.Context, agentID string, data json.RawMessage) error {
|
||||
var ipReport protocol.PublicIPReportData
|
||||
if err := json.Unmarshal(data, &ipReport); err != nil {
|
||||
h.logger.Error("failed to unmarshal public ip report", zap.Error(err))
|
||||
return err
|
||||
}
|
||||
return h.agentService.UpdatePublicIP(ctx, agentID, ipReport.IPv4, ipReport.IPv6)
|
||||
}
|
||||
|
||||
func (h *AgentHandler) handleSSHLoginEventMessage(ctx context.Context, agentID string, data json.RawMessage) error {
|
||||
var eventData protocol.SSHLoginEvent
|
||||
if err := json.Unmarshal(data, &eventData); err != nil {
|
||||
@@ -281,3 +298,36 @@ func (h *AgentHandler) sendSSHLoginConfig(conn *websocket.Conn, agentID string)
|
||||
}
|
||||
return conn.WriteMessage(websocket.TextMessage, msgData)
|
||||
}
|
||||
|
||||
func (h *AgentHandler) sendPublicIPConfig(conn *websocket.Conn, agentID string) error {
|
||||
config, err := h.propertyService.GetPublicIPConfig(context.Background())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !config.Enabled || (!config.IPv4Enabled && !config.IPv6Enabled) {
|
||||
return nil
|
||||
}
|
||||
|
||||
ipv4Enabled := config.IsIPv4Target(agentID)
|
||||
ipv6Enabled := config.IsIPv6Target(agentID)
|
||||
if !ipv4Enabled && !ipv6Enabled {
|
||||
return nil
|
||||
}
|
||||
|
||||
msgData, err := json.Marshal(protocol.OutboundMessage{
|
||||
Type: protocol.MessageTypePublicIPConfig,
|
||||
Data: protocol.PublicIPConfigData{
|
||||
Enabled: config.Enabled,
|
||||
IntervalSeconds: config.IntervalSeconds,
|
||||
IPv4Enabled: ipv4Enabled,
|
||||
IPv6Enabled: ipv6Enabled,
|
||||
IPv4APIs: config.IPv4APIs,
|
||||
IPv6APIs: config.IPv6APIs,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return conn.WriteMessage(websocket.TextMessage, msgData)
|
||||
}
|
||||
Reference in new issue
Block a user