Open-GeoIP is a simple and high-performance IP address geographic information query service
install and run
Binary runs directly
exist release Download the latest from [release] package, run it directly after decompression.
Notice:release
The built-in database files in ipdb-go middle city.free.ipdb
for testing purposes only, does not guarantee the accuracy of the data.
If it is used in a production environment, please obtain a commercial license, orregister maxmind
account, get the free version of GeoLite2-City.mmdb
database file, and update the configuration file to replace the data source with maxmind
.
tar -zxvf open-geoip-0.1.0-linux-amd64.tar.gz
cd open-geoip/
./control start
Access port 80 of your server to use.
systemctl hosting
Supposed to be deployed in /opt/open-geoip
directory, if deployed in other directories modify open-geoip.service
middle WorkingDirectory
and ExecStart
Just two fields.
cp open-geoip.service /etc/systemd/system/
systemctl daemon-reload
systemctl enable open-geoip
systemctl start open-geoip
Automatic database update
maxmind
Automatic update if required mmdb
database, only in theregisterone maxmind
account, get a LicenseKey and configure him to cfg.json
middle AutoDownload.MaxmindLicenseKey
In, or configure to the system environment variable MAXMIND_LICENSE_KEY
Just in.
Compile and package
git clone https://github.com/ECNU/open-geoip.git
cd open-geoip/
chmod +x control
./control pack
custom page
Revise templates
under the directory index.html
That’s right, the relevant resource files are in assets
Under contents.
Configuration instructions
according to cfg.json.example
file, create cfg.json
file, and further modify the configuration according to your needs.
{
"logger": {
"dir": "logs/",
"level": "DEBUG",
"keepHours": 24
},
"campus": {
"continent": "亚洲",
"country": "中国",
"province": "上海",
"city": "上海",
"district": "华东师范大学",
"isp": "校园网",
"areaCode": "310000",
"countryEnglish": "China",
"countryCode": "CN",
"longitude": "",
"latitude": "",
"ips": [
"10.0.0.0/8",
"192.168.0.0/16",
"172.16.0.0/12"
]
},
"db": {
"maxmind": "GeoLite2-City.mmdb",
"qqzengip": "",
"ipdb":""
},
"source": {
"ipv4": "maxmind",
"ipv6": "maxmind"
},
"autoDownload":{
"enabled":false,
"MaxmindLicenseKey":"",
"targetFilePath":"",
"timeout":3,
"interval":24
},
"http": {
"listen": "0.0.0.0:80",
"trustProxy": ["127.0.0.1", "::1"],
"cors":["http://localhost"],
"x-api-key": "this-is-key"
}
}
configuration item | type | illustrate |
---|---|---|
logger | object | A section containing logging settings |
logger.dir | string | Directory to store log files |
logger.level | string | The level of the log, such as DEBUG, INFO, WARN, or ERROR |
logger. keepHours | number | Number of hours to keep log files before deleting |
campus | object | A section that contains information about the campus intranet |
campus.continent | string | Continent where the park is located |
campus.country | string | Country where the park is located |
campus.province | string | Province where the park is located |
campus.city | string | City where the park is located |
campus.district | string | The district (administrative district) where the park is located |
campus.isp | string | ISP operator in the park |
campus.areaCode | string | The administrative division where the park is located (domestic part) |
campus.countryEnglish | string | The English name of the country where the park is located |
campus.countryCode | string | The country code of the country where the park is located |
campus.longitude | string | Longitude of the park |
campus.latitude | string | Latitude of the park |
campus.ips | array | An array of IP ranges belonging to the park, if the IP address of this part is hit, the content in the configuration file will be used to return |
db | object | A section containing database settings |
db.maxmind | string | The file path of the MaxMind GeoLite2 database, if autDownload is configured as true, then the configuration here will not take effect |
db.qqzengip | string | The path of the file of the qqzengip database |
db.ipdb | string | The path to the file of the ipip.net database |
source | object | A section containing settings for IP sources |
source.ipv4 | string | The source of IPv4 information, which can be configured as maxmind/qqzengip/ipdb |
source.ipv6 | string | The source of IPv6 information, which can be configured as maxmind/qqzengip/ipdb |
autoDownload | object | A section containing settings for automatically updating the database |
autoDownload. enabled | bool | Whether to enable automatic database update |
autoDownload. MaxmindLicenseKey | string | MaxMind License Key for automatic renewal MaxMind GeoLite2 The database can also be configured in environment variables MAXMIND_LICENSE_KEY middle.If neither is configured, then maxmind The automatic update of the will report an error |
autoDownload. targetFilePath | string | Automatically update the target file path of the database. If this parameter is not configured, the default value is ./ the automatic update database will be downloaded to this directory |
autoDownload.timeout | number | The timeout time for automatically updating the database, the unit is second, if this parameter is not configured, the default value is 3 |
autoDownload. interval | number | The interval time for automatically updating the database, the unit is hour, if this parameter is not configured, the default value is 24 |
http | object | A section containing HTTP server settings |
http.listen | string | The address and port where the HTTP server listens |
http.trustProxy | array | An array of IP addresses of trusted proxies. When the service is published behind the reverse proxy, it must be configured correctly, otherwise the address of xff cannot be obtained correctly. |
http.cors | array | A list of domain names that allow cross-domain access. The domain names in the configuration can be accessed across domains /myip and /myip/format interface |
http.x-api-key | string | The API key required to access the openapi interface |
APIs
myip
The myip interface is used to return the IP address of the requester. For some terminals without browsers, this interface can be used to easily obtain their own IP address information (especially after nat).
It can also be called by the front end of the website configured with CORS
Provides two style interfaces: simple string and json formatting.
# curl http://localhost/myip
# 192.168.0.100
# curl http://localhost/myip/format
# {"errCode":0,"errMsg":"success","requestId":"0f40823e-04ce-4def-9af2-71e7e1403ec8","data":{"ip":"192.168.0.100"}}
search api
The searchapi interface is browser-oriented, provides an IP address query interface, and outputs converted strings to simplify front-end parsing.
This interface is protected by captcha and throttling measures to prevent possible malicious crawlers (ToDo)
The path he visits is http://localhost/ip
openapi
The openapi interface is oriented to third-party applications, providing an IP address query interface, and performing authorization verification through X-API-KEY.
In the case of multi-tenancy, it is recommended to further carry out agent encapsulation and authorization distribution through the API gateway.
curl -H "X-API-KEY: this-is-key" http://localhost/api/v1/network/ip?ip=2001:da8:8005:a405:250:56ff:feaf:8c28
{
"errCode": 0,
"errMsg": "success",
"requestId": "7ead62f7-3f15-4822-ad1e-cf7915a8299f",
"data": {
"ip": "2001:da8:8005:a405:250:56ff:feaf:8c28",
"continent": "亚洲",
"country": "中国",
"province": "上海",
"city": "上海",
"district": "",
"isp": "",
"areaCode": "",
"countryEnglish": "China",
"countryCode": "CN",
"longitude": "121.458100",
"latitude": "31.222200"
}
}
benchmark
based on maxmind
database,web
Service Performance Test
# go test -bench=. -benchmem
goos: linux
goarch: amd64
pkg: github.com/ECNU/open-geoip
cpu: Intel(R) Xeon(R) Platinum 8369B CPU @ 2.70GHz
BenchmarkIndex-2 244190 4271 ns/op 10000 B/op 15 allocs/op
BenchmarkSeachAPIForIPv4-2 782768 1741 ns/op 1904 B/op 15 allocs/op
BenchmarkSeachAPIForIPv6-2 818250 1744 ns/op 1904 B/op 15 allocs/op
BenchmarkOpenAPIForIPv4-2 394813 3383 ns/op 2592 B/op 23 allocs/op
BenchmarkOpenAPIForIPv6-2 391868 3378 ns/op 2592 B/op 23 allocs/op
PASS
ok github.com/ECNU/open-geoip 7.044s
thank you
Some of the main functions of this project use the following open source projects, see more dependencies go.mod
.
Thanks for their open source spirit.
#opengeoip #home #page #documents #downloads #address #geographic #information #query #service #News Fast Delivery