Added support for wildcard prefix in server patterns (#191)

* 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.
This commit is contained in:
Umputun 2024-05-23 01:56:58 -05:00 committed by GitHub
parent 73c492804d
commit 57552c1798
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 19 additions and 1 deletions

View File

@ -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

View File

@ -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)

View File

@ -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("/"),
@ -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 {