refactor default clamavd port to 3310 and change http response codes to more descriptive ones

This commit is contained in:
Niilo Ursin 2017-08-28 23:28:10 +03:00
parent 5784f5fc5f
commit 9b06eb5b5e

View file

@ -2,14 +2,15 @@ package main
import ( import (
"fmt" "fmt"
"time"
"strings"
"log"
"io" "io"
"io/ioutil" "io/ioutil"
"log"
"net/http" "net/http"
"github.com/dutchcoders/go-clamd"
"os" "os"
"strings"
"time"
"github.com/dutchcoders/go-clamd"
) )
var opts map[string]string var opts map[string]string
@ -45,18 +46,27 @@ func scanHandler(w http.ResponseWriter, r *http.Request) {
} }
fmt.Printf(time.Now().Format(time.RFC3339) + " Started scanning: " + part.FileName() + "\n") fmt.Printf(time.Now().Format(time.RFC3339) + " Started scanning: " + part.FileName() + "\n")
var abort chan bool; var abort chan bool
response, err := c.ScanStream(part, abort); response, err := c.ScanStream(part, abort)
for s := range response { for s := range response {
if strings.Contains(s.Status, "FOUND") { w.Header().Set("Content-Type", "application/json; charset=utf-8")
//return_string := []string{"Result: ", s.Status, ", Description: ", s.Description} respJson := fmt.Sprintf("{ Status: \"%s\", Description: \"%s\" }", s.Status, s.Description)
http.Error(w, "Result: " + s.Status + ", Description: " + s.Description, http.StatusInternalServerError) switch s.Status {
case clamd.RES_OK:
w.WriteHeader(http.StatusOK)
case clamd.RES_FOUND:
w.WriteHeader(http.StatusNotAcceptable)
case clamd.RES_ERROR:
w.WriteHeader(http.StatusBadRequest)
case clamd.RES_PARSE_ERROR:
w.WriteHeader(http.StatusPreconditionFailed)
default:
w.WriteHeader(http.StatusNotImplemented)
} }
fmt.Fprint(w, respJson)
fmt.Printf(time.Now().Format(time.RFC3339) + " Scan result for: %v, %v\n", part.FileName(), 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") fmt.Printf(time.Now().Format(time.RFC3339) + " Finished scanning: " + part.FileName() + "\n")
} }
default: default:
w.WriteHeader(http.StatusMethodNotAllowed) w.WriteHeader(http.StatusMethodNotAllowed)
@ -73,7 +83,7 @@ func main() {
} }
if opts["CLAMD_PORT"] == "" { if opts["CLAMD_PORT"] == "" {
opts["CLAMD_PORT"] = "tcp://localhost:3031" opts["CLAMD_PORT"] = "tcp://localhost:3310"
} }
fmt.Printf("Starting clamav rest bridge\n") fmt.Printf("Starting clamav rest bridge\n")
@ -87,14 +97,16 @@ func main() {
os.Exit(1) os.Exit(1)
} }
for version_string := range version { for version_string := range version {
fmt.Printf("Clamd version: %v\n", version_string) fmt.Printf("Clamd version: %#v\n", version_string.Raw)
} }
fmt.Printf("Connected to clamd on %v\n", opts["CLAMD_PORT"]) fmt.Printf("Connected to clamd on %v\n", opts["CLAMD_PORT"])
http.HandleFunc("/scan", scanHandler) http.HandleFunc("/scan", scanHandler)
//Listen on port PORT //Listen on port PORT
if opts["PORT"] == "" { opts["PORT"] = "9000" } if opts["PORT"] == "" {
opts["PORT"] = "9000"
}
fmt.Printf("Listening on port " + opts["PORT"]) fmt.Printf("Listening on port " + opts["PORT"])
http.ListenAndServe(":" + opts["PORT"], nil) http.ListenAndServe(":"+opts["PORT"], nil)
} }