Browse Source

Added "push" method to insert IP at top of the chain

pull/1/head
jfirles 4 years ago
parent
commit
fdc2c1e0c1
1 changed files with 29 additions and 0 deletions
  1. +29
    -0
      go/iptables-api.go

+ 29
- 0
go/iptables-api.go View File

@ -71,6 +71,7 @@ func main() {
router := mux.NewRouter() router := mux.NewRouter()
router.HandleFunc("/addip/{ipaddress}", addIPAddress).Methods("GET") router.HandleFunc("/addip/{ipaddress}", addIPAddress).Methods("GET")
router.HandleFunc("/puship/{ipaddress}", pushIPAddress).Methods("GET")
router.HandleFunc("/blockip/{ipaddress}", addIPAddress).Methods("GET") router.HandleFunc("/blockip/{ipaddress}", addIPAddress).Methods("GET")
router.HandleFunc("/flushchain", flushChain).Methods("GET") router.HandleFunc("/flushchain", flushChain).Methods("GET")
router.HandleFunc("/removeip/{ipaddress}", removeIPAddress).Methods("GET") router.HandleFunc("/removeip/{ipaddress}", removeIPAddress).Methods("GET")
@ -196,6 +197,14 @@ func iptableHandle(proto string, task string, ipvar string) (string, error) {
} else { } else {
return "added", nil 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": case "delete":
err = ipt.DeleteIfExists("filter", "APIBANLOCAL", "-s", ipvar, "-d", "0/0", "-j", targetChain) err = ipt.DeleteIfExists("filter", "APIBANLOCAL", "-s", ipvar, "-d", "0/0", "-j", targetChain)
if err != nil { 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) { func addIPAddress(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
params := mux.Vars(r) params := mux.Vars(r)


Loading…
Cancel
Save