dubbo.apache.org/dubbo-go/v3@v3.1.1/common/match.go (about)

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one or more
     3   * contributor license agreements.  See the NOTICE file distributed with
     4   * this work for additional information regarding copyright ownership.
     5   * The ASF licenses this file to You under the Apache License, Version 2.0
     6   * (the "License"); you may not use this file except in compliance with
     7   * the License.  You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  
    18  package common
    19  
    20  import (
    21  	"fmt"
    22  	"net"
    23  	"regexp"
    24  	"strings"
    25  )
    26  
    27  import (
    28  	"dubbo.apache.org/dubbo-go/v3/common/constant"
    29  )
    30  
    31  type ParamMatch struct {
    32  	Key   string      `yaml:"key" json:"key,omitempty" property:"key"`
    33  	Value StringMatch `yaml:"value" json:"value,omitempty" property:"value"`
    34  }
    35  
    36  func (p *ParamMatch) IsMatch(url *URL) bool {
    37  	return p.Value.IsMatch(url.GetParam(p.Key, ""))
    38  }
    39  
    40  type StringMatch struct {
    41  	Exact    string `yaml:"exact" json:"exact,omitempty" property:"exact"`
    42  	Prefix   string `yaml:"prefix" json:"prefix,omitempty" property:"prefix"`
    43  	Regex    string `yaml:"regex" json:"regex,omitempty" property:"regex"`
    44  	Noempty  string `yaml:"noempty" json:"noempty,omitempty" property:"noempty"`
    45  	Empty    string `yaml:"empty" json:"empty,omitempty" property:"empty"`
    46  	Wildcard string `yaml:"wildcard" json:"wildcard,omitempty" property:"wildcard"`
    47  }
    48  
    49  func (p *StringMatch) IsMatch(value string) bool {
    50  	if p.Exact != "" {
    51  		return p.Exact == value
    52  	} else if p.Prefix != "" {
    53  		return strings.HasPrefix(value, p.Prefix)
    54  	} else if p.Regex != "" {
    55  		match, _ := regexp.MatchString(p.Regex, value)
    56  		return match
    57  	} else if p.Wildcard != "" {
    58  		return value == p.Wildcard || constant.AnyValue == p.Wildcard
    59  	} else if p.Empty != "" {
    60  		return value == ""
    61  	} else if p.Noempty != "" {
    62  		return value != ""
    63  	}
    64  	return false
    65  }
    66  
    67  type AddressMatch struct {
    68  	Wildcard string `yaml:"wildcard" json:"wildcard,omitempty" property:"wildcard"`
    69  	Cird     string `yaml:"cird" json:"cird,omitempty" property:"cird"`
    70  	Exact    string `yaml:"exact" json:"exact,omitempty" property:"exact"`
    71  }
    72  
    73  func (p *AddressMatch) IsMatch(value string) bool {
    74  	if p.Cird != "" && value != "" {
    75  		_, ipnet, err := net.ParseCIDR(p.Cird)
    76  		if err != nil {
    77  			fmt.Println("Error", p.Cird, err)
    78  			return false
    79  		}
    80  		return ipnet.Contains(net.ParseIP(value))
    81  	}
    82  	if p.Wildcard != "" && value != "" {
    83  		if constant.AnyValue == value || constant.AnyHostValue == value {
    84  			return true
    85  		}
    86  		return IsMatchGlobPattern(p.Wildcard, value)
    87  	}
    88  	if p.Exact != "" && value != "" {
    89  		return p.Exact == value
    90  	}
    91  	return false
    92  }
    93  
    94  type ListStringMatch struct {
    95  	Oneof []StringMatch `yaml:"oneof" json:"oneof,omitempty" property:"oneof"`
    96  }
    97  
    98  func (p *ListStringMatch) IsMatch(value string) bool {
    99  	for _, match := range p.Oneof {
   100  		if match.IsMatch(value) {
   101  			return true
   102  		}
   103  	}
   104  	return false
   105  }