修复局域网连接出错问题
This commit is contained in:
@@ -2,7 +2,11 @@ package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/netip"
|
||||
"net/url"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
@@ -118,6 +122,14 @@ func (h *HTTPHandler) registerDevice(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
networkGroupKey, publicIPHash := deriveClientNetworkIdentity(c.ClientIP())
|
||||
if networkGroupKey != "" {
|
||||
input.NetworkGroupKey = networkGroupKey
|
||||
}
|
||||
if publicIPHash != "" {
|
||||
input.PublicIPHash = publicIPHash
|
||||
}
|
||||
|
||||
device, session := h.deps.DeviceService.Register(input, c.Request.UserAgent(), c.GetHeader("X-Device-Token"))
|
||||
c.JSON(http.StatusOK, gin.H{"data": gin.H{
|
||||
"id": device.ID,
|
||||
@@ -611,6 +623,46 @@ func decodeDownloadFilename(filename string) string {
|
||||
return strings.TrimSpace(decoded)
|
||||
}
|
||||
|
||||
func deriveClientNetworkIdentity(clientIP string) (string, string) {
|
||||
addr, err := netip.ParseAddr(strings.TrimSpace(clientIP))
|
||||
if err != nil {
|
||||
return "", ""
|
||||
}
|
||||
|
||||
addr = addr.Unmap()
|
||||
hash := hashIP(addr.String())
|
||||
|
||||
if addr.IsLoopback() {
|
||||
return "loopback", hash
|
||||
}
|
||||
|
||||
if addr.Is4() {
|
||||
bytes := addr.As4()
|
||||
switch {
|
||||
case bytes[0] == 10 || bytes[0] == 127:
|
||||
return fmt.Sprintf("v4:%d.%d.%d", bytes[0], bytes[1], bytes[2]), hash
|
||||
case bytes[0] == 192 && bytes[1] == 168:
|
||||
return fmt.Sprintf("v4:%d.%d.%d", bytes[0], bytes[1], bytes[2]), hash
|
||||
case bytes[0] == 172 && bytes[1] >= 16 && bytes[1] <= 31:
|
||||
return fmt.Sprintf("v4:%d.%d.%d", bytes[0], bytes[1], bytes[2]), hash
|
||||
default:
|
||||
return "pub4:" + hash[:16], hash
|
||||
}
|
||||
}
|
||||
|
||||
prefix := netip.PrefixFrom(addr, 64).Masked()
|
||||
if addr.IsPrivate() {
|
||||
return "v6:" + prefix.Addr().String(), hash
|
||||
}
|
||||
|
||||
return "pub6:" + hash[:16], hash
|
||||
}
|
||||
|
||||
func hashIP(value string) string {
|
||||
sum := sha256.Sum256([]byte(strings.TrimSpace(value)))
|
||||
return hex.EncodeToString(sum[:])
|
||||
}
|
||||
|
||||
func (h *HTTPHandler) ensureFallbackBucket(ctx context.Context, transferID string) error {
|
||||
if h.deps.MinIOClient == nil {
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user