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

add support for hyphen separated wildcard domains #246

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
add support for hyphen separated wildcard domains
  • Loading branch information
marriva committed Jul 22, 2022
commit 30823c510cd191cb3895608ef57134951f23c97c
23 changes: 22 additions & 1 deletion proxy/upstreams.go
Original file line number Diff line number Diff line change
@@ -63,6 +63,11 @@ func ParseUpstreamsConfig(upstreamConfig []string, options *upstream.Options) (*

subdomainsOnlyExclusions.Add(host)
subdomainsOnlyUpstreams[host] = nil
} else if strings.HasPrefix(host, "*-") {
subdomainsOnlyUpstreams[host[len("*"):]] = nil

host = host[len("*-"):]
subdomainsOnlyExclusions.Add(host)
} else {
domainReservedUpstreams[host] = nil
specifiedDomainUpstreams[host] = nil
@@ -104,6 +109,12 @@ func ParseUpstreamsConfig(upstreamConfig []string, options *upstream.Options) (*
subdomainsOnlyExclusions.Add(host)
log.Debug("domain %s is added to exclusions list", host)

subdomainsOnlyUpstreams[host] = append(subdomainsOnlyUpstreams[host], dnsUpstream)
} else if strings.HasPrefix(host, "*-") {
subdomainsOnlyExclusions.Add(host[len("*-"):])
log.Debug("domain %s is added to exclusions list", host[len("*-"):])

host = host[len("*"):]
subdomainsOnlyUpstreams[host] = append(subdomainsOnlyUpstreams[host], dnsUpstream)
} else {
specifiedDomainUpstreams[host] = append(specifiedDomainUpstreams[host], dnsUpstream)
@@ -148,7 +159,9 @@ func parseUpstreamLine(l string) (string, []string, error) {
// split domains list
for _, confHost := range strings.Split(domainsAndUpstream[0], "/") {
if confHost != "" {
host := strings.TrimPrefix(confHost, "*.")
host := confHost
host = strings.TrimPrefix(host, "*.")
host = strings.TrimPrefix(host, "*-")
if err := netutil.ValidateDomainName(host); err != nil {
return "", nil, err
}
@@ -204,6 +217,14 @@ func (uc *UpstreamConfig) getUpstreamsForDomain(host string) (ups []upstream.Ups
name := h[i-1]

var ok bool

if i := strings.Index(name, "-"); i >= 0 {
ups, ok = uc.DomainReservedUpstreams[name[i:]]
if ok && len(ups) > 0 {
return ups
}
}

ups, ok = uc.DomainReservedUpstreams[name]
if !ok {
continue
10 changes: 10 additions & 0 deletions proxy/upstreams_test.go
Original file line number Diff line number Diff line change
@@ -165,6 +165,8 @@ func TestGetUpstreamsForDomain_default_wildcards(t *testing.T) {
"[/*.example.org/]127.0.0.1:5303",
"[/www.example.org/]127.0.0.1:5304",
"[/*.www.example.org/]#",
"[/*-abc.example.org/]127.0.0.1:5305",
"[/*.abc.example.org/]127.0.0.1:5306",
}

uconf, err := ParseUpstreamsConfig(conf, nil)
@@ -190,6 +192,14 @@ func TestGetUpstreamsForDomain_default_wildcards(t *testing.T) {
name: "def_wildcard",
in: "abc.www.example.org.",
want: []string{"127.0.0.1:5301"},
}, {
name: "hyphen_wildcard",
in: "zxc-abc.example.org.",
want: []string{"127.0.0.1:5305"},
}, {
name: "sub_no_hyphen",
in: "zxc.abc.example.org.",
want: []string{"127.0.0.1:5306"},
}}

for _, tc := range testCases {