-
Notifications
You must be signed in to change notification settings - Fork 26
/
clamrest.go
100 lines (82 loc) · 2.46 KB
/
clamrest.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package main
import (
"fmt"
"time"
"strings"
"log"
"io"
"io/ioutil"
"net/http"
"github.com/dutchcoders/go-clamd"
"os"
)
var opts map[string]string
func init() {
log.SetOutput(ioutil.Discard)
}
//This is where the action happens.
func scanHandler(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(opts["CLAMD_PORT"])
//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")
var abort chan bool;
response, err := c.ScanStream(part, abort);
for s := range response {
if strings.Contains(s.Status, "FOUND") {
//return_string := []string{"Result: ", s.Status, ", Description: ", s.Description}
http.Error(w, "Result: " + s.Status + ", Description: " + s.Description, http.StatusInternalServerError)
}
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() {
opts = make(map[string]string)
for _, e := range os.Environ() {
pair := strings.Split(e, "=")
opts[pair[0]] = pair[1]
}
if opts["CLAMD_PORT"] == "" {
opts["CLAMD_PORT"] = "tcp://localhost:3031"
}
fmt.Printf("Starting clamav rest bridge\n")
fmt.Printf("Connecting to clamd on %v\n", opts["CLAMD_PORT"])
clamd_test := clamd.NewClamd(opts["CLAMD_PORT"])
clamd_test.Ping()
version, err := clamd_test.Version()
if err != nil {
fmt.Printf("Error getting clamd version: %v\n", err)
os.Exit(1)
}
for version_string := range version {
fmt.Printf("Clamd version: %v\n", version_string)
}
fmt.Printf("Connected to clamd on %v\n", opts["CLAMD_PORT"])
http.HandleFunc("/scan", scanHandler)
//Listen on port PORT
if opts["PORT"] == "" { opts["PORT"] = "9000" }
fmt.Printf("Listening on port " + opts["PORT"])
http.ListenAndServe(":" + opts["PORT"], nil)
}