Browse Source

fix(internal): 优化白名单查询逻辑并添加网站域名修改功能- 重构 GetWhiteStaticList函数,使用分页查询直到找到目标记录或所有页都查完
- 新增 EditServerName函数,实现修改网站域名的功能- 更新 webforwarding 服务,在修改网站域名时调用新的 EditServerName 方法
- 优化 wafformatter 中的域名处理逻辑,增加空域名判断

fusu 1 month ago
parent
commit
87e2ce4dff

+ 5 - 0
api/v1/cdn.go

@@ -122,4 +122,9 @@ type SSlCert struct {
 	DnsNames []string `json:"dnsNames" form:"dnsNames"` // 包含的DNS域名
 	CommonNames []string `json:"commonNames" form:"commonNames"` // 包含的COMMON域名
 	IsSelfSigned bool `json:"isSelfSigned" form:"isSelfSigned"`  //是否是自签名证书
+}
+
+type EditServerNames struct {
+	ServerId int64 `json:"serverId" form:"serverId"`
+	ServerNamesJSON []byte `json:"serverNamesJSON" form:"serverNamesJSON"`
 }

+ 38 - 17
internal/service/aodun.go

@@ -194,27 +194,48 @@ func (s *aoDunService) AddWhiteStaticList(ctx context.Context, isSmall bool, req
 
 // GetWhiteStaticList 查询白名单 IP 并返回其 ID
 func (s *aoDunService) GetWhiteStaticList(ctx context.Context, isSmall bool, ip string, color string) (int, error) {
-	formData := map[string]interface{}{
-		"action": "get",
-		"bwflag": color,
-		"page":   1,
-		"ip":    ip,
-	}
+	// 使用一个无限循环,直到API返回空数据页才停止
+	for i := 0; ; i++ { //  i++ 会持续请求下一页
+		formData := map[string]interface{}{
+			"action": "get",
+			"bwflag": color,
+			"page":   i,
+			"ip":     ip,
+		}
 
-	var res v1.IpGetResponse
-	err := s.sendAuthenticatedRequest(ctx, isSmall, "/v1.0/firewall/static_bw_list", formData, &res)
-	if err != nil {
-		return 0, err
-	}
+		var res v1.IpGetResponse
+		err := s.sendAuthenticatedRequest(ctx, isSmall, "/v1.0/firewall/static_bw_list", formData, &res)
+		if err != nil {
+			return 0, err // 网络或请求本身出错,直接返回
+		}
 
-	if res.Code != 0 {
-		return 0, fmt.Errorf("API 错误 (isSmall: %t): color %s,code %d, msg '%s'", isSmall, color, res.Code, res.Msg)
-	}
-	if len(res.Data) == 0 {
-		return 0, fmt.Errorf("未找到 IP '%s' 相关的 '%s'名单记录 (isSmall: %t)", ip, color, isSmall)
+		if res.Code != 0 {
+			// API返回了业务错误,直接返回
+			return 0, fmt.Errorf("API 错误 (isSmall: %t): color %s,code %d, msg '%s'", isSmall, color, res.Code, res.Msg)
+		}
+
+		// 如果当前页的数据为空,说明已经没有更多数据了,可以跳出循环。
+		// 这是分页查询结束的正确信号。
+		if len(res.Data) == 0 {
+			break
+		}
+
+		// 在当前页的数据中查找目标记录
+		for _, v := range res.Data {
+			if v.Remark == "宁波高防IP过白" {
+				// 找到了,立即返回ID
+				return v.ID, nil
+			}
+		}
+
+		// 可选:为了防止无限循环,可以加一个最大页数限制
+		if i > 50 { // 比如最多查100页
+			break
+		}
 	}
 
-	return res.Data[0].ID, nil
+	// 如果循环正常结束(所有页都查完了),说明没有找到符合条件的记录
+	return 0, fmt.Errorf("未找到 IP '%s' 相关的 '%s'名单记录 (备注: 宁波高防IP过白) (isSmall: %t)", ip, color, isSmall)
 }
 
 // DelWhiteStaticList 根据 ID 从白名单中删除 IP

+ 24 - 0
internal/service/cdn.go

@@ -33,6 +33,8 @@ type CdnService interface {
 	DelServer(ctx context.Context, serverId int64) error
 	// 添加ssl证书
 	AddSSLCert(ctx context.Context, req v1.SSlCert) (int64, error)
+	// 修改网站域名
+	EditServerName(ctx context.Context, req v1.EditServerNames) error
 }
 
 func NewCdnService(
@@ -604,4 +606,26 @@ func (s *cdnService) AddSSLCert(ctx context.Context, req v1.SSlCert) (int64, err
 		return 0, fmt.Errorf("API 错误: code %d, msg '%s'", res.Code, res.Message)
 	}
 	return res.Data.SslCertId, nil
+}
+
+
+// 修改网站域名
+func (s *cdnService) EditServerName(ctx context.Context, req v1.EditServerNames) error  {
+	formData := map[string]interface{}{
+		"serverId": req.ServerId,
+		"serverNamesJSON": req.ServerNamesJSON,
+	}
+	apiUrl := s.Url + "ServerService/updateServerNames"
+	resBody, err := s.sendDataWithTokenRetry(ctx, formData, apiUrl)
+	if err != nil {
+		return err
+	}
+	var res v1.GeneralResponse[any]
+	if err := json.Unmarshal(resBody, &res); err != nil {
+		return fmt.Errorf("反序列化响应 JSON 失败 (内容: %s): %w", string(resBody), err)
+	}
+	if res.Code != 200 {
+		return fmt.Errorf("API 错误: code %d, msg '%s'", res.Code, res.Message)
+	}
+	return nil
 }

+ 3 - 0
internal/service/wafformatter.go

@@ -155,6 +155,9 @@ func (s *wafFormatterService) ConvertToWildcardDomain(ctx context.Context, domai
 	// 1. 使用 EffectiveTLDPlusOne 获取可注册域名部分。
 	//    例如,对于 "www.google.com",这将返回 "google.com"。
 	//    对于 "a.b.c.tokyo.jp",这将返回 "c.tokyo.jp"。
+	if domain == "" {
+		return "", nil
+	}
 	registrableDomain, err := publicsuffix.EffectiveTLDPlusOne(domain)
 	if err != nil {
 		// 如果域名无效(如 IP 地址、localhost),则返回错误。

+ 27 - 1
internal/service/webforwarding.go

@@ -461,7 +461,7 @@ func (s *webForwardingService) EditWebForwarding(ctx context.Context, req *v1.We
 	}
 
 	//修改网站端口
-	if oldData.Port != req.WebForwardingData.Port || oldData.Domain != req.WebForwardingData.Domain {
+	if oldData.Port != req.WebForwardingData.Port {
 		var typeJson []byte
 		var apiType string
 		if req.WebForwardingData.IsHttps == isHttps {
@@ -480,6 +480,31 @@ func (s *webForwardingService) EditWebForwarding(ctx context.Context, req *v1.We
 		}
 	}
 
+	//修改网站域名
+	if oldData.Domain != req.WebForwardingData.Domain {
+		type serverName struct{
+			Name string `json:"name" form:"name"`
+			Type string `json:"type" form:"type"`
+		}
+		var serverData []serverName
+		var serverJson []byte
+		serverData = append(serverData, serverName{
+			Name: req.WebForwardingData.Domain,
+			Type: "full",
+		})
+		serverJson, err = json.Marshal(serverData)
+		if err != nil {
+			return err
+		}
+		err = s.cdn.EditServerName(ctx, v1.EditServerNames{
+			ServerId: int64(oldData.CdnWebId),
+			ServerNamesJSON: serverJson,
+		})
+		if err != nil {
+			return err
+		}
+	}
+
 	//修改网站名字
 	if oldData.Comment != req.WebForwardingData.Comment {
 		err = s.cdn.EditServerBasic(ctx, int64(oldData.CdnWebId), require.Tag)
@@ -507,6 +532,7 @@ func (s *webForwardingService) EditWebForwarding(ctx context.Context, req *v1.We
 		if err != nil {
 			return err
 		}
+
 		oldDomain, err := s.wafformatter.ConvertToWildcardDomain(ctx, webData.Domain)
 		if err != nil {
 			return err