func (b *Blacklist) IsBlacklistedDomain(hostname string) (bool, error) {
  elements := strings.Split(hostname, ".")
  length := len(elements)
  for i := 1; i < length; i++ {
    addr := "*." + strings.Join(elements[i:], ".")
    hash, err := calculateHash(addr)
    if err != nil {
      return false, err
    }
    if b.Contains(hash) {
      return true, nil
    }
  }
  return false, nil
}
evaluates whether a given hostname has been blacklisted
package entity
import (
  "crypto/sha1"
  "encoding/hex"
  "encoding/json"
  "fmt"
  "regexp"
  "strings"
  "golang.org/x/text/encoding/charmap"
)
var ipPattern, _ = regexp.Compile("^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$")
type Blacklist struct {
  Hashes []string
}
func NewBlacklist(hashes []string) (*Blacklist, error) {
  for i := 0; i < len(hashes); {
    hash := hashes[i]
    if hash == "" {
      hashes = append(hashes[:i], hashes[i+1:]...)
      continue  
    }
    if len(hash) != 40 {
      return nil, fmt.Errorf("encountered malformed hash \"%s\": must be exactly 40 characters long", hash)
    }
    i++
  }
  return &Blacklist{
    Hashes: hashes,
  }, nil
}
func (b *Blacklist) Serialize() ([]byte, error) {
  return json.Marshal(b.Hashes)
}
func (b *Blacklist) Deserialize(enc []byte) error {
  hashes := make([]string, 0)
  err := json.Unmarshal(enc, &hashes)
  if err != nil {
    return err
  }
  b.Hashes = hashes
  return nil
}
func (b *Blacklist) Contains(hash string) bool {
  for _, blacklistedHash := range b.Hashes {
    if blacklistedHash == hash {
      return true
    }
  }
  return false
}
func (b *Blacklist) IsBlacklisted(addr string) (bool, error) {
  hash, err := calculateHash(addr)
  if err != nil {
    return false, err
  }
  if b.Contains(hash) {
    return true, nil
  }
  if isIpAddress(addr) {
    return b.IsBlacklistedIP(addr)
  }
  return b.IsBlacklistedDomain(addr)
}
func (b *Blacklist) IsBlacklistedIP(ip string) (bool, error) {
  elements := strings.Split(ip, ".")  
  for i := 3; i > 0; i-- {
    addr := strings.Join(elements[:i], ".") + ".*"
    hash, err := calculateHash(addr)
    if err != nil {
      return false, err
    }
    if b.Contains(hash) {
      return true, nil
    }
  }
  return false, nil
}