From f786b2acba15e9d760151c1486f89e28abacc5ce Mon Sep 17 00:00:00 2001 From: Amine Benseddik Date: Tue, 25 Jun 2024 11:48:49 +0200 Subject: [PATCH] update to latest go version --- .gitignore | 3 +- .golangci.yml | 86 ++++++++++++++++++++++++++++++++++ Dockerfile | 24 ++++++++-- Makefile | 38 +++++++++++---- README.md | 9 ++-- cmd/root.go | 29 ++++++------ cmd/version.go | 21 +++++---- go.mod | 38 +++++++++++---- go.sum | 100 ++++++++++++++++++++++++++++++++++------ main.go | 13 ++++-- pkg/config/config.go | 4 +- pkg/queue/dispatcher.go | 1 - pkg/queue/worker.go | 1 - pkg/scanner/scanner.go | 49 +++++++++++--------- pkg/utils/utils.go | 7 +-- 15 files changed, 325 insertions(+), 98 deletions(-) create mode 100644 .golangci.yml diff --git a/.gitignore b/.gitignore index eb2f930..5e4dbd7 100644 --- a/.gitignore +++ b/.gitignore @@ -27,4 +27,5 @@ _testmain.go vendor -reverse-scan \ No newline at end of file +reverse-scan +bin/* diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 0000000..0baf379 --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,86 @@ +run: + concurrency: 4 + issues-exit-code: 1 + tests: true + +output: + formats: + - format: colored-line-number + print-issued-lines: true + print-linter-name: true + +linters: + enable: + - errcheck + - gosimple + - govet + - ineffassign + - staticcheck + - typecheck + - unused + - asciicheck + - bodyclose + - dogsled + - durationcheck + - errorlint + - exhaustive + - exportloopref + - forcetypeassert + - gci + - gochecknoglobals + - gochecknoinits + - goconst + - gocritic + - gocyclo + - gofmt + - gofumpt + - goimports + - gomodguard + - goprintffuncname + - gosec + - importas + - makezero + - misspell + - nakedret + - nestif + - nilerr + - noctx + - nolintlint + - prealloc + - predeclared + - revive + - rowserrcheck + - sqlclosecheck + - thelper + - tparallel + - unconvert + - unparam + - wastedassign + - whitespace + +linters-settings: + gocyclo: + min-complexity: 35 + + revive: + rules: + - name: exported + disabled: true + +issues: + exclude-use-default: false + exclude-rules: + - text: "SA1029" + linters: + - staticcheck + + # Exclude some linters from running on test files + - path: _test\.go + linters: + # bodyclose reports some false-positives when using a test request recorder + - bodyclose + # It's overkill to use `NewRequestWithContext` in tests + - noctx + - goerr113 + # It's expected to have repeated text appearances in unit tests, and overkill to define constants for those + - goconst diff --git a/Dockerfile b/Dockerfile index 06d2131..8db43f9 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,19 @@ -FROM golang:1.12 -ADD . /app -WORKDIR /app -RUN make install -ENTRYPOINT [ "/go/bin/reverse-scan" ] \ No newline at end of file +FROM golang:1.22-alpine AS build + +RUN apk add --no-cache bash git make build-base + +WORKDIR /build + +COPY go.mod go.sum ./ + +RUN go mod download + +COPY . . + +RUN make bin/reverse-scan + +FROM alpine:3 AS runtime + +COPY --from=build /build/bin/reverse-scan /usr/local/bin/reverse-scan + +ENTRYPOINT [ "/usr/local/bin/reverse-scan" ] diff --git a/Makefile b/Makefile index 8baa4aa..917f6ad 100644 --- a/Makefile +++ b/Makefile @@ -1,8 +1,30 @@ -GO111MODULE=on -GO ?= go -GOTEST = go test -v -bench\=. -WORKDIR ?= $(shell pwd) - -.PHONY: install -install: - $(GO) install -ldflags="-s -w" -tags netgo +SHELL := /bin/bash + +BUILD_FILES = $(shell go list -f '{{range .GoFiles}}{{$$.Dir}}/{{.}}\ +{{end}}' ./...) + +VERSION ?= $(shell git describe --tags 2>/dev/null || git rev-parse --short HEAD) +DATE_FMT = +%Y-%m-%d +ifdef SOURCE_DATE_EPOCH + BUILD_DATE ?= $(shell date -u -d "@$(SOURCE_DATE_EPOCH)" "$(DATE_FMT)" 2>/dev/null || date -u -r "$(SOURCE_DATE_EPOCH)" "$(DATE_FMT)" 2>/dev/null || date -u "$(DATE_FMT)") +else + BUILD_DATE ?= $(shell date "$(DATE_FMT)") +endif + +GO_LDFLAGS := -s -w +GO_LDFLAGS := -X go.pixelfactory.io/pkg/version.REVISION=$(VERSION) $(GO_LDFLAGS) +GO_LDFLAGS := -X go.pixelfactory.io/pkg/version.BUILDDATE=$(BUILD_DATE) $(GO_LDFLAGS) +bin/reverse-scan: $(BUILD_FILES) + @go build -trimpath -ldflags "$(GO_LDFLAGS)" -o "$@" + +gofmt: + @diff -u <(echo -n) <(gofmt -d -s .) +.PHONY: gofmt + +lint: + @golangci-lint run ./... +.PHONY: lint + +vet: + @go vet ./... +.PHONY: vet diff --git a/README.md b/README.md index bc0ba32..89ea131 100644 --- a/README.md +++ b/README.md @@ -3,11 +3,12 @@ Perform reverse DNS lookups on huge network ranges This utility uses the "Dispatcher/Workers" pattern discribed here : -- https://gobyexample.com/worker-pools -- https://nesv.github.io/golang/2014/02/25/worker-queues-in-go.html -- http://marcio.io/2015/07/handling-1-million-requests-per-minute-with-golang/ -# Getting Started +- +- +- + +## Getting Started Download the binary : diff --git a/cmd/root.go b/cmd/root.go index 54df194..3c15e8d 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -9,33 +9,36 @@ import ( "github.com/spf13/cobra" ) -var rootCmd = cobra.Command{ - Use: "reverse-scan", - Short: "Reverse Scan", - Run: run, -} - -var version string - // NewRootCmd will setup and return the root command -func NewRootCmd(v string) *cobra.Command { - // Set Version and ProgramName - version = v +func NewRootCmd() *cobra.Command { + rootCmd := &cobra.Command{ + Use: "reverse-scan", + Short: "Reverse Scan", + Run: run, + } rootCmd.PersistentFlags().StringP("start", "s", "", "ip range start") rootCmd.PersistentFlags().StringP("end", "e", "", "ip range end") rootCmd.PersistentFlags().StringP("output", "o", "", "csv output file") rootCmd.PersistentFlags().IntP("workers", "w", 8, "number of workers") - return &rootCmd + versionCmd := NewVersionCmd() + rootCmd.AddCommand(versionCmd) + + return rootCmd } func run(cmd *cobra.Command, args []string) { c, err := config.LoadConfig(cmd) + if err != nil { + log.Fatal(err) + return + } + + err = scanner.Start(c) if err != nil { log.Fatal(err) } - scanner.Start(c) os.Exit(0) } diff --git a/cmd/version.go b/cmd/version.go index b27061b..f539a28 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -4,16 +4,19 @@ import ( "fmt" "github.com/spf13/cobra" + "go.pixelfactory.io/pkg/version" ) -func init() { - rootCmd.AddCommand(versionCmd) -} +// NewVersionCmd will setup and return the version command +func NewVersionCmd() *cobra.Command { + versionCmd := &cobra.Command{ + Use: "version", + Short: "Print the version number and build date", + Run: func(cmd *cobra.Command, args []string) { + fmt.Printf("version: %s\n", version.REVISION) + fmt.Printf("build-date: %s\n", version.BUILDDATE) + }, + } -var versionCmd = &cobra.Command{ - Use: "version", - Short: "Print the version number", - Run: func(cmd *cobra.Command, args []string) { - fmt.Printf("%s\n", version) - }, + return versionCmd } diff --git a/go.mod b/go.mod index 398a357..e4eedae 100644 --- a/go.mod +++ b/go.mod @@ -1,13 +1,35 @@ module github.com/amine7536/reverse-scan -go 1.12 +go 1.22 require ( - github.com/gosuri/uilive v0.0.0-20170323041506-ac356e6e42cd // indirect - github.com/gosuri/uiprogress v0.0.0-20170224063937-d0567a9d84a1 - github.com/inconshreveable/mousetrap v1.0.0 // indirect - github.com/mattn/go-isatty v0.0.2 // indirect - github.com/spf13/cobra v0.0.0-20170624150100-4d647c8944eb - github.com/spf13/pflag v1.0.0 // indirect - golang.org/x/sys v0.0.0-20170627160855-90796e5a05ce // indirect + github.com/gosuri/uiprogress v0.0.1 + github.com/spf13/cobra v1.8.1 + github.com/spf13/viper v1.19.0 + go.pixelfactory.io/pkg/version v0.1.0 +) + +require ( + github.com/fsnotify/fsnotify v1.7.0 // indirect + github.com/gosuri/uilive v0.0.4 // indirect + github.com/hashicorp/hcl v1.0.0 // indirect + github.com/inconshreveable/mousetrap v1.1.0 // indirect + github.com/magiconair/properties v1.8.7 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect + github.com/mitchellh/mapstructure v1.5.0 // indirect + github.com/pelletier/go-toml/v2 v2.2.2 // indirect + github.com/sagikazarmark/locafero v0.4.0 // indirect + github.com/sagikazarmark/slog-shim v0.1.0 // indirect + github.com/sourcegraph/conc v0.3.0 // indirect + github.com/spf13/afero v1.11.0 // indirect + github.com/spf13/cast v1.6.0 // indirect + github.com/spf13/pflag v1.0.5 // indirect + github.com/subosito/gotenv v1.6.0 // indirect + go.uber.org/atomic v1.9.0 // indirect + go.uber.org/multierr v1.9.0 // indirect + golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect + golang.org/x/sys v0.21.0 // indirect + golang.org/x/text v0.14.0 // indirect + gopkg.in/ini.v1 v1.67.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 00c0272..6547f26 100644 --- a/go.sum +++ b/go.sum @@ -1,14 +1,86 @@ -github.com/gosuri/uilive v0.0.0-20170323041506-ac356e6e42cd h1:1e+0Z+T4t1mKL5xxvxXh5FkjuiToQGKreCobLu7lR3Y= -github.com/gosuri/uilive v0.0.0-20170323041506-ac356e6e42cd/go.mod h1:qkLSc0A5EXSP6B04TrN4oQoxqFI7A8XvoXSlJi8cwk8= -github.com/gosuri/uiprogress v0.0.0-20170224063937-d0567a9d84a1 h1:4iPLwzjiWGBQnYdtKbg/JNlGlEEvklrrMdjypdA1LKQ= -github.com/gosuri/uiprogress v0.0.0-20170224063937-d0567a9d84a1/go.mod h1:C1RTYn4Sc7iEyf6j8ft5dyoZ4212h8G1ol9QQluh5+0= -github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= -github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= -github.com/mattn/go-isatty v0.0.2 h1:F+DnWktyadxnOrohKLNUC9/GjFii5RJgY4GFG6ilggw= -github.com/mattn/go-isatty v0.0.2/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= -github.com/spf13/cobra v0.0.0-20170624150100-4d647c8944eb h1:u90uxwFTjDAD6g/U2AsUr4DUu/lDLknjDRAqhuM2L6U= -github.com/spf13/cobra v0.0.0-20170624150100-4d647c8944eb/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ= -github.com/spf13/pflag v1.0.0 h1:oaPbdDe/x0UncahuwiPxW1GYJyilRAdsPnq3e1yaPcI= -github.com/spf13/pflag v1.0.0/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= -golang.org/x/sys v0.0.0-20170627160855-90796e5a05ce h1:kBeK9Kz3rv5dnIqoEcwmvjvextKbN9+Fe/3IjAvh858= -golang.org/x/sys v0.0.0-20170627160855-90796e5a05ce/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= +github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8= +github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0= +github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA= +github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM= +github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= +github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/gosuri/uilive v0.0.4 h1:hUEBpQDj8D8jXgtCdBu7sWsy5sbW/5GhuO8KBwJ2jyY= +github.com/gosuri/uilive v0.0.4/go.mod h1:V/epo5LjjlDE5RJUcqx8dbw+zc93y5Ya3yg8tfZ74VI= +github.com/gosuri/uiprogress v0.0.1 h1:0kpv/XY/qTmFWl/SkaJykZXrBBzwwadmW8fRb7RJSxw= +github.com/gosuri/uiprogress v0.0.1/go.mod h1:C1RTYn4Sc7iEyf6j8ft5dyoZ4212h8G1ol9QQluh5+0= +github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= +github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= +github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= +github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= +github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= +github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY= +github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0= +github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +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/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM= +github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= +github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8= +github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= +github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6keLGt6kNQ= +github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4= +github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE= +github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ= +github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo= +github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0= +github.com/spf13/afero v1.11.0 h1:WJQKhtpdm3v2IzqG8VMqrr6Rf3UYpEF239Jy9wNepM8= +github.com/spf13/afero v1.11.0/go.mod h1:GH9Y3pIexgf1MTIWtNGyogA5MwRIDXGUr+hbWNoBjkY= +github.com/spf13/cast v1.6.0 h1:GEiTHELF+vaR5dhz3VqZfFSzZjYbgeKDpBxQVS4GYJ0= +github.com/spf13/cast v1.6.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo= +github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM= +github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +github.com/spf13/viper v1.19.0 h1:RWq5SEjt8o25SROyN3z2OrDB9l7RPd3lwTWU8EcEdcI= +github.com/spf13/viper v1.19.0/go.mod h1:GQUN9bilAbhU/jgc1bKs99f/suXKeUMct8Adx5+Ntkg= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +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= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8= +github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU= +go.pixelfactory.io/pkg/version v0.1.0 h1:HK+uvMADE1bZ/Rdwet4o1YF7F/c5ZdEwfBnZWyrpa9U= +go.pixelfactory.io/pkg/version v0.1.0/go.mod h1:wc4uuUNsbqgcfL0amMAZfxsRZHMqTQGm7f4wkYo1Fs4= +go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE= +go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= +go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI= +go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ= +golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g= +golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws= +golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= +golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA= +gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go index 95958cd..b928a51 100644 --- a/main.go +++ b/main.go @@ -4,16 +4,19 @@ import ( "log" "github.com/amine7536/reverse-scan/cmd" + "github.com/spf13/cobra" + "github.com/spf13/viper" + "go.pixelfactory.io/pkg/version" ) -const ( - // Version : app version - Version = "v0.2.3" -) +func initConfig() { + viper.Set("revision", version.REVISION) +} func main() { + cobra.OnInitialize(initConfig) - if err := cmd.NewRootCmd(Version).Execute(); err != nil { + if err := cmd.NewRootCmd().Execute(); err != nil { log.Fatal(err) } } diff --git a/pkg/config/config.go b/pkg/config/config.go index 6f1f7cf..40f2172 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -5,7 +5,6 @@ import ( "net" "github.com/amine7536/reverse-scan/pkg/utils" - "github.com/spf13/cobra" ) @@ -20,7 +19,6 @@ type Config struct { // LoadConfig loads the config from a file if specified, otherwise from the environment func LoadConfig(cmd *cobra.Command) (*Config, error) { - start, err := cmd.Flags().GetString("start") if err != nil { return nil, err @@ -81,7 +79,7 @@ func validateConfig(start string, end string, output string, workers int) (*Conf } if !utils.IsValidPath(output) { - return nil, fmt.Errorf("Invalid output file : %s", output) + return nil, fmt.Errorf("Invalid output file: %s", output) } config := Config{ diff --git a/pkg/queue/dispatcher.go b/pkg/queue/dispatcher.go index 6a516a5..be79879 100644 --- a/pkg/queue/dispatcher.go +++ b/pkg/queue/dispatcher.go @@ -12,7 +12,6 @@ type Dispatcher struct { // NewDispatcher returns a new dispatcher func NewDispatcher(maxWorkers int, results chan Job) *Dispatcher { - return &Dispatcher{ MaxWorkers: maxWorkers, WorkerPool: make(chan chan Job, maxWorkers), diff --git a/pkg/queue/worker.go b/pkg/queue/worker.go index 484894f..49e22e4 100644 --- a/pkg/queue/worker.go +++ b/pkg/queue/worker.go @@ -46,7 +46,6 @@ func (w Worker) Start() { // Stop working // log.Printf("Stopping WorkerID=%v", w.ID) return - } } }() diff --git a/pkg/scanner/scanner.go b/pkg/scanner/scanner.go index 7528531..2cb2a65 100644 --- a/pkg/scanner/scanner.go +++ b/pkg/scanner/scanner.go @@ -5,30 +5,35 @@ import ( "log" "os" - "github.com/gosuri/uiprogress" - "github.com/amine7536/reverse-scan/pkg/config" "github.com/amine7536/reverse-scan/pkg/queue" "github.com/amine7536/reverse-scan/pkg/utils" + "github.com/gosuri/uiprogress" ) // Start scanner -func Start(c *config.Config) { - +func Start(c *config.Config) error { + var err error hosts, _ := utils.GetHosts(c.CIDR) results := make(chan queue.Job) defer close(results) log.Printf("Resolving from %v to %v", c.StartIP, c.EndIP) - log.Printf("Caluculated CIDR is %s", c.CIDR) + log.Printf("Calculated CIDR is %s", c.CIDR) log.Printf("Number of IPs to scan: %v", len(hosts)) log.Printf("Starting %v Workers", c.WORKERS) file, err := os.Create(c.CSV) if err != nil { - log.Fatal(err) + log.Println(err) + return err } - defer file.Close() + + defer func() { + if err := file.Close(); err != nil { + log.Printf("Failed to close file: %v", err) + } + }() writer := csv.NewWriter(file) defer writer.Flush() @@ -42,7 +47,6 @@ func Start(c *config.Config) { dispatch := queue.NewDispatcher(c.WORKERS, results) dispatch.Run() defer dispatch.Stop() - // time.Sleep(time.Second * 10) // Send Jobs to Dispatch for _, ip := range hosts { @@ -52,21 +56,20 @@ func Start(c *config.Config) { // Wait for results r := 0 -resultLoop: - for { - select { - case job := <-results: - err := writer.Write(append([]string{job.IP}, job.Names...)) - if err != nil { - log.Fatal(err) - } - writer.Flush() - bar.Incr() - r++ - if r == len(hosts) { - // We got all jobs back, we stop - break resultLoop - } + for job := range results { + err := writer.Write(append([]string{job.IP}, job.Names...)) + if err != nil { + log.Println(err) + return err + } + writer.Flush() + bar.Incr() + r++ + if r == len(hosts) { + // We got all jobs back, we stop + break } } + + return nil } diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index e415ed4..54c7b7b 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -2,7 +2,6 @@ package utils import ( "fmt" - "io/ioutil" "net" "os" ) @@ -70,8 +69,10 @@ func IsValidPath(fp string) bool { // Attempt to create it var d []byte - if err := ioutil.WriteFile(fp, d, 0644); err == nil { - os.Remove(fp) // And delete it + if err := os.WriteFile(fp, d, 0o600); err == nil { + if err := os.Remove(fp); err != nil { + return false + } return true }