Browse Source

Merge pull request #1 from jfirles/main

Added "push" method to insert IP at top of the chain
test-string
Fred Posner 4 years ago
committed by GitHub
parent
commit
46aa086db2
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 42 additions and 0 deletions
  1. +42
    -0
      go/iptables-api.go

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

@ -73,6 +73,7 @@ func main() {
router.HandleFunc("/addip/{ipaddress}", addIPAddress).Methods("GET")
router.HandleFunc("/blockip/{ipaddress}", addIPAddress).Methods("GET")
router.HandleFunc("/flushchain", flushChain).Methods("GET")
router.HandleFunc("/puship/{ipaddress}", pushIPAddress).Methods("GET")
router.HandleFunc("/removeip/{ipaddress}", removeIPAddress).Methods("GET")
router.HandleFunc("/unblockip/{ipaddress}", removeIPAddress).Methods("GET")
router.HandleFunc("/", rAddIPAddress).Methods("POST")
@ -212,12 +213,53 @@ func iptableHandle(proto string, task string, ipvar string) (string, error) {
} else {
return "flushed", nil
}
case "push":
var exists = false
exists, err = ipt.Exists("filter", "APIBANLOCAL", "-s", ipvar, "-d", "0/0", "-j", targetChain)
if err != nil {
log.Println("iptableHandler: error checking if ip already exists", err)
return "error checking if ip already exists in the chain", err
} else {
if exists {
err = errors.New("ip already exists")
log.Println("iptableHandler: ip already exists", err)
return "ip already exists", err
} else {
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
}
}
}
default:
log.Println("iptableHandler: unknown task")
return "", errors.New("unknown task")
}
}
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)


Loading…
Cancel
Save