From 832d0fd453df439b3df11ddf2a60fa10adb535eb Mon Sep 17 00:00:00 2001 From: Oliver Date: Sun, 26 Jul 2015 13:37:23 +0100 Subject: [PATCH] Initial commit --- .godir | 0 eicar.com.txt | 1 + server.go | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+) create mode 100644 .godir create mode 100644 eicar.com.txt create mode 100644 server.go diff --git a/.godir b/.godir new file mode 100644 index 0000000..e69de29 diff --git a/eicar.com.txt b/eicar.com.txt new file mode 100644 index 0000000..704cac8 --- /dev/null +++ b/eicar.com.txt @@ -0,0 +1 @@ +X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H* diff --git a/server.go b/server.go new file mode 100644 index 0000000..a7f9ad5 --- /dev/null +++ b/server.go @@ -0,0 +1,73 @@ +package main + +import ( + "fmt" + "time" + "strings" + "log" + "io" + "io/ioutil" + "net/http" + "github.com/dutchcoders/go-clamd" +) + +func init() { + log.SetOutput(ioutil.Discard) +} + +//This is where the action happens. +func uploadHandler(w http.ResponseWriter, r *http.Request) { + switch r.Method { + //POST takes the uploaded file(s) and saves it to disk. + case "POST": + c := clamd.NewClamd("tcp://localhost:3310") + //get the multipart reader for the request. + reader, err := r.MultipartReader() + + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + //copy each part to destination. + for { + part, err := reader.NextPart() + if err == io.EOF { + break + } + + //if part.FileName() is empty, skip this iteration. + if part.FileName() == "" { + continue + } + + fmt.Printf(time.Now().Format(time.RFC3339) + " Started scanning: " + part.FileName() + "\n") + response, err := c.ScanStream(part); + for s := range response { + if strings.Contains(s, "FOUND") { + http.Error(w, s, http.StatusInternalServerError) + } else { + fmt.Fprintf(w, s) + } + fmt.Printf(time.Now().Format(time.RFC3339) + " Scan result for: %v, %v\n", part.FileName(), s) + } + fmt.Printf(time.Now().Format(time.RFC3339) + " Finished scanning: " + part.FileName() + "\n") + + } + default: + w.WriteHeader(http.StatusMethodNotAllowed) + } +} + +func main() { + + fmt.Printf("Starting clamav rest bridge\n") + + http.HandleFunc("/scan", uploadHandler) + + //static file handler. + http.Handle("/assets/", http.StripPrefix("/assets/", http.FileServer(http.Dir("assets")))) + + //Listen on port 8080 + http.ListenAndServe(":3030", nil) +}