Skip to content

Commit

Permalink
Added support for wildcard prefix in server patterns (#191)
Browse files Browse the repository at this point in the history
* Added support for wildcard prefix in server patterns

This update introduces the ability to use a wildcard prefix in server patterns for domain matching. It also includes corresponding tests for this new functionality, ensuring "*.domain.com" style patterns can be handled correctly.
  • Loading branch information
umputun authored May 23, 2024
1 parent 73c4928 commit 57552c1
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:

- name: build and test
run: |
go test -race -v -timeout=100s -covermode=atomic -coverprofile=$GITHUB_WORKSPACE/profile.cov_tmp ./...
go test -v -timeout=100s -p 1 -covermode=atomic -coverprofile=$GITHUB_WORKSPACE/profile.cov_tmp ./...
go build -race ./...
cat $GITHUB_WORKSPACE/profile.cov_tmp | grep -v "mocks" | grep -v "_mock" > $GITHUB_WORKSPACE/profile.cov
working-directory: app
Expand Down
10 changes: 10 additions & 0 deletions app/discovery/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,16 @@ func findMatchingMappers(s *Service, srvName string) []URLMapper {
continue
}

// handle *.example.com simple patterns
if strings.HasPrefix(mapperServer, "*.") {
domainPattern := mapperServer[1:] // strip the '*'
if strings.HasSuffix(srvName, domainPattern) {
s.mappersCache[srvName] = mapper
return mapper
}
continue
}

re, err := regexp.Compile(mapperServer)
if err != nil {
log.Printf("[WARN] invalid regexp %s: %s", mapperServer, err)
Expand Down
8 changes: 8 additions & 0 deletions app/discovery/discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ func TestService_MatchServerRegex(t *testing.T) {
Dst: "http://127.0.0.1:8080/${host}/blah/$1", MatchType: MTProxy, dead: false},
{Server: "(.*)\\.test-domain\\.(com|org)", SrcMatch: *regexp.MustCompile("^/bar/(.*)"),
Dst: "http://127.0.0.2:8080/$1/foo", MatchType: MTProxy, dead: false},
{Server: "*.test-domain2.com", SrcMatch: *regexp.MustCompile("^/foo/(.*)"),
Dst: "http://127.0.0.3:8080/$1/bar", MatchType: MTProxy, dead: false},

// strict match
{Server: "test-prefix.exact.com", SrcMatch: *regexp.MustCompile("/"),
Expand Down Expand Up @@ -253,6 +255,12 @@ func TestService_MatchServerRegex(t *testing.T) {
src: "/",
res: Matches{MTProxy, nil},
},
{
name: "pattern server with *.test-domain2.com match",
server: "test.test-domain2.com",
src: "/foo/123",
res: Matches{MTProxy, []MatchedRoute{{Destination: "http://127.0.0.3:8080/123/bar", Alive: true}}},
},
}

for i, tt := range tbl {
Expand Down

0 comments on commit 57552c1

Please sign in to comment.