Using IPinfo’s MMDB data downloads with Golang
The .mmdb
file format is a special binary database type requiring specific packages/libraries to read. The .mmdb
file format facilitates extremely fast IP lookup operations.
I highly recommend reading this article before getting started: How to choose the best file format for your IPinfo database?
0. Prerequisite: Downloading the IPinfo IP database in the .mmdb
format
Before you begin, download the .mmdb
format data. Keep this database in our same directory.
For this example, I use the IPinfo IP to Privacy Detection Database. IPinfo IP to Privacy Detection Database is stored as standard_privacy.mmdb
.
The database schema for the IPinfo IP to Privacy Detection Database: Privacy Detection Database - IPinfo.io
1. Installation of maxminddb-golang
After running go mod init
, you must install the maxminddb-golang
. This library will perform the IP lookup operations.
Please note that the required package is an MMDB reader, not an API package.
To install the package, run the following command:
go get github.com/oschwald/maxminddb-golang
You can find the documentation for the package here: maxminddb package - github.com/oschwald/maxminddb-golang - Go Packages
2. Usage
After the installation of the maxminddb-golang
package, you can use the following code to lookup the IPinfo results from the MMDB file.
package main
import (
"fmt"
"log"
"net"
"github.com/oschwald/maxminddb-golang"
)
// Refer to the IPinfo database documentation to create the struct: https://ipinfo.io/developers/database-types
type IPinfoResult struct {
Hosting string `maxminddb:"hosting"`
Proxy string `maxminddb:"proxy"`
Tor string `maxminddb:"tor"`
Vpn string `maxminddb:"vpn"`
Relay string `maxminddb:"relay"`
Service string `maxminddb:"service"`
}
func main() {
// Replace the following with the path to your IPinfo IP database.
// My IP to Privacy Detection database is available in the same directory
dbPath := "./standard_privacy.mmdb"
// Open the MaxMind DB file
db, err := maxminddb.Open(dbPath)
if err != nil {
log.Fatal(err)
}
defer db.Close()
// Provide an IP address that you want to lookup
// For example, I am looking up the random IP address
ip := net.ParseIP("188.241.83.0")
// Create a struct to store the result
var result IPinfoResult
// Lookup the IP address in the MaxMind DB
err = db.Lookup(ip, &result)
if err != nil {
log.Fatal(err)
}
// Print the entire result payload
fmt.Printf("%+v\n", result)
// Print the individual result payload
fmt.Printf("Hosting: %s\n", result.Hosting)
fmt.Printf("Proxy: %s\n", result.Proxy)
fmt.Printf("Tor: %s\n", result.Tor)
fmt.Printf("VPN: %s\n", result.Vpn)
fmt.Printf("Relay: %s\n", result.Relay)
fmt.Printf("Anonymous IP Service Name: %s\n", result.Service)
}
Output:
{Hosting:true Proxy: Tor: Vpn:true Relay: Service:Windscribe}
Hosting: true
Proxy:
Tor:
VPN: true
Relay:
Anonymous IP Service Name: Windscribe
Some points to note:
- If the IP address is not available, for instance, if you look for a non-anonymous IP address in the IP to Privacy database, you will get empty results similar to this:
{Hosting: Proxy: Tor: Vpn: Relay: Service:}
Hosting:
Proxy:
Tor:
VPN:
Relay:
Anonymous IP Service Name:
-
You might need to convert the string boolean values in the database to
bool
types. -
To create the
IPinfoResult
struct, you should refer to our database schema documentation: Database Types - IPinfo.io