Compare commits

..

No commits in common. "f264bd5604e593ecc9127e043be7a4a9884c1810" and "352f8bb4ce856e32816c3356cc8098c5f5983697" have entirely different histories.

6 changed files with 26 additions and 99 deletions

View File

@ -24,14 +24,26 @@ import (
"github.com/urfave/cli/v2"
)
// 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, ".")
}
func handleSubdomain(pagesDomain, domain, cname, path, giteaUrl, defaultCsp string, giteaClient *repo.GiteaClient, w http.ResponseWriter) {
username := ""
if cname != "" {
// If we are accessed via a CNAME, then CNAME contains our <user>.<pages domain> value.
username = dns.ExtractUsername(pagesDomain, cname)
username = extractUsername(pagesDomain, cname)
} else {
// If we are directly accessed, then domain contains our <user>.<pages domain> value.
username = dns.ExtractUsername(pagesDomain, domain)
username = extractUsername(pagesDomain, domain)
}
// Strip the leading /

View File

@ -1,9 +1,9 @@
package dns
package main
import "testing"
func TestExtractUsernameSimple(t *testing.T) {
username := ExtractUsername(
username := extractUsername(
"pages.local",
"papatutuwawa.pages.local",
)
@ -13,7 +13,7 @@ func TestExtractUsernameSimple(t *testing.T) {
}
func TestExtractUsernameDot(t *testing.T) {
username := ExtractUsername(
username := extractUsername(
"pages.local",
"polynom.me.pages.local",
)

View File

@ -2,7 +2,6 @@ package dns
import (
"net"
"errors"
"strings"
"time"
@ -16,10 +15,6 @@ const (
// The key that the TXT record will have to start with, e.g.
// "repo=some-random-repo".
TxtRepoKey = "repo="
TxtCNAMERecord = "_rio-cname."
TxtCNAMEKey = "cname="
)
var (
@ -66,53 +61,14 @@ func LookupCNAME(domain string) (string, error) {
return cname.(string), nil
}
cname, err := lookupCNAME(domain)
if err == nil {
cnameCache.Set(domain, cname, cache.DefaultExpiration)
return cname.(string), nil
}
altCname, err := lookupCNAMETxt(domain)
if err == nil {
cnameCache.Set(domain, altCname, cache.DefaultExpiration)
return altCname, nil
}
return "", err
}
// Lookup the CNAME by trying to find a CNAME RR. Contrary to net.LookupCNAME,
// this method fails if we find no CNAME RR.
func lookupCNAME(domain string) (string, error) {
query, err := net.LookupCNAME(domain)
if err == nil {
if query[len(query)-1] == '.' {
query = query[:len(query)-1]
}
// Fail if we have no CNAME RR.
if query == domain {
return "", errors.New("CNAME is equal to domain")
}
cnameCache.Set(domain, query, cache.DefaultExpiration)
return query, nil
}
return "", err
}
// Performs an alternative CNAME lookup by looking for a special TXT record.
func lookupCNAMETxt(domain string) (string, error) {
txts, err := net.LookupTXT(TxtCNAMERecord+domain)
if err == nil {
for _, txt := range txts {
if !strings.HasPrefix(txt, TxtCNAMEKey) {
continue
}
return strings.TrimPrefix(txt, TxtCNAMEKey), nil
}
}
return "", err
}

View File

@ -1,32 +0,0 @@
package dns
import "testing"
func cleanCache() {
cnameCache.Flush()
txtRepoCache.Flush()
}
func TestAltCNAME(t *testing.T) {
defer cleanCache()
res, err := lookupCNAMETxt("moxxy.org")
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if res != "moxxy.pages.polynom.me" {
t.Fatalf("Unexpected alt CNAME: %s", res)
}
}
func TestLookupCNAMEWithAlt(t *testing.T) {
defer cleanCache()
res, err := LookupCNAME("moxxy.org")
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if res != "moxxy.pages.polynom.me" {
t.Fatalf("Unexpected alt CNAME: %s", res)
}
}

View File

@ -1,15 +0,0 @@
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, ".")
}

View File

@ -44,6 +44,7 @@ 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,
@ -55,6 +56,7 @@ 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
}
@ -62,6 +64,10 @@ 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)
@ -75,10 +81,10 @@ func getUsername(sni, pagesDomain string) (string, error) {
return "", errors.New("CNAME does not resolve to subdomain of pages domain")
}
return dns.ExtractUsername(pagesDomain, query), nil
return usernameFromDomain(query), nil
}
return dns.ExtractUsername(pagesDomain, sni), nil
return usernameFromDomain(sni), nil
}
func MakeTlsConfig(pagesDomain, cachePath string, cache *certificates.CertificatesCache, acmeClient *lego.Client, giteaClient *repo.GiteaClient) *tls.Config {