82 lines
1.6 KiB
Go
82 lines
1.6 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"flag"
|
||
|
"fmt"
|
||
|
"git.e3t.cc/e3team/pancheri"
|
||
|
"gopkg.in/yaml.v2"
|
||
|
"io"
|
||
|
"os"
|
||
|
"strings"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
func main() {
|
||
|
hostsPath := flag.String("hostsfile", "", "Hosts file to convert to a dnsbl")
|
||
|
voidIp := flag.String("voidip", "0.0.0.0", "IP that this hosts file uses as a void IP")
|
||
|
printUsage := flag.Bool("help", false, "Print command line usage")
|
||
|
|
||
|
flag.Parse()
|
||
|
|
||
|
if *printUsage {
|
||
|
flag.Usage()
|
||
|
os.Exit(0)
|
||
|
}
|
||
|
|
||
|
f, err := os.Open(*hostsPath)
|
||
|
if err != nil {
|
||
|
fmt.Printf("error opening hosts file: %s", err)
|
||
|
os.Exit(1)
|
||
|
}
|
||
|
|
||
|
buf := new(strings.Builder)
|
||
|
_, err = io.Copy(buf, f)
|
||
|
if err != nil {
|
||
|
fmt.Printf("error reading hosts file: %s", err)
|
||
|
os.Exit(1)
|
||
|
}
|
||
|
|
||
|
err = f.Close()
|
||
|
if err != nil {
|
||
|
fmt.Printf("error closing hosts file: %s", err)
|
||
|
os.Exit(1)
|
||
|
}
|
||
|
|
||
|
split := strings.Split(buf.String(), "\n")
|
||
|
|
||
|
blackhole := pancheri.BlackholeFile{
|
||
|
DenyDomains: *new([]string),
|
||
|
}
|
||
|
|
||
|
for _, line := range split {
|
||
|
if strings.HasPrefix(line, "#") {
|
||
|
continue
|
||
|
}
|
||
|
if line == "" {
|
||
|
continue
|
||
|
}
|
||
|
if !strings.HasPrefix(line, *voidIp) {
|
||
|
continue
|
||
|
}
|
||
|
|
||
|
lineSplit := strings.Split(line, " ")
|
||
|
|
||
|
if strings.Join(lineSplit[1:], " ") == *voidIp {
|
||
|
continue
|
||
|
}
|
||
|
|
||
|
blackhole.DenyDomains = append(blackhole.DenyDomains, strings.Join(lineSplit[1:], " "))
|
||
|
}
|
||
|
|
||
|
marshaled, err := yaml.Marshal(blackhole)
|
||
|
|
||
|
if err != nil {
|
||
|
fmt.Printf("error saving blacklist file: %s", err)
|
||
|
os.Exit(1)
|
||
|
}
|
||
|
|
||
|
fmt.Printf("# Compiled by pancheri-compile at %s\n", time.Now().Format(time.RFC3339))
|
||
|
fmt.Printf("# %d hosts loaded from %s\n", len(blackhole.DenyDomains), *hostsPath)
|
||
|
fmt.Printf("%s", marshaled)
|
||
|
}
|