Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@ cleanimports:
application:
cd src/clammit && go install

gets: gcfg testify

gets: gcfg testify go-clamd

gcfg:
[ -d src/gopkg.in/gcfg.v1 ] || go get gopkg.in/gcfg.v1

go-clamd:
[ -d src/github.com/Freeaqingme/go-clamd ] || go get github.com/Freeaqingme/go-clamd

testify:
[ -d src/gopkg.in/testify.v1 ] || go get gopkg.in/stretchr/testify.v1

Expand Down
24 changes: 5 additions & 19 deletions src/clammit/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"flag"
"fmt"
"gopkg.in/gcfg.v1"
"io/ioutil"
"log"
"net"
"net/http"
Expand Down Expand Up @@ -78,8 +77,6 @@ type ApplicationConfig struct {
TestPages bool `gcfg:"test-pages"`
// If true, will log the progression of each request through the forwarder
Debug bool `gcfg:"debug"`
// If true, will log the annoying clamd messages
DebugClam bool `gcfg:"debug-clam"`
// Number of CPU threads to use
NumThreads int `gcfg:"num-threads"`
}
Expand All @@ -97,7 +94,6 @@ var DefaultApplicationConfig = ApplicationConfig{
Logfile: "",
TestPages: true,
Debug: false,
DebugClam: false,
NumThreads: runtime.NumCPU(),
}

Expand Down Expand Up @@ -197,10 +193,6 @@ func main() {
}
router.HandleFunc("/", scanForwardHandler)

if !ctx.Config.App.DebugClam {
log.SetOutput(ioutil.Discard) // go-clamd has irritating logging, so turn it off
}

if listener, err := getListener(ctx.Config.App.Listen, socketPerms); err != nil {
ctx.Logger.Fatal("Unable to listen on: ", ctx.Config.App.Listen, ", reason: ", err)
} else {
Expand Down Expand Up @@ -369,31 +361,25 @@ func infoHandler(w http.ResponseWriter, req *http.Request) {
if response, err := ctx.Scanner.Version(); err != nil {
info.ScannerVersion = err.Error()
} else {
for s := range response {
info.ScannerVersion += s
}
info.ScannerVersion = response
}
/*
* Validate the Clamd response for a viral string
*/
reader := bytes.NewReader(EICAR)
if response, err := ctx.Scanner.Scan(reader); err != nil {
if result, err := ctx.Scanner.Scan(reader); err != nil {
info.TestScanVirusResult = err.Error()
} else {
for s := range response {
info.TestScanVirusResult += s
}
info.TestScanVirusResult = result.String()
}
/*
* Validate the Clamd response for a non-viral string
*/
reader = bytes.NewReader([]byte("foo bar mcgrew"))
if response, err := ctx.Scanner.Scan(reader); err != nil {
if result, err := ctx.Scanner.Scan(reader); err != nil {
info.TestScanCleanResult = err.Error()
} else {
for s := range response {
info.TestScanCleanResult += s
}
info.TestScanCleanResult = result.String()
}
}
// Aaaand return
Expand Down
59 changes: 40 additions & 19 deletions src/clammit/scanner/clamav.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package scanner

import (
clamd "github.com/dutchcoders/go-clamd"
clamd "github.com/Freeaqingme/go-clamd"
"io"
)

Expand All @@ -23,40 +23,61 @@ func (c *Clamav) SetAddress(url string) {
}

func (c *Clamav) HasVirus(reader io.Reader) (bool, error) {
response, err := c.Scan(reader)
result, err := c.Scan(reader)
if err != nil {
return false, err
}

result := false
for s := range response {
if s != "stream: OK" {
if c.debug {
c.logger.Printf(" %v", s)
}
result = true
}
}
return result.Virus, nil
}

func (c *Clamav) Scan(reader io.Reader) (*Result, error) {
if c.debug {
c.logger.Println(" result of scan:", result)
c.logger.Println("Sending to clamav")
}

return result, nil
}
ch, err := c.clam.ScanStream(reader, nil)
if err != nil {
return nil, err
}
var status string

r := (<-ch)

switch r.Status {
case clamd.RES_OK:
status = RES_CLEAN
case clamd.RES_FOUND:
status = RES_FOUND
case clamd.RES_ERROR:
case clamd.RES_PARSE_ERROR:
default:
status = RES_ERROR
}

result := &Result{
Status: status,
Virus: status == RES_FOUND,
Description: r.Description,
}

func (c *Clamav) Scan(reader io.Reader) (chan string, error) {
if c.debug {
c.logger.Println("Sending to clamav")
c.logger.Println(" result of scan:", result)
}

return c.clam.ScanStream(reader)
return result, nil
}

func (c *Clamav) Ping() error {
return c.clam.Ping()
}

func (c *Clamav) Version() (chan string, error) {
return c.clam.Version()
func (c *Clamav) Version() (string, error) {
ch, err := c.clam.Version()
if err != nil {
return "", err
}

r := (<-ch)
return r.Raw, nil
}
37 changes: 35 additions & 2 deletions src/clammit/scanner/scanner.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package scanner

import (
"fmt"
"io"
"io/ioutil"
"log"
Expand Down Expand Up @@ -39,7 +40,7 @@ type Scanner interface {
* This function performs the actual virus scan and returns an engine-specific
* response string
*/
Scan(reader io.Reader) (chan string, error)
Scan(reader io.Reader) (*Result, error)

/*
* Tests the liveliness of the underlying scan engine
Expand All @@ -49,7 +50,7 @@ type Scanner interface {
/*
* Returns the version of the underlying scan engine
*/
Version() (chan string, error)
Version() (string, error)
}

/*
Expand Down Expand Up @@ -89,3 +90,35 @@ func (e *Engine) SetLogger(logger *log.Logger, debug bool) {
e.logger = logger
e.debug = debug
}

/*
* Scanner result statuses
*/
const (
RES_CLEAN = "CLEAN"
RES_FOUND = "FOUND"
RES_ERROR = "ERROR"
)

/*
* Embeds a scan result.
*
* Status is one of the RES_* constants
* Virus is true or false depending a Virus has been detected
* Description is an extended status, containing the virus name
*/
type Result struct {
Status string
Virus bool
Description string
}

func (r *Result) String() string {
ret := fmt.Sprintf("Status: %s; Virus: %v", r.Status, r.Virus)

if r.Virus {
ret += fmt.Sprintf("; Description: %s", r.Description)
}

return ret
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading