Compare commits
4 Commits
v0.1
...
c0a109466f
| Author | SHA1 | Date | |
|---|---|---|---|
| c0a109466f | |||
| 7b412e404c | |||
| 87d9dca1ce | |||
| 4ddaa5a4c0 |
@@ -19,7 +19,7 @@ steps:
|
|||||||
- md5
|
- md5
|
||||||
- sha1
|
- sha1
|
||||||
- sha256
|
- sha256
|
||||||
title: v0.1
|
title: ${DRONE_TAG}
|
||||||
prerelease: true
|
prerelease: true
|
||||||
when:
|
when:
|
||||||
event:
|
event:
|
||||||
|
|||||||
63
blacklist.go
63
blacklist.go
@@ -1,11 +1,13 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/miekg/dns"
|
"github.com/miekg/dns"
|
||||||
)
|
)
|
||||||
@@ -26,12 +28,10 @@ func loadBlacklist(config []configBlacklist) map[string]bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
domains := parseRawBlacklist(element, *raw)
|
domains := parseRawBlacklist(element, *raw)
|
||||||
|
log.Printf("Added %d blocked domains", len(domains))
|
||||||
list = append(list, domains...)
|
list = append(list, domains...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// list = removeDuplicates(list)
|
|
||||||
// sort.Strings(list)
|
|
||||||
|
|
||||||
domainMap := make(map[string]bool)
|
domainMap := make(map[string]bool)
|
||||||
for _, e := range list {
|
for _, e := range list {
|
||||||
domainMap[e] = true
|
domainMap[e] = true
|
||||||
@@ -40,38 +40,49 @@ func loadBlacklist(config []configBlacklist) map[string]bool {
|
|||||||
return domainMap
|
return domainMap
|
||||||
}
|
}
|
||||||
|
|
||||||
func removeDuplicates(elements []string) []string {
|
|
||||||
encountered := map[string]bool{}
|
|
||||||
result := []string{}
|
|
||||||
|
|
||||||
for v := range elements {
|
|
||||||
if !encountered[elements[v]] {
|
|
||||||
encountered[elements[v]] = true
|
|
||||||
|
|
||||||
result = append(result, elements[v])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
func requestBacklist(blacklist configBlacklist) (*string, error) {
|
func requestBacklist(blacklist configBlacklist) (*string, error) {
|
||||||
|
if blacklist.URL != "" {
|
||||||
|
return getBlacklistFromURL(blacklist.URL)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, errors.New("No blacklist provided")
|
||||||
|
}
|
||||||
|
|
||||||
|
func getBlacklistFromURL(url string) (*string, error) {
|
||||||
// Request list
|
// Request list
|
||||||
resp, err := http.Get(blacklist.URL)
|
resp, err := http.Get(url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
if resp.StatusCode != 200 {
|
||||||
|
log.Printf("Got %d status code. Continueing anyway.", resp.StatusCode)
|
||||||
|
}
|
||||||
|
|
||||||
body, err := ioutil.ReadAll(resp.Body)
|
body, err := ioutil.ReadAll(resp.Body)
|
||||||
bodyString := string(body)
|
bodyString := string(body)
|
||||||
|
|
||||||
log.Printf("Downloaded blacklist %s", blacklist.URL)
|
log.Printf("Downloaded blacklist %s", url)
|
||||||
|
|
||||||
return &bodyString, err
|
return &bodyString, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseRawBlacklist(blacklist configBlacklist, raw string) []string {
|
func parseRawBlacklist(blacklist configBlacklist, raw string) []string {
|
||||||
|
switch blacklist.Format {
|
||||||
|
case "host":
|
||||||
|
return parseHostFormat(raw)
|
||||||
|
case "line":
|
||||||
|
return parseLineFormat(raw)
|
||||||
|
default:
|
||||||
|
log.Printf("Failed to parse blacklist. Format not supported: %s", blacklist.Format)
|
||||||
|
log.Println("Supported types are: host, line")
|
||||||
|
return make([]string, 0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseHostFormat(raw string) []string {
|
||||||
finalList := make([]string, 0)
|
finalList := make([]string, 0)
|
||||||
reg := regexp.MustCompile(`(?mi)^\s*(#*)\s*(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s+([a-zA-Z0-9\.\- ]+)$`)
|
reg := regexp.MustCompile(`(?mi)^\s*(#*)\s*(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s+([a-zA-Z0-9\.\- ]+)$`)
|
||||||
matches := reg.FindAllStringSubmatch(raw, -1)
|
matches := reg.FindAllStringSubmatch(raw, -1)
|
||||||
@@ -84,6 +95,18 @@ func parseRawBlacklist(blacklist configBlacklist, raw string) []string {
|
|||||||
return finalList
|
return finalList
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func parseLineFormat(raw string) []string {
|
||||||
|
list := make([]string, 0)
|
||||||
|
|
||||||
|
for _, line := range strings.Split(raw, "\n") {
|
||||||
|
if !strings.HasPrefix(line, "#") {
|
||||||
|
list = append(list, line)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return list
|
||||||
|
}
|
||||||
|
|
||||||
func handleBlockedDomain(w dns.ResponseWriter, r *dns.Msg) {
|
func handleBlockedDomain(w dns.ResponseWriter, r *dns.Msg) {
|
||||||
q := r.Question[0]
|
q := r.Question[0]
|
||||||
|
|
||||||
|
|||||||
@@ -25,3 +25,5 @@ address: 0.0.0.0:8053
|
|||||||
blacklist:
|
blacklist:
|
||||||
- url: https://raw.githubusercontent.com/anudeepND/blacklist/master/adservers.txt
|
- url: https://raw.githubusercontent.com/anudeepND/blacklist/master/adservers.txt
|
||||||
format: host
|
format: host
|
||||||
|
- url: https://blocklistproject.github.io/Lists/alt-version/ads-nl.txt
|
||||||
|
format: line
|
||||||
|
|||||||
Reference in New Issue
Block a user