diff --git a/go/topology/data_node.go b/go/topology/data_node.go index 19f3870de..4fea289f8 100644 --- a/go/topology/data_node.go +++ b/go/topology/data_node.go @@ -6,6 +6,7 @@ import ( "github.com/chrislusf/seaweedfs/go/glog" "github.com/chrislusf/seaweedfs/go/storage" + "net" ) type DataNode struct { @@ -87,7 +88,7 @@ func (dn *DataNode) MatchLocation(ip string, port int) bool { } func (dn *DataNode) Url() string { - return dn.Ip + ":" + strconv.Itoa(dn.Port) + return net.JoinHostPort(dn.Ip, strconv.Itoa(dn.Port)) } func (dn *DataNode) ToMap() interface{} { diff --git a/go/topology/rack.go b/go/topology/rack.go index 2029f1e44..634103f95 100644 --- a/go/topology/rack.go +++ b/go/topology/rack.go @@ -1,6 +1,7 @@ package topology import ( + "net" "strconv" "time" ) @@ -40,13 +41,13 @@ func (r *Rack) GetOrCreateDataNode(ip string, port int, publicUrl string, maxVol return dn } } - dn := NewDataNode(ip + ":" + strconv.Itoa(port)) + dn := NewDataNode(net.JoinHostPort(ip, strconv.Itoa(port))) dn.Ip = ip dn.Port = port if publicUrl == "" { - publicUrl = ip + ":" + strconv.Itoa(port) + publicUrl = net.JoinHostPort(ip, strconv.Itoa(port)) } else if publicUrl[0] == ':' { - publicUrl = ip + publicUrl + publicUrl = net.JoinHostPort(ip, publicUrl[1:]) } dn.PublicUrl = publicUrl dn.maxVolumeCount = maxVolumeCount diff --git a/go/topology/store_replicate.go b/go/topology/store_replicate.go index 772bf8516..46d9e23e3 100644 --- a/go/topology/store_replicate.go +++ b/go/topology/store_replicate.go @@ -10,6 +10,7 @@ import ( "github.com/chrislusf/seaweedfs/go/security" "github.com/chrislusf/seaweedfs/go/storage" "github.com/chrislusf/seaweedfs/go/util" + "net" ) func ReplicatedWrite(masterNode string, s *storage.Store, @@ -69,7 +70,7 @@ func ReplicatedDelete(masterNode string, store *storage.Store, func distributedOperation(masterNode string, store *storage.Store, volumeId storage.VolumeId, op func(location operation.Location) bool) bool { if lookupResult, lookupErr := operation.LookupNoCache(masterNode, volumeId.String()); lookupErr == nil { length := 0 - selfUrl := (store.Ip + ":" + strconv.Itoa(store.Port)) + selfUrl := net.JoinHostPort(store.Ip, strconv.Itoa(store.Port)) results := make(chan bool) for _, location := range lookupResult.Locations { if location.Url != selfUrl { diff --git a/go/weed/master.go b/go/weed/master.go index fda19429d..c4889ce57 100644 --- a/go/weed/master.go +++ b/go/weed/master.go @@ -12,6 +12,7 @@ import ( "github.com/chrislusf/seaweedfs/go/util" "github.com/chrislusf/seaweedfs/go/weed/weed_server" "github.com/gorilla/mux" + "net" ) func init() { @@ -64,7 +65,7 @@ func runMaster(cmd *Command, args []string) bool { masterWhiteList, *masterSecureKey, ) - listeningAddress := *masterBindIp + ":" + strconv.Itoa(*mport) + listeningAddress := net.JoinHostPort(*masterBindIp, strconv.Itoa(*mport)) glog.V(0).Infoln("Start Seaweed Master", util.VERSION, "at", listeningAddress) @@ -75,7 +76,7 @@ func runMaster(cmd *Command, args []string) bool { go func() { time.Sleep(100 * time.Millisecond) - myMasterAddress := *masterIp + ":" + strconv.Itoa(*mport) + myMasterAddress := net.JoinHostPort(*masterIp, strconv.Itoa(*mport)) var peers []string if *masterPeers != "" { peers = strings.Split(*masterPeers, ",") diff --git a/go/weed/server.go b/go/weed/server.go index 22aae9e13..7c8b0e9f0 100644 --- a/go/weed/server.go +++ b/go/weed/server.go @@ -15,6 +15,7 @@ import ( "github.com/chrislusf/seaweedfs/go/util" "github.com/chrislusf/seaweedfs/go/weed/weed_server" "github.com/gorilla/mux" + "net" ) type ServerOptions struct { @@ -107,7 +108,7 @@ func runServer(cmd *Command, args []string) bool { *isStartingFiler = true } - *filerOptions.master = *serverIp + ":" + strconv.Itoa(*masterPort) + *filerOptions.master = net.JoinHostPort(*serverIp, strconv.Itoa(*masterPort)) if *filerOptions.defaultReplicaPlacement == "" { *filerOptions.defaultReplicaPlacement = *masterDefaultReplicaPlacement @@ -201,8 +202,8 @@ func runServer(cmd *Command, args []string) bool { serverWhiteList, *serverSecureKey, ) - glog.V(0).Infoln("Start Seaweed Master", util.VERSION, "at", *serverIp+":"+strconv.Itoa(*masterPort)) - masterListener, e := util.NewListener(*serverBindIp+":"+strconv.Itoa(*masterPort), time.Duration(*serverTimeout)*time.Second) + glog.V(0).Infoln("Start Seaweed Master", util.VERSION, "at", net.JoinHostPort(*serverIp, strconv.Itoa(*masterPort))) + masterListener, e := util.NewListener(net.JoinHostPort(*serverBindIp, strconv.Itoa(*masterPort)), time.Duration(*serverTimeout)*time.Second) if e != nil { glog.Fatalf("Master startup error: %v", e) } @@ -210,7 +211,7 @@ func runServer(cmd *Command, args []string) bool { go func() { raftWaitForMaster.Wait() time.Sleep(100 * time.Millisecond) - myAddress := *serverIp + ":" + strconv.Itoa(*masterPort) + myAddress := net.JoinHostPort(*serverIp, strconv.Itoa(*masterPort)) var peers []string if *serverPeers != "" { peers = strings.Split(*serverPeers, ",") @@ -232,7 +233,7 @@ func runServer(cmd *Command, args []string) bool { *volumePublicPort = *volumePort } if *volumeServerPublicUrl == "" { - *volumeServerPublicUrl = *serverIp + ":" + strconv.Itoa(*volumePublicPort) + *volumeServerPublicUrl = net.JoinHostPort(*serverIp, strconv.Itoa(*volumePublicPort)) } isSeperatedPublicPort := *volumePublicPort != *volumePort volumeMux := http.NewServeMux() @@ -251,20 +252,20 @@ func runServer(cmd *Command, args []string) bool { *serverIp, *volumePort, *volumeServerPublicUrl, folders, maxCounts, volumeNeedleMapKind, - *serverIp+":"+strconv.Itoa(*masterPort), *volumePulse, *serverDataCenter, *serverRack, + net.JoinHostPort(*serverIp, strconv.Itoa(*masterPort)), *volumePulse, *serverDataCenter, *serverRack, serverWhiteList, *volumeFixJpgOrientation, *volumeReadRedirect, ) - glog.V(0).Infoln("Start Seaweed volume server", util.VERSION, "at", *serverIp+":"+strconv.Itoa(*volumePort)) + glog.V(0).Infoln("Start Seaweed volume server", util.VERSION, "at", net.JoinHostPort(*serverIp, strconv.Itoa(*volumePort))) volumeListener, eListen := util.NewListener( - *serverBindIp+":"+strconv.Itoa(*volumePort), + net.JoinHostPort(*serverBindIp, strconv.Itoa(*volumePort)), time.Duration(*serverTimeout)*time.Second, ) if eListen != nil { glog.Fatalf("Volume server listener error: %v", eListen) } if isSeperatedPublicPort { - publicListeningAddress := *serverIp + ":" + strconv.Itoa(*volumePublicPort) + publicListeningAddress := net.JoinHostPort(*serverIp, strconv.Itoa(*volumePublicPort)) glog.V(0).Infoln("Start Seaweed volume server", util.VERSION, "public at", publicListeningAddress) publicListener, e := util.NewListener(publicListeningAddress, time.Duration(*serverTimeout)*time.Second) if e != nil { diff --git a/go/weed/volume.go b/go/weed/volume.go index 36b0317e2..2ab8491d4 100644 --- a/go/weed/volume.go +++ b/go/weed/volume.go @@ -12,6 +12,7 @@ import ( "github.com/chrislusf/seaweedfs/go/storage" "github.com/chrislusf/seaweedfs/go/util" "github.com/chrislusf/seaweedfs/go/weed/weed_server" + "net" ) var ( @@ -108,7 +109,7 @@ func runVolume(cmd *Command, args []string) bool { *v.publicPort = *v.port } if *v.publicUrl == "" { - *v.publicUrl = *v.ip + ":" + strconv.Itoa(*v.publicPort) + *v.publicUrl = net.JoinHostPort(*v.ip, strconv.Itoa(*v.publicPort)) } isSeperatedPublicPort := *v.publicPort != *v.port @@ -134,14 +135,14 @@ func runVolume(cmd *Command, args []string) bool { *v.fixJpgOrientation, *v.readRedirect, ) - listeningAddress := *v.bindIp + ":" + strconv.Itoa(*v.port) + listeningAddress := net.JoinHostPort(*v.bindIp, strconv.Itoa(*v.port)) glog.V(0).Infoln("Start Seaweed volume server", util.VERSION, "at", listeningAddress) listener, e := util.NewListener(listeningAddress, time.Duration(*v.idleConnectionTimeout)*time.Second) if e != nil { glog.Fatalf("Volume server listener error:%v", e) } if isSeperatedPublicPort { - publicListeningAddress := *v.bindIp + ":" + strconv.Itoa(*v.publicPort) + publicListeningAddress := net.JoinHostPort(*v.bindIp, strconv.Itoa(*v.publicPort)) glog.V(0).Infoln("Start Seaweed volume server", util.VERSION, "public at", publicListeningAddress) publicListener, e := util.NewListener(publicListeningAddress, time.Duration(*v.idleConnectionTimeout)*time.Second) if e != nil { diff --git a/go/weed/weed_server/master_server_handlers_admin.go b/go/weed/weed_server/master_server_handlers_admin.go index 38a68762d..748c98fda 100644 --- a/go/weed/weed_server/master_server_handlers_admin.go +++ b/go/weed/weed_server/master_server_handlers_admin.go @@ -7,11 +7,12 @@ import ( "io/ioutil" "net/http" "strconv" - "strings" "net/url" "sync" + "net" + "github.com/chrislusf/seaweedfs/go/glog" "github.com/chrislusf/seaweedfs/go/operation" "github.com/chrislusf/seaweedfs/go/storage" @@ -27,7 +28,7 @@ func (ms *MasterServer) collectionDeleteHandler(w http.ResponseWriter, r *http.R return } for _, server := range collection.ListVolumeServers() { - _, err := util.Get(server.Ip+":"+strconv.Itoa(server.Port), "/admin/delete_collection", url.Values{"collection": r.Form["collection"]}) + _, err := util.Get(net.JoinHostPort(server.Ip, strconv.Itoa(server.Port)), "/admin/delete_collection", url.Values{"collection": r.Form["collection"]}) if err != nil { writeJsonError(w, r, http.StatusInternalServerError, err) return @@ -48,7 +49,12 @@ func (ms *MasterServer) dirJoinHandler(w http.ResponseWriter, r *http.Request) { return } if *joinMessage.Ip == "" { - *joinMessage.Ip = r.RemoteAddr[0:strings.Index(r.RemoteAddr, ":")] + if ip, _, e := net.SplitHostPort(r.RemoteAddr); e == nil { + *joinMessage.Ip = ip + } else { + glog.V(2).Infof("SplitHostPort (%s) error, %v", r.RemoteAddr, e) + *joinMessage.Ip = r.RemoteAddr + } } if glog.V(4) { if jsonData, jsonError := json.Marshal(joinMessage); jsonError != nil {