-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsophie.go
305 lines (257 loc) · 6.07 KB
/
sophie.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
// Copyright (C) 2018-2021 Andrew Colin Kissa <[email protected]>
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at http://mozilla.org/MPL/2.0/.
/*
Package sophie implements the sophie protocol
Sophie - Golang Sophie protocol implementation
*/
package sophie
import (
"context"
"fmt"
"io"
"net"
"net/textproto"
"os"
"strings"
"time"
)
const (
defaultSleep = 1 * time.Second
defaultTimeout = 15 * time.Second
defaultCmdTimeout = 1 * time.Minute
defaultSock = "/var/lib/savdid/savdid.sock"
invalidRespErr = "Invalid server response: %s"
unsupportedProtoErr = "Protocol: %s is not supported"
unixSockErr = "The unix socket: %s does not exist"
unknownStatusErr = "Unknown status"
noSizeErr = "The content length could not be determined"
tcpDirErr = "Scanning directories not supported on a TCP connection"
)
type readerWithLen interface {
Len() int
}
// Response is the response from the server
type Response struct {
Filename string
Signature string
Infected bool
Raw string
}
// A Client represents a Sophie client.
type Client struct {
network string
address string
connTimeout time.Duration
connRetries int
connSleep time.Duration
cmdTimeout time.Duration
}
// SetConnTimeout sets the connection timeout
func (c *Client) SetConnTimeout(t time.Duration) {
if t > 0 {
c.connTimeout = t
}
}
// SetCmdTimeout sets the cmd timeout
func (c *Client) SetCmdTimeout(t time.Duration) {
if t > 0 {
c.cmdTimeout = t
}
}
// SetConnRetries sets the number of times
// connection is retried
func (c *Client) SetConnRetries(s int) {
if s < 0 {
s = 0
}
c.connRetries = s
}
// SetConnSleep sets the connection retry sleep
// duration in seconds
func (c *Client) SetConnSleep(s time.Duration) {
if s > 0 {
c.connSleep = s
}
}
// Scan a file or directory
func (c *Client) Scan(ctx context.Context, p string) (r *Response, err error) {
r, err = c.fileCmd(ctx, p)
return
}
// ScanStream submits a stream for scanning
// func (c *Client) ScanStream(i io.Reader) (r *Response, err error) {
// r, err = c.readerCmd(i)
// return
// }
// ScanReader scans an io.reader
func (c *Client) ScanReader(ctx context.Context, i io.Reader) (r *Response, err error) {
r, err = c.readerCmd(ctx, i)
return
}
func (c *Client) dial(ctx context.Context) (conn net.Conn, err error) {
d := &net.Dialer{}
if c.connTimeout > 0 {
d.Timeout = c.connTimeout
}
for i := 0; i <= c.connRetries; i++ {
conn, err = d.DialContext(ctx, c.network, c.address)
if e, ok := err.(net.Error); ok && e.Timeout() {
time.Sleep(c.connSleep)
continue
}
break
}
return
}
func (c *Client) fileCmd(ctx context.Context, p string) (r *Response, err error) {
var id uint
var isTCP bool
var f *os.File
var conn net.Conn
var stat os.FileInfo
var tc *textproto.Conn
if stat, err = os.Stat(p); os.IsNotExist(err) {
return
}
if c.network != "unix" && c.network != "unixpacket" {
isTCP = true
if stat.IsDir() {
err = fmt.Errorf(tcpDirErr)
return
}
}
if isTCP {
if f, err = os.Open(p); err != nil {
return
}
defer f.Close()
r, err = c.readerCmd(ctx, f)
} else {
conn, err = c.dial(ctx)
if err != nil {
return
}
tc = textproto.NewConn(conn)
defer tc.Close()
id = tc.Next()
tc.StartRequest(id)
conn.SetDeadline(time.Now().Add(c.cmdTimeout))
if err = tc.PrintfLine("%s", p); err != nil {
tc.EndRequest(id)
return
}
tc.EndRequest(id)
tc.StartResponse(id)
defer tc.EndResponse(id)
conn.SetDeadline(time.Now().Add(c.cmdTimeout))
r, err = c.processResponse(tc, p)
}
return
}
func (c *Client) readerCmd(ctx context.Context, i io.Reader) (r *Response, err error) {
var id uint
var l string
var clen int64
var conn net.Conn
var stat os.FileInfo
var tc *textproto.Conn
conn, err = c.dial(ctx)
if err != nil {
return
}
tc = textproto.NewConn(conn)
defer tc.Close()
switch v := i.(type) {
case readerWithLen:
clen = int64(v.Len())
case *os.File:
stat, err = v.Stat()
if err != nil {
return
}
clen = stat.Size()
default:
err = fmt.Errorf(noSizeErr)
return
}
id = tc.Next()
tc.StartRequest(id)
conn.SetDeadline(time.Now().Add(c.cmdTimeout))
if err = tc.PrintfLine("stream/%d", clen); err != nil {
tc.EndRequest(id)
return
}
conn.SetDeadline(time.Now().Add(c.cmdTimeout))
if l, err = tc.ReadLine(); err != nil {
tc.EndRequest(id)
return
}
if l != "OK" {
err = fmt.Errorf(invalidRespErr, l)
tc.EndRequest(id)
return
}
conn.SetDeadline(time.Now().Add(c.cmdTimeout))
if _, err = io.Copy(tc.Writer.W, i); err != nil {
tc.EndRequest(id)
return
}
tc.W.Flush()
tc.EndRequest(id)
tc.StartResponse(id)
defer tc.EndResponse(id)
conn.SetDeadline(time.Now().Add(c.cmdTimeout))
r, err = c.processResponse(tc, "")
return
}
func (c *Client) processResponse(tc *textproto.Conn, p string) (r *Response, err error) {
var l string
if l, err = tc.ReadLine(); err != nil {
return
}
r = &Response{}
if p == "" {
r.Filename = "stream"
} else {
r.Filename = p
}
if strings.HasPrefix(l, "-1") {
err = fmt.Errorf(unknownStatusErr)
} else if strings.HasPrefix(l, "1") || strings.HasPrefix(l, "0") {
r.Raw = l
if strings.HasPrefix(l, "1") {
r.Signature = l[2:]
r.Infected = true
}
} else {
err = fmt.Errorf(invalidRespErr, l)
}
return
}
// NewClient returns a new Sophie client.
func NewClient(network, address string) (c *Client, err error) {
if network == "" && address == "" {
network = "unix"
address = defaultSock
}
if network != "unix" && network != "unixpacket" && network != "tcp" && network != "tcp4" && network != "tcp6" {
err = fmt.Errorf(unsupportedProtoErr, network)
return
}
if network == "unix" || network == "unixpacket" {
if _, err = os.Stat(address); os.IsNotExist(err) {
err = fmt.Errorf(unixSockErr, address)
return
}
}
c = &Client{
network: network,
address: address,
connTimeout: defaultTimeout,
connSleep: defaultSleep,
cmdTimeout: defaultCmdTimeout,
}
return
}