From 08229728e67593de4a09d0d712c9060eff1259b9 Mon Sep 17 00:00:00 2001 From: thatmattlove Date: Sun, 6 Aug 2023 14:58:26 -0400 Subject: [PATCH] use go-asn for as parsing (adds support for multiple AS formats) --- go.mod | 3 ++- go.sum | 10 ++++++---- pkg/bgptools.go | 9 +++++---- pkg/bgptools_test.go | 3 ++- pkg/parse.go | 21 --------------------- pkg/parse_test.go | 27 --------------------------- pkg/validate.go | 3 ++- pkg/validate_test.go | 5 +++-- 8 files changed, 20 insertions(+), 61 deletions(-) delete mode 100644 pkg/parse.go delete mode 100644 pkg/parse_test.go diff --git a/go.mod b/go.mod index ffdd363..5a86afb 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,8 @@ require ( github.com/miekg/dns v1.1.55 github.com/pterm/pterm v0.12.63 github.com/spf13/cobra v1.7.0 - github.com/stretchr/testify v1.8.0 + github.com/stretchr/testify v1.8.4 + github.com/thatmattlove/go-asn v0.0.2 ) require ( diff --git a/go.sum b/go.sum index 03c1f10..2f2df84 100644 --- a/go.sum +++ b/go.sum @@ -69,13 +69,15 @@ github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRM github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= -github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/thatmattlove/go-asn v0.0.1 h1:PihaHAE1RV/pqRZnQp+3ws0D1eWFoKfg4p+iDKdKBWo= +github.com/thatmattlove/go-asn v0.0.1/go.mod h1:hXwRKWCIf2X6G/ZfvFVosBKZxEpPv6mABgOvmEBdNrI= +github.com/thatmattlove/go-asn v0.0.2 h1:zVhXS21vuZDb18RKggOOGhDPKNUV1MycwRFh7GCAHIE= +github.com/thatmattlove/go-asn v0.0.2/go.mod h1:hXwRKWCIf2X6G/ZfvFVosBKZxEpPv6mABgOvmEBdNrI= github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778/go.mod h1:2MuV+tbUrU1zIOPMxZ5EncGwgmMJsa+9ucAQZXxsObs= github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no= github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM= diff --git a/pkg/bgptools.go b/pkg/bgptools.go index ebb1d92..bfd1594 100644 --- a/pkg/bgptools.go +++ b/pkg/bgptools.go @@ -10,10 +10,11 @@ import ( "github.com/biter777/countries" "github.com/thatmattlove/addr/pkg/whois" + goasn "github.com/thatmattlove/go-asn" ) type Response struct { - ASN uint64 + ASN goasn.ASN IP *net.IP Prefix *net.IPNet Country countries.CountryCode @@ -86,7 +87,7 @@ func ParseResponse(res string) (*Response, error) { allocatedStr := values[5] // 'Allocated' column. name := values[6] // 'AS Name' column. - asn, err := ParseASN(asnStr) + asn, err := goasn.Parse(asnStr) if err != nil { return nil, err } @@ -128,7 +129,7 @@ func ParseResponse(res string) (*Response, error) { } func QueryASN(asnStr string) (*Response, error) { - asn, err := ParseASN(asnStr) + asn, err := goasn.Parse(asnStr) if err != nil { return nil, err } @@ -136,7 +137,7 @@ func QueryASN(asnStr string) (*Response, error) { if err != nil { return nil, err } - result, err := w.Query(fmt.Sprintf("as%d", asn)) + result, err := w.Query(fmt.Sprintf("as%s", asn.ASPlain())) if err != nil { return nil, err } diff --git a/pkg/bgptools_test.go b/pkg/bgptools_test.go index ad96cea..7ec7cdd 100644 --- a/pkg/bgptools_test.go +++ b/pkg/bgptools_test.go @@ -7,6 +7,7 @@ import ( "github.com/biter777/countries" "github.com/stretchr/testify/assert" addr "github.com/thatmattlove/addr/pkg" + goasn "github.com/thatmattlove/go-asn" ) const ( @@ -41,7 +42,7 @@ func Test_QueryASN(t *testing.T) { q := "as14525" asn, err := addr.QueryASN(q) assert.NoError(t, err, q) - assert.Equal(t, uint64(14525), asn.ASN, q) + assert.True(t, asn.ASN.Equal(goasn.MustParse("14525")), q) assert.Equal(t, "ARIN", asn.Registry, q) assert.Equal(t, countries.USA, asn.Country, q) assert.Equal(t, "Stellar Technologies Inc.", asn.Name, q) diff --git a/pkg/parse.go b/pkg/parse.go deleted file mode 100644 index a879f25..0000000 --- a/pkg/parse.go +++ /dev/null @@ -1,21 +0,0 @@ -package addr - -import ( - "fmt" - "regexp" - "strconv" -) - -func ParseASN(in string) (uint64, error) { - p := regexp.MustCompile(`([0-9]+)`) - matches := p.FindStringSubmatch(in) - if len(matches) == 0 { - err := fmt.Errorf("failed to parse '%s' as ASN", in) - return 0, err - } - asn, err := strconv.ParseUint(matches[0], 10, 64) - if err != nil { - return 0, err - } - return asn, nil -} diff --git a/pkg/parse_test.go b/pkg/parse_test.go deleted file mode 100644 index 8e7b202..0000000 --- a/pkg/parse_test.go +++ /dev/null @@ -1,27 +0,0 @@ -package addr_test - -import ( - "testing" - - "github.com/stretchr/testify/assert" - addr "github.com/thatmattlove/addr/pkg" -) - -func Test_ParseASN(t *testing.T) { - t.Run("valid asn", func(t *testing.T) { - t.Parallel() - asn, err := addr.ParseASN("as14525") - assert.NoError(t, err) - assert.Equal(t, uint64(14525), asn) - }) - t.Run("invalid asn 1", func(t *testing.T) { - t.Parallel() - _, err := addr.ParseASN("not an asn") - assert.Error(t, err) - }) - t.Run("invalid asn 2", func(t *testing.T) { - t.Parallel() - _, err := addr.ParseASN("18446744073709551616") - assert.Error(t, err) - }) -} diff --git a/pkg/validate.go b/pkg/validate.go index 449ac66..5f178e4 100644 --- a/pkg/validate.go +++ b/pkg/validate.go @@ -4,6 +4,7 @@ import ( "net" "github.com/biter777/countries" + goasn "github.com/thatmattlove/go-asn" ) type IPValidator struct { @@ -107,7 +108,7 @@ func (ipv *IPValidator) Validate() (bool, *Response) { return true, nil } response := &Response{ - ASN: 0, + ASN: goasn.ASN{0, 0, 0, 0}, IP: &ipv.IP, Prefix: pfx, Name: txt, diff --git a/pkg/validate_test.go b/pkg/validate_test.go index bc9d8ad..5f057be 100644 --- a/pkg/validate_test.go +++ b/pkg/validate_test.go @@ -9,6 +9,7 @@ import ( "github.com/biter777/countries" "github.com/stretchr/testify/assert" addr "github.com/thatmattlove/addr/pkg" + goasn "github.com/thatmattlove/go-asn" ) func TestIPValidator_NewIPValidator(t *testing.T) { @@ -122,7 +123,7 @@ func TestIPValidator_Validate(t *testing.T) { shouldQuery, response := v.Validate() assert.False(t, shouldQuery) assert.IsType(t, &addr.Response{}, response) - assert.Equal(t, uint64(0), response.ASN) + assert.Equal(t, goasn.ASN{0, 0, 0, 0}, response.ASN) assert.Equal(t, addr.TXT_LINK_LOCAL, response.Name) assert.Equal(t, countries.USA, response.Country) assert.Equal(t, &ip, response.IP) @@ -145,7 +146,7 @@ func TestIPValidator_Validate(t *testing.T) { shouldQuery, response := v.Validate() assert.False(t, shouldQuery) assert.IsType(t, &addr.Response{}, response) - assert.Equal(t, uint64(0), response.ASN) + assert.Equal(t, goasn.ASN{0, 0, 0, 0}, response.ASN) assert.Equal(t, addr.TXT_DOC, response.Name) assert.Equal(t, countries.USA, response.Country) assert.Equal(t, &ip, response.IP)