diff --git a/Gopkg.lock b/Gopkg.lock index 21804bb1..3531ab81 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -59,7 +59,7 @@ revision = "f58a169a71a51037728990b2d3597a14f56b525b" [[projects]] - digest = "1:5533d679fd9129a0af86235a01837db24bd66bf368d9a03aaf577a54d3d1e098" + digest = "1:bfbb93ae71a839764d94c1ba50f3868ecc576871274cf29db1903bf32efce620" name = "github.com/evilsocket/islazy" packages = [ "data", @@ -71,8 +71,8 @@ "zip", ] pruneopts = "UT" - revision = "ba851ad172f4be37fcaab106c8c9ebe42e3fa4ac" - version = "v1.7.0" + revision = "e86063a516840e32de87d39c214a6937b4d22e04" + version = "v1.8.0" [[projects]] branch = "master" diff --git a/Gopkg.toml b/Gopkg.toml index 87d848e4..be2ca61c 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -25,7 +25,7 @@ # unused-packages = true [[constraint]] name = "github.com/evilsocket/islazy" - version = "1.7.0" + version = "1.8.0" [[constraint]] branch = "master" diff --git a/caplets/caplet.go b/caplets/caplet.go index 4ef35101..b36c1a8a 100644 --- a/caplets/caplet.go +++ b/caplets/caplet.go @@ -2,9 +2,10 @@ package caplets import ( "fmt" - "os" "path/filepath" "strings" + + "github.com/evilsocket/islazy/fs" ) type Caplet struct { @@ -15,39 +16,23 @@ type Caplet struct { } func (cap *Caplet) Eval(argv []string, lineCb func(line string) error) error { - // the caplet might include other files (include directive, proxy modules, etc), - // temporarily change the working directory - cwd, err := os.Getwd() - if err != nil { - return fmt.Errorf("error while getting current working directory: %v", err) - } - - capPath := filepath.Dir(cap.Path) - if err := os.Chdir(capPath); err != nil { - return fmt.Errorf("error while changing current working directory: %v", err) - } - - defer func() { - if err := os.Chdir(cwd); err != nil { - fmt.Printf("error while restoring working directory: %v\n", err) - } - }() - if argv == nil { argv = []string{} } + // the caplet might include other files (include directive, proxy modules, etc), + // temporarily change the working directory + return fs.Chdir(filepath.Dir(cap.Path), func() error { + for _, line := range cap.Code { + // replace $0 with argv[0], $1 with argv[1] and so on + for i, arg := range argv { + what := fmt.Sprintf("$%d", i) + line = strings.Replace(line, what, arg, -1) + } - for _, line := range cap.Code { - // replace $0 with argv[0], $1 with argv[1] and so on - for i, arg := range argv { - what := fmt.Sprintf("$%d", i) - line = strings.Replace(line, what, arg, -1) + if err := lineCb(line); err != nil { + return err + } } - - if err = lineCb(line); err != nil { - return err - } - } - - return nil + return nil + }) } diff --git a/caplets/manager.go b/caplets/manager.go index 042d95bd..66a4f920 100644 --- a/caplets/manager.go +++ b/caplets/manager.go @@ -1,7 +1,6 @@ package caplets import ( - "bufio" "fmt" "os" "path/filepath" @@ -10,7 +9,6 @@ import ( "sync" "github.com/evilsocket/islazy/fs" - "github.com/evilsocket/islazy/str" ) var ( @@ -68,24 +66,19 @@ func Load(name string) (error, *Caplet) { Path: filename, Code: make([]string, 0), } - - input, err := os.Open(filename) - if err != nil { - return fmt.Errorf("error reading caplet %s: %v", filename, err), nil - } - defer input.Close() - - scanner := bufio.NewScanner(input) - scanner.Split(bufio.ScanLines) - for scanner.Scan() { - line := str.Trim(scanner.Text()) - if line == "" || line[0] == '#' { - continue - } - cap.Code = append(cap.Code, line) - } - cache[name] = cap + + if reader, err := fs.LineReader(filename); err != nil { + return fmt.Errorf("error reading caplet %s: %v", filename, err), nil + } else { + for line := range reader { + if line == "" || line[0] == '#' { + continue + } + cap.Code = append(cap.Code, line) + } + } + return nil, cap } } diff --git a/vendor/github.com/evilsocket/islazy/data/unsortedkv.go b/vendor/github.com/evilsocket/islazy/data/unsortedkv.go index ecf32faf..73de1fe7 100644 --- a/vendor/github.com/evilsocket/islazy/data/unsortedkv.go +++ b/vendor/github.com/evilsocket/islazy/data/unsortedkv.go @@ -3,6 +3,7 @@ package data import ( "bytes" "encoding/gob" + "encoding/json" "io/ioutil" "os" "sync" @@ -43,6 +44,32 @@ func NewUnsortedKV(fileName string, flushPolicy FlushPolicy) (*UnsortedKV, error return ukv, nil } +// NewDiskUnsortedKV returns an UnsortedKV that flushed data on disk +// every time it gets updated. +func NewDiskUnsortedKV(fileName string) (*UnsortedKV, error) { + return NewUnsortedKV(fileName, FlushOnEdit) +} + +// NewDiskUnsortedKVReader returns an UnsortedKV from disk as a reader +// but it doesn't flush any modifications on disk. +func NewDiskUnsortedKVReader(fileName string) (*UnsortedKV, error) { + return NewUnsortedKV(fileName, FlushNone) +} + +// NewMemUnsortedKV returns an UnsortedKV that only lives in +// memory and never persists on disk. +func NewMemUnsortedKV() (*UnsortedKV, error) { + return NewUnsortedKV("", FlushNone) +} + +// MarshalJSON is used to serialize the UnsortedKV data structure to +// JSON correctly. +func (u *UnsortedKV) MarshalJSON() ([]byte, error) { + u.Lock() + defer u.Unlock() + return json.Marshal(u.m) +} + // Has return true if name exists in the store. func (u *UnsortedKV) Has(name string) bool { u.Lock() @@ -132,3 +159,10 @@ func (u *UnsortedKV) Each(cb func(k, v string) bool) { } } } + +// Empty returns bool if the store is empty. +func (u *UnsortedKV) Empty() bool { + u.Lock() + defer u.Unlock() + return len(u.m) == 0 +} diff --git a/vendor/github.com/evilsocket/islazy/fs/misc.go b/vendor/github.com/evilsocket/islazy/fs/misc.go index cff81304..6fafd674 100644 --- a/vendor/github.com/evilsocket/islazy/fs/misc.go +++ b/vendor/github.com/evilsocket/islazy/fs/misc.go @@ -5,6 +5,11 @@ import ( "os/user" "path/filepath" "strings" + "sync" +) + +var ( + cwdLock = sync.Mutex{} ) // Expand will expand a path with ~ to a full path of the current user. @@ -23,3 +28,28 @@ func Exists(path string) bool { } return true } + +// Chdir changes the process current working directory to the specified +// one, executes the callback and then restores the original working directory. +func Chdir(path string, cb func() error) error { + cwdLock.Lock() + defer cwdLock.Unlock() + + cwd, err := os.Getwd() + if err != nil { + return err + } + // make sure that whatever happens we restore the original + // working directory of the process + defer func() { + if err := os.Chdir(cwd); err != nil { + panic(err) + } + }() + // change folder + if err := os.Chdir(path); err != nil { + return err + } + // run the callback once inside the folder + return cb() +}