From 4237a34c31b18bc6e9fd69234b3b72fe8753e2ce Mon Sep 17 00:00:00 2001 From: Vladimir Mihailenco Date: Thu, 31 May 2018 10:25:40 +0300 Subject: [PATCH] cluster: fix origin addr check --- cluster.go | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/cluster.go b/cluster.go index 5fad373..0c58c85 100644 --- a/cluster.go +++ b/cluster.go @@ -411,7 +411,7 @@ func newClusterState( var nodes []*clusterNode for i, slotNode := range slot.Nodes { addr := slotNode.Addr - if !isLoopbackOrigin && isLoopbackAddr(addr) { + if !isLoopbackOrigin && useOriginAddr(origin, addr) { addr = origin } @@ -1494,6 +1494,29 @@ func (c *ClusterClient) PSubscribe(channels ...string) *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 { host, _, err := net.SplitHostPort(addr) if err != nil {