Skip to content

Commit

Permalink
Merge pull request #144 from projectdiscovery/issue-138-default-client
Browse files Browse the repository at this point in the history
Implement DefaultClient in asn package
  • Loading branch information
Mzack9999 authored Feb 13, 2023
2 parents 5fc81bf + 8e6046f commit 72e91b7
Show file tree
Hide file tree
Showing 7 changed files with 220 additions and 151 deletions.
1 change: 0 additions & 1 deletion .github/workflows/build-test.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
name: 🔨 Build Test
on:
push:
pull_request:
workflow_dispatch:

Expand Down
1 change: 0 additions & 1 deletion .github/workflows/lint-test.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
name: 🙏🏻 Lint Test
on:
push:
pull_request:
workflow_dispatch:

Expand Down
38 changes: 23 additions & 15 deletions asn/asn.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,46 @@ import (
"github.com/projectdiscovery/mapcidr"
)

type ASNClient struct {
client *asnmap.Client
}
var DefaultClient *asnmap.Client

func New() ASNClient {
return ASNClient{
client: asnmap.NewClient(),
func init() {
var err error
DefaultClient, err = asnmap.NewClient()
// DefaultClient must exist
if err != nil {
panic(err)
}
}

// GetCIDRsForASNNum returns the slice of cidrs for given ASN number
// accept the ASN number like 'AS15133' and returns the CIDRs for that ASN
func (c *ASNClient) GetCIDRsForASNNum(value string) ([]*net.IPNet, error) {
func GetCIDRsForASNNum(value string) ([]*net.IPNet, error) {
var cidrs []*net.IPNet
if len(value) < 3 {
return nil, fmt.Errorf("invalid asn number %s", value)
}
// drop the AS suffix
asn := asnmap.ASN(value[2:])
for _, cidr := range asnmap.GetCIDR(c.client.GetData(asn)) {
// filter IPv6 CIDR
data, err := DefaultClient.GetData(value[2:])
if err != nil {
return nil, err
}
cidrs, err = asnmap.GetCIDR(data)
if err != nil {
return nil, err
}

var filteredCIDRs []*net.IPNet
for _, cidr := range cidrs {
if mapcidr.IsIPv4(cidr.IP) {
cidrs = append(cidrs, cidr)
filteredCIDRs = append(filteredCIDRs, cidr)
}
}
return cidrs, nil
return filteredCIDRs, nil
}

// GetIPAddressesAsStream returns the chan of IP address for given ASN number
// returning the string chan for optimizing the memory
func (c *ASNClient) GetIPAddressesAsStream(value string) (chan string, error) {
cidrs, err := c.GetCIDRsForASNNum(value)
func GetIPAddressesAsStream(value string) (chan string, error) {
cidrs, err := GetCIDRsForASNNum(value)
if err != nil {
return nil, err
}
Expand Down
6 changes: 2 additions & 4 deletions asn/asn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,10 @@ func Test_asnClient_GetCIDRsForASNNum(t *testing.T) {
expected: []string{},
},
}
asnClient := New()

for _, tt := range tests {
var result []string
got, err := asnClient.GetCIDRsForASNNum(tt.asnNumber)
got, err := GetCIDRsForASNNum(tt.asnNumber)
if err != nil {
require.ErrorContains(t, err, "invalid asn number")
}
Expand Down Expand Up @@ -62,10 +61,9 @@ func TestASNClient_GetIPAddressesAsStream(t *testing.T) {
expectedOutputFile: "tests/AS134029.txt",
},
}
asnClient := New()
for _, tt := range tests {
var result []string
got, err := asnClient.GetIPAddressesAsStream(tt.asnNumber)
got, err := GetIPAddressesAsStream(tt.asnNumber)
if err != nil {
require.ErrorContains(t, err, "invalid asn number")
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/mapcidr/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,6 @@ func process(wg *sync.WaitGroup, chancidr, outputchan chan string) {
hasSort = options.SortAscending || options.SortDescending
ipRangeList = make([][]net.IP, 0)
asnNumberList []string
asnClient = asn.New()
)

ranger, _ = ipranger.New()
Expand Down Expand Up @@ -367,7 +366,7 @@ func process(wg *sync.WaitGroup, chancidr, outputchan chan string) {
}

for _, asnNumber := range asnNumberList {
cidrs, err := asnClient.GetCIDRsForASNNum(asnNumber)
cidrs, err := asn.GetCIDRsForASNNum(asnNumber)
if err != nil {
gologger.Fatal().Msgf("%s\n", err)
}
Expand Down
60 changes: 34 additions & 26 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,41 @@ go 1.18
require (
github.com/logrusorgru/aurora v2.0.3+incompatible
github.com/pkg/errors v0.9.1
github.com/projectdiscovery/asnmap v0.0.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/gologger v1.1.7
github.com/projectdiscovery/ipranger v0.0.4
github.com/projectdiscovery/utils v0.0.4-0.20221214110533-9f95ee986a54
github.com/stretchr/testify v1.8.1
golang.org/x/exp v0.0.0-20221106115401-f9659909a136
golang.org/x/exp v0.0.0-20221230185412-738e83a70c30
)

require (
github.com/AndreasBriese/bbloom v0.0.0-20190825152654-46b345b51c96 // indirect
github.com/DataDog/zstd v1.4.5 // indirect
github.com/DataDog/zstd v1.5.2 // indirect
github.com/Mzack9999/go-http-digest-auth-client v0.6.1-0.20220414142836-eb8883508809 // indirect
github.com/akrylysov/pogreb v0.10.1 // indirect
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d // indirect
github.com/aymerick/douceur v0.2.0 // indirect
github.com/cespare/xxhash v1.1.0 // indirect
github.com/cespare/xxhash/v2 v2.1.1 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/cnf/structhash v0.0.0-20201127153200-e1b16c1ebc08 // indirect
github.com/cockroachdb/errors v1.8.1 // indirect
github.com/cockroachdb/logtags v0.0.0-20190617123548-eb05cc24525f // indirect
github.com/cockroachdb/pebble v0.0.0-20210728210723-48179f1d4dae // indirect
github.com/cockroachdb/redact v1.0.8 // indirect
github.com/cockroachdb/sentry-go v0.6.1-cockroachdb.2 // 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/dgraph-io/badger v1.6.2 // indirect
github.com/dgraph-io/ristretto v0.0.3 // indirect
github.com/dsnet/compress v0.0.1 // indirect
github.com/dustin/go-humanize v1.0.0 // indirect
github.com/gogo/protobuf v1.3.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/gorilla/css v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.11.7 // 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/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
Expand All @@ -50,29 +48,39 @@ require (
github.com/nwaples/rardecode v1.1.0 // indirect
github.com/pierrec/lz4 v2.6.0+incompatible // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/projectdiscovery/fileutil v0.0.3 // indirect
github.com/projectdiscovery/hmap v0.0.2 // indirect
github.com/projectdiscovery/hmap v0.0.6 // indirect
github.com/projectdiscovery/iputil v0.0.2 // indirect
github.com/projectdiscovery/networkpolicy v0.0.2-0.20221025011504-22b13ce54fff // indirect
github.com/projectdiscovery/retryabledns v1.0.17 // indirect
github.com/projectdiscovery/retryablehttp-go v1.0.2 // indirect
github.com/projectdiscovery/sliceutil v0.0.1 // indirect
github.com/projectdiscovery/retryabledns v1.0.20 // indirect
github.com/projectdiscovery/retryablehttp-go v1.0.8 // indirect
github.com/projectdiscovery/stringsutil v0.0.2 // indirect
github.com/prometheus/client_golang v1.14.0 // indirect
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/rogpeppe/go-internal v1.9.0 // indirect
github.com/saintfish/chardet v0.0.0-20120816061221-3af4cd4741ca // 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
github.com/tidwall/gjson v1.14.3 // indirect
github.com/tidwall/grect v0.1.4 // indirect
github.com/tidwall/match v1.1.1 // indirect
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/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect
github.com/yl2chen/cidranger v1.0.2 // indirect
go.etcd.io/bbolt v1.3.5 // 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.1.0 // indirect
golang.org/x/sys v0.1.0 // indirect
golang.org/x/text v0.4.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
google.golang.org/protobuf v1.26.0 // 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
)
Loading

0 comments on commit 72e91b7

Please sign in to comment.