Explorar o código

Retrieve list sources from config file

Signed-off-by: Antoine Tenart <antoine.tenart@ack.tf>
Antoine Tenart %!s(int64=5) %!d(string=hai) anos
pai
achega
1aa7397b4b
Modificáronse 1 ficheiros con 37 adicións e 13 borrados
  1. 37 13
      main.go

+ 37 - 13
main.go

@@ -1,23 +1,20 @@
 package main
 
 import (
+	"bufio"
 	"errors"
+	"flag"
 	"fmt"
 	"io/ioutil"
+	"log"
 	"net"
 	"net/http"
+	"os"
 	"strings"
 )
 
 var (
-	lists = []string{
-		"https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts",
-		"https://mirror1.malwaredomains.com/files/justdomains",
-		"http://sysctl.org/cameleon/hosts",
-		"https://s3.amazonaws.com/lists.disconnect.me/simple_tracking.txt",
-		"https://s3.amazonaws.com/lists.disconnect.me/simple_ad.txt",
-		"https://hosts-file.net/ad_servers.txt",
-	}
+	lists = flag.String("lists", "/etc/blacklists", "Blacklist sources")
 	invalid = []string{
 		"localhost", "localhost.localdomain", "local", "broadcasthost",
 		"ip6-localhost", "ip6-loopback", "ip6-localnet",
@@ -36,19 +33,46 @@ var (
 `
 )
 
+type Server struct {
+	Lists []string
+}
+
 func main() {
-	http.HandleFunc("/", generateList)
-	http.ListenAndServe(":8000", nil)
+	flag.Parse()
+
+	s := &Server{ readLists(), }
+	s.Serve()
+}
+
+func readLists() []string {
+	file, err := os.Open(*lists)
+	if err != nil {
+		return nil
+	}
+	defer file.Close()
+
+	var sources []string
+	scanner := bufio.NewScanner(file)
+	for scanner.Scan() {
+		sources = append(sources, scanner.Text())
+	}
+
+	return sources
+}
+
+func (s *Server) Serve() {
+	http.HandleFunc("/", s.generateList)
+	log.Fatal(http.ListenAndServe(":8000", nil))
 }
 
-func generateList(w http.ResponseWriter, r *http.Request) {
+func (s *Server) generateList(w http.ResponseWriter, r *http.Request) {
 	c := make(chan string)
-	for _, list := range lists {
+	for _, list := range s.Lists {
 		go handle(list, c)
 	}
 
 	var zones []string
-	for range lists {
+	for range s.Lists {
 		zones = append(zones, <-c)
 	}