Parse Result instead of only returning a string

This commit is contained in:
Dolf Schimmel (Freeaqingme)
2016-04-03 17:05:04 +02:00
parent 1b4a274e85
commit ace0f266eb
2 changed files with 77 additions and 35 deletions

49
conn.go
View File

@@ -30,6 +30,8 @@ import (
"fmt"
"io"
"net"
"regexp"
"strconv"
"strings"
"sync"
"time"
@@ -38,6 +40,10 @@ import (
const CHUNK_SIZE = 1024
const TCP_TIMEOUT = time.Second * 2
var resultRegex = regexp.MustCompile(
`^(?P<path>[^:]+): ((?P<desc>[^:]+)(\((?P<virhash>([^:]+)):(?P<virsize>\d+)\))? )?(?P<status>FOUND|ERROR|OK)$`,
)
type CLAMDConn struct {
net.Conn
}
@@ -75,18 +81,13 @@ func (conn *CLAMDConn) sendChunk(data []byte) error {
return err
}
func (c *CLAMDConn) readResponse() (chan string, *sync.WaitGroup, error) {
func (c *CLAMDConn) readResponse() (chan *ScanResult, *sync.WaitGroup, error) {
var wg sync.WaitGroup
wg.Add(1)
// read data
reader := bufio.NewReader(c)
ch := make(chan *ScanResult)
// reading
ch := make(chan string)
// var dataArrays []string
go func() {
defer func() {
close(ch)
@@ -104,14 +105,44 @@ func (c *CLAMDConn) readResponse() (chan string, *sync.WaitGroup, error) {
}
line = strings.TrimRight(line, " \t\r\n")
ch <- line
ch <- parseResult(line)
}
}()
return ch, &wg, nil
}
func parseResult(line string) *ScanResult {
res := &ScanResult{}
res.Raw = line
matches := resultRegex.FindStringSubmatch(line)
if len(matches) == 0 {
res.Status = RES_PARSE_ERROR
return res
}
for i, name := range resultRegex.SubexpNames() {
switch name {
case "path":
res.Path = matches[i]
case "desc":
res.Description = matches[i]
case "virhash":
res.Hash = matches[i]
case "virsize":
i, err := strconv.Atoi(matches[i])
if err == nil {
res.Size = i
}
case "status":
res.Status = matches[i]
}
}
return res
}
func newCLAMDTcpConn(address string) (*CLAMDConn, error) {
conn, err := net.DialTimeout("tcp", address, TCP_TIMEOUT)