Skip to content

Commit

Permalink
Merge pull request #8 from bestgopher/fix-some
Browse files Browse the repository at this point in the history
fix: check if usr is nil
  • Loading branch information
dwisiswant0 committed Dec 31, 2020
2 parents 2763677 + 4df8f6b commit 5233e76
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 12 deletions.
4 changes: 3 additions & 1 deletion internal/runner/constant.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package runner

import "time"

var (
opt *Options
uhost []string
Expand Down Expand Up @@ -41,5 +43,5 @@ Examples:
ssb -w wordlist.txt -t 1m -c 1000 root@localhost
`
timeout = "30s"
timeout = 30 * time.Second
)
6 changes: 3 additions & 3 deletions internal/runner/parser.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package runner

import (
"bufio"
"flag"
"fmt"
"io"
"os"
"time"

Expand All @@ -19,7 +19,7 @@ type Options struct {
timeout time.Duration
output string
port int
list *bufio.Scanner
list io.ReadCloser
file *os.File

user string
Expand All @@ -29,7 +29,7 @@ type Options struct {
// Parse arguments
func Parse() *Options {
opt = &Options{}
opt.timeout, _ = time.ParseDuration(timeout)
opt.timeout = timeout

flag.IntVar(&opt.port, "p", 22, "")
flag.IntVar(&opt.concurrent, "c", 100, "")
Expand Down
5 changes: 3 additions & 2 deletions internal/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
func New(opt *Options) {
opt.showInfo()

defer opt.Close()
job := make(chan string)
cur := opt.concurrent
swg := sizedwaitgroup.New(cur)
Expand All @@ -29,8 +30,8 @@ func New(opt *Options) {
}()
}

for opt.list.Scan() {
job <- opt.list.Text()
for scanner := opt.listScanner(); scanner.Scan(); {
job <- scanner.Text()
}

close(job)
Expand Down
21 changes: 18 additions & 3 deletions internal/runner/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ func (opt *Options) validate() error {
if len(uhost) < 2 {
usr, err := user.Current()
if err != nil {
usr.Username = "root"
opt.user = "root"
} else {
opt.user = usr.Username
}
opt.user = usr.Username
opt.host = uhost[0]
} else {
opt.user = uhost[0]
Expand All @@ -35,7 +36,7 @@ func (opt *Options) validate() error {
if err != nil {
return err
}
opt.list = bufio.NewScanner(f)
opt.list = f
} else {
return errors.New("No wordlist file provided")
}
Expand All @@ -49,3 +50,17 @@ func (opt *Options) validate() error {

return nil
}

func (opt *Options) listScanner() *bufio.Scanner {
return bufio.NewScanner(opt.list)
}

func (opt *Options) Close() {
if opt.list != nil {
_ = opt.list.Close()
}

if opt.file != nil {
_ = opt.file.Close()
}
}
5 changes: 2 additions & 3 deletions pkg/ssb/ssb.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package ssb

import (
"fmt"
"strconv"
"time"

"golang.org/x/crypto/ssh"
Expand All @@ -22,10 +21,10 @@ func New(username string, password string, timeout time.Duration) *ssh.ClientCon

// Connect for dialing to SSH server
func Connect(host string, port int, config *ssh.ClientConfig) (bool, error) {
_, err := ssh.Dial("tcp", fmt.Sprintf("%s:%s", host, strconv.Itoa(port)), config)
client, err := ssh.Dial("tcp", fmt.Sprintf("%s:%d", host, port), config)
if err != nil {
return false, err
}

_ = client.Close()
return true, nil
}

0 comments on commit 5233e76

Please sign in to comment.