From 73f9b192229b8991f1cb1913029e6e58c88815f3 Mon Sep 17 00:00:00 2001 From: wyx2685 Date: Tue, 6 Feb 2024 20:26:30 +0900 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=9C=AC=E5=9C=B0=E8=8A=82?= =?UTF-8?q?=E7=82=B9Hash=E8=BE=85=E5=8A=A9=E5=88=A4=E6=96=AD=E4=BF=A1?= =?UTF-8?q?=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/panel/node.go | 20 +++++++++++++++----- api/panel/panel.go | 1 + core/hy2/config.go | 2 -- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/api/panel/node.go b/api/panel/node.go index abd9bb8..965c66f 100644 --- a/api/panel/node.go +++ b/api/panel/node.go @@ -1,7 +1,9 @@ package panel import ( + "crypto/sha256" "encoding/base64" + "encoding/hex" "fmt" "reflect" "strconv" @@ -130,7 +132,19 @@ func (c *Client) GetNodeInfo() (node *NodeInfo, err error) { r, err := c.client. R(). SetHeader("If-None-Match", c.nodeEtag). + ForceContentType("application/json"). Get(path) + + if r.StatusCode() == 304 { + return nil, nil + } + hash := sha256.Sum256(r.Body()) + newBodyHash := hex.EncodeToString(hash[:]) + if c.responseBodyHash == newBodyHash { + return nil, nil + } + c.responseBodyHash = newBodyHash + c.nodeEtag = r.Header().Get("ETag") if err = c.checkResponse(r, path, err); err != nil { return nil, err } @@ -141,9 +155,6 @@ func (c *Client) GetNodeInfo() (node *NodeInfo, err error) { r.RawBody().Close() } }() - if r.StatusCode() == 304 { - return nil, nil - } } else { return nil, fmt.Errorf("received nil response") } @@ -274,8 +285,7 @@ func (c *Client) GetNodeInfo() (node *NodeInfo, err error) { cm.Routes = nil cm.BaseConfig = nil - c.nodeEtag = r.Header().Get("ETag") - return + return node, nil } func intervalToTime(i interface{}) time.Duration { diff --git a/api/panel/panel.go b/api/panel/panel.go index 882f97b..c298d05 100644 --- a/api/panel/panel.go +++ b/api/panel/panel.go @@ -23,6 +23,7 @@ type Client struct { NodeId int nodeEtag string userEtag string + responseBodyHash string LastReportOnline map[int]int } diff --git a/core/hy2/config.go b/core/hy2/config.go index f35e1f2..9155311 100644 --- a/core/hy2/config.go +++ b/core/hy2/config.go @@ -307,10 +307,8 @@ func extractPortFromAddr(addr string) int { } func formatAddress(ip string, port int) string { - // 检查 IP 地址是否为 IPv6 if strings.Contains(ip, ":") { return fmt.Sprintf("[%s]:%d", ip, port) } - // 对于 IPv4 地址,直接返回 IP:Port 格式 return fmt.Sprintf("%s:%d", ip, port) }