summaryrefslogtreecommitdiff
path: root/api
diff options
context:
space:
mode:
authorsinanmohd <sinan@sinanmohd.com>2024-07-07 15:20:11 +0530
committersinanmohd <sinan@sinanmohd.com>2024-07-07 15:52:59 +0530
commite0f32fb48caf9397ac87fdd6c6597d888f9a8a40 (patch)
tree0a9c5ccabf13192264d7967533a4d8a0ddb16529 /api
parent60aa1b7adbd133a5b1679ab2feac4ec17e3b48b5 (diff)
api/dns: init
Diffstat (limited to 'api')
-rw-r--r--api/dns.go64
-rw-r--r--api/main.go15
2 files changed, 73 insertions, 6 deletions
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)
+ }
+}
diff --git a/api/main.go b/api/main.go
index 32f7d08..eb8715f 100644
--- a/api/main.go
+++ b/api/main.go
@@ -7,6 +7,7 @@ import (
"net"
"sinanmohd.com/redq/db"
+ "sinanmohd.com/redq/dns"
"sinanmohd.com/redq/usage"
)
@@ -16,9 +17,9 @@ const (
)
type ApiReq struct {
- Type string `json:"type"`
- Action string `json:"action"`
- Arg string `json:"arg"`
+ Type string `json:"type"`
+ Action string `json:"action"`
+ Arg []string `json:"arg"`
}
type Api struct {
@@ -42,7 +43,7 @@ func New() (*Api, error) {
return &a, nil
}
-func (a *Api) Run(u *usage.Usage, queries *db.Queries, ctxDb context.Context) {
+func (a *Api) Run(u *usage.Usage, d *dns.Dns, queries *db.Queries, ctxDb context.Context) {
for {
conn, err := a.sock.Accept()
if err != nil {
@@ -50,11 +51,11 @@ func (a *Api) Run(u *usage.Usage, queries *db.Queries, ctxDb context.Context) {
continue
}
- go handleConn(conn, u, queries, ctxDb)
+ go handleConn(conn, u, d, queries, ctxDb)
}
}
-func handleConn(conn net.Conn, u *usage.Usage, queries *db.Queries, ctxDb context.Context) {
+func handleConn(conn net.Conn, u *usage.Usage, d *dns.Dns, queries *db.Queries, ctxDb context.Context) {
defer conn.Close()
var req ApiReq
buf := make([]byte, bufSize)
@@ -76,6 +77,8 @@ func handleConn(conn net.Conn, u *usage.Usage, queries *db.Queries, ctxDb contex
handleBandwidth(conn, u)
case "usage":
handleUsage(conn, u, queries, ctxDb)
+ case "dns":
+ handleDns(conn, d, req.Arg, req.Action)
default:
log.Printf("invalid request type: %s", req.Type)
}