dubbo.apache.org/dubbo-go/v3@v3.1.1/common/host_util.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  	"os"
    22  	"strconv"
    23  	"strings"
    24  )
    25  
    26  import (
    27  	"github.com/dubbogo/gost/log/logger"
    28  	gxnet "github.com/dubbogo/gost/net"
    29  )
    30  
    31  import (
    32  	"dubbo.apache.org/dubbo-go/v3/common/constant"
    33  )
    34  
    35  var (
    36  	localIp       string
    37  	localHostname string
    38  )
    39  
    40  func GetLocalIp() string {
    41  	if len(localIp) != 0 {
    42  		return localIp
    43  	}
    44  	localIp, _ = gxnet.GetLocalIP()
    45  	return localIp
    46  }
    47  
    48  func GetLocalHostName() string {
    49  	if len(localHostname) != 0 {
    50  		return localHostname
    51  	}
    52  	hostname, err := os.Hostname()
    53  	if err != nil {
    54  		logger.Errorf("can not get local hostname")
    55  	}
    56  	localHostname = hostname
    57  	return localHostname
    58  }
    59  
    60  func HandleRegisterIPAndPort(url *URL) {
    61  	// if developer define registry port and ip, use it first.
    62  	if ipToRegistry := os.Getenv(constant.DubboIpToRegistryKey); len(ipToRegistry) > 0 {
    63  		url.Ip = ipToRegistry
    64  	}
    65  	if len(url.Ip) == 0 {
    66  		url.Ip = GetLocalIp()
    67  	}
    68  	if portToRegistry := os.Getenv(constant.DubboPortToRegistryKey); isValidPort(portToRegistry) {
    69  		url.Port = portToRegistry
    70  	}
    71  	if len(url.Port) == 0 || url.Port == "0" {
    72  		url.Port = constant.DubboDefaultPortToRegistry
    73  	}
    74  }
    75  
    76  func isValidPort(port string) bool {
    77  	if len(port) == 0 {
    78  		return false
    79  	}
    80  
    81  	portInt, err := strconv.Atoi(port)
    82  	return err == nil && portInt > 0 && portInt < 65536
    83  }
    84  
    85  func IsMatchGlobPattern(pattern, value string) bool {
    86  	if constant.AnyValue == pattern {
    87  		return true
    88  	}
    89  	if pattern == "" && value == "" {
    90  		return true
    91  	}
    92  	if pattern == "" || value == "" {
    93  		return false
    94  	}
    95  
    96  	i := strings.Index(pattern, constant.AnyValue)
    97  	if i == -1 { // doesn't find "*"
    98  		return value == pattern
    99  	} else if i == len(pattern)-1 { // "*" is at the end
   100  		return strings.HasPrefix(value, pattern[0:i])
   101  	} else if i == 0 { // "*" is at the beginning
   102  		return strings.HasSuffix(value, pattern[i+1:])
   103  	} else { // "*" is in the middle
   104  		prefix := pattern[0:i]
   105  		suffix := pattern[i+1:]
   106  		return strings.HasPrefix(value, prefix) && strings.HasSuffix(value, suffix)
   107  	}
   108  }