From abba1b1f79d00ad13eb40942ab76bb3809052268 Mon Sep 17 00:00:00 2001 From: 0xDiBa Date: Mon, 28 Sep 2015 01:44:48 +0300 Subject: [PATCH] API new implementation --- src/web-api/handlers.go | 128 ++++++++++++++++++++++++++++++++++++++++ src/web-api/router.go | 25 ++++++++ src/web-api/routes.go | 33 +++++++++++ src/web-api/web-api.go | 76 +----------------------- 4 files changed, 189 insertions(+), 73 deletions(-) create mode 100644 src/web-api/handlers.go create mode 100644 src/web-api/router.go create mode 100644 src/web-api/routes.go diff --git a/src/web-api/handlers.go b/src/web-api/handlers.go new file mode 100644 index 000000000..1f9aefea4 --- /dev/null +++ b/src/web-api/handlers.go @@ -0,0 +1,128 @@ +package main + +import ( + "github.com/streadway/amqp" + "log" + "net/http" +) + +func ScanHandler(w http.ResponseWriter, r *http.Request) { + + var ( + status int + err error + ) + + defer func() { + if nil != err { + http.Error(w, err.Error(), status) + } + }() + + domain := r.FormValue("target") + + if validateDomain(domain) { + + conn, err := amqp.Dial("amqp://guest:guest@localhost:5672/") + if err != nil { + log.Fatal(err) + } + defer conn.Close() + + ch, err := conn.Channel() + if err != nil { + log.Fatal(err) + } + defer ch.Close() + + status = http.StatusOK + + log.Println("Publishing", domain) + err = ch.Publish( + "amq.direct", // exchange + "scan_ready", // routing key + false, // mandatory + false, + amqp.Publishing{ + DeliveryMode: amqp.Persistent, + ContentType: "text/plain", + Body: []byte(domain), + }) + + } else { + status = http.StatusBadRequest + return + } + +} + +func ResultHandler(w http.ResponseWriter, r *http.Request) { + + var ( + status int + err error + ) + + defer func() { + if nil != err { + http.Error(w, err.Error(), status) + } + }() + + domain := r.FormValue("target") + + if validateDomain(domain) { + + status = http.StatusOK + + } else { + status = http.StatusBadRequest + return + } + +} + +func CertificateHandler(w http.ResponseWriter, r *http.Request) { + + var ( + status int + err error + ) + + defer func() { + if nil != err { + http.Error(w, err.Error(), status) + } + }() + + domain := r.FormValue("target") + + if validateDomain(domain) { + + raw := r.FormValue("raw") + + rawCert := false + + if raw == "true" { + rawCert = true + } + + log.Println("rawCert:", rawCert) + + status = http.StatusOK + + } else { + status = http.StatusBadRequest + return + } + +} + +func validateDomain(domain string) bool { + + // TODO + // Need to validate the domain, in a way, + // before passing it to the retriever queue + + return true +} diff --git a/src/web-api/router.go b/src/web-api/router.go new file mode 100644 index 000000000..e9c8c99e7 --- /dev/null +++ b/src/web-api/router.go @@ -0,0 +1,25 @@ +package main + +import ( + "github.com/gorilla/mux" + "net/http" +) + +func NewRouter() *mux.Router { + + router := mux.NewRouter().StrictSlash(true) + for _, route := range routes { + var handler http.Handler + + handler = route.HandlerFunc + + router. + Methods(route.Method). + Path(route.Pattern). + Name(route.Name). + Handler(handler) + + } + + return router +} diff --git a/src/web-api/routes.go b/src/web-api/routes.go new file mode 100644 index 000000000..2deeeceef --- /dev/null +++ b/src/web-api/routes.go @@ -0,0 +1,33 @@ +package main + +import "net/http" + +type Route struct { + Name string + Method string + Pattern string + HandlerFunc http.HandlerFunc +} + +type Routes []Route + +var routes = Routes{ + Route{ + "Scan", + "POST", + "/api/v1/scan", + ScanHandler, + }, + Route{ + "Results", + "GET", + "/api/v1/results", + ResultHandler, + }, + Route{ + "Certificate", + "GET", + "/api/v1/certificate", + CertificateHandler, + }, +} diff --git a/src/web-api/web-api.go b/src/web-api/web-api.go index 1581a02a5..021ce679b 100644 --- a/src/web-api/web-api.go +++ b/src/web-api/web-api.go @@ -1,86 +1,16 @@ package main import ( - "github.com/gorilla/mux" - "github.com/streadway/amqp" "log" "net/http" - "fmt" ) func main() { - r := mux.NewRouter() - // try to filter files to download, example only - r.HandleFunc("/website/{domain}", DownloadHandler) - - //- - http.Handle("/", r) + router := NewRouter() // wait for clients - http.ListenAndServe(":8083", nil) -} + err := http.ListenAndServe(":8083", router) -func failOnError(err error, msg string) { - if err != nil { - log.Fatalf("%s: %s", msg, err) - panic(fmt.Sprintf("%s: %s", msg, err)) - } + log.Fatal(err) } - -func DownloadHandler(res http.ResponseWriter, req *http.Request) { - - var ( - status int - err error - ) - - defer func() { - if nil != err { - http.Error(res, err.Error(), status) - } - }() - - vars := mux.Vars(req) - domain := vars["domain"] - - fmt.Print(domain) - - if validateDomain(domain){ - - conn, err := amqp.Dial("amqp://guest:guest@localhost:5672/") - failOnError(err, "Failed to connect to RabbitMQ") - defer conn.Close() - - ch, err := conn.Channel() - failOnError(err, "Failed to open a channel") - defer ch.Close() - - status = http.StatusOK - err = ch.Publish( - "", // exchange - "scan_ready_queue", // routing key - false, // mandatory - false, - amqp.Publishing{ - DeliveryMode: amqp.Persistent, - ContentType: "text/plain", - Body: []byte(domain), - }) - - - }else{ - status = http.StatusBadRequest - return - } - -} - -func validateDomain( domain string ) ( bool ){ - - // TODO - // Need to validate the domain, in a way, - // before passing it to the retriever queue - - return true -} \ No newline at end of file