mirror of
https://github.com/umputun/reproxy.git
synced 2025-06-30 22:13:42 +02:00
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:
2
.github/workflows/ci.yml
vendored
2
.github/workflows/ci.yml
vendored
@ -22,7 +22,7 @@ jobs:
|
|||||||
|
|
||||||
- name: build and test
|
- name: build and test
|
||||||
run: |
|
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 ./...
|
go build -race ./...
|
||||||
cat $GITHUB_WORKSPACE/profile.cov_tmp | grep -v "mocks" | grep -v "_mock" > $GITHUB_WORKSPACE/profile.cov
|
cat $GITHUB_WORKSPACE/profile.cov_tmp | grep -v "mocks" | grep -v "_mock" > $GITHUB_WORKSPACE/profile.cov
|
||||||
working-directory: app
|
working-directory: app
|
||||||
|
@ -233,6 +233,16 @@ func findMatchingMappers(s *Service, srvName string) []URLMapper {
|
|||||||
continue
|
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)
|
re, err := regexp.Compile(mapperServer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("[WARN] invalid regexp %s: %s", mapperServer, err)
|
log.Printf("[WARN] invalid regexp %s: %s", mapperServer, err)
|
||||||
|
@ -197,6 +197,8 @@ func TestService_MatchServerRegex(t *testing.T) {
|
|||||||
Dst: "http://127.0.0.1:8080/${host}/blah/$1", MatchType: MTProxy, dead: false},
|
Dst: "http://127.0.0.1:8080/${host}/blah/$1", MatchType: MTProxy, dead: false},
|
||||||
{Server: "(.*)\\.test-domain\\.(com|org)", SrcMatch: *regexp.MustCompile("^/bar/(.*)"),
|
{Server: "(.*)\\.test-domain\\.(com|org)", SrcMatch: *regexp.MustCompile("^/bar/(.*)"),
|
||||||
Dst: "http://127.0.0.2:8080/$1/foo", MatchType: MTProxy, dead: false},
|
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
|
// strict match
|
||||||
{Server: "test-prefix.exact.com", SrcMatch: *regexp.MustCompile("/"),
|
{Server: "test-prefix.exact.com", SrcMatch: *regexp.MustCompile("/"),
|
||||||
@ -253,6 +255,12 @@ func TestService_MatchServerRegex(t *testing.T) {
|
|||||||
src: "/",
|
src: "/",
|
||||||
res: Matches{MTProxy, nil},
|
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 {
|
for i, tt := range tbl {
|
||||||
|
Reference in New Issue
Block a user