Improving cli parsing for Allow/Deny option

This commit is contained in:
mzack 2021-08-22 21:07:23 +02:00
parent 4f7f352677
commit 7a59d6702b
6 changed files with 39 additions and 6 deletions

View File

@ -1,5 +1,7 @@
package customlist
import "github.com/projectdiscovery/httpx/common/fileutil"
// CustomList for fastdialer
type CustomList []string
@ -10,6 +12,7 @@ func (c *CustomList) String() string {
// Set a new global header
func (c *CustomList) Set(value string) error {
*c = append(*c, value)
values := fileutil.LoadCidrsFromSliceOrFile(value)
*c = append(*c, values...)
return nil
}

View File

@ -3,9 +3,14 @@ package fileutil
import (
"bufio"
"errors"
"io/ioutil"
"net"
"os"
"path/filepath"
"regexp"
"github.com/projectdiscovery/fileutil"
"github.com/projectdiscovery/httpx/common/stringz"
)
// FileExists checks if a file exists and is not a directory
@ -70,3 +75,21 @@ func FileNameIsGlob(pattern string) bool {
_, err := regexp.Compile(pattern)
return err == nil
}
func LoadCidrsFromSliceOrFile(option string) (networkList []string) {
items := stringz.SplitByCharAndTrimSpace(option, ",")
for _, item := range items {
// ip
if net.ParseIP(item) != nil {
networkList = append(networkList, item)
} else if _, _, err := net.ParseCIDR(item); err == nil {
networkList = append(networkList, item)
} else if fileutil.FileExists(item) {
if filedata, err := ioutil.ReadFile(item); err == nil && len(filedata) > 0 {
networkList = append(networkList, LoadCidrsFromSliceOrFile(string(filedata))...)
}
}
}
return networkList
}

1
go.mod
View File

@ -19,6 +19,7 @@ require (
github.com/projectdiscovery/cryptoutil v0.0.0-20210805184155-b5d2512f9345
github.com/projectdiscovery/fastdialer v0.0.13-0.20210815100514-360f851a5b80
github.com/projectdiscovery/fdmax v0.0.3
github.com/projectdiscovery/fileutil v0.0.0-20210804142714-ebba15fa53ca
github.com/projectdiscovery/goconfig v0.0.0-20210804090219-f893ccd0c69c
github.com/projectdiscovery/gologger v1.1.4
github.com/projectdiscovery/hmap v0.0.2-0.20210630092648-6c0a1b362caa

4
go.sum
View File

@ -87,6 +87,8 @@ github.com/json-iterator/go v1.1.11 h1:uVUAXhF2To8cbw/3xN3pxj6kk7TYKs98NIrTqPlMW
github.com/json-iterator/go v1.1.11/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/karrick/godirwalk v1.16.1 h1:DynhcF+bztK8gooS0+NDJFrdNZjJ3gzVzC545UNA9iw=
github.com/karrick/godirwalk v1.16.1/go.mod h1:j4mkqPuvaLI8mp1DroR3P6ad7cyYd4c1qeJ3RV7ULlk=
github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
@ -144,6 +146,8 @@ github.com/projectdiscovery/fastdialer v0.0.13-0.20210815100514-360f851a5b80 h1:
github.com/projectdiscovery/fastdialer v0.0.13-0.20210815100514-360f851a5b80/go.mod h1:RkRbxqDCcCFhfNUbkzBIz/ieD4uda2JuUA4WJ+RLee0=
github.com/projectdiscovery/fdmax v0.0.3 h1:FM6lv9expZ/rEEBI9tkRh6tx3DV0gtpwzdc0h7bGPqg=
github.com/projectdiscovery/fdmax v0.0.3/go.mod h1:NWRcaR7JTO7fC27H4jCl9n7Z+KIredwpgw1fV+4KrKI=
github.com/projectdiscovery/fileutil v0.0.0-20210804142714-ebba15fa53ca h1:xT//ApxoeQRbt9GgL/122688bhGy9hur8YG0Qh69k5I=
github.com/projectdiscovery/fileutil v0.0.0-20210804142714-ebba15fa53ca/go.mod h1:U+QCpQnX8o2N2w0VUGyAzjM3yBAe4BKedVElxiImsx0=
github.com/projectdiscovery/goconfig v0.0.0-20210804090219-f893ccd0c69c h1:1XRSp+44bhWudAWz+2+wHYJBHvDfE8mk9uWpzX+DU9k=
github.com/projectdiscovery/goconfig v0.0.0-20210804090219-f893ccd0c69c/go.mod h1:mBv7GRD5n3WNbFE9blG8ynzXTM5eh9MmwaK6EOyn6Pk=
github.com/projectdiscovery/gologger v1.0.1/go.mod h1:Ok+axMqK53bWNwDSU1nTNwITLYMXMdZtRc8/y1c7sWE=

View File

@ -6,6 +6,7 @@ import (
"os"
"regexp"
"github.com/projectdiscovery/fileutil"
"github.com/projectdiscovery/goconfig"
"github.com/projectdiscovery/gologger"
"github.com/projectdiscovery/gologger/formatter"
@ -13,7 +14,7 @@ import (
"github.com/projectdiscovery/httpx/common/customheader"
"github.com/projectdiscovery/httpx/common/customlist"
customport "github.com/projectdiscovery/httpx/common/customports"
"github.com/projectdiscovery/httpx/common/fileutil"
fileutilz "github.com/projectdiscovery/httpx/common/fileutil"
"github.com/projectdiscovery/httpx/common/stringz"
)
@ -290,7 +291,7 @@ func ParseOptions() *Options {
}
func (options *Options) validateOptions() {
if options.InputFile != "" && !fileutil.FileNameIsGlob(options.InputFile) && !fileutil.FileExists(options.InputFile) {
if options.InputFile != "" && !fileutilz.FileNameIsGlob(options.InputFile) && !fileutil.FileExists(options.InputFile) {
gologger.Fatal().Msgf("File %s does not exist!\n", options.InputFile)
}

View File

@ -31,11 +31,12 @@ import (
// automatic fd max increase if running as root
_ "github.com/projectdiscovery/fdmax/autofdmax"
"github.com/projectdiscovery/fileutil"
"github.com/projectdiscovery/gologger"
"github.com/projectdiscovery/hmap/store/hybrid"
pdhttputil "github.com/projectdiscovery/httputil"
customport "github.com/projectdiscovery/httpx/common/customports"
"github.com/projectdiscovery/httpx/common/fileutil"
fileutilz "github.com/projectdiscovery/httpx/common/fileutil"
"github.com/projectdiscovery/httpx/common/httputilz"
"github.com/projectdiscovery/httpx/common/httpx"
"github.com/projectdiscovery/httpx/common/slice"
@ -250,7 +251,7 @@ func New(options *Options) (*Runner, error) {
func (r *Runner) prepareInput() {
// Check if the user requested multiple paths
if fileutil.FileExists(r.options.RequestURIs) {
r.options.requestURIs = fileutil.LoadFile(r.options.RequestURIs)
r.options.requestURIs = fileutilz.LoadFile(r.options.RequestURIs)
} else if r.options.RequestURIs != "" {
r.options.requestURIs = strings.Split(r.options.RequestURIs, ",")
}
@ -267,7 +268,7 @@ func (r *Runner) prepareInput() {
gologger.Fatal().Msgf("Could read input file '%s': %s\n", r.options.InputFile, err)
}
} else if r.options.InputFile != "" {
files, err := fileutil.ListFilesWithPattern(r.options.InputFile)
files, err := fileutilz.ListFilesWithPattern(r.options.InputFile)
if err != nil {
gologger.Fatal().Msgf("No input provided: %s", err)
}