From a70b5b56ffdd3c11ba23ccae6df5d3c385934825 Mon Sep 17 00:00:00 2001 From: Lykin <137850705+tiny-craft@users.noreply.github.com> Date: Fri, 6 Sep 2024 15:08:53 +0800 Subject: [PATCH] pref: compatible with IPv6 address --- backend/services/browser_service.go | 4 ++-- backend/services/connection_service.go | 13 ++++++------- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/backend/services/browser_service.go b/backend/services/browser_service.go index a5d0866..4ec06c7 100644 --- a/backend/services/browser_service.go +++ b/backend/services/browser_service.go @@ -301,8 +301,8 @@ func (b *browserService) createRedisClient(ctx context.Context, selConn types.Co return } -// get a redis client from local cache or create a new open -// if db >= 0, will also switch to db index +// get a redis client from local cache or create a new one +// if db >= 0, it will also switch to target database index func (b *browserService) getRedisClient(server string, db int) (item *connectionItem, err error) { b.mutex.Lock() defer b.mutex.Unlock() diff --git a/backend/services/connection_service.go b/backend/services/connection_service.go index f57757f..2d628dc 100644 --- a/backend/services/connection_service.go +++ b/backend/services/connection_service.go @@ -5,7 +5,6 @@ import ( "crypto/tls" "crypto/x509" "errors" - "fmt" "github.com/klauspost/compress/zip" "github.com/redis/go-redis/v9" "github.com/vrischmann/userdir" @@ -65,7 +64,7 @@ func (c *connectionService) buildOption(config types.ConnectionConfig) (*redis.O } else if config.Proxy.Type == 2 { // use custom proxy proxyUrl := url.URL{ - Host: fmt.Sprintf("%s:%d", config.Proxy.Addr, config.Proxy.Port), + Host: net.JoinHostPort(config.Proxy.Addr, strconv.Itoa(config.Proxy.Port)), } if len(config.Proxy.Username) > 0 { proxyUrl.User = url.UserPassword(config.Proxy.Username, config.Proxy.Password) @@ -111,7 +110,7 @@ func (c *connectionService) buildOption(config types.ConnectionConfig) (*redis.O return nil, errors.New("invalid login type") } - sshAddr = fmt.Sprintf("%s:%d", config.SSH.Addr, config.SSH.Port) + sshAddr = net.JoinHostPort(config.SSH.Addr, strconv.Itoa(config.SSH.Port)) } var tlsConfig *tls.Config @@ -168,9 +167,9 @@ func (c *connectionService) buildOption(config types.ConnectionConfig) (*redis.O port = config.Port } if len(config.Addr) <= 0 { - option.Addr = fmt.Sprintf("127.0.0.1:%d", port) + option.Addr = net.JoinHostPort("127.0.0.1", strconv.Itoa(port)) } else { - option.Addr = fmt.Sprintf("%s:%d", config.Addr, port) + option.Addr = net.JoinHostPort(config.Addr, strconv.Itoa(port)) } } @@ -224,7 +223,7 @@ func (c *connectionService) createRedisClient(config types.ConnectionConfig) (re if len(addr) < 2 { return nil, errors.New("cannot get master address") } - option.Addr = fmt.Sprintf("%s:%s", addr[0], addr[1]) + option.Addr = net.JoinHostPort(addr[0], addr[1]) option.Username = config.Sentinel.Username option.Password = config.Sentinel.Password } @@ -310,7 +309,7 @@ func (c *connectionService) ListSentinelMasters(config types.ConnectionConfig) ( if infoMap, ok := info.(map[any]any); ok { retInfo = append(retInfo, map[string]string{ "name": infoMap["name"].(string), - "addr": fmt.Sprintf("%s:%s", infoMap["ip"].(string), infoMap["port"].(string)), + "addr": net.JoinHostPort(infoMap["ip"].(string), infoMap["port"].(string)), }) } }