瀏覽代碼

refactor(internal/service/api/waf): 优化域名转换逻辑

- 在 wafformatter.go 中添加对 IP 地址和域名的区分处理
- 使用 ConvertToPunycodeIfIDN 函数将域名转换为 Punycode
- 在 webforwarding.go 中简化了地址处理逻辑,直接使用 v.Addr
fusu 1 周之前
父節點
當前提交
fd3b0df714
共有 2 個文件被更改,包括 19 次插入16 次删除
  1. 17 1
      internal/service/api/waf/wafformatter.go
  2. 2 15
      internal/service/api/waf/webforwarding.go

+ 17 - 1
internal/service/api/waf/wafformatter.go

@@ -418,9 +418,25 @@ func (s *wafFormatterService) AddOrigin(ctx context.Context, req v1.WebJson) (in
 	if err != nil {
 		return 0, fmt.Errorf("无效的后端地址: %s", err)
 	}
+
+
+	// 将 IP 转换为 Punycode
+	var hostPuny string
+	hostPuny = ip
+	// 如果是 IP 地址,不需要转换
+	if ok := net.ParseIP(ip); ok == nil {
+		_, hostPuny, err = s.ConvertToPunycodeIfIDN(ctx, ip)
+		if err != nil {
+			return 0, err
+		}
+	}
+
+
+
+
 	addr := v1.Addr{
 		Protocol: req.ApiType,
-		Host:     ip,
+		Host:     hostPuny,
 		Port:     port,
 	}
 	id, err := s.cdn.CreateOrigin(ctx, v1.Origin{

+ 2 - 15
internal/service/api/waf/webforwarding.go

@@ -432,24 +432,11 @@ func (s *webForwardingService) AddWebForwarding(ctx context.Context, req *v1.Web
 		if v.IsHttps == isHttps {
 			apiType = protocolHttps
 		}
-		host, port, err := net.SplitHostPort(v.Addr)
-		if err != nil {
-			return err
-		}
-		var addrPunyPort string
-		addrPunyPort = v.Addr
-		// 如果是 IP 地址,不需要转换
-		if ok := net.ParseIP(host); ok == nil {
-			_, hostPuny, err := s.wafformatter.ConvertToPunycodeIfIDN(ctx, host)
-			if err != nil {
-				return err
-			}
-			addrPunyPort = fmt.Sprintf("%s:%s", hostPuny, port)
-		}
+
 
 		id, err := s.wafformatter.AddOrigin(ctx, v1.WebJson{
 			ApiType:     apiType,
-			BackendList: addrPunyPort,
+			BackendList: v.Addr,
 			Host:        v.CustomHost,
 			Comment:     req.WebForwardingData.Comment,
 		})