Initial commit

This commit is contained in:
Oliver 2015-07-26 13:37:23 +01:00
commit 832d0fd453
3 changed files with 74 additions and 0 deletions

0
.godir Normal file
View file

1
eicar.com.txt Normal file
View file

@ -0,0 +1 @@
X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*

73
server.go Normal file
View file

@ -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)
}