-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #114 from projectdiscovery/dev
v1.0.3
- Loading branch information
Showing
23 changed files
with
1,381 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
|
||
.vscode | ||
mapcidr | ||
cmd/mapcidr/mapcidr | ||
.vscode | ||
cmd/integration-test/mapcidr | ||
cmd/integration-test/integration-test | ||
integration_tests/mapcidr | ||
integration_tests/goldenfiles | ||
integration_tests/integration-test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package asn | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"strconv" | ||
"strings" | ||
|
||
asnmap "github.com/projectdiscovery/asnmap/libs" | ||
"github.com/projectdiscovery/mapcidr" | ||
) | ||
|
||
type ASNClient struct { | ||
client *asnmap.Client | ||
} | ||
|
||
func New() ASNClient { | ||
return ASNClient{ | ||
client: asnmap.NewClient(), | ||
} | ||
} | ||
|
||
// 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) { | ||
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 | ||
if mapcidr.IsIPv4(cidr.IP) { | ||
cidrs = append(cidrs, cidr) | ||
} | ||
} | ||
return cidrs, 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) | ||
if err != nil { | ||
return nil, err | ||
} | ||
ret := make(chan string) | ||
go func() { | ||
defer close(ret) | ||
for _, cidr := range cidrs { | ||
ips, _ := mapcidr.IPAddressesAsStream(cidr.String()) | ||
for ip := range ips { | ||
ret <- ip | ||
} | ||
} | ||
}() | ||
return ret, nil | ||
} | ||
|
||
// IsASN checks if the given input is ASN or not, | ||
// its possible to have an domain name starting with AS/as prefix. | ||
func IsASN(value string) bool { | ||
if len(value) > 2 && strings.HasPrefix(strings.ToUpper(value), "AS") { | ||
_, err := strconv.Atoi(value[2:]) | ||
return err == nil | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package asn | ||
|
||
import ( | ||
"io/ioutil" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_asnClient_GetCIDRsForASNNum(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
asnNumber string | ||
expected []string | ||
}{ | ||
{ | ||
name: "ASN Number 1", | ||
asnNumber: "AS14421", | ||
expected: []string{"216.101.17.0/24"}, | ||
}, | ||
{ | ||
name: "ASN Number 2", | ||
asnNumber: "AS7712", | ||
expected: []string{"118.67.200.0/23", "118.67.202.0/24", "118.67.203.0/24", "118.67.204.0/22"}, | ||
}, | ||
{ | ||
name: "Wrong ASN number", | ||
asnNumber: "AS", | ||
expected: []string{}, | ||
}, | ||
} | ||
asnClient := New() | ||
|
||
for _, tt := range tests { | ||
var result []string | ||
got, err := asnClient.GetCIDRsForASNNum(tt.asnNumber) | ||
if err != nil { | ||
require.ErrorContains(t, err, "invalid asn number") | ||
} | ||
for _, cidr := range got { | ||
result = append(result, cidr.String()) | ||
} | ||
require.ElementsMatch(t, tt.expected, result, "could not get correct cidrs") | ||
} | ||
} | ||
|
||
func TestASNClient_GetIPAddressesAsStream(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
asnNumber string | ||
expectedOutputFile string | ||
}{ | ||
{ | ||
name: "ASN Number 1", | ||
asnNumber: "AS14421", | ||
expectedOutputFile: "goldenfiles/AS14421.txt", | ||
}, | ||
{ | ||
name: "ASN Number 2", | ||
asnNumber: "AS134029", | ||
expectedOutputFile: "goldenfiles/AS134029.txt", | ||
}, | ||
} | ||
asnClient := New() | ||
for _, tt := range tests { | ||
var result []string | ||
got, err := asnClient.GetIPAddressesAsStream(tt.asnNumber) | ||
if err != nil { | ||
require.ErrorContains(t, err, "invalid asn number") | ||
} | ||
for ip := range got { | ||
result = append(result, ip) | ||
} | ||
// read the expectedOutputFile | ||
fileContent, err := ioutil.ReadFile(tt.expectedOutputFile) | ||
require.Nil(t, err, "could not read the expectedOutputFile file") | ||
items := strings.Split(string(fileContent), "\n") | ||
|
||
require.ElementsMatch(t, items, result, "could not get correct cidrs") | ||
} | ||
} |
Oops, something went wrong.