refactor(cors): 🛠️ improve host handling in CORS config
Some checks failed
Build , Vet Test, and Lint / Run Vet Tests (1.24.x) (push) Successful in -27m44s
Build , Vet Test, and Lint / Lint Code (push) Successful in -27m5s
Build , Vet Test, and Lint / Build (push) Successful in -27m29s
Tests / Unit Tests (push) Successful in -27m48s
Build , Vet Test, and Lint / Run Vet Tests (1.23.x) (push) Successful in 2m22s
Tests / Integration Tests (push) Failing after -28m1s

* Change loop to use index for server instances
* Simplify appending external URLs
* Clean up commented code for clarity
This commit is contained in:
Hein
2026-01-06 14:07:56 +02:00
parent 987244019c
commit 6ea200bb2b
2 changed files with 8 additions and 9 deletions

View File

@@ -20,17 +20,16 @@ func DefaultCORSConfig() CORSConfig {
configManager := config.GetConfigManager() configManager := config.GetConfigManager()
cfg, _ := configManager.GetConfig() cfg, _ := configManager.GetConfig()
hosts := make([]string, 0) hosts := make([]string, 0)
//hosts = append(hosts, "*") // hosts = append(hosts, "*")
_, _, ipsList := config.GetIPs() _, _, ipsList := config.GetIPs()
for _, server := range cfg.Servers.Instances { for i := range cfg.Servers.Instances {
server := cfg.Servers.Instances[i]
hosts = append(hosts, fmt.Sprintf("http://%s:%d", server.Host, server.Port)) hosts = append(hosts, fmt.Sprintf("http://%s:%d", server.Host, server.Port))
hosts = append(hosts, fmt.Sprintf("https://%s:%d", server.Host, server.Port)) hosts = append(hosts, fmt.Sprintf("https://%s:%d", server.Host, server.Port))
hosts = append(hosts, fmt.Sprintf("http://%s:%d", "localhost", server.Port)) hosts = append(hosts, fmt.Sprintf("http://%s:%d", "localhost", server.Port))
for _, extURL := range server.ExternalURLs { hosts = append(hosts, server.ExternalURLs...)
hosts = append(hosts, extURL)
}
for _, ip := range ipsList { for _, ip := range ipsList {
hosts = append(hosts, fmt.Sprintf("http://%s:%d", ip.String(), server.Port)) hosts = append(hosts, fmt.Sprintf("http://%s:%d", ip.String(), server.Port))
hosts = append(hosts, fmt.Sprintf("https://%s:%d", ip.String(), server.Port)) hosts = append(hosts, fmt.Sprintf("https://%s:%d", ip.String(), server.Port))

View File

@@ -110,13 +110,13 @@ func (sc *ServersConfig) GetDefault() (*ServerInstanceConfig, error) {
} }
// GetIPs - GetIP for pc // GetIPs - GetIP for pc
func GetIPs() (string, string, []net.IP) { func GetIPs() (hostname string, ipList string, ipNetList []net.IP) {
defer func() { defer func() {
if err := recover(); err != nil { if err := recover(); err != nil {
fmt.Println("Recovered in GetIPs", err) fmt.Println("Recovered in GetIPs", err)
} }
}() }()
hostname, _ := os.Hostname() hostname, _ = os.Hostname()
ipaddrlist := make([]net.IP, 0) ipaddrlist := make([]net.IP, 0)
iplist := "" iplist := ""
addrs, err := net.LookupIP(hostname) addrs, err := net.LookupIP(hostname)
@@ -125,7 +125,7 @@ func GetIPs() (string, string, []net.IP) {
} }
for _, a := range addrs { for _, a := range addrs {
//cfg.LogInfo("\nFound IP Host Address: %s", a) // cfg.LogInfo("\nFound IP Host Address: %s", a)
if strings.Contains(a.String(), "127.0.0.1") { if strings.Contains(a.String(), "127.0.0.1") {
continue continue
} }
@@ -135,7 +135,7 @@ func GetIPs() (string, string, []net.IP) {
if iplist == "" { if iplist == "" {
iff, _ := net.InterfaceAddrs() iff, _ := net.InterfaceAddrs()
for _, a := range iff { for _, a := range iff {
//cfg.LogInfo("\nFound IP Address: %s", a) // cfg.LogInfo("\nFound IP Address: %s", a)
if strings.Contains(a.String(), "127.0.0.1") { if strings.Contains(a.String(), "127.0.0.1") {
continue continue
} }