Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: cache clientIP in GetConsoleHTTPClient #3056

Merged
merged 13 commits into from
Oct 9, 2023
25 changes: 6 additions & 19 deletions restapi/client-admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"net/http"
"net/url"
"strings"
"sync"
"time"

"github.com/minio/console/pkg/utils"
Expand Down Expand Up @@ -482,15 +481,6 @@ func newAdminFromCreds(accessKey, secretKey, endpoint string, tlsEnabled bool) (
return minioClient, nil
}

// httpClient is a custom http client, this client should not be called directly and instead be
// called using GetConsoleHTTPClient() to ensure is initialized and the certificates are loaded correctly
var httpClients = struct {
sync.Mutex
m map[string]*http.Client
}{
m: make(map[string]*http.Client),
}

// isLocalAddress returns true if the url contains an IPv4/IPv6 hostname
// that points to the local machine - FQDN are not supported
func isLocalIPEndpoint(addr string) bool {
Expand Down Expand Up @@ -520,17 +510,14 @@ func GetConsoleHTTPClient(address string, clientIP string) *http.Client {
address = u.Hostname()
}

httpClients.Lock()
client, ok := httpClients.m[address]
httpClients.Unlock()
if ok {
return client
client := PrepareConsoleHTTPClient(isLocalIPAddress(address), clientIP)

if client.Transport != nil {
if transport, ok := client.Transport.(*ConsoleTransport); ok {
transport.ClientIP = clientIP
}
harshavardhana marked this conversation as resolved.
Show resolved Hide resolved
}

client = PrepareConsoleHTTPClient(isLocalIPAddress(address), clientIP)
httpClients.Lock()
httpClients.m[address] = client
httpClients.Unlock()
return client
}

Expand Down
Loading