Skip to content

Commit

Permalink
Merge pull request #148 from projectdiscovery/dev
Browse files Browse the repository at this point in the history
mapcidr v1.1.0
  • Loading branch information
ehsandeep authored Feb 13, 2023
2 parents f66b99f + b4ce971 commit 43c0610
Show file tree
Hide file tree
Showing 27 changed files with 702 additions and 200 deletions.
36 changes: 36 additions & 0 deletions .github/workflows/autorelease-tag.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: 🔖 Auto release tag

on:
workflow_dispatch:
schedule:
- cron: '0 0 * * 0'

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Get Commit Count
id: get_commit
run: git rev-list `git rev-list --tags --no-walk --max-count=1`..HEAD --count | xargs -I {} echo COMMIT_COUNT={} >> $GITHUB_OUTPUT

- name: Create release and tag
if: ${{ steps.get_commit.outputs.COMMIT_COUNT > 0 }}
id: tag_version
uses: mathieudutour/[email protected]
with:
github_token: ${{ secrets.GITHUB_TOKEN }}

- name: Create a GitHub release
if: ${{ steps.get_commit.outputs.COMMIT_COUNT > 0 }}
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ steps.tag_version.outputs.new_tag }}
release_name: Release ${{ steps.tag_version.outputs.new_tag }}
body: ${{ steps.tag_version.outputs.changelog }}
3 changes: 1 addition & 2 deletions .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 All @@ -13,7 +12,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.18
go-version: 1.19

- name: Check out code
uses: actions/checkout@v3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/dockerhub-push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
password: ${{ secrets.DOCKER_TOKEN }}

- name: Build and push
uses: docker/build-push-action@v3
uses: docker/build-push-action@v4
with:
context: .
platforms: linux/amd64,linux/arm64,linux/arm
Expand Down
5 changes: 2 additions & 3 deletions .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 All @@ -12,11 +11,11 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.18
go-version: 1.19
- name: Checkout code
uses: actions/checkout@v3
- name: Run golangci-lint
uses: golangci/golangci-lint-action@v3.2.0
uses: golangci/golangci-lint-action@v3.4.0
with:
version: latest
args: --timeout 5m
Expand Down
22 changes: 10 additions & 12 deletions .github/workflows/release-binary.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
name: 🎉 Release Binary

on:
create:
tags:
- v*
workflow_dispatch:

jobs:
release:
runs-on: ubuntu-latest
runs-on: ubuntu-latest-16-cores
steps:
- name: "Check out code"
uses: actions/checkout@v3
Expand All @@ -18,15 +15,16 @@ jobs:
- name: "Set up Go"
uses: actions/setup-go@v3
with:
go-version: 1.18
go-version: 1.19

- env:
GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
SLACK_WEBHOOK: "${{ secrets.RELEASE_SLACK_WEBHOOK }}"

name: "Create release on GitHub"
uses: goreleaser/goreleaser-action@v3
- name: "Create release on GitHub"
uses: goreleaser/goreleaser-action@v4
with:
args: "release --rm-dist"
version: latest
workdir: .
workdir: .
env:
GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
SLACK_WEBHOOK: "${{ secrets.RELEASE_SLACK_WEBHOOK }}"
DISCORD_WEBHOOK_ID: "${{ secrets.DISCORD_WEBHOOK_ID }}"
DISCORD_WEBHOOK_TOKEN: "${{ secrets.DISCORD_WEBHOOK_TOKEN }}"
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ cmd/mapcidr/mapcidr
cmd/integration-test/mapcidr
cmd/integration-test/integration-test
integration_tests/mapcidr
integration_tests/goldenfiles
integration_tests/tests
integration_tests/integration-test
dist
51 changes: 32 additions & 19 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -1,28 +1,41 @@
before:
hooks:
- go mod tidy

builds:
- binary: mapcidr
main: cmd/mapcidr/main.go
goos:
- linux
- windows
- darwin
goarch:
- amd64
- 386
- arm
- arm64
- binary: '{{ .ProjectName }}'
main: cmd/mapcidr/main.go
goos: [windows,linux,darwin]
goarch: [amd64,386,arm,arm64]
ignore:
- goos: darwin
goarch: 386
- goos: windows
goarch: arm
- goos: windows
goarch: arm64

# release options (https://goreleaser.com/customization/release/)

release:
draft: true
prerelease: auto

archives:
- id: tgz
format: tar.gz
replacements:
darwin: macOS
format_overrides:
- goos: windows
format: zip
- format: zip
replacements:
darwin: macOS

checksum:
algorithm: sha256

announce:
slack:
enabled: true
channel: '#release'
username: GoReleaser
message_template: '{{ .ProjectName }} {{ .Tag }} is out! Check it out at {{ .ReleaseURL }}'
message_template: 'New Release: {{ .ProjectName }} {{.Tag}} is published! Check it out at {{ .ReleaseURL }}'

discord:
enabled: true
message_template: '**New Release: {{ .ProjectName }} {{.Tag}}** is published! Check it out at {{ .ReleaseURL }}'
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.2-alpine AS build-env
FROM golang:1.20.0-alpine AS build-env
RUN go install -v github.com/projectdiscovery/mapcidr/cmd/mapcidr@latest

FROM alpine:latest
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
18 changes: 8 additions & 10 deletions asn/asn_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package asn

import (
"io/ioutil"
"strings"
"os"
"testing"

stringsutil "github.com/projectdiscovery/utils/strings"
"github.com/stretchr/testify/require"
)

Expand All @@ -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 All @@ -54,28 +53,27 @@ func TestASNClient_GetIPAddressesAsStream(t *testing.T) {
{
name: "ASN Number 1",
asnNumber: "AS14421",
expectedOutputFile: "goldenfiles/AS14421.txt",
expectedOutputFile: "tests/AS14421.txt",
},
{
name: "ASN Number 2",
asnNumber: "AS134029",
expectedOutputFile: "goldenfiles/AS134029.txt",
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")
}
for ip := range got {
result = append(result, ip)
}
// read the expectedOutputFile
fileContent, err := ioutil.ReadFile(tt.expectedOutputFile)
fileContent, err := os.ReadFile(tt.expectedOutputFile)
require.Nil(t, err, "could not read the expectedOutputFile file")
items := strings.Split(string(fileContent), "\n")
items := stringsutil.SplitAny(string(fileContent), "\n", "\r")

require.ElementsMatch(t, items, result, "could not get correct cidrs")
}
Expand Down
File renamed without changes.
File renamed without changes.
5 changes: 2 additions & 3 deletions cmd/integration-test/integration-test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@ var (
debug = os.Getenv("DEBUG") == "true"
customTest = os.Getenv("TEST")
errored = false
success = aurora.Green("[✓]").String()
failed = aurora.Red("[✘]").String()
)

func main() {
success := aurora.Green("[✓]").String()
failed := aurora.Red("[✘]").String()

for name, testCase := range mapcidrTestcases {
if customTest != "" && !strings.Contains(name, customTest) {
continue // only run tests user asked
Expand Down
Loading

0 comments on commit 43c0610

Please sign in to comment.