@@ -10,6 +10,7 @@ import (
1010 "os"
1111 "path"
1212 "reflect"
13+ "regexp"
1314 "strconv"
1415 "strings"
1516 "syscall"
@@ -46,6 +47,28 @@ import (
4647type WebsiteService struct {
4748}
4849
50+ func buildRedirectHostPattern (domain string ) string {
51+ labels := strings .Split (domain , "." )
52+ patternLabels := make ([]string , 0 , len (labels ))
53+ for _ , label := range labels {
54+ if label == "*" {
55+ patternLabels = append (patternLabels , `[^.]+` )
56+ continue
57+ }
58+ patternLabels = append (patternLabels , regexp .QuoteMeta (label ))
59+ }
60+ return fmt .Sprintf ("^%s$" , strings .Join (patternLabels , `\.` ))
61+ }
62+
63+ func parseRedirectHostPattern (pattern string ) string {
64+ pattern = strings .Trim (pattern , "'" )
65+ pattern = strings .TrimPrefix (pattern , "^" )
66+ pattern = strings .TrimSuffix (pattern , "$" )
67+ pattern = strings .ReplaceAll (pattern , `[^.]+` , "*" )
68+ pattern = strings .ReplaceAll (pattern , `\.` , "." )
69+ return pattern
70+ }
71+
4972type IWebsiteService interface {
5073 PageWebsite (req request.WebsiteSearch ) (int64 , []response.WebsiteRes , error )
5174 GetWebsites () ([]response.WebsiteDTO , error )
@@ -1728,7 +1751,7 @@ func (w WebsiteService) OperateRedirect(req request.NginxRedirectReq) (err error
17281751 for _ , domain := range req .Domains {
17291752 block .Directives = append (block .Directives , & components.Directive {
17301753 Name : "if" ,
1731- Parameters : []string {"($host" , "~" , fmt .Sprintf ("'^ %s')" , domain )},
1754+ Parameters : []string {"($host" , "~" , fmt .Sprintf ("'%s')" , buildRedirectHostPattern ( domain ) )},
17321755 Block : returnBlock ,
17331756 })
17341757 }
@@ -1834,7 +1857,7 @@ func (w WebsiteService) GetRedirect(id uint) (res []response.NginxRedirectConfig
18341857 for _ , ifDir := range dirs {
18351858 params := ifDir .GetParameters ()
18361859 if len (params ) > 2 && params [0 ] == "($host" {
1837- domain := strings . Trim ( strings . Trim ( params [2 ], "'" ), "^" )
1860+ domain := parseRedirectHostPattern ( params [2 ])
18381861 redirectConfig .Domains = append (redirectConfig .Domains , domain )
18391862 if len (redirectConfig .Domains ) > 1 {
18401863 continue
0 commit comments