-
Notifications
You must be signed in to change notification settings - Fork 1
/
clamav-client.go
85 lines (71 loc) · 1.59 KB
/
clamav-client.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
package rpcsrv
import (
"encoding/binary"
"fmt"
"io"
"net"
)
const (
chunkSize = 1024 * 1024
goodResponse = "stream: OK\n"
)
var clamAVNetwork string
var clamAVAddress string
var clamAVConfigured bool
// ClamAVConfigure enables ClamAV integration via clamd socket.
// Should be called only before Start.
func ClamAVConfigure(network, address string) {
clamAVNetwork = network
clamAVAddress = address
clamAVConfigured = true
}
func clamAVScan(data []byte) error {
if !clamAVConfigured {
return nil
}
conn, err := net.Dial(clamAVNetwork, clamAVAddress)
if err != nil {
return err
}
defer func() {
// To calm down errcheck
closeErr := conn.Close()
if closeErr != nil {
return
}
}()
_, err = conn.Write([]byte("nINSTREAM\n"))
if err != nil {
return err
}
for remainderSize := len(data); remainderSize > 0; remainderSize -= chunkSize {
thisChunkSize := chunkSize
if remainderSize < chunkSize {
thisChunkSize = remainderSize
}
thisChunkSizeBytes := make([]byte, 4)
binary.BigEndian.PutUint32(thisChunkSizeBytes, uint32(thisChunkSize))
_, err = conn.Write(thisChunkSizeBytes)
if err != nil {
return err
}
thisChunk := data[len(data)-remainderSize : len(data)-remainderSize+thisChunkSize]
_, err = conn.Write(thisChunk)
if err != nil {
return err
}
}
_, err = conn.Write([]byte{0, 0, 0, 0})
if err != nil {
return err
}
responseBytes, err := io.ReadAll(conn)
if err != nil {
return err
}
response := string(responseBytes)
if response != goodResponse {
return fmt.Errorf("unexpected response from clamd '%v'", response)
}
return nil
}