Improvements
This commit is contained in:
parent
f1b9ac1027
commit
07a0607fd9
1 changed files with 63 additions and 49 deletions
60
clamrest.go
60
clamrest.go
|
@ -3,7 +3,6 @@ package main
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
@ -21,24 +20,33 @@ func init() {
|
||||||
log.SetOutput(ioutil.Discard)
|
log.SetOutput(ioutil.Discard)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Error struct {
|
||||||
|
Error string `json:"Error"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeError(w http.ResponseWriter, statusCode int, err string) {
|
||||||
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||||
|
w.WriteHeader(statusCode)
|
||||||
|
|
||||||
|
errJson, _ := json.Marshal(&Error{err})
|
||||||
|
if errJson != nil {
|
||||||
|
fmt.Fprint(w, string(errJson))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func home(w http.ResponseWriter, r *http.Request) {
|
func home(w http.ResponseWriter, r *http.Request) {
|
||||||
c := clamd.NewClamd(opts["CLAMD_PORT"])
|
c := clamd.NewClamd(opts["CLAMD_PORT"])
|
||||||
|
|
||||||
response, err := c.Stats()
|
response, err := c.Stats()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errJson, eErr := json.Marshal(err)
|
writeError(w, http.StatusInternalServerError, "Could not get stats: "+err.Error())
|
||||||
if eErr != nil {
|
|
||||||
fmt.Println(eErr)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
fmt.Fprint(w, string(errJson))
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
resJson, eRes := json.Marshal(response)
|
resJson, eRes := json.Marshal(response)
|
||||||
if eRes != nil {
|
if eRes != nil {
|
||||||
fmt.Println(eRes)
|
writeError(w, http.StatusInternalServerError, "Could not marshal JSON: "+eRes.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||||
|
@ -58,12 +66,7 @@ func scanPathHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
response, err := c.AllMatchScanFile(path)
|
response, err := c.AllMatchScanFile(path)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errJson, eErr := json.Marshal(err)
|
writeError(w, http.StatusInternalServerError, "Could not scan file: "+err.Error())
|
||||||
if eErr != nil {
|
|
||||||
fmt.Println(eErr)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
fmt.Fprint(w, string(errJson))
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -92,28 +95,39 @@ func scanHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
reader, err := r.MultipartReader()
|
reader, err := r.MultipartReader()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
writeError(w, http.StatusInternalServerError, "Could not initialize reader: "+err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
//copy each part to destination.
|
|
||||||
for {
|
|
||||||
part, err := reader.NextPart()
|
part, err := reader.NextPart()
|
||||||
if err == io.EOF {
|
if err != nil {
|
||||||
break
|
writeError(w, http.StatusInternalServerError, "Could not read file: "+err.Error())
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
//if part.FileName() is empty, skip this iteration.
|
//if part.FileName() is empty, skip this iteration.
|
||||||
if part.FileName() == "" {
|
if part.FileName() == "" {
|
||||||
continue
|
writeError(w, http.StatusBadRequest, "Filename is empty")
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
||||||
|
if err != nil {
|
||||||
|
writeError(w, http.StatusInternalServerError, "Could not scan file: "+err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
for s := range response {
|
for s := range response {
|
||||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||||
respJson := fmt.Sprintf("{ Status: \"%s\", Description: \"%s\" }", s.Status, s.Description)
|
|
||||||
|
respJson, err := json.Marshal(&s)
|
||||||
|
if err != nil {
|
||||||
|
writeError(w, http.StatusInternalServerError, "Could not marshal JSON: "+err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
switch s.Status {
|
switch s.Status {
|
||||||
case clamd.RES_OK:
|
case clamd.RES_OK:
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
|
@ -126,11 +140,11 @@ func scanHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
default:
|
default:
|
||||||
w.WriteHeader(http.StatusNotImplemented)
|
w.WriteHeader(http.StatusNotImplemented)
|
||||||
}
|
}
|
||||||
fmt.Fprint(w, respJson)
|
|
||||||
|
fmt.Fprint(w, string(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)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue