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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user