Merge ASN lookup

Co-authored-by: raunsbaekdk <mike+git@raunsbaek.dk>
This commit is contained in:
Alphakilo 2019-06-10 21:26:35 +02:00
parent ab280dbeaa
commit e191c2eded
No known key found for this signature in database
GPG Key ID: 454F291F8E83602D
7 changed files with 59 additions and 17 deletions

2
.gitignore vendored
View File

@ -1,3 +1,5 @@
/data/
/custom.html
/vendor/
.vscode/
/bin/

View File

@ -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)

View File

@ -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

View File

@ -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

View File

@ -40,8 +40,8 @@ type Response struct {
Hostname string `json:"hostname,omitempty"`
Latitude float64 `json:"latitude,omitempty"`
Longitude float64 `json:"longitude,omitempty"`
AutonomousSystemNumber string `json:"asn_number,omitempty"`
AutonomousSystemOrganization string `json:"asn_organization,omitempty"`
ASN string `json:"asn,omitempty"`
ASNOrg string `json:"asn_org,omitempty"`
}
type PortResponse struct {
@ -102,7 +102,7 @@ func (s *Server) newResponse(r *http.Request) (Response, error) {
}
var autonomousSystemNumber string
if asn.AutonomousSystemNumber > 0 {
autonomousSystemNumber = "AS" + strconv.FormatUint(uint64(asn.AutonomousSystemNumber), 10);
autonomousSystemNumber = fmt.Sprintf("AS%d", asn.AutonomousSystemNumber)
}
return Response{
IP: ip,
@ -114,8 +114,8 @@ func (s *Server) newResponse(r *http.Request) (Response, error) {
Hostname: hostname,
Latitude: city.Latitude,
Longitude: city.Longitude,
AutonomousSystemNumber: autonomousSystemNumber,
AutonomousSystemOrganization: asn.AutonomousSystemOrganization,
ASN: autonomousSystemNumber,
ASNOrg: asn.AutonomousSystemOrganization,
}, nil
}
@ -182,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 {
@ -314,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

View File

@ -20,15 +20,15 @@ func (t *testDb) Country(net.IP) (geo.Country, error) {
return geo.Country{Name: "Elbonia", ISO: "EB", IsEU: new(bool)}, nil
}
func (t *testDb) City(net.IP) (database.City, error) {
return database.City{Name: "Bornyasherk"}, nil
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) (database.ASN, error) {
return database.ASN{AutonomousSystemNumber: 59795, AutonomousSystemOrganization: "Hosting4Real"}, 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 (t *testDb) IsEmpty() bool { return false }
func testServer() *Server {
return &Server{gr: &testDb{}, LookupAddr: lookupAddr, LookupPort: lookupPort}
@ -74,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 {
@ -133,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,"asn_number":"AS59795","asn_organization":"Hosting4Real"}`, 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},

View File

@ -6,8 +6,8 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="What is my IP address?">
<link href="https://fonts.googleapis.com/css?family=Open+Sans" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/pure/0.6.2/pure-min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/pure/0.6.2/grids-responsive-min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/pure/1.0.0/pure-min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/pure/1.0.0/grids-responsive-min.css">
<style>
html, .pure-g [class *= "pure-u"] {
background-color: white;
@ -28,6 +28,10 @@
font-size: 36px;
padding: 6px;
}
.footer {
border-top: 1px solid #eee;
background: rgb(250, 250, 250) !important;
}
</style>
</head>
<body>
@ -73,6 +77,15 @@ $ http {{ .Host }}/country-iso
<pre>
$ 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">
@ -127,5 +140,14 @@ $ 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 .ASN .ASNOrg }}
<div class="pure-g">
<div class="pure-u-1-1 footer">
<p><small>This product includes GeoLite2 data created by MaxMind,
available from
<a href="https://www.maxmind.com">https://www.maxmind.com</a>.</small></p>
</div>
</div>
{{ end }}
</body>
</html>