From e0f32fb48caf9397ac87fdd6c6597d888f9a8a40 Mon Sep 17 00:00:00 2001 From: sinanmohd Date: Sun, 7 Jul 2024 15:20:11 +0530 Subject: api/dns: init --- api/dns.go | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 api/dns.go (limited to 'api/dns.go') diff --git a/api/dns.go b/api/dns.go new file mode 100644 index 0000000..f8d345c --- /dev/null +++ b/api/dns.go @@ -0,0 +1,64 @@ +package api + +import ( + "encoding/json" + "log" + "net" + + "sinanmohd.com/redq/dns" +) + +type DnsResp map[string]string + +func handleDnsBlock(conn net.Conn, d *dns.Dns, domains []string) { + resp := make(DnsResp) + + for _, domain := range domains { + err := d.Block(domain) + if err != nil { + resp[domain] = err.Error() + } else { + resp[domain] = "blocked" + } + } + + buf, err := json.Marshal(resp) + if err != nil { + log.Printf("marshaling json: %s", err) + return + } + + conn.Write(buf) +} + +func handleDnsUnblock(conn net.Conn, d *dns.Dns, domains []string) { + resp := make(DnsResp) + + for _, domain := range domains { + err := d.Unblock(domain) + if err != nil { + resp[domain] = err.Error() + } else { + resp[domain] = "unblocked" + } + } + + buf, err := json.Marshal(resp) + if err != nil { + log.Printf("marshaling json: %s", err) + return + } + + conn.Write(buf) +} + +func handleDns(conn net.Conn, d *dns.Dns, domains []string, action string) { + switch action { + case "block": + handleDnsBlock(conn, d, domains) + case "unblock": + handleDnsUnblock(conn, d, domains) + default: + log.Printf("handling dns: invalid action '%s'", action) + } +} -- cgit v1.2.3