mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
fix: use CRC32 to shorten app subdomain (#9645)
This commit is contained in:
@@ -2,6 +2,7 @@ package httpapi
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"hash/crc32"
|
||||
"net"
|
||||
"regexp"
|
||||
"strings"
|
||||
@@ -18,6 +19,8 @@ var (
|
||||
nameRegex))
|
||||
|
||||
validHostnameLabelRegex = regexp.MustCompile(`^[a-z0-9]([-a-z0-9]*[a-z0-9])?$`)
|
||||
|
||||
crcTable = crc32.MakeTable(crc32.IEEE)
|
||||
)
|
||||
|
||||
// ApplicationURL is a parsed application URL hostname.
|
||||
@@ -39,7 +42,12 @@ func (a ApplicationURL) String() string {
|
||||
_, _ = appURL.WriteString(a.WorkspaceName)
|
||||
_, _ = appURL.WriteString("--")
|
||||
_, _ = appURL.WriteString(a.Username)
|
||||
return appURL.String()
|
||||
hostname := appURL.String()
|
||||
|
||||
if len(hostname) < 64 { // max length for the subdomain level
|
||||
return hostname
|
||||
}
|
||||
return fmt.Sprintf("app-%08x", crc32.Checksum([]byte(hostname), crcTable))
|
||||
}
|
||||
|
||||
// ParseSubdomainAppURL parses an ApplicationURL from the given subdomain. If
|
||||
|
||||
Reference in New Issue
Block a user