Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mpolden/echoip.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore2
-rw-r--r--Makefile3
-rw-r--r--README.md10
-rw-r--r--cmd/echoip/main.go3
-rw-r--r--heroku.yml2
-rw-r--r--http/http.go19
-rw-r--r--http/http_test.go9
-rw-r--r--index.html11
-rw-r--r--iputil/geo/geo.go38
9 files changed, 86 insertions, 11 deletions
diff --git a/.gitignore b/.gitignore
index 5022fb8..e208308 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,5 @@
/data/
/custom.html
/vendor/
+.vscode/
+/bin/ \ No newline at end of file
diff --git a/Makefile b/Makefile
index d1da7df..87e44fd 100644
--- a/Makefile
+++ b/Makefile
@@ -23,13 +23,14 @@ lint: check-fmt vet
install: deps
go install ./...
-databases := GeoLite2-City GeoLite2-Country
+databases := GeoLite2-City GeoLite2-Country GeoLite2-ASN
$(databases):
mkdir -p data
curl -fsSL -m 30 https://geolite.maxmind.com/download/geoip/database/$@.tar.gz | tar $(TAR_OPTS) --strip-components=1 -C $(CURDIR)/data -xzf - '*.mmdb'
test ! -f data/GeoLite2-City.mmdb || mv data/GeoLite2-City.mmdb data/city.mmdb
test ! -f data/GeoLite2-Country.mmdb || mv data/GeoLite2-Country.mmdb data/country.mmdb
+ test ! -f data/GeoLite2-ASN.mmdb || mv data/GeoLite2-ASN.mmdb data/asn.mmdb
geoip-download: $(databases)
diff --git a/README.md b/README.md
index f41ec21..d1d1823 100644
--- a/README.md
+++ b/README.md
@@ -37,6 +37,9 @@ EB
$ curl ifconfig.co/city
Bornyasherk
+
+$ curl ifconfig.co/asn
+AS59795
```
As JSON:
@@ -48,7 +51,9 @@ $ curl -H 'Accept: application/json' ifconfig.co # or curl ifconfig.co/json
"country": "Elbonia",
"country_iso": "EB",
"ip": "127.0.0.1",
- "ip_decimal": 2130706433
+ "ip_decimal": 2130706433,
+ "asn": "AS59795",
+ "asn_org": "Hosting4Real"
}
```
@@ -74,7 +79,7 @@ between IPv4 and IPv6 lookup.
* Supports HTTPS
* Supports common command-line clients (e.g. `curl`, `httpie`, `wget` and `fetch`)
* JSON output
-* Country and city lookup using the MaxMind GeoIP database
+* ASN, country and city lookup using the MaxMind GeoIP database
* Port testing
* Open source under the [BSD 3-Clause license](https://opensource.org/licenses/BSD-3-Clause)
@@ -111,6 +116,7 @@ Usage:
Application Options:
-f, --country-db=FILE Path to GeoIP country database
-c, --city-db=FILE Path to GeoIP city database
+ -a, --asn-db=FILE Path to GeoIP ASN database
-l, --listen=ADDR Listening address (default: :8080)
-r, --reverse-lookup Perform reverse hostname lookups
-p, --port-lookup Enable port lookup
diff --git a/cmd/echoip/main.go b/cmd/echoip/main.go
index bccdeb8..9f18681 100644
--- a/cmd/echoip/main.go
+++ b/cmd/echoip/main.go
@@ -16,6 +16,7 @@ func main() {
var opts struct {
CountryDBPath string `short:"f" long:"country-db" description:"Path to GeoIP country database" value-name:"FILE" default:""`
CityDBPath string `short:"c" long:"city-db" description:"Path to GeoIP city database" value-name:"FILE" default:""`
+ ASNDBPath string `short:"a" long:"asn-db" description:"Path to GeoIP ASN database" value-name:"FILE" default:""`
Listen string `short:"l" long:"listen" description:"Listening address" value-name:"ADDR" default:":8080"`
ReverseLookup bool `short:"r" long:"reverse-lookup" description:"Perform reverse hostname lookups"`
PortLookup bool `short:"p" long:"port-lookup" description:"Enable port lookup"`
@@ -28,7 +29,7 @@ func main() {
}
log := log.New(os.Stderr, "echoip: ", 0)
- r, err := geo.Open(opts.CountryDBPath, opts.CityDBPath)
+ r, err := geo.Open(opts.CountryDBPath, opts.CityDBPath, opts.ASNDBPath)
if err != nil {
log.Fatal(err)
}
diff --git a/heroku.yml b/heroku.yml
index 0d550a5..1e3ea0d 100644
--- a/heroku.yml
+++ b/heroku.yml
@@ -4,4 +4,4 @@ build:
pre:
- make geoip-download
run:
- web: echoip -f data/country.mmdb -c data/city.mmdb -p -r -H CF-Connecting-IP -H X-Forwarded-For -l :$PORT
+ web: echoip -f data/country.mmdb -c data/city.mmdb -a data/asn.mmdb -p -r -H CF-Connecting-IP -H X-Forwarded-For -l :$PORT
diff --git a/http/http.go b/http/http.go
index ae755c0..663effd 100644
--- a/http/http.go
+++ b/http/http.go
@@ -40,6 +40,8 @@ type Response struct {
Hostname string `json:"hostname,omitempty"`
Latitude float64 `json:"latitude,omitempty"`
Longitude float64 `json:"longitude,omitempty"`
+ ASN string `json:"asn,omitempty"`
+ ASNOrg string `json:"asn_org,omitempty"`
}
type PortResponse struct {
@@ -93,10 +95,15 @@ func (s *Server) newResponse(r *http.Request) (Response, error) {
ipDecimal := iputil.ToDecimal(ip)
country, _ := s.gr.Country(ip)
city, _ := s.gr.City(ip)
+ asn, _ := s.gr.ASN(ip)
var hostname string
if s.LookupAddr != nil {
hostname, _ = s.LookupAddr(ip)
}
+ var autonomousSystemNumber string
+ if asn.AutonomousSystemNumber > 0 {
+ autonomousSystemNumber = fmt.Sprintf("AS%d", asn.AutonomousSystemNumber)
+ }
return Response{
IP: ip,
IPDecimal: ipDecimal,
@@ -107,6 +114,8 @@ func (s *Server) newResponse(r *http.Request) (Response, error) {
Hostname: hostname,
Latitude: city.Latitude,
Longitude: city.Longitude,
+ ASN: autonomousSystemNumber,
+ ASNOrg: asn.AutonomousSystemOrganization,
}, nil
}
@@ -173,6 +182,15 @@ func (s *Server) CLICoordinatesHandler(w http.ResponseWriter, r *http.Request) *
return nil
}
+func (s *Server) CLIASNHandler(w http.ResponseWriter, r *http.Request) *appError {
+ response, err := s.newResponse(r)
+ if err != nil {
+ return internalServerError(err)
+ }
+ fmt.Fprintf(w, "%s\n", response.ASN)
+ return nil
+}
+
func (s *Server) JSONHandler(w http.ResponseWriter, r *http.Request) *appError {
response, err := s.newResponse(r)
if err != nil {
@@ -305,6 +323,7 @@ func (s *Server) Handler() http.Handler {
r.Route("GET", "/country-iso", s.CLICountryISOHandler)
r.Route("GET", "/city", s.CLICityHandler)
r.Route("GET", "/coordinates", s.CLICoordinatesHandler)
+ r.Route("GET", "/asn", s.CLIASNHandler)
}
// Browser
diff --git a/http/http_test.go b/http/http_test.go
index d7dd35f..cca5e9b 100644
--- a/http/http_test.go
+++ b/http/http_test.go
@@ -24,6 +24,10 @@ func (t *testDb) City(net.IP) (geo.City, error) {
return geo.City{Name: "Bornyasherk", Latitude: 63.416667, Longitude: 10.416667}, nil
}
+func (t *testDb) ASN(net.IP) (geo.ASN, error) {
+ return geo.ASN{AutonomousSystemNumber: 59795, AutonomousSystemOrganization: "Hosting4Real"}, nil
+}
+
func (t *testDb) IsEmpty() bool { return false }
func testServer() *Server {
@@ -70,6 +74,7 @@ func TestCLIHandlers(t *testing.T) {
{s.URL + "/coordinates", "63.416667,10.416667\n", 200, "", ""},
{s.URL + "/city", "Bornyasherk\n", 200, "", ""},
{s.URL + "/foo", "404 page not found", 404, "", ""},
+ {s.URL + "/asn", "AS59795\n", 200, "", ""},
}
for _, tt := range tests {
@@ -91,7 +96,7 @@ func TestDisabledHandlers(t *testing.T) {
server := testServer()
server.LookupPort = nil
server.LookupAddr = nil
- server.gr, _ = geo.Open("", "")
+ server.gr, _ = geo.Open("", "", "")
s := httptest.NewServer(server.Handler())
var tests = []struct {
@@ -129,7 +134,7 @@ func TestJSONHandlers(t *testing.T) {
out string
status int
}{
- {s.URL, `{"ip":"127.0.0.1","ip_decimal":2130706433,"country":"Elbonia","country_eu":false,"country_iso":"EB","city":"Bornyasherk","hostname":"localhost","latitude":63.416667,"longitude":10.416667}`, 200},
+ {s.URL, `{"ip":"127.0.0.1","ip_decimal":2130706433,"country":"Elbonia","country_eu":false,"country_iso":"EB","city":"Bornyasherk","hostname":"localhost","latitude":63.416667,"longitude":10.416667,"asn":"AS59795","asn_org":"Hosting4Real"}`, 200},
{s.URL + "/port/foo", `{"error":"invalid port: foo"}`, 400},
{s.URL + "/port/0", `{"error":"invalid port: 0"}`, 400},
{s.URL + "/port/65537", `{"error":"invalid port: 65537"}`, 400},
diff --git a/index.html b/index.html
index 430964e..e988e7e 100644
--- a/index.html
+++ b/index.html
@@ -78,6 +78,15 @@ $ http {{ .Host }}/country-iso
$ http {{ .Host }}/city
{{ .City }}</pre>
{{ end }}
+{{ if .ASN }}
+ <h2>ASN lookup</h2>
+ <pre>
+$ http ip.alphakilo.eu/asn
+{{ .ASN }}
+{{ if .ASNOrg }}</pre>
+ <p>Looks like you're with {{ .ASNOrg }}</p>
+{{ end }}
+{{ end }}
</div>
<div class="pure-u-1 pure-u-md-1-2">
<h2>JSON output</h2>
@@ -131,7 +140,7 @@ $ http {{ .Host }}/port/8080
</div>
</div>
<a href="https://github.com/mpolden/echoip" class="github-corner"><svg width="80" height="80" viewBox="0 0 250 250" style="fill:#151513; color:#fff; position: absolute; top: 0; border: 0; right: 0;"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a><style>.github-corner:hover .octo-arm{animation:octocat-wave 560ms ease-in-out}@keyframes octocat-wave{0%,100%{transform:rotate(0)}20%,60%{transform:rotate(-25deg)}40%,80%{transform:rotate(10deg)}}@media (max-width:500px){.github-corner:hover .octo-arm{animation:none}.github-corner .octo-arm{animation:octocat-wave 560ms ease-in-out}}</style>
- {{ if or .Country .City }}
+ {{ if or .Country .City .ASN .ASNOrg }}
<div class="pure-g">
<div class="pure-u-1-1 footer">
<p><small>This product includes GeoLite2 data created by MaxMind,
diff --git a/iputil/geo/geo.go b/iputil/geo/geo.go
index e87b42f..4543d3d 100644
--- a/iputil/geo/geo.go
+++ b/iputil/geo/geo.go
@@ -10,6 +10,7 @@ import (
type Reader interface {
Country(net.IP) (Country, error)
City(net.IP) (City, error)
+ ASN(net.IP) (ASN, error)
IsEmpty() bool
}
@@ -25,13 +26,19 @@ type City struct {
Longitude float64
}
+type ASN struct {
+ AutonomousSystemNumber uint
+ AutonomousSystemOrganization string
+}
+
type geoip struct {
country *geoip2.Reader
city *geoip2.Reader
+ asn *geoip2.Reader
}
-func Open(countryDB, cityDB string) (Reader, error) {
- var country, city *geoip2.Reader
+func Open(countryDB, cityDB string, asnDB string) (Reader, error) {
+ var country, city, asn *geoip2.Reader
if countryDB != "" {
r, err := geoip2.Open(countryDB)
if err != nil {
@@ -46,7 +53,14 @@ func Open(countryDB, cityDB string) (Reader, error) {
}
city = r
}
- return &geoip{country: country, city: city}, nil
+ if asnDB != "" {
+ r, err := geoip2.Open(asnDB)
+ if err != nil {
+ return nil, err
+ }
+ asn = r
+ }
+ return &geoip{country: country, city: city, asn: asn}, nil
}
func (g *geoip) Country(ip net.IP) (Country, error) {
@@ -96,6 +110,24 @@ func (g *geoip) City(ip net.IP) (City, error) {
return city, nil
}
+func (g *geoip) ASN(ip net.IP) (ASN, error) {
+ asn := ASN{}
+ if g.asn == nil {
+ return asn, nil
+ }
+ record, err := g.asn.ASN(ip)
+ if err != nil {
+ return asn, err
+ }
+ if record.AutonomousSystemNumber > 0 {
+ asn.AutonomousSystemNumber = record.AutonomousSystemNumber
+ }
+ if record.AutonomousSystemOrganization != "" {
+ asn.AutonomousSystemOrganization = record.AutonomousSystemOrganization
+ }
+ return asn, nil
+}
+
func (g *geoip) IsEmpty() bool {
return g.country == nil && g.city == nil
}