diff --git a/cluster/router/chain/chain.go b/cluster/router/chain/chain.go index 1fd6fb5e24644eaa237ebbf483e2ee21096a0760..6ae9920e0a7f381b505627e71ff06e4ab715e016 100644 --- a/cluster/router/chain/chain.go +++ b/cluster/router/chain/chain.go @@ -65,7 +65,7 @@ func (c *RouterChain) Route(invokers []protocol.Invoker, url *common.URL, invoca return finalInvokers } -// Notify router chain of the initial addresses from registry at the first time. Notify whenever addresses in registry change. +// SetInvokers router chain of the initial addresses from registry at the first time. Notify whenever addresses in registry change. func (c *RouterChain) SetInvokers(invokers []protocol.Invoker) { for _, r := range c.routers { if notifyRouter, ok := r.(router.NotifyRouter); ok { diff --git a/cluster/router/tag/router_rule.go b/cluster/router/tag/router_rule.go index 78479c0f1d4c2468c73083ec1f324f4ec86e31e2..b35d455a6e763056b5d52da7f494258a22fd4b8c 100644 --- a/cluster/router/tag/router_rule.go +++ b/cluster/router/tag/router_rule.go @@ -39,7 +39,7 @@ import ( */ // RouterRule RouterRule config read from config file or config center type RouterRule struct { - router.BaseRouterRule `yaml:",inline""` + router.BaseRouterRule `yaml:",inline"` Tags []Tag addressToTagNames map[string][]string tagNameToAddresses map[string][]string @@ -52,12 +52,12 @@ func getRule(rawRule string) (*RouterRule, error) { return r, err } r.RawRule = rawRule - r.init() + r.parseTags() return r, nil } -// init use for flattening tags data to @addressToTagNames and @tagNameToAddresses -func (t *RouterRule) init() { +// parseTags use for flattening tags data to @addressToTagNames and @tagNameToAddresses +func (t *RouterRule) parseTags() { t.addressToTagNames = make(map[string][]string, 2*len(t.Tags)) t.tagNameToAddresses = make(map[string][]string, len(t.Tags)) for _, tag := range t.Tags { diff --git a/cluster/router/tag/tag.go b/cluster/router/tag/tag.go index 73d10b5db4a1664c4d9d014045b475fac757684b..468b426a4cfe9f0534ad80a74b2315bdbd85b9c0 100644 --- a/cluster/router/tag/tag.go +++ b/cluster/router/tag/tag.go @@ -21,19 +21,3 @@ type Tag struct { Name string Addresses []string } - -func (t *Tag) getName() string { - return t.Name -} - -func (t *Tag) setName(name string) { - t.Name = name -} - -func (t *Tag) getAddresses() []string { - return t.Addresses -} - -func (t *Tag) setAddresses(addresses []string) { - t.Addresses = addresses -} diff --git a/cluster/router/tag/tag_router.go b/cluster/router/tag/tag_router.go index c5bc87675bf170067d23f59bb66f1d2ad68a5bd8..c3ee3cbc162a772eed2c812ecb47207c9a1df95a 100644 --- a/cluster/router/tag/tag_router.go +++ b/cluster/router/tag/tag_router.go @@ -18,13 +18,12 @@ package tag import ( - "errors" "net" "strconv" - "strings" ) import ( + gxnet "github.com/dubbogo/gost/net" perrors "github.com/pkg/errors" ) @@ -272,144 +271,14 @@ OUTER: } func checkAddressMatch(addresses []string, host, port string) bool { + addr := net.JoinHostPort(constant.ANYHOST_VALUE, port) for _, address := range addresses { - if matchIp(address, host, port) { + if gxnet.MatchIP(address, host, port) { return true } - if address == net.JoinHostPort(constant.ANYHOST_VALUE, port) { + if address == addr { return true } } return false } - -// TODO: Already moved to dubbogo/gost, after gost by merged the follows codes will be deleted. -func matchIp(pattern, host, port string) bool { - // if the pattern is subnet format, it will not be allowed to config port param in pattern. - if strings.Contains(pattern, "/") { - _, subnet, _ := net.ParseCIDR(pattern) - if subnet != nil && subnet.Contains(net.ParseIP(host)) { - return true - } - return false - } - return matchIpRange(pattern, host, port) -} - -func matchIpRange(pattern, host, port string) bool { - if pattern == "" || host == "" { - logger.Error("Illegal Argument pattern or hostName. Pattern:" + pattern + ", Host:" + host) - return false - } - - pattern = strings.TrimSpace(pattern) - if "*.*.*.*" == pattern || "*" == pattern { - return true - } - - isIpv4 := true - ip4 := net.ParseIP(host).To4() - - if ip4 == nil { - isIpv4 = false - } - - hostAndPort := getPatternHostAndPort(pattern, isIpv4) - if hostAndPort[1] != "" && hostAndPort[1] != port { - return false - } - - pattern = hostAndPort[0] - splitCharacter := "." - if !isIpv4 { - splitCharacter = ":" - } - - mask := strings.Split(pattern, splitCharacter) - // check format of pattern - if err := checkHostPattern(pattern, mask, isIpv4); err != nil { - logger.Error(err) - return false - } - - if pattern == host { - return true - } - - // short name condition - if !ipPatternContains(pattern) { - return pattern == host - } - - ipAddress := strings.Split(host, splitCharacter) - for i := 0; i < len(mask); i++ { - if "*" == mask[i] || mask[i] == ipAddress[i] { - continue - } else if strings.Contains(mask[i], "-") { - rangeNumStrs := strings.Split(mask[i], "-") - if len(rangeNumStrs) != 2 { - logger.Error("There is wrong format of ip Address: " + mask[i]) - return false - } - min := getNumOfIpSegment(rangeNumStrs[0], isIpv4) - max := getNumOfIpSegment(rangeNumStrs[1], isIpv4) - ip := getNumOfIpSegment(ipAddress[i], isIpv4) - if ip < min || ip > max { - return false - } - } else if "0" == ipAddress[i] && "0" == mask[i] || "00" == mask[i] || "000" == mask[i] || "0000" == mask[i] { - continue - } else if mask[i] != ipAddress[i] { - return false - } - } - return true -} - -func ipPatternContains(pattern string) bool { - return strings.Contains(pattern, "*") || strings.Contains(pattern, "-") -} - -func checkHostPattern(pattern string, mask []string, isIpv4 bool) error { - if !isIpv4 { - if len(mask) != 8 && ipPatternContains(pattern) { - return errors.New("If you config ip expression that contains '*' or '-', please fill qualified ip pattern like 234e:0:4567:0:0:0:3d:*. ") - } - if len(mask) != 8 && !strings.Contains(pattern, "::") { - return errors.New("The host is ipv6, but the pattern is not ipv6 pattern : " + pattern) - } - } else { - if len(mask) != 4 { - return errors.New("The host is ipv4, but the pattern is not ipv4 pattern : " + pattern) - } - } - return nil -} - -func getPatternHostAndPort(pattern string, isIpv4 bool) []string { - result := make([]string, 2) - if strings.HasPrefix(pattern, "[") && strings.Contains(pattern, "]:") { - end := strings.Index(pattern, "]:") - result[0] = pattern[1:end] - result[1] = pattern[end+2:] - } else if strings.HasPrefix(pattern, "[") && strings.HasSuffix(pattern, "]") { - result[0] = pattern[1 : len(pattern)-1] - result[1] = "" - } else if isIpv4 && strings.Contains(pattern, ":") { - end := strings.Index(pattern, ":") - result[0] = pattern[:end] - result[1] = pattern[end+1:] - } else { - result[0] = pattern - } - return result -} - -func getNumOfIpSegment(ipSegment string, isIpv4 bool) int { - if isIpv4 { - ipSeg, _ := strconv.Atoi(ipSegment) - return ipSeg - } - ipSeg, _ := strconv.ParseInt(ipSegment, 0, 16) - return int(ipSeg) -} diff --git a/go.mod b/go.mod index 197f2a3012896affd499ab95dd5abd9263d4010f..30f7a7770b6c3a4f85f2bb606abff131dc40bd7c 100644 --- a/go.mod +++ b/go.mod @@ -15,7 +15,7 @@ require ( github.com/docker/go-connections v0.4.0 // indirect github.com/dubbogo/getty v1.3.7 github.com/dubbogo/go-zookeeper v1.0.1 - github.com/dubbogo/gost v1.9.0 + github.com/dubbogo/gost v1.9.1 github.com/elazarl/go-bindata-assetfs v1.0.0 // indirect github.com/emicklei/go-restful/v3 v3.0.0 github.com/frankban/quicktest v1.4.1 // indirect diff --git a/go.sum b/go.sum index 6df5fc5248feb0281deeb3bc57e83453ae52ea7a..aa46f2164da294f59c8b7e1e6601eb53501c8e72 100644 --- a/go.sum +++ b/go.sum @@ -149,6 +149,8 @@ github.com/dubbogo/go-zookeeper v1.0.1 h1:irLzvOsDOTNsN8Sv9tvYYxVu6DCQfLtziZQtUH github.com/dubbogo/go-zookeeper v1.0.1/go.mod h1:fn6n2CAEer3novYgk9ULLwAjuV8/g4DdC2ENwRb6E+c= github.com/dubbogo/gost v1.9.0 h1:UT+dWwvLyJiDotxJERO75jB3Yxgsdy10KztR5ycxRAk= github.com/dubbogo/gost v1.9.0/go.mod h1:pPTjVyoJan3aPxBPNUX0ADkXjPibLo+/Ib0/fADXSG8= +github.com/dubbogo/gost v1.9.1 h1:0/PPFo13zPbjt4Ia0zYWMFi3C6rAe9X7O1J2Iv+BHNM= +github.com/dubbogo/gost v1.9.1/go.mod h1:pPTjVyoJan3aPxBPNUX0ADkXjPibLo+/Ib0/fADXSG8= github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= github.com/elazarl/go-bindata-assetfs v0.0.0-20160803192304-e1a2a7ec64b0 h1:ZoRgc53qJCfSLimXqJDrmBhnt5GChDsExMCK7t48o0Y= github.com/elazarl/go-bindata-assetfs v0.0.0-20160803192304-e1a2a7ec64b0/go.mod h1:v+YaWX3bdea5J/mo8dSETolEo7R71Vk1u8bnjau5yw4=