Skip to content

Commit

Permalink
Merge pull request #23 from robgonnella/use-thread-safe-progress-bar
Browse files Browse the repository at this point in the history
Uses thread-safe progress bar
  • Loading branch information
robgonnella authored Jan 10, 2024
2 parents 97ecb18 + 10aa9c7 commit 0218516
Show file tree
Hide file tree
Showing 11 changed files with 109 additions and 128 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Lint

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version-file: 'go.mod'

- name: Lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.55.2
skip-pkg-cache: true
11 changes: 2 additions & 9 deletions .github/workflows/test-coverage.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
name: Test
name: Test and Update Coverage

on:
pull_request:
branches:
- main

jobs:
test:
test_coverage:
runs-on: ubuntu-latest
name: Test and update coverage badge
steps:
- name: Checkout
uses: actions/checkout@v4
Expand All @@ -25,12 +24,6 @@ jobs:
- name: Install dependencies
run: sudo apt update && sudo apt install -y make libpcap-dev

- name: Lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.55.2
skip-pkg-cache: true

- name: Run Test
run: |
make test
Expand Down
9 changes: 1 addition & 8 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ on:
- '!v*'

jobs:
build:

build_and_test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Expand All @@ -25,11 +24,5 @@ jobs:
- name: Build
run: make

- name: Lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.55.2
skip-pkg-cache: true

- name: Test
run: make test
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ test:
-v \
-coverprofile $(coverage_profile) \
-covermode=atomic \
-race \
./...

