|
|
|
@ -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) |
|
|
|
|