dashboard, p2p, vendor: visualize peers (#19247)
* dashboard, p2p: visualize peers * dashboard: change scale to green to red
This commit is contained in:
committed by
Péter Szilágyi
parent
1591b63306
commit
1a29bf0ee2
11
vendor/github.com/apilayer/freegeoip/AUTHORS
generated
vendored
Normal file
11
vendor/github.com/apilayer/freegeoip/AUTHORS
generated
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
# This is the official list of freegeoip authors for copyright purposes.
|
||||
# This file is distinct from the CONTRIBUTORS file.
|
||||
#
|
||||
# Names should be added to this file as
|
||||
# Name or Organization <email address>
|
||||
#
|
||||
# The email address is not required for organizations.
|
||||
#
|
||||
# Please keep the list sorted.
|
||||
|
||||
Alexandre Fiori <fiorix@gmail.com>
|
22
vendor/github.com/apilayer/freegeoip/CONTRIBUTORS
generated
vendored
Normal file
22
vendor/github.com/apilayer/freegeoip/CONTRIBUTORS
generated
vendored
Normal file
@@ -0,0 +1,22 @@
|
||||
# This is the official list of freegeoip contributors for copyright purposes.
|
||||
# This file is distinct from the AUTHORS file.
|
||||
#
|
||||
# Names should be added to this file as
|
||||
# Name or Organization <email address>
|
||||
#
|
||||
# Please keep the list sorted.
|
||||
#
|
||||
# Use the following command to generate the list:
|
||||
#
|
||||
# git shortlog -se | awk '{print $2 " " $3 " " $4}'
|
||||
#
|
||||
# The email address is not required for organizations.
|
||||
|
||||
Alex Goretoy <alex@goretoy.com>
|
||||
Gleicon Moraes <gleicon@gmail.com>
|
||||
Leandro Pereira <leandro@hardinfo.org>
|
||||
Lucas Fontes <lxfontes@gmail.com>
|
||||
Matthias Nehlsen <matthias.nehlsen@gmail.com>
|
||||
Melchi <melchi.si@gmail.com>
|
||||
Nick Muerdter <stuff@nickm.org>
|
||||
Vladimir Agafonkin <agafonkin@gmail.com>
|
25
vendor/github.com/apilayer/freegeoip/Dockerfile
generated
vendored
Normal file
25
vendor/github.com/apilayer/freegeoip/Dockerfile
generated
vendored
Normal file
@@ -0,0 +1,25 @@
|
||||
FROM golang:1.9
|
||||
|
||||
COPY cmd/freegeoip/public /var/www
|
||||
|
||||
ADD . /go/src/github.com/apilayer/freegeoip
|
||||
RUN \
|
||||
cd /go/src/github.com/apilayer/freegeoip/cmd/freegeoip && \
|
||||
go get -d && go install && \
|
||||
apt-get update && apt-get install -y libcap2-bin && \
|
||||
setcap cap_net_bind_service=+ep /go/bin/freegeoip && \
|
||||
apt-get clean && rm -rf /var/lib/apt/lists/* && \
|
||||
useradd -ms /bin/bash freegeoip
|
||||
|
||||
USER freegeoip
|
||||
ENTRYPOINT ["/go/bin/freegeoip"]
|
||||
|
||||
EXPOSE 8080
|
||||
|
||||
# CMD instructions:
|
||||
# Add "-use-x-forwarded-for" if your server is behind a reverse proxy
|
||||
# Add "-public", "/var/www" to enable the web front-end
|
||||
# Add "-internal-server", "8888" to enable the pprof+metrics server
|
||||
#
|
||||
# Example:
|
||||
# CMD ["-use-x-forwarded-for", "-public", "/var/www", "-internal-server", "8888"]
|
55
vendor/github.com/apilayer/freegeoip/HISTORY.md
generated
vendored
Normal file
55
vendor/github.com/apilayer/freegeoip/HISTORY.md
generated
vendored
Normal file
@@ -0,0 +1,55 @@
|
||||
# History of freegeoip.net
|
||||
|
||||
The freegeoip software is the result of a web server research project that
|
||||
started in 2009, written in Python and hosted on
|
||||
[Google App Engine](http://appengine.google.com). It was rapidly adopted by
|
||||
many developers around the world due to its simplistic and straightforward
|
||||
HTTP API, causing the free account on GAE to exceed its quota every day
|
||||
after few hours of operation.
|
||||
|
||||
A year later freegeoip 1.0 was released, and the freegeoip.net domain
|
||||
moved over to its own server infrastructure. The software was rewritten
|
||||
using the [Cyclone](http://cyclone.io) web framework, backed by
|
||||
[Twisted](http://twistedmatrix.com) and [PyPy](http://pypy.org) in
|
||||
production. That's when the first database management tool was created,
|
||||
a script that would download many pieces of information from the Internet
|
||||
to create the IP database, an sqlite flat file used by the server.
|
||||
|
||||
This version of the Python server shipped with a much better front-end as
|
||||
well, but still as a server-side rendered template inherited from the GAE
|
||||
version. It was only circa 2011 that freegeoip got its first standalone
|
||||
front-end based on jQuery, and is when Twitter bootstrap was first used.
|
||||
|
||||
Python played an important role in the early life of freegeoip and
|
||||
allowed the service to grow and evolve fast. It provided a lot of
|
||||
flexibility in building and maintaining the IP database using multiple
|
||||
sources of data. This version of the server lasted until 2013, when
|
||||
it was once again rewritten from scratch, this time in Go. The database
|
||||
tool, however, remained intact.
|
||||
|
||||
In 2013 the Go version was released as freegeoip 2.0 and this version
|
||||
had many iterations. The first versions of the server written in Go were
|
||||
very rustic, practically a verbatim transcription of the Python server.
|
||||
Took a while until it started looking more like common Go code, and to
|
||||
have tests.
|
||||
|
||||
Another important change that shipped with v2 was a front-end based on
|
||||
AngularJS, but still mixed with some jQuery. The Google map in the front
|
||||
page was made optional to put more focus on the HTTP API. The popularity
|
||||
of freegeoip has increased considerably over the years of 2013 and 2014,
|
||||
calling for more.
|
||||
|
||||
Enter freegeoip 3.0, an evolution of the Go server. The foundation of
|
||||
freegeoip, which is the IP database and HTTP API, now lives in a Go
|
||||
package that other developers can leverage. The freegeoip web server is
|
||||
built on this package making its code cleaner, the server faster,
|
||||
and requires zero maintenance for the IP database. The server downloads
|
||||
the file from MaxMind and keep it up to date in background.
|
||||
|
||||
This and other changes make it very Docker friendly.
|
||||
|
||||
The front-end has been trimmed down to a single index.html file that loads
|
||||
CSS and JS from CDNs on the internet. The JS part is based on AngularJS
|
||||
and handles the search request and response of the public site. The
|
||||
optional map has become a link to Google Maps following the lat/long
|
||||
of the query results.
|
27
vendor/github.com/apilayer/freegeoip/LICENSE
generated
vendored
Normal file
27
vendor/github.com/apilayer/freegeoip/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,27 @@
|
||||
Copyright (c) 2009 The freegeoip authors. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are
|
||||
met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above
|
||||
copyright notice, this list of conditions and the following disclaimer
|
||||
in the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
* The names of authors or contributors may NOT be used to endorse or
|
||||
promote products derived from this software without specific prior
|
||||
written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
1
vendor/github.com/apilayer/freegeoip/Procfile
generated
vendored
Normal file
1
vendor/github.com/apilayer/freegeoip/Procfile
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
web: freegeoip -http :${PORT} -use-x-forwarded-for -public /app/cmd/freegeoip/public -quota-backend map -quota-max 10000
|
259
vendor/github.com/apilayer/freegeoip/README.md
generated
vendored
Normal file
259
vendor/github.com/apilayer/freegeoip/README.md
generated
vendored
Normal file
@@ -0,0 +1,259 @@
|
||||

|
||||
|
||||
# freegeoip - Important Announcement
|
||||
|
||||
*[The old freegeoip API is now deprecated and will be discontinued on July 1st, 2018]*
|
||||
|
||||
Launched more than 6 years ago, the freegeoip.net API has grown into one of the biggest and most widely used APIs for IP to location services worldwide. The API is used by thousands of developers, SMBs and large corporations around the globe and is currently handling more than 2 billion requests per day. After years of operation and the API remaining almost unchanged, today we announce the complete re-launch of freegeoip into a faster, more advanced and more scalable API service called ipstack (https://ipstack.com). All users that wish to continue using our IP to location service will be required to sign up to obtain a free API access key and perform a few simple changes to their integration. While the new API offers the ability to return data in the same structure as the old freegeoip API, the new API structure offers various options of delivering much more advanced data for IP Addresses.
|
||||
|
||||
## Required Changes to Legacy Integrations (freegeoip.net/json/xml)
|
||||
|
||||
As of March 31 2018 the old freegeoip API is deprecated and a completely re-designed API is now accessible at http://api.ipstack.com. While the new API offers the same capabilities as the old one and also has the option of returning data in the legacy format, the API URL has now changed and all users are required to sign up for a free API Access Key to use the service.
|
||||
|
||||
1. Get a free ipstack Account and Access Key
|
||||
|
||||
Head over to https://ipstack.com and follow the instructions to create your account and obtain your access token. If you only need basic IP to Geolocation data and do not require more than 10,000 requests per month, you can use the free account. If you'd like more advanced features or more requests than included in the free account you will need to choose one of the paid options. You can find an overview of all available plans at https://ipstack.com/product
|
||||
|
||||
2. Integrate the new API URL
|
||||
|
||||
The new API comes with a completely new endpoint (api.ipstack.com) and requires you to append your API Access Key to the URL as a GET parameter. For complete integration instructions, please head over to the API Documentation at https://ipstack.com/documentation. While the new API offers a completely reworked response structure with many additional data points, we also offer the option to receive results in the old freegeoip.net format in JSON or XML.
|
||||
|
||||
To receive your API results in the old freegeoip format, please simply append &legacy=1 to the new API URL.
|
||||
|
||||
JSON Example: http://api.ipstack.com/186.116.207.169?access_key=YOUR_ACCESS_KEY&output=json&legacy=1
|
||||
|
||||
XML Example: http://api.ipstack.com/186.116.207.169?access_key=YOUR_ACCESS_KEY&output=xml&legacy=1
|
||||
|
||||
## New features with ipstack
|
||||
While the new ipstack service now runs on a commercial/freemium model, we have worked hard at building a faster, more scalable, and more advanced IP to location API product. You can read more about all the new features by navigating to https://ipstack.com, but here's a list of the most important changes and additions:
|
||||
|
||||
- We're still free for basic usage
|
||||
|
||||
While we now offer paid / premium options for our more advanced users, our core product and IP to Country/Region/City product is still completely free of charge for up to 10,000 requests per month. If you need more advanced data or more requests, you can choose one of the paid plans listed at https://ipstack.com/product
|
||||
|
||||
- Batch Requests
|
||||
|
||||
Need to validate more than 1 IP Address in a single API Call? Our new Bulk Lookup Feature (available on our paid plans) allows you to geolocate up to 50 IP Addresses in a single API Call.
|
||||
|
||||
- Much more Data
|
||||
|
||||
While the old freegeoip API was limited to provide only the most basic IP to location data, our new API provides more than 20 additional data points including Language, Time Zone, Current Time, Currencies, Connection & ASN Information, and much more. To learn more about all the data points available, please head over to the ipstack website.
|
||||
|
||||
- Security & Fraud Prevention Tools
|
||||
|
||||
Do you want to prevent fraudulent traffic from arriving at your website or from abusing your service? Easily spot malicious / proxy / VPN traffic by using our new Security Module, which outputs a lot of valuable security information about an IP Address.
|
||||
|
||||
Next Steps
|
||||
|
||||
- Deprecation of the old API
|
||||
|
||||
While we want to keep the disruption to our current users as minimal as possible, we are planning to shut the old API down on July 1st, 2018. This should give all users enough time to adapt to changes, and should we still see high volumes of traffic going to the old API by that date, we may decide to extend it further. In any case, we highly recommend you switch to the new API as soon as possible. We will keep you posted here about any changes to the planned shutdown date.
|
||||
|
||||
- Any Questions? Please get in touch!
|
||||
|
||||
It's very important to ensure a smooth transition to ipstack for all freegeoip API users. If you are a developer that has published a plugin/addon that includes the legacy API, we recommend you get in touch with us and also share this announcement with your users. If you have any questions about the transition or the new API, please get in touch with us at support@ipstack.com
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# freegeoip - Deprecated Documentation
|
||||
|
||||
[](https://heroku.com/deploy)
|
||||
|
||||
This is the source code of the freegeoip software. It contains both the web server that empowers freegeoip.net, and a package for the [Go](http://golang.org) programming language that enables any web server to support IP geolocation with a simple and clean API.
|
||||
|
||||
See http://en.wikipedia.org/wiki/Geolocation for details about geolocation.
|
||||
|
||||
Developers looking for the Go API can skip to the [Package freegeoip](#packagefreegeoip) section below.
|
||||
|
||||
## Running
|
||||
|
||||
This section is for people who desire to run the freegeoip web server on their own infrastructure. The easiest and most generic way of doing this is by using Docker. All examples below use Docker.
|
||||
|
||||
### Docker
|
||||
|
||||
#### Install Docker
|
||||
|
||||
Docker has [install instructions for many platforms](https://docs.docker.com/engine/installation/),
|
||||
including
|
||||
- [Ubuntu](https://docs.docker.com/engine/installation/linux/docker-ce/ubuntu/)
|
||||
- [CentOS](https://docs.docker.com/engine/installation/linux/docker-ce/centos/)
|
||||
- [Mac](https://docs.docker.com/docker-for-mac/install/)
|
||||
|
||||
#### Run the API in a container
|
||||
|
||||
```bash
|
||||
docker run --restart=always -p 8080:8080 -d apilayer/freegeoip
|
||||
```
|
||||
|
||||
#### Test
|
||||
|
||||
```bash
|
||||
curl localhost:8080/json/1.2.3.4
|
||||
# => {"ip":"1.2.3.4","country_code":"US","country_name":"United States", # ...
|
||||
```
|
||||
|
||||
### Other Linux, OS X, FreeBSD, and Windows
|
||||
|
||||
There are [pre-compiled binaries](https://github.com/apilayer/freegeoip/releases) available.
|
||||
|
||||
### Production configuration
|
||||
|
||||
For production workloads you may want to use different configuration for the freegeoip web server, for example:
|
||||
|
||||
* Enabling the "internal server" for collecting metrics and profiling/tracing the freegeoip web server on demand
|
||||
* Monitoring the internal server using [Prometheus](https://prometheus.io), or exporting your metrics to [New Relic](https://newrelic.com)
|
||||
* Serving the freegeoip API over HTTPS (TLS) using your own certificates, or provisioned automatically using [LetsEncrypt.org](https://letsencrypt.org)
|
||||
* Configuring [HSTS](https://en.wikipedia.org/wiki/HTTP_Strict_Transport_Security) to restrict your browser clients to always use HTTPS
|
||||
* Configuring the read and write timeouts to avoid stale clients consuming server resources
|
||||
* Configuring the freegeoip web server to read the client IP (for logs, etc) from the X-Forwarded-For header when running behind a reverse proxy
|
||||
* Configuring [CORS](https://en.wikipedia.org/wiki/Cross-origin_resource_sharing) to restrict access to your API to specific domains
|
||||
* Configuring a specific endpoint path prefix other than the default "/" (thus /json, /xml, /csv) to serve the API alongside other APIs on the same host
|
||||
* Optimizing your round trips by enabling [TCP Fast Open](https://en.wikipedia.org/wiki/TCP_Fast_Open) on your OS and the freegeoip web server
|
||||
* Setting up usage limits (quotas) for your clients (per client IP) based on requests per time interval; we support various backends such as in-memory map (for single instance), or redis or memcache for distributed deployments
|
||||
* Serve the default [GeoLite2 City](http://dev.maxmind.com/geoip/geoip2/geolite2/) free database that is downloaded and updated automatically in background on a configurable schedule, or
|
||||
* Serve the commercial [GeoIP2 City](https://www.maxmind.com/en/geoip2-city) database from MaxMind, either as a local file that you provide and update periodically (so the server can reload it), or configured to be downloaded periodically using your API key
|
||||
|
||||
See the [Server Options](#serveroptions) section below for more information on configuring the server.
|
||||
|
||||
For automation, check out the [freegeoip chef cookbook](https://supermarket.chef.io/cookbooks/freegeoip) or the (legacy) [Ansible Playbook](./cmd/freegeoip/ansible-playbook) for Ubuntu 14.04 LTS.
|
||||
|
||||
<a name="serveroptions">
|
||||
|
||||
### Server Options
|
||||
|
||||
To see all the available options, use the `-help` option:
|
||||
|
||||
```bash
|
||||
docker run --rm -it apilayer/freegeoip -help
|
||||
```
|
||||
|
||||
If you're using LetsEncrypt.org to provision your TLS certificates, you have to listen for HTTPS on port 443. Following is an example of the server listening on 3 different ports: metrics + pprof (8888), http (80), and https (443):
|
||||
|
||||
```bash
|
||||
docker run -p 8888:8888 -p 80:8080 -p 443:8443 -d apilayer/freegeoip \
|
||||
-internal-server=:8888 \
|
||||
-http=:8080 \
|
||||
-https=:8443 \
|
||||
-hsts=max-age=31536000 \
|
||||
-letsencrypt \
|
||||
-letsencrypt-hosts=myfancydomain.io
|
||||
```
|
||||
|
||||
You can configure the freegeiop web server via command line flags or environment variables. The names of environment variables are the same for command line flags, but prefixed with FREEGEOIP, all upperscase, separated by underscores. If you want to use environment variables instead:
|
||||
|
||||
```bash
|
||||
$ cat prod.env
|
||||
FREEGEOIP_INTERNAL_SERVER=:8888
|
||||
FREEGEOIP_HTTP=:8080
|
||||
FREEGEOIP_HTTPS=:8443
|
||||
FREEGEOIP_HSTS=max-age=31536000
|
||||
FREEGEOIP_LETSENCRYPT=true
|
||||
FREEGEOIP_LETSENCRYPT_HOSTS=myfancydomain.io
|
||||
|
||||
$ docker run --env-file=prod.env -p 8888:8888 -p 80:8080 -p 443:8443 -d apilayer/freegeoip
|
||||
```
|
||||
|
||||
By default, HTTP/2 is enabled over HTTPS. You can disable by passing the `-http2=false` flag.
|
||||
|
||||
Also, the Docker image of freegeoip does not provide the web page from freegeiop.net, it only provides the API. If you want to serve that page, you can pass the `-public=/var/www` parameter in the command line. You can also tell Docker to mount that directory as a volume on the host machine and have it serve your own page, using Docker's `-v` parameter.
|
||||
|
||||
If the freegeoip web server is running behind a reverse proxy or load balancer, you have to run it passing the `-use-x-forwarded-for` parameter and provide the `X-Forwarded-For` HTTP header in all requests. This is for the freegeoip web server be able to log the client IP, and to perform geolocation lookups when an IP is not provided to the API, e.g. `/json/` (uses client IP) vs `/json/1.2.3.4`.
|
||||
|
||||
## Database
|
||||
|
||||
The current implementation uses the free [GeoLite2 City](http://dev.maxmind.com/geoip/geoip2/geolite2/) database from MaxMind.
|
||||
|
||||
In the past we had databases from other providers, and at some point even our own database comprised of data from different sources. This means it might change in the future.
|
||||
|
||||
If you have purchased the commercial database from MaxMind, you can point the freegeoip web server or (Go API, for dev) to the URL containing the file, or local file, and the server will use it.
|
||||
|
||||
In case of files on disk, you can replace the file with a newer version and the freegeoip web server will reload it automatically in background. If instead of a file you use a URL (the default), we periodically check the URL in background to see if there's a new database version available, then download the reload it automatically.
|
||||
|
||||
All responses from the freegeiop API contain the date that the database was downloaded in the X-Database-Date HTTP header.
|
||||
|
||||
## API
|
||||
|
||||
The freegeoip API is served by endpoints that encode the response in different formats.
|
||||
|
||||
Example:
|
||||
|
||||
```bash
|
||||
curl freegeoip.net/json/
|
||||
```
|
||||
|
||||
Returns the geolocation information of your own IP address, the source IP address of the connection.
|
||||
|
||||
You can pass a different IP or hostname. For example, to lookup the geolocation of `github.com` the server resolves the name first, then uses the first IP address available, which might be IPv4 or IPv6:
|
||||
|
||||
```bash
|
||||
curl freegeoip.net/json/github.com
|
||||
```
|
||||
|
||||
Same semantics are available for the `/xml/{ip}` and `/csv/{ip}` endpoints.
|
||||
|
||||
JSON responses can be encoded as JSONP, by adding the `callback` parameter:
|
||||
|
||||
```bash
|
||||
curl freegeoip.net/json/?callback=foobar
|
||||
```
|
||||
|
||||
The callback parameter is ignored on all other endpoints.
|
||||
|
||||
## Metrics and profiling
|
||||
|
||||
The freegeoip web server can provide metrics about its usage, and also supports runtime profiling and tracing.
|
||||
|
||||
Both are disabled by default, but can be enabled by passing the `-internal-server` parameter in the command line. Metrics are generated for [Prometheus](http://prometheus.io) and can be queried at `/metrics` even with curl.
|
||||
|
||||
HTTP pprof is available at `/debug/pprof` and the examples from the [pprof](https://golang.org/pkg/net/http/pprof/) package documentation should work on the freegeiop web server.
|
||||
|
||||
<a name="packagefreegeoip">
|
||||
|
||||
## Package freegeoip
|
||||
|
||||
The freegeoip package for the Go programming language provides two APIs:
|
||||
|
||||
- A database API that requires zero maintenance of the IP database;
|
||||
- A geolocation `http.Handler` that can be used/served by any http server.
|
||||
|
||||
tl;dr if all you want is code then see the `example_test.go` file.
|
||||
|
||||
Otherwise check out the godoc reference.
|
||||
|
||||
[](https://godoc.org/github.com/apilayer/freegeoip)
|
||||
[](http://travis-ci.org/apilayer/freegeoip)
|
||||
[](https://goreportcard.com/report/github.com/apilayer/freegeoip)
|
||||
|
||||
### Features
|
||||
|
||||
- Zero maintenance
|
||||
|
||||
The DB object alone can download an IP database file from the internet and service lookups to your program right away. It will auto-update the file in background and always magically work.
|
||||
|
||||
- DevOps friendly
|
||||
|
||||
If you do care about the database and have the commercial version of the MaxMind database, you can update the database file with your program running and the DB object will load it in background. You can focus on your stuff.
|
||||
|
||||
- Extensible
|
||||
|
||||
Besides the database part, the package provides an `http.Handler` object that you can add to your HTTP server to service IP geolocation lookups with the same simplistic API of freegeoip.net. There's also an interface for crafting your own HTTP responses encoded in any format.
|
||||
|
||||
### Install
|
||||
|
||||
Download the package:
|
||||
|
||||
go get -d github.com/apilayer/freegeoip/...
|
||||
|
||||
Install the web server:
|
||||
|
||||
go install github.com/apilayer/freegeoip/cmd/freegeoip
|
||||
|
||||
Test coverage is quite good, and test code may help you find the stuff you need.
|
7
vendor/github.com/apilayer/freegeoip/app.json
generated
vendored
Normal file
7
vendor/github.com/apilayer/freegeoip/app.json
generated
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"name": "freegeoip",
|
||||
"description": "IP geolocation web server",
|
||||
"website": "https://github.com/apilayer/freegeoip",
|
||||
"success_url": "/",
|
||||
"keywords": ["golang", "geoip", "api"]
|
||||
}
|
453
vendor/github.com/apilayer/freegeoip/db.go
generated
vendored
Normal file
453
vendor/github.com/apilayer/freegeoip/db.go
generated
vendored
Normal file
@@ -0,0 +1,453 @@
|
||||
// Copyright 2009 The freegeoip authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
package freegeoip
|
||||
|
||||
import (
|
||||
"compress/gzip"
|
||||
"crypto/md5"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"math"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/howeyc/fsnotify"
|
||||
"github.com/oschwald/maxminddb-golang"
|
||||
)
|
||||
|
||||
var (
|
||||
// ErrUnavailable may be returned by DB.Lookup when the database
|
||||
// points to a URL and is not yet available because it's being
|
||||
// downloaded in background.
|
||||
ErrUnavailable = errors.New("no database available")
|
||||
|
||||
// Local cached copy of a database downloaded from a URL.
|
||||
defaultDB = filepath.Join(os.TempDir(), "freegeoip", "db.gz")
|
||||
|
||||
// MaxMindDB is the URL of the free MaxMind GeoLite2 database.
|
||||
MaxMindDB = "http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.mmdb.gz"
|
||||
)
|
||||
|
||||
// DB is the IP geolocation database.
|
||||
type DB struct {
|
||||
file string // Database file name.
|
||||
checksum string // MD5 of the unzipped database file
|
||||
reader *maxminddb.Reader // Actual db object.
|
||||
notifyQuit chan struct{} // Stop auto-update and watch goroutines.
|
||||
notifyOpen chan string // Notify when a db file is open.
|
||||
notifyError chan error // Notify when an error occurs.
|
||||
notifyInfo chan string // Notify random actions for logging
|
||||
closed bool // Mark this db as closed.
|
||||
lastUpdated time.Time // Last time the db was updated.
|
||||
mu sync.RWMutex // Protects all the above.
|
||||
|
||||
updateInterval time.Duration // Update interval.
|
||||
maxRetryInterval time.Duration // Max retry interval in case of failure.
|
||||
}
|
||||
|
||||
// Open creates and initializes a DB from a local file.
|
||||
//
|
||||
// The database file is monitored by fsnotify and automatically
|
||||
// reloads when the file is updated or overwritten.
|
||||
func Open(dsn string) (*DB, error) {
|
||||
db := &DB{
|
||||
file: dsn,
|
||||
notifyQuit: make(chan struct{}),
|
||||
notifyOpen: make(chan string, 1),
|
||||
notifyError: make(chan error, 1),
|
||||
notifyInfo: make(chan string, 1),
|
||||
}
|
||||
err := db.openFile()
|
||||
if err != nil {
|
||||
db.Close()
|
||||
return nil, err
|
||||
}
|
||||
err = db.watchFile()
|
||||
if err != nil {
|
||||
db.Close()
|
||||
return nil, fmt.Errorf("fsnotify failed for %s: %s", dsn, err)
|
||||
}
|
||||
return db, nil
|
||||
}
|
||||
|
||||
// MaxMindUpdateURL generates the URL for MaxMind paid databases.
|
||||
func MaxMindUpdateURL(hostname, productID, userID, licenseKey string) (string, error) {
|
||||
limiter := func(r io.Reader) *io.LimitedReader {
|
||||
return &io.LimitedReader{R: r, N: 1 << 30}
|
||||
}
|
||||
baseurl := "https://" + hostname + "/app/"
|
||||
// Get the file name for the product ID.
|
||||
u := baseurl + "update_getfilename?product_id=" + productID
|
||||
resp, err := http.Get(u)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
md5hash := md5.New()
|
||||
_, err = io.Copy(md5hash, limiter(resp.Body))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
sum := md5hash.Sum(nil)
|
||||
hexdigest1 := hex.EncodeToString(sum[:])
|
||||
// Get our client IP address.
|
||||
resp, err = http.Get(baseurl + "update_getipaddr")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
md5hash = md5.New()
|
||||
io.WriteString(md5hash, licenseKey)
|
||||
_, err = io.Copy(md5hash, limiter(resp.Body))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
sum = md5hash.Sum(nil)
|
||||
hexdigest2 := hex.EncodeToString(sum[:])
|
||||
// Generate the URL.
|
||||
params := url.Values{
|
||||
"db_md5": {hexdigest1},
|
||||
"challenge_md5": {hexdigest2},
|
||||
"user_id": {userID},
|
||||
"edition_id": {productID},
|
||||
}
|
||||
u = baseurl + "update_secure?" + params.Encode()
|
||||
return u, nil
|
||||
}
|
||||
|
||||
// OpenURL creates and initializes a DB from a URL.
|
||||
// It automatically downloads and updates the file in background, and
|
||||
// keeps a local copy on $TMPDIR.
|
||||
func OpenURL(url string, updateInterval, maxRetryInterval time.Duration) (*DB, error) {
|
||||
db := &DB{
|
||||
file: defaultDB,
|
||||
notifyQuit: make(chan struct{}),
|
||||
notifyOpen: make(chan string, 1),
|
||||
notifyError: make(chan error, 1),
|
||||
notifyInfo: make(chan string, 1),
|
||||
updateInterval: updateInterval,
|
||||
maxRetryInterval: maxRetryInterval,
|
||||
}
|
||||
db.openFile() // Optional, might fail.
|
||||
go db.autoUpdate(url)
|
||||
err := db.watchFile()
|
||||
if err != nil {
|
||||
db.Close()
|
||||
return nil, fmt.Errorf("fsnotify failed for %s: %s", db.file, err)
|
||||
}
|
||||
return db, nil
|
||||
}
|
||||
|
||||
func (db *DB) watchFile() error {
|
||||
watcher, err := fsnotify.NewWatcher()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
dbdir, err := db.makeDir()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
go db.watchEvents(watcher)
|
||||
return watcher.Watch(dbdir)
|
||||
}
|
||||
|
||||
func (db *DB) watchEvents(watcher *fsnotify.Watcher) {
|
||||
for {
|
||||
select {
|
||||
case ev := <-watcher.Event:
|
||||
if ev.Name == db.file && (ev.IsCreate() || ev.IsModify()) {
|
||||
db.openFile()
|
||||
}
|
||||
case <-watcher.Error:
|
||||
case <-db.notifyQuit:
|
||||
watcher.Close()
|
||||
return
|
||||
}
|
||||
time.Sleep(time.Second) // Suppress high-rate events.
|
||||
}
|
||||
}
|
||||
|
||||
func (db *DB) openFile() error {
|
||||
reader, checksum, err := db.newReader(db.file)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
stat, err := os.Stat(db.file)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
db.setReader(reader, stat.ModTime(), checksum)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (db *DB) newReader(dbfile string) (*maxminddb.Reader, string, error) {
|
||||
f, err := os.Open(dbfile)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
defer f.Close()
|
||||
gzf, err := gzip.NewReader(f)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
defer gzf.Close()
|
||||
b, err := ioutil.ReadAll(gzf)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
checksum := fmt.Sprintf("%x", md5.Sum(b))
|
||||
mmdb, err := maxminddb.FromBytes(b)
|
||||
return mmdb, checksum, err
|
||||
}
|
||||
|
||||
func (db *DB) setReader(reader *maxminddb.Reader, modtime time.Time, checksum string) {
|
||||
db.mu.Lock()
|
||||
defer db.mu.Unlock()
|
||||
if db.closed {
|
||||
reader.Close()
|
||||
return
|
||||
}
|
||||
if db.reader != nil {
|
||||
db.reader.Close()
|
||||
}
|
||||
db.reader = reader
|
||||
db.lastUpdated = modtime.UTC()
|
||||
db.checksum = checksum
|
||||
select {
|
||||
case db.notifyOpen <- db.file:
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
||||
func (db *DB) autoUpdate(url string) {
|
||||
backoff := time.Second
|
||||
for {
|
||||
db.sendInfo("starting update")
|
||||
err := db.runUpdate(url)
|
||||
if err != nil {
|
||||
bs := backoff.Seconds()
|
||||
ms := db.maxRetryInterval.Seconds()
|
||||
backoff = time.Duration(math.Min(bs*math.E, ms)) * time.Second
|
||||
db.sendError(fmt.Errorf("download failed (will retry in %s): %s", backoff, err))
|
||||
} else {
|
||||
backoff = db.updateInterval
|
||||
}
|
||||
db.sendInfo("finished update")
|
||||
select {
|
||||
case <-db.notifyQuit:
|
||||
return
|
||||
case <-time.After(backoff):
|
||||
// Sleep till time for the next update attempt.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (db *DB) runUpdate(url string) error {
|
||||
yes, err := db.needUpdate(url)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !yes {
|
||||
return nil
|
||||
}
|
||||
tmpfile, err := db.download(url)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = db.renameFile(tmpfile)
|
||||
if err != nil {
|
||||
// Cleanup the tempfile if renaming failed.
|
||||
os.RemoveAll(tmpfile)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (db *DB) needUpdate(url string) (bool, error) {
|
||||
stat, err := os.Stat(db.file)
|
||||
if err != nil {
|
||||
return true, nil // Local db is missing, must be downloaded.
|
||||
}
|
||||
|
||||
resp, err := http.Head(url)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
// Check X-Database-MD5 if it exists
|
||||
headerMd5 := resp.Header.Get("X-Database-MD5")
|
||||
if len(headerMd5) > 0 && db.checksum != headerMd5 {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
if stat.Size() != resp.ContentLength {
|
||||
return true, nil
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func (db *DB) download(url string) (tmpfile string, err error) {
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
tmpfile = filepath.Join(os.TempDir(),
|
||||
fmt.Sprintf("_freegeoip.%d.db.gz", time.Now().UnixNano()))
|
||||
f, err := os.Create(tmpfile)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer f.Close()
|
||||
_, err = io.Copy(f, resp.Body)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return tmpfile, nil
|
||||
}
|
||||
|
||||
func (db *DB) makeDir() (dbdir string, err error) {
|
||||
dbdir = filepath.Dir(db.file)
|
||||
_, err = os.Stat(dbdir)
|
||||
if err != nil {
|
||||
err = os.MkdirAll(dbdir, 0755)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
return dbdir, nil
|
||||
}
|
||||
|
||||
func (db *DB) renameFile(name string) error {
|
||||
os.Rename(db.file, db.file+".bak") // Optional, might fail.
|
||||
_, err := db.makeDir()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return os.Rename(name, db.file)
|
||||
}
|
||||
|
||||
// Date returns the UTC date the database file was last modified.
|
||||
// If no database file has been opened the behaviour of Date is undefined.
|
||||
func (db *DB) Date() time.Time {
|
||||
db.mu.RLock()
|
||||
defer db.mu.RUnlock()
|
||||
return db.lastUpdated
|
||||
}
|
||||
|
||||
// NotifyClose returns a channel that is closed when the database is closed.
|
||||
func (db *DB) NotifyClose() <-chan struct{} {
|
||||
return db.notifyQuit
|
||||
}
|
||||
|
||||
// NotifyOpen returns a channel that notifies when a new database is
|
||||
// loaded or reloaded. This can be used to monitor background updates
|
||||
// when the DB points to a URL.
|
||||
func (db *DB) NotifyOpen() (filename <-chan string) {
|
||||
return db.notifyOpen
|
||||
}
|
||||
|
||||
// NotifyError returns a channel that notifies when an error occurs
|
||||
// while downloading or reloading a DB that points to a URL.
|
||||
func (db *DB) NotifyError() (errChan <-chan error) {
|
||||
return db.notifyError
|
||||
}
|
||||
|
||||
// NotifyInfo returns a channel that notifies informational messages
|
||||
// while downloading or reloading.
|
||||
func (db *DB) NotifyInfo() <-chan string {
|
||||
return db.notifyInfo
|
||||
}
|
||||
|
||||
func (db *DB) sendError(err error) {
|
||||
db.mu.RLock()
|
||||
defer db.mu.RUnlock()
|
||||
if db.closed {
|
||||
return
|
||||
}
|
||||
select {
|
||||
case db.notifyError <- err:
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
||||
func (db *DB) sendInfo(message string) {
|
||||
db.mu.RLock()
|
||||
defer db.mu.RUnlock()
|
||||
if db.closed {
|
||||
return
|
||||
}
|
||||
select {
|
||||
case db.notifyInfo <- message:
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
||||
// Lookup performs a database lookup of the given IP address, and stores
|
||||
// the response into the result value. The result value must be a struct
|
||||
// with specific fields and tags as described here:
|
||||
// https://godoc.org/github.com/oschwald/maxminddb-golang#Reader.Lookup
|
||||
//
|
||||
// See the DefaultQuery for an example of the result struct.
|
||||
func (db *DB) Lookup(addr net.IP, result interface{}) error {
|
||||
db.mu.RLock()
|
||||
defer db.mu.RUnlock()
|
||||
if db.reader != nil {
|
||||
return db.reader.Lookup(addr, result)
|
||||
}
|
||||
return ErrUnavailable
|
||||
}
|
||||
|
||||
// DefaultQuery is the default query used for database lookups.
|
||||
type DefaultQuery struct {
|
||||
Continent struct {
|
||||
Names map[string]string `maxminddb:"names"`
|
||||
} `maxminddb:"continent"`
|
||||
Country struct {
|
||||
ISOCode string `maxminddb:"iso_code"`
|
||||
Names map[string]string `maxminddb:"names"`
|
||||
} `maxminddb:"country"`
|
||||
Region []struct {
|
||||
ISOCode string `maxminddb:"iso_code"`
|
||||
Names map[string]string `maxminddb:"names"`
|
||||
} `maxminddb:"subdivisions"`
|
||||
City struct {
|
||||
Names map[string]string `maxminddb:"names"`
|
||||
} `maxminddb:"city"`
|
||||
Location struct {
|
||||
Latitude float64 `maxminddb:"latitude"`
|
||||
Longitude float64 `maxminddb:"longitude"`
|
||||
MetroCode uint `maxminddb:"metro_code"`
|
||||
TimeZone string `maxminddb:"time_zone"`
|
||||
} `maxminddb:"location"`
|
||||
Postal struct {
|
||||
Code string `maxminddb:"code"`
|
||||
} `maxminddb:"postal"`
|
||||
}
|
||||
|
||||
// Close closes the database.
|
||||
func (db *DB) Close() {
|
||||
db.mu.Lock()
|
||||
defer db.mu.Unlock()
|
||||
if !db.closed {
|
||||
db.closed = true
|
||||
close(db.notifyQuit)
|
||||
close(db.notifyOpen)
|
||||
close(db.notifyError)
|
||||
close(db.notifyInfo)
|
||||
}
|
||||
if db.reader != nil {
|
||||
db.reader.Close()
|
||||
db.reader = nil
|
||||
}
|
||||
}
|
14
vendor/github.com/apilayer/freegeoip/doc.go
generated
vendored
Normal file
14
vendor/github.com/apilayer/freegeoip/doc.go
generated
vendored
Normal file
@@ -0,0 +1,14 @@
|
||||
// Copyright 2009 The freegeoip authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
// Package freegeoip provides an API for searching the geolocation of IP
|
||||
// addresses. It uses a database that can be either a local file or a
|
||||
// remote resource from a URL.
|
||||
//
|
||||
// Local databases are monitored by fsnotify and reloaded when the file is
|
||||
// either updated or overwritten.
|
||||
//
|
||||
// Remote databases are automatically downloaded and updated in background
|
||||
// so you can focus on using the API and not managing the database.
|
||||
package freegeoip
|
BIN
vendor/github.com/apilayer/freegeoip/freegeo-warning.png
generated
vendored
Normal file
BIN
vendor/github.com/apilayer/freegeoip/freegeo-warning.png
generated
vendored
Normal file
Binary file not shown.
After Width: | Height: | Size: 14 KiB |
Reference in New Issue
Block a user