github.com/polarismesh/polaris@v1.17.8/plugin/whitelist/ip_whitelist.go (about)

     1  /**
     2   * Tencent is pleased to support the open source community by making Polaris available.
     3   *
     4   * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
     5   *
     6   * Licensed under the BSD 3-Clause License (the "License");
     7   * you may not use this file except in compliance with the License.
     8   * You may obtain a copy of the License at
     9   *
    10   * https://opensource.org/licenses/BSD-3-Clause
    11   *
    12   * Unless required by applicable law or agreed to in writing, software distributed
    13   * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
    14   * CONDITIONS OF ANY KIND, either express or implied. See the License for the
    15   * specific language governing permissions and limitations under the License.
    16   */
    17  
    18  package whitelist
    19  
    20  import (
    21  	"errors"
    22  
    23  	"github.com/polarismesh/polaris/plugin"
    24  )
    25  
    26  const PluginName = "whitelist"
    27  
    28  func init() {
    29  	plugin.RegisterPlugin(PluginName, &ipWhitelist{})
    30  }
    31  
    32  type ipWhitelist struct {
    33  	ips map[string]bool
    34  }
    35  
    36  // Name 插件名称
    37  func (i *ipWhitelist) Name() string {
    38  	return PluginName
    39  }
    40  
    41  // Initialize 初始化IP白名单插件
    42  func (i *ipWhitelist) Initialize(conf *plugin.ConfigEntry) error {
    43  	i.ips = make(map[string]bool)
    44  	ips, ok := conf.Option["ip"].([]interface{})
    45  	if !ok {
    46  		return errors.New("whitelist plugin initialize error")
    47  	}
    48  	for _, ip := range ips {
    49  		i.ips[ip.(string)] = true
    50  	}
    51  	return nil
    52  }
    53  
    54  // Destroy 销毁插件
    55  func (i *ipWhitelist) Destroy() error {
    56  	return nil
    57  }
    58  
    59  // Contain 白名单是否包含IP
    60  func (i *ipWhitelist) Contain(entry interface{}) bool {
    61  	ip, _ := entry.(string)
    62  	return i.ips[ip]
    63  }