Skip to content

Commit

Permalink
增加本地节点Hash辅助判断信息
Browse files Browse the repository at this point in the history
  • Loading branch information
wyx2685 committed Feb 6, 2024
1 parent 0d274bc commit 73f9b19
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
20 changes: 15 additions & 5 deletions api/panel/node.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package panel

import (
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"fmt"
"reflect"
"strconv"
Expand Down Expand Up @@ -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
}
Expand All @@ -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")
}
Expand Down Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions api/panel/panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type Client struct {
NodeId int
nodeEtag string
userEtag string
responseBodyHash string
LastReportOnline map[int]int
}

Expand Down
2 changes: 0 additions & 2 deletions core/hy2/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 73f9b19

Please sign in to comment.