Skip to content

Commit

Permalink
use go-asn for as parsing (adds support for multiple AS formats)
Browse files Browse the repository at this point in the history
  • Loading branch information
thatmattlove committed Aug 6, 2023
1 parent f25f9bc commit 0822972
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 61 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
10 changes: 6 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
9 changes: 5 additions & 4 deletions pkg/bgptools.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -128,15 +129,15 @@ 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
}
w, err := whois.New(WHOIS_HOST, WHOIS_PORT)
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
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/bgptools_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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)
Expand Down
21 changes: 0 additions & 21 deletions pkg/parse.go

This file was deleted.

27 changes: 0 additions & 27 deletions pkg/parse_test.go

This file was deleted.

3 changes: 2 additions & 1 deletion pkg/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net"

"github.com/biter777/countries"
goasn "github.com/thatmattlove/go-asn"
)

type IPValidator struct {
Expand Down Expand Up @@ -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,
Expand Down
5 changes: 3 additions & 2 deletions pkg/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 0822972

Please sign in to comment.