diff --git a/providers/cloudflare/singleredirect.go b/providers/cloudflare/singleredirect.go index 033ac1a9d..9344f390d 100644 --- a/providers/cloudflare/singleredirect.go +++ b/providers/cloudflare/singleredirect.go @@ -122,6 +122,13 @@ func makeRuleFromPattern(pattern, replacement string, temporary bool) (string, s // meta.*yodeya.com/* (wildcard in host) h := simpleGlobToRegex(host) matcher = fmt.Sprintf(`http.host matches r###"%s"###`, h) + + } else if !strings.Contains(host, `*`) && strings.Count(path, `*`) == 1 && strings.HasSuffix(path, "*") { + // domain.tld/.well-known* (wildcard in path) + matcher = fmt.Sprintf(`(starts_with(http.request.uri.path, "%s") and http.host eq "%s")`, + path[0:len(path)-1], + host) + } // replacement @@ -130,6 +137,13 @@ func makeRuleFromPattern(pattern, replacement string, temporary bool) (string, s // https://stackexchange.com/ (no substitutions) expr = fmt.Sprintf(`"%s"`, replacement) + } else if host[0] == '*' && strings.Count(host, `*`) == 1 && strings.Count(replacement, `$`) == 1 && len(rpath) > 3 && strings.HasSuffix(rpath, "/$2") { + // *stackoverflowenterprise.com/* -> https://www.stackoverflowbusiness.com/enterprise/$2 + expr = fmt.Sprintf(`concat("https://%s", "%s", http.request.uri.path)`, + rhost, + rpath[0:len(rpath)-3], + ) + } else if strings.Count(replacement, `$`) == 1 && rpath == `/$1` { // https://i.sstatic.net/$1 ($1 at end) expr = fmt.Sprintf(`concat("https://%s/", http.request.uri.path)`, rhost) @@ -139,6 +153,10 @@ func makeRuleFromPattern(pattern, replacement string, temporary bool) (string, s // https://careers.stackoverflow.com/$2 expr = fmt.Sprintf(`concat("https://%s/", http.request.uri.path)`, rhost) + } else if strings.Count(replacement, `$`) == 1 && strings.HasSuffix(replacement, `$1`) { + // https://social.domain.tld/.well-known$1 + expr = fmt.Sprintf(`concat("https://%s", http.request.uri.path)`, rhost) + } // Not implemented diff --git a/providers/cloudflare/singleredirect_test.go b/providers/cloudflare/singleredirect_test.go index 6c1ac2355..1b14f64d2 100644 --- a/providers/cloudflare/singleredirect_test.go +++ b/providers/cloudflare/singleredirect_test.go @@ -183,7 +183,44 @@ func Test_makeSingleDirectRule(t *testing.T) { wantExpr: `concat("https://stackexchange.com/", http.request.uri.path)`, wantErr: false, }, + + { + name: "pro-sumer1", + pattern: "domain.tld/.well-known*", + replace: "https://social.domain.tld/.well-known$1", + wantMatch: `(starts_with(http.request.uri.path, "/.well-known") and http.host eq "domain.tld")`, + wantExpr: `concat("https://social.domain.tld", http.request.uri.path)`, + wantErr: false, + }, + { + name: "pro-sumer2", + pattern: "domain.tld/users*", + replace: "https://social.domain.tld/users$1", + wantMatch: `(starts_with(http.request.uri.path, "/users") and http.host eq "domain.tld")`, + wantExpr: `concat("https://social.domain.tld", http.request.uri.path)`, + wantErr: false, + }, + { + name: "pro-sumer3", + pattern: "domain.tld/@*", + replace: `https://social.domain.tld/@$1`, + wantMatch: `(starts_with(http.request.uri.path, "/@") and http.host eq "domain.tld")`, + wantExpr: `concat("https://social.domain.tld", http.request.uri.path)`, + wantErr: false, + }, + + { + name: "stackentwild", + pattern: "*stackoverflowenterprise.com/*", + replace: "https://www.stackoverflowbusiness.com/enterprise/$2", + wantMatch: `http.host eq "stackoverflowenterprise.com" or ends_with(http.host, ".stackoverflowenterprise.com")`, + wantExpr: `concat("https://www.stackoverflowbusiness.com", "/enterprise", http.request.uri.path)`, + wantErr: false, + }, + + // } + for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { gotMatch, gotExpr, err := makeRuleFromPattern(tt.pattern, tt.replace, true)