.PHONY: print-coverage
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# go-lanscan
![Coverage](https://img.shields.io/badge/Coverage-92.5%25-brightgreen)
![Coverage](https://img.shields.io/badge/Coverage-92.2%25-brightgreen)

A network cli and golang package that allows you to perform arp and syn
scanning on a local area network.
Expand Down
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/jedib0t/go-pretty v4.3.0+incompatible
github.com/klauspost/oui v0.0.0-20150225163751-35b4deb627f8
github.com/rs/zerolog v1.31.0
github.com/schollz/progressbar/v3 v3.14.1
github.com/spf13/cobra v1.8.0
github.com/stretchr/testify v1.8.4
github.com/thediveo/netdb v1.0.3
Expand All @@ -23,6 +24,7 @@ require (
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/oklog/ulid v1.3.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
Expand All @@ -32,7 +34,8 @@ require (
github.com/stretchr/objx v0.5.1 // indirect
go.mongodb.org/mongo-driver v1.13.1 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/term v0.16.0 // indirect
golang.org/x/tools v0.14.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
14 changes: 12 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ github.com/jackpal/gateway v1.0.13 h1:fJccMvawxx0k7S1q7Fy/SXFE0R3hMXkMuw8y9SofWA
github.com/jackpal/gateway v1.0.13/go.mod h1:6c8LjW+FVESFmwxaXySkt7fU98Yv806ADS3OY6Cvh2U=
github.com/jedib0t/go-pretty v4.3.0+incompatible h1:CGs8AVhEKg/n9YbUenWmNStRW2PHJzaeDodcfvRAbIo=
github.com/jedib0t/go-pretty v4.3.0+incompatible/go.mod h1:XemHduiw8R651AF9Pt4FwCTKeG3oo7hrHJAoznj9nag=
github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213/go.mod h1:vNUNkEQ1e29fT/6vq2aBdFsgNPmy8qMdSay1npru+Sw=
github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
github.com/klauspost/oui v0.0.0-20150225163751-35b4deb627f8 h1:8vTSNy6M0xiuAOmKh271gD8sr6mM+5RzXAiqIUL0KmE=
github.com/klauspost/oui v0.0.0-20150225163751-35b4deb627f8/go.mod h1:iaF36Fc2UmrXJ7AGL+fEZU9WWuZiB+4dp9tQtADeZ6A=
Expand All @@ -46,6 +47,8 @@ github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWE
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db h1:62I3jR2EmQ4l5rM/4FEfDWcRD+abF5XlKShorW5LRoQ=
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db/go.mod h1:l0dey0ia/Uv7NcFFVbCLtqEBQbrT4OCwCSKTEv6enCw=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc=
Expand All @@ -69,6 +72,8 @@ github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.31.0 h1:FcTR3NnLWW+NnTwwhFWiJSZr4ECLpqCm6QsEnyvbV4A=
github.com/rs/zerolog v1.31.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/schollz/progressbar/v3 v3.14.1 h1:VD+MJPCr4s3wdhTc7OEJ/Z3dAeBzJ7yKH/P4lC5yRTI=
github.com/schollz/progressbar/v3 v3.14.1/go.mod h1:Zc9xXneTzWXF81TGoqL71u0sBPjULtEHYtj/WVgVy8E=
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
Expand All @@ -78,6 +83,7 @@ github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSS
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/objx v0.5.1 h1:4VhoImhV/Bm0ToFkXFi8hXNXwpDRZ/ynw3amt82mzq0=
github.com/stretchr/objx v0.5.1/go.mod h1:/iHQpkQwBD6DLUmQ4pE+s1TXdob1mORJ4/UFdrifcy0=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
Expand Down Expand Up @@ -121,10 +127,14 @@ golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.14.0/go.mod h1:TySc+nGkYR6qt8km8wUhuFRTVSMIX3XPR58y2lC8vww=
golang.org/x/term v0.16.0 h1:m+B6fahuftsE9qjo0VWp2FW0mB3MTJvR0BaMQrq0pmE=
golang.org/x/term v0.16.0/go.mod h1:yn7UURbUtPyrVJPGPq404EukNFxcm/foM+bV/bfcDsY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
Expand Down
89 changes: 37 additions & 52 deletions internal/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import (
"sync"
"time"

"github.com/jedib0t/go-pretty/progress"
"github.com/jedib0t/go-pretty/table"
"github.com/robgonnella/go-lanscan/internal/logger"
"github.com/robgonnella/go-lanscan/internal/util"
"github.com/robgonnella/go-lanscan/pkg/scanner"
"github.com/rs/zerolog"
"github.com/schollz/progressbar/v3"
)

// DeviceResult represents a discovered network device
Expand Down Expand Up @@ -70,9 +70,8 @@ type Core struct {
outFile string
portLen int
results *Results
pw progress.Writer
arpTracker *progress.Tracker
synTracker *progress.Tracker
arpProgress *progressbar.ProgressBar
synProgress *progressbar.ProgressBar
requestNotifier chan *scanner.Request
errorChan chan error
scanner scanner.Scanner
Expand All @@ -99,15 +98,10 @@ func (c *Core) Initialize(
printJSON bool,
outFile string,
) {
pw := progressWriter()

results := &Results{
Devices: []*DeviceResult{},
}

arpTracker := &progress.Tracker{Message: "starting arp scan"}
arpTracker.Total = int64(targetLen)

if noProgress {
logger.SetGlobalLevel(zerolog.Disabled)
} else {
Expand All @@ -118,9 +112,8 @@ func (c *Core) Initialize(
c.results = results
c.errorChan = make(chan error)
c.portLen = portLen
c.pw = pw
c.arpTracker = arpTracker
c.synTracker = &progress.Tracker{Message: "starting syn scan"}
c.arpProgress = newProgressBar(targetLen, "performing arp scan")
c.synProgress = newProgressBar(1, "performing syn scan")
c.noProgress = noProgress
c.arpOnly = arpOnly
c.printJSON = printJSON
Expand All @@ -132,9 +125,7 @@ func (c *Core) Run() error {
start := time.Now()

if !c.noProgress {
c.pw.AppendTracker(c.arpTracker)
go c.monitorRequestNotifications()
go c.pw.Render()
}

// run in go routine so we can process in results in parallel
Expand Down Expand Up @@ -243,10 +234,8 @@ func (c *Core) processArpDone() {
c.printArpResults()

if !c.noProgress && !c.arpOnly && len(c.results.Devices) > 0 {
c.synTracker.Total = int64(
len(c.results.Devices) * c.portLen,
)
c.pw.AppendTracker(c.synTracker)
size := len(c.results.Devices) * c.portLen
c.synProgress.ChangeMax(size)
}

if !c.arpOnly && len(c.results.Devices) == 0 {
Expand Down Expand Up @@ -361,55 +350,51 @@ func (c *Core) monitorRequestNotifications() {
for r := range c.requestNotifier {
switch r.Type {
case scanner.ArpRequest:
c.arpTracker.Increment(1)
// nolint:errcheck
c.arpProgress.Add(1)

message := fmt.Sprintf("arp - scanning %s", r.IP)

if c.arpTracker.IsDone() {
message = "arp - scan complete"
// delay to print line after message is updated
time.AfterFunc(time.Millisecond*100, func() {
c.log.Info().Msg("compiling arp results...")
})
}
c.arpProgress.Describe("\033[36m" + message + "\033[0m")

c.arpTracker.Message = message
if c.arpProgress.IsFinished() {
// nolint:errcheck
c.arpProgress.Clear()
c.log.Info().Msg("compiling arp results...")
}
case scanner.SynRequest:
c.synTracker.Increment(1)
// nolint:errcheck
c.synProgress.Add(1)

message := fmt.Sprintf(
"syn - scanning port %d on %s",
r.Port,
r.IP,
)

if c.synTracker.IsDone() {
message = "syn - scan complete"
// delay to print line after message is updated
time.AfterFunc(time.Millisecond*100, func() {
c.log.Info().Msg("compiling syn results...")
})
}
c.synProgress.Describe("\033[36m" + message + "\033[0m")

c.synTracker.Message = message
if c.synProgress.IsFinished() {
// nolint:errcheck
c.synProgress.Clear()
c.log.Info().Msg("compiling syn results...")
}
}
}
}

// helpers
func progressWriter() progress.Writer {
pw := progress.NewWriter()
pw.SetOutputWriter(os.Stdout)
pw.SetAutoStop(false)
pw.SetTrackerLength(25)
pw.SetMessageWidth(47)
pw.SetNumTrackersExpected(1)
pw.SetSortBy(progress.SortByPercentDsc)
pw.SetStyle(progress.StyleDefault)
pw.SetTrackerPosition(progress.PositionRight)
pw.SetUpdateFrequency(time.Millisecond * 100)
pw.Style().Colors = progress.StyleColorsExample
pw.Style().Options.PercentFormat = "%4.3f%%"

return pw
func newProgressBar(size int, msg string) *progressbar.ProgressBar {
return progressbar.NewOptions(size,
progressbar.OptionUseANSICodes(true),
progressbar.OptionEnableColorCodes(true),
progressbar.OptionShowBytes(true),
progressbar.OptionSetWidth(25),
progressbar.OptionSetDescription("\033[36m"+msg+"\033[0m"),
progressbar.OptionSetTheme(progressbar.Theme{
Saucer: "[green]=[reset]",
SaucerHead: "[green]>[reset]",
SaucerPadding: " ",
BarStart: "[",
BarEnd: "]",
}))
}
Loading

0 comments on commit 0218516

Please sign in to comment.