forked from niilo/clamav-rest
-
Notifications
You must be signed in to change notification settings - Fork 1
/
clamrest.go
129 lines (109 loc) · 3.04 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package main
import (
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"time"
"github.com/dutchcoders/go-clamd"
)
var opts map[string]string
func init() {
log.SetOutput(ioutil.Discard)
}
func home(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "...running...")
}
//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 {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
respJson := fmt.Sprintf("{ Status: \"%s\", Description: \"%s\" }", s.Status, s.Description)
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) + " Finished scanning: " + part.FileName() + "\n")
}
default:
w.WriteHeader(http.StatusMethodNotAllowed)
}
}
func waitForClamD(port string, times int) {
clamdTest := clamd.NewClamd(port)
clamdTest.Ping()
version, err := clamdTest.Version()
if err != nil {
if times < 30 {
fmt.Printf("clamD not running, waiting times [%v]\n", times)
time.Sleep(time.Second * 4)
waitForClamD(port, times+1)
} else {
fmt.Printf("Error getting clamd version: %v\n", err)
os.Exit(1)
}
} else {
for version_string := range version {
fmt.Printf("Clamd version: %#v\n", version_string.Raw)
}
}
}
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:3310"
}
fmt.Printf("Starting clamav rest bridge\n")
fmt.Printf("Connecting to clamd on %v\n", opts["CLAMD_PORT"])
waitForClamD(opts["CLAMD_PORT"], 1)
fmt.Printf("Connected to clamd on %v\n", opts["CLAMD_PORT"])
http.HandleFunc("/scan", scanHandler)
http.HandleFunc("/", home)
//Listen on port PORT
if opts["PORT"] == "" {
opts["PORT"] = "9000"
}
fmt.Printf("Listening on port " + opts["PORT"])
http.ListenAndServe(":"+opts["PORT"], nil)
}