ffuff/pkg/input/command.go
M. Ángel Jimeno 19937c4929
pkg: handle gosimple linter findings (#322)
This change is an attempt to handle gosimple linter finfings in order to
make the code easier to follow. It includes the following changes:

- use strings.Contains instead of strings.Index != -1
- use time.Since which is the standard library helper. See https://github.com/golang/go/blob/go1.15.2/src/time/time.go#L866-L867
- remove unneeded return statements at the end of methods
- preallocate maps when their capacity is known
- avoid underscoring values when they can be omitted
- avoid fmt.Sprintf() calls when the only argument is already a string

Signed-off-by: Miguel Ángel Jimeno <miguelangel4b@gmail.com>
2020-10-03 10:45:07 +03:00

70 lines
1.5 KiB
Go

package input
import (
"bytes"
"os"
"os/exec"
"strconv"
"github.com/ffuf/ffuf/pkg/ffuf"
)
type CommandInput struct {
config *ffuf.Config
count int
keyword string
command string
}
func NewCommandInput(keyword string, value string, conf *ffuf.Config) (*CommandInput, error) {
var cmd CommandInput
cmd.keyword = keyword
cmd.config = conf
cmd.count = 0
cmd.command = value
return &cmd, nil
}
//Keyword returns the keyword assigned to this InternalInputProvider
func (c *CommandInput) Keyword() string {
return c.keyword
}
//Position will return the current position in the input list
func (c *CommandInput) Position() int {
return c.count
}
//ResetPosition will reset the current position of the InternalInputProvider
func (c *CommandInput) ResetPosition() {
c.count = 0
}
//IncrementPosition increments the current position in the inputprovider
func (c *CommandInput) IncrementPosition() {
c.count += 1
}
//Next will increment the cursor position, and return a boolean telling if there's iterations left
func (c *CommandInput) Next() bool {
return c.count < c.config.InputNum
}
//Value returns the input from command stdoutput
func (c *CommandInput) Value() []byte {
var stdout bytes.Buffer
os.Setenv("FFUF_NUM", strconv.Itoa(c.count))
cmd := exec.Command(SHELL_CMD, SHELL_ARG, c.command)
cmd.Stdout = &stdout
err := cmd.Run()
if err != nil {
return []byte("")
}
return stdout.Bytes()
}
//Total returns the size of wordlist
func (c *CommandInput) Total() int {
return c.config.InputNum
}