Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ip2asn service #83

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions cmd/dnstoys/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/knadh/dns.toys/internal/services/epoch"
"github.com/knadh/dns.toys/internal/services/excuse"
"github.com/knadh/dns.toys/internal/services/fx"
"github.com/knadh/dns.toys/internal/services/ip2asn"
"github.com/knadh/dns.toys/internal/services/num2words"
"github.com/knadh/dns.toys/internal/services/random"
"github.com/knadh/dns.toys/internal/services/sudoku"
Expand Down Expand Up @@ -346,6 +347,13 @@ func main() {
help = append(help, []string{"return a developer excuse", "dig excuse @%s"})
}

// Ip to ASN
if ko.Bool("ip2asn.enabled") {
i := ip2asn.New()
h.register("ip2asn", i, mux)
help = append(help, []string{"returns asn info for an ip", "dig 8.8.8.8.ip2asn @%s"})
}

// Prepare the static help response for the `help` query.
for _, l := range help {
r, err := dns.NewRR(fmt.Sprintf("help. %d TXT \"%s\" \"%s\"", HELP_TTL, l[0], fmt.Sprintf(l[1], h.domain)))
Expand Down
3 changes: 3 additions & 0 deletions config.sample.toml
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,6 @@ enabled = true
[excuse]
enabled = true
file = "data/excuses.txt"

[ip2asn]
enabled = true
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ require (

require (
github.com/fsnotify/fsnotify v1.4.9 // indirect
github.com/jamesog/iptoasn v0.1.0 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/pelletier/go-toml v1.7.0 // indirect
github.com/pkg/errors v0.8.1 // indirect
golang.org/x/mod v0.4.2 // indirect
golang.org/x/net v0.7.0 // indirect
golang.org/x/sys v0.5.0 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb/go.mod h1:+NfK9FKe
github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM=
github.com/hold7door/wnram v0.0.0-20220807183601-04f0a21e3770 h1:ck4he2WPwVnS2pcz00GnKwSYLty70piwRc7Nl2wofZ8=
github.com/hold7door/wnram v0.0.0-20220807183601-04f0a21e3770/go.mod h1:U3LJZ94aKEjmTi2AePM5CGeGM8LofC6j+A9tFeQCEGM=
github.com/jamesog/iptoasn v0.1.0 h1:MMusklXAc+1driF/5JhHRtdryfJHpIHOgKVVua1LMOQ=
github.com/jamesog/iptoasn v0.1.0/go.mod h1:lNWbU+KANs4wBv7Ym3HWQqJTHzB4/SlRyuLWOfNt61k=
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc=
Expand All @@ -66,6 +68,7 @@ github.com/knadh/koanf v1.4.1 h1:Z0VGW/uo8NJmjd+L1Dc3S5frq6c62w5xQ9Yf4Mg3wFQ=
github.com/knadh/koanf v1.4.1/go.mod h1:1cfH5223ZeZUOs8FU2UdTmaNfHpqgtjV0+NHjRO43gs=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/miekg/dns v1.1.49 h1:qe0mQU3Z/XpFeE+AEBo2rqaS1IPBJ3anmqZ4XiZJVG8=
github.com/miekg/dns v1.1.49/go.mod h1:e3IlAVfNqAllflbibAZEWOXOQ+Ynzk/dDozDxY7XnME=
github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc=
Expand All @@ -85,10 +88,13 @@ github.com/mitchellh/reflectwalk v1.0.2 h1:G2LzWKi524PWgd3mLHV8Y5k7s6XUvT0Gef6zx
github.com/mitchellh/reflectwalk v1.0.2/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
github.com/npillmayer/nestext v0.1.3/go.mod h1:h2lrijH8jpicr25dFY+oAJLyzlya6jhnuG+zWp9L0Uk=
github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA=
github.com/olekukonko/tablewriter v0.0.1/go.mod h1:vsDQFd/mU46D+Z4whnwzcISnGGzXWMclvtLoiIKAKIo=
github.com/pascaldekloe/goe v0.1.0/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/pelletier/go-toml v1.7.0 h1:7utD74fnzVc/cpcyy8sjrlFr5vYpypUixARcHIMIGuI=
github.com/pelletier/go-toml v1.7.0/go.mod h1:vwGMzjaWMwyfHwgIBhI2YUM4fB6nL6lVAvS1LBMMhTE=
github.com/pierrec/lz4 v2.0.5+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=
Expand Down
33 changes: 33 additions & 0 deletions internal/services/ip2asn/ip2asn.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package ip2asn

import (
"fmt"

"github.com/jamesog/iptoasn"
)

// TTL is set to 900 seconds (15 minutes).
const TTL = 900

type Ip2Asn struct{}

// Returns a new Ip2Asn
func New() *Ip2Asn {
return &Ip2Asn{}
}

// Query to get ASN info from IP
func (asn *Ip2Asn) Query(q string) ([]string, error) {
res, err := iptoasn.LookupIP(q)
if err != nil {
return nil, err
}

s := fmt.Sprintf("%v %s", res.ASNum, res.ASName)
return []string{fmt.Sprintf(`%s %d TXT "%s"`, q, TTL, s)}, nil
}

// Dump produces a gob dump of the cached data.
func (asn *Ip2Asn) Dump() ([]byte, error) {
return nil, nil
}