iplistget/sources/ipdeny.go

56 lines
941 B
Go

package sources
import (
"bufio"
"errors"
"iplistget/common"
"net"
"strings"
)
func init() {
common.RegisterSource(IPDeny{})
}
type IPDeny struct {
URL string
}
func (s IPDeny) Get(url string) ([]net.IPNet, error) {
if url != "" {
s.URL = url
} else {
return nil, errors.New("You have to provide a full URL to zone file for this source module, as it don't have a default one!")
}
reader, err := common.URLReader(s.URL)
if err != nil {
return nil, err
}
defer reader.Close()
ipmap := map[string]bool{}
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
ip := scanner.Text()
if !strings.Contains(ip, "/") {
ip += "/32"
}
ipmap[ip] = true
}
if err := scanner.Err(); err != nil {
return nil, err
}
iplist := make([]net.IPNet, 0, len(ipmap))
for key, _ := range ipmap {
if _, ipnet, err := net.ParseCIDR(key); err == nil {
iplist = append(iplist, *ipnet)
}
}
return iplist, nil
}