Browse Source

fix(internal/service): 修复 Web转发规则中非 ASCII域名导致的问题

-增加对 Web 转发规则中 BackendList 字段的处理
- 使用 SplitHostPort 分离主机名和端口
- 对主机名进行 Punycode转换(如果是 IDN)- 重新组合主机名和端口,生成兼容的地址
fusu 1 month ago
parent
commit
b1e8a44b10
1 changed files with 16 additions and 1 deletions
  1. 16 1
      internal/service/webforwarding.go

+ 16 - 1
internal/service/webforwarding.go

@@ -351,9 +351,24 @@ 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: v.Addr,
+			BackendList: addrPunyPort,
 			Host:        v.CustomHost,
 			Comment:     req.WebForwardingData.Comment,
 		})