Browse Source

Adding examples for usage and Squid-Cache external_acl helper

Eliezer Croitoru 2 years ago
parent
commit
d27a31786c

+ 56 - 0
examples-multiple-peers/cli-rbl-test-tool.go

@@ -0,0 +1,56 @@
+package main
+
+import (
+	"github.com/elico/drbl-peer"
+	"flag"
+	"fmt"
+	"github.com/chzyer/readline"
+	"io"
+)
+
+var blockWeight int
+var timeout int
+var peersFileName string
+var debug bool
+
+func main() {
+	flag.IntVar(&blockWeight, "block-weight", 128, "Peers blacklist weight")
+	flag.IntVar(&timeout, "query-timeout", 30, "Timeout for all peers response")
+	flag.BoolVar(&debug, "debug", false, "Run in debug mode")
+	flag.StringVar(&peersFileName, "peers-filename", "peersfile.txt", "Blacklists peers filename")
+
+	flag.Parse()
+
+	drblPeers, _ := drblpeer.NewPeerListFromFile(peersFileName, int64(blockWeight), timeout, debug)
+	if debug {
+		fmt.Println("Peers", drblPeers)
+	}
+
+	l, err := readline.NewEx(&readline.Config{
+		//		HistoryFile:     "/tmp/readline.tmp",
+		InterruptPrompt: "^C",
+		EOFPrompt:       "exit",
+	})
+	if err != nil {
+		panic(err)
+	}
+	defer l.Close()
+
+	for {
+		l.SetPrompt("Enter domain name: ")
+		line, err := l.Readline()
+		if err == readline.ErrInterrupt {
+			if len(line) == 0 {
+				break
+			} else {
+				continue
+			}
+		} else if err == io.EOF {
+			break
+		}
+
+		res, weight := drblPeers.Check(line)
+		fmt.Println("Result =>", res, weight)
+	}
+
+}

+ 64 - 0
examples-multiple-peers/squid-external-acl-helper.go

@@ -0,0 +1,64 @@
+package main
+
+import (
+	"github.com/elico/drbl-peer"
+	"bufio"
+	"flag"
+	"fmt"
+	"os"
+	"strconv"
+	"strings"
+)
+
+var blockWeight int
+var timeout int
+var peersFileName string
+var debug bool
+
+var drblPeers *drblpeer.DrblPeers
+
+func process_request(line string) {
+	answer := "ERR"
+	lparts := strings.Split(strings.TrimRight(line, "\n"), " ")
+	if len(lparts[0]) > 0 {
+		if debug {
+			fmt.Fprintln(os.Stderr, "ERRlog: Proccessing request => \""+strings.TrimRight(line, "\n")+"\"")
+		}
+	}
+	block, weight := drblPeers.Check(lparts[1])
+	if block {
+		answer = "OK"
+	}
+	fmt.Println(lparts[0] + " " + answer + " weight=" + strconv.FormatInt(weight, 10))
+}
+
+func main() {
+	flag.IntVar(&blockWeight, "block-weight", 128, "Peers blacklist weight")
+	flag.IntVar(&timeout, "query-timeout", 30, "Timeout for all peers response")
+	flag.BoolVar(&debug, "debug", false, "Run in debug mode")
+	flag.StringVar(&peersFileName, "peers-filename", "peersfile.txt", "Blacklists peers filename")
+
+	flag.Parse()
+
+	drblPeers, _ = drblpeer.NewPeerListFromFile(peersFileName, int64(blockWeight), timeout, debug)
+	if debug {
+		fmt.Println("Peers", drblPeers)
+	}
+
+	reader := bufio.NewReader(os.Stdin)
+
+	for {
+		line, err := reader.ReadString('\n')
+
+		if err != nil {
+			// You may check here if err == io.EOF
+			break
+		}
+		if strings.HasPrefix(line, "q") || strings.HasPrefix(line, "Q") {
+			fmt.Fprintln(os.Stderr, "ERRlog: Exiting cleanly")
+			break
+		}
+
+		go process_request(line)
+	}
+}

+ 43 - 0
examples-multiple-peers/static-in-code-peers-with-readline.go

@@ -0,0 +1,43 @@
+package main
+
+import (
+	"github.com/elico/drbl-peer"
+	"fmt"
+	"github.com/chzyer/readline"
+	"io"
+)
+
+func main() {
+	peerOne := drblpeer.New("199.85.126.20", "dns", "/", 53, int64(128), []string{"156.154.175.216", "156.154.176.216"})
+	peerTwo := drblpeer.New("199.85.127.20", "dns", "/", 53, int64(128), []string{"156.154.175.216", "156.154.176.216"})
+
+	drblPeers := drblpeer.DrblPeers{[]drblpeer.DrblClient{*peerOne, *peerTwo}, int64(128), 30, false}
+
+	l, err := readline.NewEx(&readline.Config{
+		//		HistoryFile:     "/tmp/readline.tmp",
+		InterruptPrompt: "^C",
+		EOFPrompt:       "exit",
+	})
+	if err != nil {
+		panic(err)
+	}
+	defer l.Close()
+
+	for {
+		l.SetPrompt("Enter domain name: ")
+		line, err := l.Readline()
+		if err == readline.ErrInterrupt {
+			if len(line) == 0 {
+				break
+			} else {
+				continue
+			}
+		} else if err == io.EOF {
+			break
+		}
+
+		res, weight := drblPeers.Check(line)
+		fmt.Println("Result =>", res, weight)
+	}
+
+}

+ 26 - 0
examples-multiple-peers/static-in-code-peers.go

@@ -0,0 +1,26 @@
+package main
+
+import (
+	"github.com/elico/drbl-peer"
+	"bufio"
+	"fmt"
+	"os"
+)
+
+func main() {
+	peerOne := drblpeer.New("199.85.126.20", "dns", "/", 53, int64(128), []string{"156.154.175.216", "156.154.176.216"})
+	peerTwo := drblpeer.New("199.85.127.20", "dns", "/", 53, int64(128), []string{"156.154.175.216", "156.154.176.216"})
+
+	drblPeers := drblpeer.DrblPeers{[]drblpeer.DrblClient{*peerOne, *peerTwo}, int64(128), 30, false}
+
+	reader := bufio.NewReader(os.Stdin)
+	for {
+		fmt.Print("Enter domain name: ")
+		text, _ := reader.ReadString('\n')
+		// The len - 1 is since there is a "new line" character on every new line
+		res, weight := drblPeers.Check(text[:len(text)-1])
+
+		fmt.Println("Result =>", res, weight)
+	}
+
+}

+ 23 - 0
examples-single-peer/single-peer.go

@@ -0,0 +1,23 @@
+package main
+
+import (
+	"github.com/elico/drbl-peer"
+	"bufio"
+	"fmt"
+	"os"
+)
+
+func main() {
+	//        blockresults := []string{"156.154.175.216", "156.154.176.216"}
+	//        resolver := dns_resolver.New([]string{"199.85.126.20", "199.85.127.20"})
+
+	peer := drblpeer.New("199.85.126.20", "dns", "/", 53, int64(128), []string{"156.154.175.216", "156.154.176.216"})
+	reader := bufio.NewReader(os.Stdin)
+	for {
+		fmt.Print("Enter domain name: ")
+		text, _ := reader.ReadString('\n')
+		res, _, _, _, _ := peer.Check(text[:len(text)-1])
+		fmt.Println("Result =>", res, peer.Weight)
+	}
+
+}