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()
cfg, _ := configManager.GetConfig()
hosts := make([]string, 0)
//hosts = append(hosts, "*")
// hosts = append(hosts, "*")
_, _, 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("https://%s:%d", server.Host, server.Port))
hosts = append(hosts, fmt.Sprintf("http://%s:%d", "localhost", server.Port))
for _, extURL := range server.ExternalURLs {
hosts = append(hosts, extURL)
}
hosts = append(hosts, server.ExternalURLs...)
for _, ip := range ipsList {
hosts = append(hosts, fmt.Sprintf("http://%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
func GetIPs() (string, string, []net.IP) {
func GetIPs() (hostname string, ipList string, ipNetList []net.IP) {
defer func() {
if err := recover(); err != nil {
fmt.Println("Recovered in GetIPs", err)
}
}()
hostname, _ := os.Hostname()
hostname, _ = os.Hostname()
ipaddrlist := make([]net.IP, 0)
iplist := ""
addrs, err := net.LookupIP(hostname)
@@ -125,7 +125,7 @@ func GetIPs() (string, string, []net.IP) {
}
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") {
continue
}
@@ -135,7 +135,7 @@ func GetIPs() (string, string, []net.IP) {
if iplist == "" {
iff, _ := net.InterfaceAddrs()
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") {
continue
}