-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
189 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |