cluster: fix origin addr check

This commit is contained in:
Vladimir Mihailenco 2018-05-31 10:25:40 +03:00
parent ef33be46cd
commit 4237a34c31
1 changed files with 24 additions and 1 deletions

View File

@ -411,7 +411,7 @@ func newClusterState(
var nodes []*clusterNode var nodes []*clusterNode
for i, slotNode := range slot.Nodes { for i, slotNode := range slot.Nodes {
addr := slotNode.Addr addr := slotNode.Addr
if !isLoopbackOrigin && isLoopbackAddr(addr) { if !isLoopbackOrigin && useOriginAddr(origin, addr) {
addr = origin addr = origin
} }
@ -1494,6 +1494,29 @@ func (c *ClusterClient) PSubscribe(channels ...string) *PubSub {
return pubsub return pubsub
} }
func useOriginAddr(originAddr, nodeAddr string) bool {
nodeHost, nodePort, err := net.SplitHostPort(nodeAddr)
if err != nil {
return false
}
nodeIP := net.ParseIP(nodeHost)
if nodeIP == nil {
return false
}
if !nodeIP.IsLoopback() {
return false
}
_, originPort, err := net.SplitHostPort(originAddr)
if err != nil {
return false
}
return nodePort == originPort
}
func isLoopbackAddr(addr string) bool { func isLoopbackAddr(addr string) bool {
host, _, err := net.SplitHostPort(addr) host, _, err := net.SplitHostPort(addr)
if err != nil { if err != nil {