-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathclamrest.go
373 lines (323 loc) · 10.4 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"strings"
"time"
"github.com/dutchcoders/go-clamd"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/collectors"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var opts map[string]string
var noOfFoundViruses = prometheus.NewCounter(prometheus.CounterOpts{
Name: "no_of_found_viruses",
Help: "The total number of found viruses",
})
func init() {
log.SetOutput(io.Discard)
}
func clamversion(w http.ResponseWriter, r *http.Request) {
c := clamd.NewClamd(opts["CLAMD_PORT"])
version, err := c.Version()
if err != nil {
errJson, eErr := json.Marshal(err)
if eErr != nil {
fmt.Println(eErr)
return
}
fmt.Fprint(w, string(errJson))
return
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
for version_string := range version {
if strings.HasPrefix(version_string.Raw, "ClamAV ") {
version_values := strings.Split(strings.Replace(version_string.Raw, "ClamAV ", "", 1), "/")
respJson := fmt.Sprintf("{ \"Clamav\": \"%s\" }", version_values[0])
if len(version_values) == 3 {
respJson = fmt.Sprintf("{ \"Clamav\": \"%s\", \"Signature\": \"%s\" , \"Signature_date\": \"%s\" }", version_values[0], version_values[1], version_values[2])
}
fmt.Fprint(w, string(respJson))
} else {
w.WriteHeader(http.StatusInternalServerError)
}
}
}
func home(w http.ResponseWriter, r *http.Request) {
c := clamd.NewClamd(opts["CLAMD_PORT"])
response, err := c.Stats()
if err != nil {
errJson, eErr := json.Marshal(err)
if eErr != nil {
fmt.Println(eErr)
return
}
fmt.Fprint(w, string(errJson))
return
}
resJson, eRes := json.Marshal(response)
if eRes != nil {
fmt.Println(eRes)
return
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
fmt.Fprint(w, string(resJson))
}
func scanPathHandler(w http.ResponseWriter, r *http.Request) {
paths, ok := r.URL.Query()["path"]
if !ok || len(paths[0]) < 1 {
log.Println("Url Param 'path' is missing")
return
}
path := paths[0]
c := clamd.NewClamd(opts["CLAMD_PORT"])
response, err := c.AllMatchScanFile(path)
if err != nil {
errJson, eErr := json.Marshal(err)
if eErr != nil {
fmt.Println(eErr)
return
}
fmt.Fprint(w, string(errJson))
return
}
var scanResults []*clamd.ScanResult
for responseItem := range response {
if responseItem.Status == clamd.RES_FOUND {
noOfFoundViruses.Inc()
}
scanResults = append(scanResults, responseItem)
}
resJson, eRes := json.Marshal(scanResults)
if eRes != nil {
fmt.Println(eRes)
return
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
fmt.Fprint(w, string(resJson))
}
func v2ScanHandler(w http.ResponseWriter, r *http.Request) {
scanner(w, r, 2)
}
// old endpoint version, set deprecation header to indicate usage of the new /v2/scan
func scanHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Deprecation", "version=v1")
v2url := fmt.Sprintf("%s%s/v2/scan", string(r.URL.Scheme), r.Host)
w.Header().Add("Link", fmt.Sprintf("%v; rel=successor-version", v2url))
scanner(w, r, 1)
}
// This is where the action happens.
func scanner(w http.ResponseWriter, r *http.Request, version int) {
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.
resp := []scanResponse{}
for {
part, err := reader.NextPart()
if err == io.EOF {
break
}
//if part.FileName() is empty, skip this iteration.
if part.FileName() == "" {
if version == 2 {
fileResp := scanResponse{Status: "ERROR", Description: "MimePart FileName missing", httpStatus: 422}
resp = append(resp, fileResp)
fmt.Printf("%v Not scanning, MimePart FileName not supplied\n", time.Now().Format(time.RFC3339))
}
continue
}
fmt.Printf("%v Started scanning: %v\n", time.Now().Format(time.RFC3339), part.FileName())
var abort chan bool
response, err := c.ScanStream(part, abort)
if err != nil {
//error occurred, response is nil, create a custom response and send it on the channel to handle it together with the other errors.
response = make(chan *clamd.ScanResult)
scanErrResult := &clamd.ScanResult{Status: clamd.RES_PARSE_ERROR, Description: "File size limit exceeded"}
go func() {
response <- scanErrResult
close(response)
fmt.Printf("%v Clamd returned an error, probably a too large file as input (causing broken pipe and closed connection) %v\n", time.Now().Format(time.RFC3339), err)
//The underlying service closes the connection if the file is to large, logging output
//We never receive the clamd output of `^INSTREAM: Size limit reached` up here, just a closed connection.
}()
}
for s := range response {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
eachResp := scanResponse{Status: s.Status, Description: s.Description}
if version == 2 {
eachResp.FileName = part.FileName()
fmt.Printf("scanned file %v", part.FileName())
}
//Set each possible status and then send the most appropriate one
eachResp.httpStatus = getHttpStatusByClamStatus(s)
resp = append(resp, eachResp)
fmt.Printf("%v Scan result for: %v, %v\n", time.Now().Format(time.RFC3339), part.FileName(), s)
}
fmt.Printf("%v Finished scanning: %v\n", time.Now().Format(time.RFC3339), part.FileName())
}
w.WriteHeader(getResponseStatus(resp))
if version == 2 {
jsonRes, jErr := json.Marshal(resp)
if jErr != nil {
fmt.Printf("Error marshalling json, %v\n", jErr)
}
fmt.Fprint(w, string(jsonRes))
} else {
for _, v := range resp {
jsonRes, jErr := json.Marshal(v)
if jErr != nil {
fmt.Printf("Error marshalling json, %v\n", jErr)
}
fmt.Fprint(w, string(jsonRes))
}
}
default:
w.WriteHeader(http.StatusMethodNotAllowed)
}
}
func getHttpStatusByClamStatus(result *clamd.ScanResult) int {
switch result.Status {
case clamd.RES_OK:
return http.StatusOK //200
case clamd.RES_FOUND:
fmt.Printf("%v Virus FOUND\n", time.Now().Format(time.RFC3339))
return http.StatusNotAcceptable //406
case clamd.RES_ERROR:
return http.StatusBadRequest //400
case clamd.RES_PARSE_ERROR:
if result.Description == "File size limit exceeded" {
return http.StatusRequestEntityTooLarge //413
} else {
return http.StatusPreconditionFailed //412
}
default:
return http.StatusNotImplemented //501
}
}
// this func returns 406 if one file contains a virus
func getResponseStatus(responses []scanResponse) int {
result := 200
for _, r := range responses {
switch r.httpStatus {
case 406:
//uptick the prometheus counter for detected viruses.
noOfFoundViruses.Inc()
//early return if virus is found
return 406
case 400:
result = 400
case 412:
result = 412
case 413:
result = 413
case 422:
result = 422
case 501:
result = 501
}
}
return result
}
func scanHandlerBody(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
c := clamd.NewClamd(opts["CLAMD_PORT"])
fmt.Printf("%v Started scanning plain body\n", time.Now().Format(time.RFC3339))
var abort chan bool
defer r.Body.Close()
response, err := c.ScanStream(r.Body, abort)
w.Header().Set("Content-Type", "application/json; charset=utf-8")
if err != nil {
w.WriteHeader(http.StatusRequestEntityTooLarge)
resp := scanResponse{Status: clamd.RES_PARSE_ERROR, Description: "File size limit exceeded"}
fmt.Printf("%v Clamd returned error, broken pipe and closed connection can indicate too large file, %v", time.Now().Format(time.RFC3339), err)
jsonResp, jErr := json.Marshal(resp)
if jErr != nil {
fmt.Printf("%v Error marshalling json, %v", time.Now().Format(time.RFC3339), jErr)
}
fmt.Fprint(w, string(jsonResp))
return
}
for s := range response {
resp := scanResponse{Status: s.Status, Description: s.Description}
//respJson := fmt.Sprintf("{ Status: %q, Description: %q }", s.Status, s.Description)
resp.httpStatus = getHttpStatusByClamStatus(s)
resps := []scanResponse{}
resps = append(resps, resp)
w.WriteHeader(getResponseStatus(resps))
fmt.Fprint(w, resp)
fmt.Printf("%v Scan result for plain body: %v\n", time.Now().Format(time.RFC3339), s)
}
}
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)
// https://github.com/prometheus/client_golang/blob/main/examples/gocollector/main.go
reg := prometheus.NewRegistry()
reg.MustRegister(collectors.NewBuildInfoCollector())
reg.MustRegister(collectors.NewGoCollector(
collectors.WithGoCollections(collectors.GoRuntimeMemStatsCollection | collectors.GoRuntimeMetricsCollection),
))
reg.MustRegister(noOfFoundViruses)
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("/v2/scan", v2ScanHandler)
http.HandleFunc("/scanPath", scanPathHandler)
http.HandleFunc("/scanHandlerBody", scanHandlerBody)
http.HandleFunc("/version", clamversion)
http.HandleFunc("/", home)
// Prometheus metrics
http.Handle("/metrics", promhttp.HandlerFor(
reg,
promhttp.HandlerOpts{
// Opt into OpenMetrics to support exemplars.
EnableOpenMetrics: true,
},
))
// Start the HTTPS server in a goroutine
go http.ListenAndServeTLS(fmt.Sprintf(":%s", opts["SSL_PORT"]), "/etc/ssl/clamav-rest/server.crt", "/etc/ssl/clamav-rest/server.key", nil)
// Start the HTTP server
http.ListenAndServe(fmt.Sprintf(":%s", opts["PORT"]), nil)
}