Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLOUDFLAREAPI: Handle more singleredirect patterns #3024

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions providers/cloudflare/singleredirect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand Down
37 changes: 37 additions & 0 deletions providers/cloudflare/singleredirect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading