summaryrefslogtreecommitdiff
path: root/api/filter.go
diff options
context:
space:
mode:
Diffstat (limited to 'api/filter.go')
-rw-r--r--api/filter.go91
1 files changed, 91 insertions, 0 deletions
diff --git a/api/filter.go b/api/filter.go
new file mode 100644
index 0000000..3b6664d
--- /dev/null
+++ b/api/filter.go
@@ -0,0 +1,91 @@
+package api
+
+import (
+ "encoding/json"
+ "log"
+ "net"
+
+ "github.com/cilium/cilium/pkg/mac"
+ "sinanmohd.com/redq/bpf/filter"
+)
+
+type FilterResp map[string]string
+
+func handleFilterBlock(conn net.Conn, f *filter.Filter, macs []string) {
+ resp := make(FilterResp)
+
+ for _, mac_string := range macs {
+ mac, err := mac.ParseMAC(mac_string)
+ if err != nil {
+ resp[mac_string] = err.Error()
+ continue
+ }
+
+ mac_cilium64, err := mac.Uint64()
+ if err != nil {
+ resp[mac_string] = err.Error()
+ continue
+ }
+
+ err = f.Block(uint64(mac_cilium64))
+ if err != nil {
+ resp[mac_string] = err.Error()
+ continue
+ }
+
+ resp[mac_string] = "blocked"
+ }
+
+ buf, err := json.Marshal(resp)
+ if err != nil {
+ log.Printf("marshaling json: %s", err)
+ return
+ }
+
+ conn.Write(buf)
+}
+
+func handleFilterUnblock(conn net.Conn, f *filter.Filter, macs []string) {
+ resp := make(FilterResp)
+
+ for _, mac_string := range macs {
+ mac, err := mac.ParseMAC(mac_string)
+ if err != nil {
+ resp[mac_string] = err.Error()
+ continue
+ }
+
+ mac_cilium64, err := mac.Uint64()
+ if err != nil {
+ resp[mac_string] = err.Error()
+ continue
+ }
+
+ err = f.Unblock(uint64(mac_cilium64))
+ if err != nil {
+ resp[mac_string] = err.Error()
+ continue
+ }
+
+ resp[mac_string] = "unblocked"
+ }
+
+ buf, err := json.Marshal(resp)
+ if err != nil {
+ log.Printf("marshaling json: %s", err)
+ return
+ }
+
+ conn.Write(buf)
+}
+
+func handleFilter(conn net.Conn, f *filter.Filter, macs []string, action string) {
+ switch action {
+ case "block":
+ handleFilterBlock(conn, f, macs)
+ case "unblock":
+ handleFilterUnblock(conn, f, macs)
+ default:
+ log.Printf("handling dns: invalid action '%s'", action)
+ }
+}