You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
56 lines
1007 B
56 lines
1007 B
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"net"
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
type Blocklist struct {
|
|
Source string
|
|
Domains map[string]string
|
|
}
|
|
|
|
func FetchBlockList(source string) (Blocklist, error) {
|
|
blocklist := Blocklist{
|
|
Source: source,
|
|
Domains: map[string]string{},
|
|
}
|
|
|
|
response, err := http.Get(source)
|
|
if err != nil {
|
|
return blocklist, err
|
|
}
|
|
defer response.Body.Close()
|
|
|
|
if response.StatusCode != http.StatusOK {
|
|
return blocklist, fmt.Errorf("non 200 status: %d", response.StatusCode)
|
|
}
|
|
|
|
scanner := bufio.NewScanner(response.Body)
|
|
for scanner.Scan() {
|
|
line := scanner.Text()
|
|
if line == "" || line[0] == '#' {
|
|
continue
|
|
}
|
|
|
|
domain := line
|
|
if frags := strings.Split(line, " "); len(frags) > 1 {
|
|
if net.ParseIP(frags[1]) != nil {
|
|
continue
|
|
}
|
|
domain = frags[1]
|
|
}
|
|
|
|
if strings.Contains(domain, "localhost") || strings.Contains(domain, "loopback") || domain == "broadcasthost" {
|
|
continue
|
|
}
|
|
|
|
blocklist.Domains[domain] = "enabled"
|
|
}
|
|
|
|
return blocklist, nil
|
|
}
|