handle ipv6 addresses

This commit is contained in:
Chris Lu 2021-09-07 16:43:54 -07:00
parent 35c8ea495f
commit 0128239c0f
16 changed files with 42 additions and 20 deletions

View file

@ -54,7 +54,7 @@ public class FilerGrpcClient {
.negotiationType(NegotiationType.TLS) .negotiationType(NegotiationType.TLS)
.sslContext(sslContext)); .sslContext(sslContext));
filerAddress = String.format("%s:%d", host, grpcPort - 10000); filerAddress = SeaweedUtil.joinHostPort(host, grpcPort - 10000);
FilerProto.GetFilerConfigurationResponse filerConfigurationResponse = FilerProto.GetFilerConfigurationResponse filerConfigurationResponse =
this.getBlockingStub().getFilerConfiguration( this.getBlockingStub().getFilerConfiguration(

View file

@ -43,4 +43,14 @@ public class SeaweedUtil {
String name = fullpath.substring(sep + 1); String name = fullpath.substring(sep + 1);
return new String[]{parent, name}; 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;
}
} }

View file

@ -134,7 +134,7 @@ func runFiler(cmd *Command, args []string) bool {
go stats_collect.StartMetricsServer(*f.metricsHttpPort) 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) startDelay := time.Duration(2)
if *filerStartS3 { if *filerStartS3 {
filerS3Options.filer = &filerAddress filerS3Options.filer = &filerAddress

View file

@ -115,7 +115,7 @@ func runCopy(cmd *Command, args []string) bool {
} }
filerGrpcPort := filerPort + 10000 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") copy.grpcDialOption = security.LoadClientTLS(util.GetViper(), "grpc.client")
masters, collection, replication, dirBuckets, maxMB, cipher, err := readFilerConfiguration(copy.grpcDialOption, filerGrpcAddress) masters, collection, replication, dirBuckets, maxMB, cipher, err := readFilerConfiguration(copy.grpcDialOption, filerGrpcAddress)

View file

@ -194,7 +194,7 @@ func runServer(cmd *Command, args []string) bool {
filerOptions.disableHttp = serverDisableHttp filerOptions.disableHttp = serverDisableHttp
masterOptions.disableHttp = serverDisableHttp masterOptions.disableHttp = serverDisableHttp
filerAddress := fmt.Sprintf("%s:%d", *serverIp, *filerOptions.port) filerAddress := util.JoinHostPort(*serverIp, *filerOptions.port)
s3Options.filer = &filerAddress s3Options.filer = &filerAddress
webdavOptions.filer = &filerAddress webdavOptions.filer = &filerAddress
msgBrokerOptions.filer = &filerAddress msgBrokerOptions.filer = &filerAddress

View file

@ -51,7 +51,7 @@ func (s *SftpServer) GetSettings() (*ftpserver.Settings, error) {
return &ftpserver.Settings{ return &ftpserver.Settings{
Listener: s.ftpListener, 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, PublicHost: s.option.IP,
PassiveTransferPortRange: portRange, PassiveTransferPortRange: portRange,
ActiveTransferPortNon20: true, ActiveTransferPortNon20: true,

View file

@ -2,6 +2,7 @@ package operation
import ( import (
"fmt" "fmt"
"github.com/chrislusf/seaweedfs/weed/util"
"strconv" "strconv"
"strings" "strings"
@ -35,7 +36,7 @@ func toVolumeServerGrpcAddress(volumeServer string) (grpcAddress string, err err
glog.Errorf("failed to parse volume server address: %v", volumeServer) glog.Errorf("failed to parse volume server address: %v", volumeServer)
return "", err 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 { func WithMasterServerClient(masterServer string, grpcDialOption grpc.DialOption, fn func(masterClient master_pb.SeaweedClient) error) error {

View file

@ -4,8 +4,8 @@ import (
"context" "context"
"fmt" "fmt"
"github.com/chrislusf/seaweedfs/weed/glog" "github.com/chrislusf/seaweedfs/weed/glog"
"github.com/chrislusf/seaweedfs/weed/util"
"math/rand" "math/rand"
"net"
"net/http" "net/http"
"strconv" "strconv"
"strings" "strings"
@ -159,7 +159,7 @@ func ParseServerAddress(server string, deltaPort int) (newServerAddress string,
newPort := int(port) + deltaPort 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) { func hostAndPort(address string) (host string, port uint64, err error) {
@ -184,7 +184,7 @@ func ServerToGrpcAddress(server string) (serverGrpcAddress string) {
grpcPort := int(port) + 10000 grpcPort := int(port) + 10000
return net.JoinHostPort(host, strconv.Itoa(grpcPort)) return util.JoinHostPort(host, grpcPort)
} }
func GrpcAddressToServerAddress(grpcAddress string) (serverAddress string) { func GrpcAddressToServerAddress(grpcAddress string) (serverAddress string) {
@ -195,7 +195,7 @@ func GrpcAddressToServerAddress(grpcAddress string) (serverAddress string) {
port := int(grpcPort) - 10000 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 { func WithMasterClient(master string, grpcDialOption grpc.DialOption, fn func(client master_pb.SeaweedClient) error) error {

View file

@ -412,7 +412,7 @@ func (fs *FilerServer) KeepConnected(stream filer_pb.SeaweedFiler_KeepConnectedS
return err return err
} }
clientName := fmt.Sprintf("%s:%d", req.Name, req.GrpcPort) clientName := util.JoinHostPort(req.Name, int(req.GrpcPort))
m := make(map[string]bool) m := make(map[string]bool)
for _, tp := range req.Resources { for _, tp := range req.Resources {
m[tp] = true m[tp] = true

View file

@ -143,7 +143,7 @@ func NewFilerServer(defaultMux, readonlyMux *http.ServeMux, option *FilerOption)
readonlyMux.HandleFunc("/", fs.readonlyFilerHandler) 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() fs.filer.LoadBuckets()

View file

@ -2,8 +2,8 @@ package weed_server
import ( import (
"context" "context"
"fmt"
"github.com/chrislusf/seaweedfs/weed/storage/backend" "github.com/chrislusf/seaweedfs/weed/storage/backend"
"github.com/chrislusf/seaweedfs/weed/util"
"net" "net"
"strings" "strings"
"time" "time"
@ -289,7 +289,7 @@ func findClientAddress(ctx context.Context, grpcPort uint32) string {
} }
if tcpAddr, ok := pr.Addr.(*net.TCPAddr); ok { if tcpAddr, ok := pr.Addr.(*net.TCPAddr); ok {
externalIP := tcpAddr.IP externalIP := tcpAddr.IP
return fmt.Sprintf("%s:%d", externalIP, grpcPort) return util.JoinHostPort(externalIP.String(), int(grpcPort))
} }
return pr.Addr.String() return pr.Addr.String()

View file

@ -224,7 +224,7 @@ func (ms *MasterServer) startAdminScripts() {
scriptLines = append(scriptLines, "unlock") 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 var shellOptions shell.ShellOptions
shellOptions.GrpcDialOption = security.LoadClientTLS(v, "grpc.master") shellOptions.GrpcDialOption = security.LoadClientTLS(v, "grpc.master")
@ -299,7 +299,7 @@ func (ms *MasterServer) createSequencer(option *MasterOption) sequence.Sequencer
case "snowflake": case "snowflake":
var err error var err error
snowflakeId := v.GetInt(SequencerSnowflakeId) 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 { if err != nil {
glog.Error(err) glog.Error(err)
seq = nil seq = nil

View file

@ -1,7 +1,6 @@
package weed_server package weed_server
import ( import (
"fmt"
"github.com/chrislusf/seaweedfs/weed/storage/types" "github.com/chrislusf/seaweedfs/weed/storage/types"
"net/http" "net/http"
"sync" "sync"
@ -113,7 +112,7 @@ func NewVolumeServer(adminMux, publicMux *http.ServeMux, ip string,
} }
go vs.heartbeat() 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 return vs
} }

View file

@ -98,7 +98,7 @@ var _ = filer_pb.FilerClient(&CommandEnv{})
func (ce *CommandEnv) WithFilerClient(fn func(filer_pb.SeaweedFilerClient) error) error { 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) return pb.WithGrpcFilerClient(filerGrpcAddress, ce.option.GrpcDialOption, fn)
} }

View file

@ -3,8 +3,10 @@ package stats
import ( import (
"fmt" "fmt"
"log" "log"
"net"
"net/http" "net/http"
"os" "os"
"strconv"
"strings" "strings"
"time" "time"
@ -180,5 +182,5 @@ func SourceName(port uint32) string {
if err != nil { if err != nil {
return "unknown" return "unknown"
} }
return fmt.Sprintf("%s:%d", hostname, port) return net.JoinHostPort(hostname, strconv.Itoa(int(port)))
} }

View file

@ -2,6 +2,8 @@ package util
import ( import (
"net" "net"
"strconv"
"strings"
"github.com/chrislusf/seaweedfs/weed/glog" "github.com/chrislusf/seaweedfs/weed/glog"
) )
@ -33,3 +35,11 @@ func DetectedHostAddress() string {
return "localhost" 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)
}