From 05ec165128c4616e56b3deb9ee61058579d361c7 Mon Sep 17 00:00:00 2001 From: Sean DuBois Date: Thu, 10 Jul 2014 00:54:26 +0000 Subject: [PATCH] Implement endpoint for running WHOIS queries --- fail2rest.go | 3 +++ whois.go | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 whois.go diff --git a/fail2rest.go b/fail2rest.go index 85d6210..50faee7 100644 --- a/fail2rest.go +++ b/fail2rest.go @@ -41,6 +41,9 @@ func main() { globalHandler(r.PathPrefix("/global").Subrouter(), fail2goConn) jailHandler(r.PathPrefix("/jail").Subrouter(), fail2goConn) + r.HandleFunc("/whois/{object}", func(res http.ResponseWriter, req *http.Request) { + whoisHandler(res, req, fail2goConn) + }).Methods("GET") http.Handle("/", r) fmt.Println(http.ListenAndServe(configuration.Addr, nil)) diff --git a/whois.go b/whois.go new file mode 100644 index 0000000..f12dcbf --- /dev/null +++ b/whois.go @@ -0,0 +1,21 @@ +package main + +import ( + "encoding/json" + "github.com/Sean-Der/fail2go" + "github.com/Sean-Der/goWHOIS" + "github.com/gorilla/mux" + "net/http" +) + +func whoisHandler(res http.ResponseWriter, req *http.Request, fail2goConn *fail2go.Conn) { + goWHOISReq := goWHOIS.NewReq(mux.Vars(req)["object"]) + WHOIS, err := goWHOISReq.Raw() + if err != nil { + writeHTTPError(res, err) + return + } + + encodedOutput, _ := json.Marshal(map[string]string{"WHOIS": WHOIS}) + res.Write(encodedOutput) +}