Skip to content

Commit

Permalink
Merge pull request #7 from ifad/remove-vendored-code
Browse files Browse the repository at this point in the history
Remove vendored code, fix #1
  • Loading branch information
vjt authored Mar 27, 2017
2 parents 646cc3f + e6e3603 commit 4a41559
Show file tree
Hide file tree
Showing 17 changed files with 89 additions and 651 deletions.
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
}
24 changes: 0 additions & 24 deletions src/clammit/vendor/github.com/dutchcoders/go-clamd/.gitignore

This file was deleted.

10 changes: 0 additions & 10 deletions src/clammit/vendor/github.com/dutchcoders/go-clamd/.travis.yml

This file was deleted.

22 changes: 0 additions & 22 deletions src/clammit/vendor/github.com/dutchcoders/go-clamd/LICENSE

This file was deleted.

35 changes: 0 additions & 35 deletions src/clammit/vendor/github.com/dutchcoders/go-clamd/README.md

This file was deleted.

Loading

0 comments on commit 4a41559

Please sign in to comment.