Skip to content

Commit

Permalink
Merge pull request #114 from projectdiscovery/dev
Browse files Browse the repository at this point in the history
v1.0.3
  • Loading branch information
ehsandeep authored Oct 12, 2022
2 parents b2e2e25 + d43b3da commit f66b99f
Show file tree
Hide file tree
Showing 23 changed files with 1,381 additions and 25 deletions.
10 changes: 10 additions & 0 deletions .github/workflows/build-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ jobs:
run: go build .
working-directory: cmd/mapcidr/

- name: Test
run: go test ./...
working-directory: .

- name: Integration Tests
env:
GH_ACTION: true
run: bash run.sh
working-directory: integration_tests/

- name: Race Test
run: go run -race . -cidr 192.168.1.224/30
working-directory: cmd/mapcidr/
2 changes: 1 addition & 1 deletion .github/workflows/lint-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.17
go-version: 1.18
- name: Checkout code
uses: actions/checkout@v3
- name: Run golangci-lint
Expand Down
9 changes: 7 additions & 2 deletions .gitignore
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
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.19.1-alpine AS build-env
FROM golang:1.19.2-alpine AS build-env
RUN go install -v github.com/projectdiscovery/mapcidr/cmd/mapcidr@latest

FROM alpine:latest
Expand Down
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,19 @@ $ echo 173.0.84.0/16 | mapcidr -count -silent
65536
```

### ASN Input

In order to get the IP address of ASN number, use the following command
```
echo AS15133 | mapcidr -silent
5.104.64.0
5.104.64.1
5.104.64.2
5.104.64.3
5.104.64.4
```

# Use mapCIDR as a library

It's possible to use the library directly in your go programs. The following code snippets outline how to divide a cidr into subnets, and how to divide the same into subnets containing a certain number of hosts
Expand Down
69 changes: 69 additions & 0 deletions asn/asn.go
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
}
82 changes: 82 additions & 0 deletions asn/asn_test.go
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")
}
}
Loading

0 comments on commit f66b99f

Please sign in to comment.