fix: Fix username extraction in the TLS handler
This commit is contained in:
15
internal/dns/username.go
Normal file
15
internal/dns/username.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package dns
|
||||
|
||||
import "strings"
|
||||
|
||||
// Extract the username from the domain name @domain that we're processing
|
||||
// at the moment.
|
||||
func ExtractUsername(pagesDomain, domain string) string {
|
||||
suffixlessDomain := strings.TrimSuffix(domain, "."+pagesDomain)
|
||||
usernameParts := strings.Split(suffixlessDomain, ".")
|
||||
if len(usernameParts) == 1 {
|
||||
return usernameParts[0]
|
||||
}
|
||||
|
||||
return strings.Join(usernameParts, ".")
|
||||
}
|
||||
23
internal/dns/username_test.go
Normal file
23
internal/dns/username_test.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package dns
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestExtractUsernameSimple(t *testing.T) {
|
||||
username := ExtractUsername(
|
||||
"pages.local",
|
||||
"papatutuwawa.pages.local",
|
||||
)
|
||||
if username != "papatutuwawa" {
|
||||
t.Fatalf("Unexpected username: '%s'", username)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtractUsernameDot(t *testing.T) {
|
||||
username := ExtractUsername(
|
||||
"pages.local",
|
||||
"polynom.me.pages.local",
|
||||
)
|
||||
if username != "polynom.me" {
|
||||
t.Fatalf("Unexpected username: '%s'", username)
|
||||
}
|
||||
}
|
||||
@@ -44,7 +44,6 @@ func unlockDomain(domain string) {
|
||||
}
|
||||
|
||||
func buildDomainList(domain, pagesDomain string) []string {
|
||||
// TODO: For wildcards, we MUST use DNS01
|
||||
if domain == pagesDomain || strings.HasSuffix(domain, pagesDomain) {
|
||||
return []string{
|
||||
pagesDomain,
|
||||
@@ -56,7 +55,6 @@ func buildDomainList(domain, pagesDomain string) []string {
|
||||
}
|
||||
|
||||
func getDomainKey(domain, pagesDomain string) string {
|
||||
// TODO: For wildcards, we MUST use DNS01
|
||||
if domain == pagesDomain || strings.HasSuffix(domain, pagesDomain) {
|
||||
return "*." + pagesDomain
|
||||
}
|
||||
@@ -64,10 +62,6 @@ func getDomainKey(domain, pagesDomain string) string {
|
||||
return domain
|
||||
}
|
||||
|
||||
func usernameFromDomain(domain string) string {
|
||||
return strings.Split(domain, ".")[0]
|
||||
}
|
||||
|
||||
func getUsername(sni, pagesDomain string) (string, error) {
|
||||
if !strings.HasSuffix(sni, pagesDomain) {
|
||||
log.Debugf("'%s' is not a subdomain of '%s'", sni, pagesDomain)
|
||||
@@ -81,10 +75,10 @@ func getUsername(sni, pagesDomain string) (string, error) {
|
||||
return "", errors.New("CNAME does not resolve to subdomain of pages domain")
|
||||
}
|
||||
|
||||
return usernameFromDomain(query), nil
|
||||
return dns.ExtractUsername(pagesDomain, query), nil
|
||||
}
|
||||
|
||||
return usernameFromDomain(sni), nil
|
||||
return dns.ExtractUsername(pagesDomain, sni), nil
|
||||
}
|
||||
|
||||
func MakeTlsConfig(pagesDomain, cachePath string, cache *certificates.CertificatesCache, acmeClient *lego.Client, giteaClient *repo.GiteaClient) *tls.Config {
|
||||
|
||||
Reference in New Issue
Block a user