From fdc2c1e0c143e5bb6742d0f2facd8f9c7f7d2b8d Mon Sep 17 00:00:00 2001 From: jfirles Date: Wed, 29 Sep 2021 14:25:24 +0200 Subject: [PATCH] Added "push" method to insert IP at top of the chain --- go/iptables-api.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/go/iptables-api.go b/go/iptables-api.go index 9e575c3..37f137b 100644 --- a/go/iptables-api.go +++ b/go/iptables-api.go @@ -71,6 +71,7 @@ func main() { router := mux.NewRouter() router.HandleFunc("/addip/{ipaddress}", addIPAddress).Methods("GET") + router.HandleFunc("/puship/{ipaddress}", pushIPAddress).Methods("GET") router.HandleFunc("/blockip/{ipaddress}", addIPAddress).Methods("GET") router.HandleFunc("/flushchain", flushChain).Methods("GET") router.HandleFunc("/removeip/{ipaddress}", removeIPAddress).Methods("GET") @@ -196,6 +197,14 @@ func iptableHandle(proto string, task string, ipvar string) (string, error) { } else { return "added", nil } + case "push": + err = ipt.Insert("filter", "APIBANLOCAL", 1, "-s", ipvar, "-d", "0/0", "-j", targetChain) + if err != nil { + log.Println("iptableHandler: error pushing address", err) + return "", err + } else { + return "pushed", nil + } case "delete": err = ipt.DeleteIfExists("filter", "APIBANLOCAL", "-s", ipvar, "-d", "0/0", "-j", targetChain) if err != nil { @@ -218,6 +227,26 @@ func iptableHandle(proto string, task string, ipvar string) (string, error) { } } +func pushIPAddress(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + params := mux.Vars(r) + log.Println("processing pushIPAddress", params["ipaddress"]) + + ipType, err := checkIPAddressv4(params["ipaddress"]) + if err != nil { + log.Println(params["ipaddress"], "is not a valid ip address") + http.Error(w, "{\"error\":\"only valid ip addresses supported\"}", http.StatusBadRequest) + return + } + + status, err := iptableHandle(ipType, "push", params["ipaddress"]) + if err != nil { + http.Error(w, "{\"error\":\""+err.Error()+"\"}", http.StatusBadRequest) + } else { + io.WriteString(w, "{\"success\":\""+status+"\"}\n") + } +} + func addIPAddress(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") params := mux.Vars(r)