From ff9c1ad70fc40ff98a2a91f79b6bd52384d6baf1 Mon Sep 17 00:00:00 2001 From: "jizhong.jiangjz" Date: Tue, 23 Jul 2024 10:39:21 +0800 Subject: [PATCH] fix listen address parse --- pkg/exporter/cmd/server.go | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/pkg/exporter/cmd/server.go b/pkg/exporter/cmd/server.go index f1b4d83..f79f67c 100644 --- a/pkg/exporter/cmd/server.go +++ b/pkg/exporter/cmd/server.go @@ -295,23 +295,20 @@ func (i *inspServer) createListener(cfg *InspServerConfig) (net.Listener, error) } rawAddr := cfg.Address - if !strings.Contains(rawAddr, "//") { + if !strings.Contains(rawAddr, "://") { log.Infof("address contains no protocol part, use tcp:// by default") rawAddr = "tcp://" + rawAddr } - protocol := "" - addr := "" - u, err := url.Parse(rawAddr) if err != nil { - return nil, fmt.Errorf("invalid address %s, valid format is [protocol://]addr[:port]", cfg.Address) + return nil, fmt.Errorf("invalid address %s, valid format is [protocol://]addr[:port]", rawAddr) } - protocol = u.Scheme + protocol := u.Scheme + addr := "" switch protocol { case "unix": - // For Unix domain sockets, the path is in the opaque part - addr = strings.TrimPrefix(cfg.Address, "unix://") + addr = u.Path if _, err = os.Stat(addr); err != nil { if !errors.Is(err, os.ErrNotExist) { return nil, fmt.Errorf("failed stat sock file %s: %w", addr, err) @@ -325,8 +322,7 @@ func (i *inspServer) createListener(cfg *InspServerConfig) (net.Listener, error) _ = os.Remove(addr) } case "tcp": - host, port, _ := net.SplitHostPort(cfg.Address) - addr = fmt.Sprintf("%s:%s", host, port) + addr = u.Host default: return nil, fmt.Errorf("unsupported protocol %s, only `tcp` and 'unix' are supported", protocol) }