-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
67 lines (57 loc) · 1.66 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package main
import (
"dns-server/blocklist"
"dns-server/resolver"
"flag"
"github.com/miekg/dns"
"github.com/prometheus/client_golang/prometheus/promhttp"
"log"
"net/http"
"os"
"os/signal"
"strconv"
"syscall"
)
func main() {
port := 8888
flag.Parse()
config := &dns.ClientConfig{
Servers: []string{resolver.DnsGoogleServer, resolver.DnsCloudFlare},
}
handler := resolver.New(config)
for z := range resolver.Zones {
z := z
dns.HandleFunc(z, func(writer dns.ResponseWriter, msg *dns.Msg) {
err := handler.DNSResolver(writer, msg)
if err != nil {
log.Printf("Failed to resolve %s: %v", z, err)
}
})
}
go func() {
srv := &dns.Server{Addr: ":" + strconv.Itoa(port), Net: "udp"}
if err := srv.ListenAndServe(); err != nil {
log.Fatalf("Failed to set udp listener %s\n", err.Error())
}
}()
go func() {
srv := &dns.Server{Addr: ":" + strconv.Itoa(port), Net: "tcp"}
if err := srv.ListenAndServe(); err != nil {
log.Fatalf("Failed to set tcp listener %s\n", err.Error())
}
}()
defaultList := blocklist.LoadBlockList("https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts")
handler.AddBatch(defaultList)
http.Handle("/metrics", promhttp.Handler())
http.Handle("/remove", http.HandlerFunc(handler.Remove))
http.Handle("/add", http.HandlerFunc(handler.AddHttp))
http.Handle("/list", http.HandlerFunc(handler.Get))
http.Handle("/health", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}))
http.ListenAndServe(":2112", nil)
sig := make(chan os.Signal)
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
s := <-sig
log.Fatalf("Signal (%v) received, stopping\n", s)
}