From 57552c1798d9b51c3872eb31468bf6d057b635e6 Mon Sep 17 00:00:00 2001 From: Umputun Date: Thu, 23 May 2024 01:56:58 -0500 Subject: [PATCH] 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. --- .github/workflows/ci.yml | 2 +- app/discovery/discovery.go | 10 ++++++++++ app/discovery/discovery_test.go | 8 ++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 9d31fa10..3562ffbf 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 diff --git a/app/discovery/discovery.go b/app/discovery/discovery.go index a8a027a3..688a9258 100644 --- a/app/discovery/discovery.go +++ b/app/discovery/discovery.go @@ -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) diff --git a/app/discovery/discovery_test.go b/app/discovery/discovery_test.go index 1ec2fef7..84161105 100644 --- a/app/discovery/discovery_test.go +++ b/app/discovery/discovery_test.go @@ -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 {