Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add self update option #163

Merged
merged 1 commit into from
Mar 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ MISCELLANEOUS:
-si, -shuffle-ip Shuffle Input IPs in random order
-sp, -shuffle-port string Shuffle Input IP:Port in random order

UPDATE:
-up, -update update mapcidr to latest version
-duc, -disable-update-check disable automatic mapcidr update check

OUTPUT:
-verbose Verbose mode
-o, -output string File to write output to
Expand Down
31 changes: 29 additions & 2 deletions cmd/mapcidr/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
asn "github.com/projectdiscovery/mapcidr/asn"
fileutil "github.com/projectdiscovery/utils/file"
sliceutil "github.com/projectdiscovery/utils/slice"
updateutils "github.com/projectdiscovery/utils/update"
)

// Options contains cli options
Expand Down Expand Up @@ -49,6 +50,7 @@ type Options struct {
IPFormats goflags.StringSlice
ZeroPadNumberOfZeroes int
ZeroPadPermute bool
DisableUpdateCheck bool
}

const banner = `
Expand All @@ -60,7 +62,7 @@ const banner = `
`

// Version is the current version of mapcidr
const Version = `v1.1.0`
const version = `v1.1.0`

// showBanner is used to show the banner to the user
func showBanner() {
Expand All @@ -71,6 +73,14 @@ func showBanner() {
gologger.Print().Msgf("Developers assume no liability and are not responsible for any misuse or damage.\n")
}

// GetUpdateCallback returns a callback function that updates mapcidr
func GetUpdateCallback() func() {
return func() {
showBanner()
updateutils.GetUpdateToolCallback("mapcidr", version)()
}
}

// ParseOptions parses the command line options for application
func ParseOptions() *Options {
options := &Options{}
Expand Down Expand Up @@ -114,6 +124,12 @@ func ParseOptions() *Options {
flagSet.StringVarP(&options.ShufflePorts, "shuffle-port", "sp", "", "Shuffle Input IP:Port in random order"),
)

//Update
flagSet.CreateGroup("update", "Update",
flagSet.CallbackVarP(GetUpdateCallback(), "update", "up", "update mapcidr to latest version"),
flagSet.BoolVarP(&options.DisableUpdateCheck, "disable-update-check", "duc", false, "disable automatic mapcidr update check"),
)

// Output
flagSet.CreateGroup("output", "Output",
flagSet.BoolVar(&options.Verbose, "verbose", false, "Verbose mode"),
Expand All @@ -132,10 +148,21 @@ func ParseOptions() *Options {
showBanner()

if options.Version {
gologger.Info().Msgf("Current Version: %s\n", Version)
gologger.Info().Msgf("Current Version: %s\n", version)
os.Exit(0)
}

if !options.DisableUpdateCheck {
latestVersion, err := updateutils.GetVersionCheckCallback("mapcidr")()
if err != nil {
if options.Verbose {
gologger.Error().Msgf("mapcidr version check failed: %v", err.Error())
}
} else {
gologger.Info().Msgf("Current mapcidr version %v %v", version, updateutils.GetVersionDescription(version, latestVersion))
}
}

// enable shuffling if ports are specified
if len(options.ShufflePorts) > 0 {
options.Shuffle = true
Expand Down
51 changes: 38 additions & 13 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,64 @@ require (
github.com/pkg/errors v0.9.1
github.com/projectdiscovery/asnmap v1.0.0
github.com/projectdiscovery/blackrock v0.0.0-20221025011524-9e4efe804fb4
github.com/projectdiscovery/goflags v0.1.6
github.com/projectdiscovery/goflags v0.1.8
github.com/projectdiscovery/gologger v1.1.8
github.com/projectdiscovery/ipranger v0.0.5
github.com/projectdiscovery/utils v0.0.7
github.com/projectdiscovery/utils v0.0.15-0.20230312150239-2528d5ff811d
github.com/stretchr/testify v1.8.1
golang.org/x/exp v0.0.0-20221230185412-738e83a70c30
golang.org/x/exp v0.0.0-20230315142452-642cacee5cc0
)

require (
aead.dev/minisign v0.2.0 // indirect
github.com/DataDog/zstd v1.5.2 // indirect
github.com/Masterminds/semver/v3 v3.2.0 // indirect
github.com/Mzack9999/go-http-digest-auth-client v0.6.1-0.20220414142836-eb8883508809 // indirect
github.com/VividCortex/ewma v1.2.0 // indirect
github.com/akrylysov/pogreb v0.10.1 // indirect
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d // indirect
github.com/alecthomas/chroma v0.10.0 // indirect
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/aymerick/douceur v0.2.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/charmbracelet/glamour v0.6.0 // indirect
github.com/cheggaaa/pb/v3 v3.1.2 // indirect
github.com/cnf/structhash v0.0.0-20201127153200-e1b16c1ebc08 // indirect
github.com/cockroachdb/errors v1.9.0 // indirect
github.com/cockroachdb/logtags v0.0.0-20211118104740-dabe8e521a4f // indirect
github.com/cockroachdb/pebble v0.0.0-20221229212011-811a8c0e741b // indirect
github.com/cockroachdb/redact v1.1.3 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dlclark/regexp2 v1.8.1 // indirect
github.com/dsnet/compress v0.0.1 // indirect
github.com/fatih/color v1.14.1 // indirect
github.com/getsentry/sentry-go v0.16.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/go-github/v30 v30.1.0 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/gorilla/css v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.15.13 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/mholt/archiver v3.1.1+incompatible // indirect
github.com/microcosm-cc/bluemonday v1.0.21 // indirect
github.com/miekg/dns v1.1.50 // indirect
github.com/microcosm-cc/bluemonday v1.0.23 // indirect
github.com/miekg/dns v1.1.52 // indirect
github.com/minio/selfupdate v0.6.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/muesli/reflow v0.3.0 // indirect
github.com/muesli/termenv v0.15.1 // indirect
github.com/nwaples/rardecode v1.1.0 // indirect
github.com/olekukonko/tablewriter v0.0.5 // indirect
github.com/pierrec/lz4 v2.6.0+incompatible // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/projectdiscovery/hmap v0.0.7 // indirect
Expand All @@ -58,8 +77,9 @@ require (
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/common v0.39.0 // indirect
github.com/prometheus/procfs v0.9.0 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/rogpeppe/go-internal v1.9.0 // indirect
github.com/saintfish/chardet v0.0.0-20120816061221-3af4cd4741ca // indirect
github.com/saintfish/chardet v0.0.0-20230101081208-5e3ef4b5456d // indirect
github.com/syndtr/goleveldb v1.0.0 // indirect
github.com/tidwall/btree v1.4.3 // indirect
github.com/tidwall/buntdb v1.2.10 // indirect
Expand All @@ -69,17 +89,22 @@ require (
github.com/tidwall/pretty v1.2.0 // indirect
github.com/tidwall/rtred v0.1.2 // indirect
github.com/tidwall/tinyqueue v0.1.1 // indirect
github.com/ulikunitz/xz v0.5.7 // indirect
github.com/ulikunitz/xz v0.5.8 // indirect
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect
github.com/yl2chen/cidranger v1.0.2 // indirect
github.com/yuin/goldmark v1.5.4 // indirect
github.com/yuin/goldmark-emoji v1.0.1 // indirect
go.etcd.io/bbolt v1.3.6 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.8.0 // indirect
golang.org/x/mod v0.6.0 // indirect
golang.org/x/net v0.5.0 // indirect
golang.org/x/sys v0.4.0 // indirect
golang.org/x/text v0.6.0 // indirect
golang.org/x/tools v0.2.0 // indirect
golang.org/x/crypto v0.7.0 // indirect
golang.org/x/mod v0.9.0 // indirect
golang.org/x/net v0.8.0 // indirect
golang.org/x/oauth2 v0.5.0 // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/text v0.8.0 // indirect
golang.org/x/tools v0.7.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/djherbis/times.v1 v1.3.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
Loading