From 0128239c0f8829bb20edd1df257840dcbfa37c0e Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Tue, 7 Sep 2021 16:43:54 -0700 Subject: [PATCH] handle ipv6 addresses --- .../main/java/seaweedfs/client/FilerGrpcClient.java | 2 +- .../src/main/java/seaweedfs/client/SeaweedUtil.java | 10 ++++++++++ weed/command/filer.go | 2 +- weed/command/filer_copy.go | 2 +- weed/command/server.go | 2 +- weed/ftpd/ftp_server.go | 2 +- weed/operation/grpc_client.go | 3 ++- weed/pb/grpc_client_server.go | 8 ++++---- weed/server/filer_grpc_server.go | 2 +- weed/server/filer_server.go | 2 +- weed/server/master_grpc_server.go | 4 ++-- weed/server/master_server.go | 4 ++-- weed/server/volume_server.go | 3 +-- weed/shell/commands.go | 2 +- weed/stats/metrics.go | 4 +++- weed/util/network.go | 10 ++++++++++ 16 files changed, 42 insertions(+), 20 deletions(-) diff --git a/other/java/client/src/main/java/seaweedfs/client/FilerGrpcClient.java b/other/java/client/src/main/java/seaweedfs/client/FilerGrpcClient.java index 6c57e2e0d..56aa35876 100644 --- a/other/java/client/src/main/java/seaweedfs/client/FilerGrpcClient.java +++ b/other/java/client/src/main/java/seaweedfs/client/FilerGrpcClient.java @@ -54,7 +54,7 @@ public class FilerGrpcClient { .negotiationType(NegotiationType.TLS) .sslContext(sslContext)); - filerAddress = String.format("%s:%d", host, grpcPort - 10000); + filerAddress = SeaweedUtil.joinHostPort(host, grpcPort - 10000); FilerProto.GetFilerConfigurationResponse filerConfigurationResponse = this.getBlockingStub().getFilerConfiguration( diff --git a/other/java/client/src/main/java/seaweedfs/client/SeaweedUtil.java b/other/java/client/src/main/java/seaweedfs/client/SeaweedUtil.java index 66ea9a98d..027e49b96 100644 --- a/other/java/client/src/main/java/seaweedfs/client/SeaweedUtil.java +++ b/other/java/client/src/main/java/seaweedfs/client/SeaweedUtil.java @@ -43,4 +43,14 @@ public class SeaweedUtil { String name = fullpath.substring(sep + 1); return new String[]{parent, name}; } + + public static String joinHostPort(String host, int port) { + if (host.startsWith("[") && host.endsWith("]")) { + return host + ":" + port; + } + if (host.indexOf(':')>=0) { + return "[" + host + "]:" + port; + } + return host + ":" + port; + } } diff --git a/weed/command/filer.go b/weed/command/filer.go index ddee0852c..b1237cc3f 100644 --- a/weed/command/filer.go +++ b/weed/command/filer.go @@ -134,7 +134,7 @@ func runFiler(cmd *Command, args []string) bool { go stats_collect.StartMetricsServer(*f.metricsHttpPort) - filerAddress := fmt.Sprintf("%s:%d", *f.ip, *f.port) + filerAddress := util.JoinHostPort(*f.ip, *f.port) startDelay := time.Duration(2) if *filerStartS3 { filerS3Options.filer = &filerAddress diff --git a/weed/command/filer_copy.go b/weed/command/filer_copy.go index 2cc0d245c..05aa96292 100644 --- a/weed/command/filer_copy.go +++ b/weed/command/filer_copy.go @@ -115,7 +115,7 @@ func runCopy(cmd *Command, args []string) bool { } filerGrpcPort := filerPort + 10000 - filerGrpcAddress := fmt.Sprintf("%s:%d", filerUrl.Hostname(), filerGrpcPort) + filerGrpcAddress := util.JoinHostPort(filerUrl.Hostname(), int(filerGrpcPort)) copy.grpcDialOption = security.LoadClientTLS(util.GetViper(), "grpc.client") masters, collection, replication, dirBuckets, maxMB, cipher, err := readFilerConfiguration(copy.grpcDialOption, filerGrpcAddress) diff --git a/weed/command/server.go b/weed/command/server.go index c784d90b9..b32d9d51e 100644 --- a/weed/command/server.go +++ b/weed/command/server.go @@ -194,7 +194,7 @@ func runServer(cmd *Command, args []string) bool { filerOptions.disableHttp = serverDisableHttp masterOptions.disableHttp = serverDisableHttp - filerAddress := fmt.Sprintf("%s:%d", *serverIp, *filerOptions.port) + filerAddress := util.JoinHostPort(*serverIp, *filerOptions.port) s3Options.filer = &filerAddress webdavOptions.filer = &filerAddress msgBrokerOptions.filer = &filerAddress diff --git a/weed/ftpd/ftp_server.go b/weed/ftpd/ftp_server.go index 4a0dca2c3..0abeeac02 100644 --- a/weed/ftpd/ftp_server.go +++ b/weed/ftpd/ftp_server.go @@ -51,7 +51,7 @@ func (s *SftpServer) GetSettings() (*ftpserver.Settings, error) { return &ftpserver.Settings{ Listener: s.ftpListener, - ListenAddr: fmt.Sprintf("%s:%d", s.option.IpBind, s.option.Port), + ListenAddr: util.JoinHostPort(s.option.IpBind, s.option.Port), PublicHost: s.option.IP, PassiveTransferPortRange: portRange, ActiveTransferPortNon20: true, diff --git a/weed/operation/grpc_client.go b/weed/operation/grpc_client.go index 025a65b38..39f70343a 100644 --- a/weed/operation/grpc_client.go +++ b/weed/operation/grpc_client.go @@ -2,6 +2,7 @@ package operation import ( "fmt" + "github.com/chrislusf/seaweedfs/weed/util" "strconv" "strings" @@ -35,7 +36,7 @@ func toVolumeServerGrpcAddress(volumeServer string) (grpcAddress string, err err glog.Errorf("failed to parse volume server address: %v", volumeServer) return "", err } - return fmt.Sprintf("%s:%d", volumeServer[0:sepIndex], port+10000), nil + return util.JoinHostPort(volumeServer[0:sepIndex], port+10000), nil } func WithMasterServerClient(masterServer string, grpcDialOption grpc.DialOption, fn func(masterClient master_pb.SeaweedClient) error) error { diff --git a/weed/pb/grpc_client_server.go b/weed/pb/grpc_client_server.go index 3a3dd0589..f296cf983 100644 --- a/weed/pb/grpc_client_server.go +++ b/weed/pb/grpc_client_server.go @@ -4,8 +4,8 @@ import ( "context" "fmt" "github.com/chrislusf/seaweedfs/weed/glog" + "github.com/chrislusf/seaweedfs/weed/util" "math/rand" - "net" "net/http" "strconv" "strings" @@ -159,7 +159,7 @@ func ParseServerAddress(server string, deltaPort int) (newServerAddress string, newPort := int(port) + deltaPort - return net.JoinHostPort(host, strconv.Itoa(newPort)), nil + return util.JoinHostPort(host, newPort), nil } func hostAndPort(address string) (host string, port uint64, err error) { @@ -184,7 +184,7 @@ func ServerToGrpcAddress(server string) (serverGrpcAddress string) { grpcPort := int(port) + 10000 - return net.JoinHostPort(host, strconv.Itoa(grpcPort)) + return util.JoinHostPort(host, grpcPort) } func GrpcAddressToServerAddress(grpcAddress string) (serverAddress string) { @@ -195,7 +195,7 @@ func GrpcAddressToServerAddress(grpcAddress string) (serverAddress string) { port := int(grpcPort) - 10000 - return net.JoinHostPort(host, strconv.Itoa(port)) + return util.JoinHostPort(host, port) } func WithMasterClient(master string, grpcDialOption grpc.DialOption, fn func(client master_pb.SeaweedClient) error) error { diff --git a/weed/server/filer_grpc_server.go b/weed/server/filer_grpc_server.go index e025e73dc..6a7df0f87 100644 --- a/weed/server/filer_grpc_server.go +++ b/weed/server/filer_grpc_server.go @@ -412,7 +412,7 @@ func (fs *FilerServer) KeepConnected(stream filer_pb.SeaweedFiler_KeepConnectedS return err } - clientName := fmt.Sprintf("%s:%d", req.Name, req.GrpcPort) + clientName := util.JoinHostPort(req.Name, int(req.GrpcPort)) m := make(map[string]bool) for _, tp := range req.Resources { m[tp] = true diff --git a/weed/server/filer_server.go b/weed/server/filer_server.go index 534bc4840..7e5e98660 100644 --- a/weed/server/filer_server.go +++ b/weed/server/filer_server.go @@ -143,7 +143,7 @@ func NewFilerServer(defaultMux, readonlyMux *http.ServeMux, option *FilerOption) readonlyMux.HandleFunc("/", fs.readonlyFilerHandler) } - fs.filer.AggregateFromPeers(fmt.Sprintf("%s:%d", option.Host, option.Port), option.Filers) + fs.filer.AggregateFromPeers(util.JoinHostPort(option.Host, int(option.Port)), option.Filers) fs.filer.LoadBuckets() diff --git a/weed/server/master_grpc_server.go b/weed/server/master_grpc_server.go index 7bb61b1bd..80cd37879 100644 --- a/weed/server/master_grpc_server.go +++ b/weed/server/master_grpc_server.go @@ -2,8 +2,8 @@ package weed_server import ( "context" - "fmt" "github.com/chrislusf/seaweedfs/weed/storage/backend" + "github.com/chrislusf/seaweedfs/weed/util" "net" "strings" "time" @@ -289,7 +289,7 @@ func findClientAddress(ctx context.Context, grpcPort uint32) string { } if tcpAddr, ok := pr.Addr.(*net.TCPAddr); ok { externalIP := tcpAddr.IP - return fmt.Sprintf("%s:%d", externalIP, grpcPort) + return util.JoinHostPort(externalIP.String(), int(grpcPort)) } return pr.Addr.String() diff --git a/weed/server/master_server.go b/weed/server/master_server.go index d2edeb6cb..7c78be379 100644 --- a/weed/server/master_server.go +++ b/weed/server/master_server.go @@ -224,7 +224,7 @@ func (ms *MasterServer) startAdminScripts() { scriptLines = append(scriptLines, "unlock") } - masterAddress := fmt.Sprintf("%s:%d", ms.option.Host, ms.option.Port) + masterAddress := util.JoinHostPort(ms.option.Host, ms.option.Port) var shellOptions shell.ShellOptions shellOptions.GrpcDialOption = security.LoadClientTLS(v, "grpc.master") @@ -299,7 +299,7 @@ func (ms *MasterServer) createSequencer(option *MasterOption) sequence.Sequencer case "snowflake": var err error snowflakeId := v.GetInt(SequencerSnowflakeId) - seq, err = sequence.NewSnowflakeSequencer(fmt.Sprintf("%s:%d", option.Host, option.Port), snowflakeId) + seq, err = sequence.NewSnowflakeSequencer(util.JoinHostPort(option.Host, option.Port), snowflakeId) if err != nil { glog.Error(err) seq = nil diff --git a/weed/server/volume_server.go b/weed/server/volume_server.go index 034521b4b..9406b5601 100644 --- a/weed/server/volume_server.go +++ b/weed/server/volume_server.go @@ -1,7 +1,6 @@ package weed_server import ( - "fmt" "github.com/chrislusf/seaweedfs/weed/storage/types" "net/http" "sync" @@ -113,7 +112,7 @@ func NewVolumeServer(adminMux, publicMux *http.ServeMux, ip string, } go vs.heartbeat() - go stats.LoopPushingMetric("volumeServer", fmt.Sprintf("%s:%d", ip, port), vs.metricsAddress, vs.metricsIntervalSec) + go stats.LoopPushingMetric("volumeServer", util.JoinHostPort(ip, port), vs.metricsAddress, vs.metricsIntervalSec) return vs } diff --git a/weed/shell/commands.go b/weed/shell/commands.go index 5497e89cc..aef71b419 100644 --- a/weed/shell/commands.go +++ b/weed/shell/commands.go @@ -98,7 +98,7 @@ var _ = filer_pb.FilerClient(&CommandEnv{}) func (ce *CommandEnv) WithFilerClient(fn func(filer_pb.SeaweedFilerClient) error) error { - filerGrpcAddress := fmt.Sprintf("%s:%d", ce.option.FilerHost, ce.option.FilerPort+10000) + filerGrpcAddress := util.JoinHostPort(ce.option.FilerHost, int(ce.option.FilerPort+10000)) return pb.WithGrpcFilerClient(filerGrpcAddress, ce.option.GrpcDialOption, fn) } diff --git a/weed/stats/metrics.go b/weed/stats/metrics.go index 48d964418..1e1681f9a 100644 --- a/weed/stats/metrics.go +++ b/weed/stats/metrics.go @@ -3,8 +3,10 @@ package stats import ( "fmt" "log" + "net" "net/http" "os" + "strconv" "strings" "time" @@ -180,5 +182,5 @@ func SourceName(port uint32) string { if err != nil { return "unknown" } - return fmt.Sprintf("%s:%d", hostname, port) + return net.JoinHostPort(hostname, strconv.Itoa(int(port))) } diff --git a/weed/util/network.go b/weed/util/network.go index 55a123667..f9fad7771 100644 --- a/weed/util/network.go +++ b/weed/util/network.go @@ -2,6 +2,8 @@ package util import ( "net" + "strconv" + "strings" "github.com/chrislusf/seaweedfs/weed/glog" ) @@ -33,3 +35,11 @@ func DetectedHostAddress() string { return "localhost" } + +func JoinHostPort(host string, port int) string { + portStr := strconv.Itoa(port) + if strings.HasPrefix(host, "[") && strings.HasSuffix(host, "]") { + return host + ":" + portStr + } + return net.JoinHostPort(host, portStr) +} \ No newline at end of file