diff --git a/README.md b/README.md
index 20c51a9..14a6094 100644
--- a/README.md
+++ b/README.md
@@ -1,8 +1,7 @@
[](https://goreportcard.com/report/github.com/ip2location/ip2location-go)
-IP2Location Go Package
-======================
+# IP2Location Go Package
This Go package provides a fast lookup of country, region, city, latitude, longitude, ZIP code, time zone, ISP, domain name, connection type, IDD code, area code, weather station code, station name, mcc, mnc, mobile brand, elevation, usage type, address type and IAB category from IP address by using IP2Location database. This package uses a file based database available at IP2Location.com. This database simply contains IP blocks as keys, and other information such as country, region, city, latitude, longitude, ZIP code, time zone, ISP, domain name, connection type, IDD code, area code, weather station code, station name, mcc, mnc, mobile brand, elevation, usage type, address type and IAB category as values. It supports both IP address in IPv4 and IPv6.
@@ -20,16 +19,65 @@ The database will be updated in monthly basis for the greater accuracy. Free LIT
The paid databases are available at https://www.ip2location.com under Premium subscription package.
+As an alternative, this package can also call the IP2Location Web Service. This requires an API key. If you don't have an existing API key, you can subscribe for one at the below:
-Installation
-=======
+https://www.ip2location.com/web-service/ip2location
+
+## Installation
```
go get github.com/ip2location/ip2location-go/v9
```
-Example
-=======
+## QUERY USING THE BIN FILE
+
+## Dependencies
+
+This package requires IP2Location BIN data file to function. You may download the BIN data file at
+* IP2Location LITE BIN Data (Free): https://lite.ip2location.com
+* IP2Location Commercial BIN Data (Comprehensive): https://www.ip2location.com
+
+
+## IPv4 BIN vs IPv6 BIN
+
+Use the IPv4 BIN file if you just need to query IPv4 addresses.
+
+Use the IPv6 BIN file if you need to query BOTH IPv4 and IPv6 addresses.
+
+
+## Methods
+
+Below are the methods supported in this package.
+
+|Method Name|Description|
+|---|---|
+|OpenDB|Initialize the package with the BIN file.|
+|Get_all|Returns the geolocation information in an object.|
+|Get_country_short|Returns the country code.|
+|Get_country_long|Returns the country name.|
+|Get_region|Returns the region name.|
+|Get_city|Returns the city name.|
+|Get_isp|Returns the ISP name.|
+|Get_latitude|Returns the latitude.|
+|Get_longitude|Returns the longitude.|
+|Get_domain|Returns the domain name.|
+|Get_zipcode|Returns the ZIP code.|
+|Get_timezone|Returns the time zone.|
+|Get_netspeed|Returns the net speed.|
+|Get_iddcode|Returns the IDD code.|
+|Get_areacode|Returns the area code.|
+|Get_weatherstationcode|Returns the weather station code.|
+|Get_weatherstationname|Returns the weather station name.|
+|Get_mcc|Returns the mobile country code.|
+|Get_mnc|Returns the mobile network code.|
+|Get_mobilebrand|Returns the mobile brand.|
+|Get_elevation|Returns the elevation in meters.|
+|Get_usagetype|Returns the usage type.|
+|Get_addresstype|Returns the address type.|
+|Get_category|Returns the IAB category.|
+|Close|Closes BIN file.|
+
+## Usage
```go
package main
@@ -82,20 +130,135 @@ func main() {
}
```
-Dependencies
-============
+## QUERY USING THE IP2LOCATION WEB SERVICE
-The complete database is available at https://www.ip2location.com under subscription package.
+## Methods
+Below are the methods supported in this package.
+|Method Name|Description|
+|---|---|
+|OpenWS| 3 input parameters:
- IP2Location API Key.
- Package (WS1 - WS25)
- Use HTTPS or HTTP
|
+|LookUp|Query IP address. This method returns an object containing the geolocation info. - country_code
- country_name
- region_name
- city_name
- latitude
- longitude
- zip_code
- time_zone
- isp
- domain
- net_speed
- idd_code
- area_code
- weather_station_code
- weather_station_name
- mcc
- mnc
- mobile_brand
- elevation
- usage_type
- address_type
- category
- continent
- name
- code
- hemisphere
- translations
- country
- name
- alpha3_code
- numeric_code
- demonym
- flag
- capital
- total_area
- population
- currency
- language
- idd_code
- tld
- is_eu
- translations
- region
- city
- geotargeting
- country_groupings
- time_zone_info
- olson
- current_time
- gmt_offset
- is_dst
- sunrise
- sunset
|
+|GetCredit|This method returns the web service credit balance in an object.|
-IPv4 BIN vs IPv6 BIN
-====================
+## Usage
-Use the IPv4 BIN file if you just need to query IPv4 addresses.
-Use the IPv6 BIN file if you need to query BOTH IPv4 and IPv6 addresses.
+```go
+package main
+
+import (
+ "github.com/ip2location/ip2location-go"
+ "fmt"
+)
+
+func main() {
+ apikey := "YOUR_API_KEY"
+ apipackage := "WS25"
+ usessl := true
+ addon := "continent,country,region,city,geotargeting,country_groupings,time_zone_info" // leave blank if no need
+ lang := "en" // leave blank if no need
+
+ ws, err := ip2location.OpenWS(apikey, apipackage, usessl)
+
+ if err != nil {
+ fmt.Print(err)
+ return
+ }
+ ip := "8.8.8.8"
+ res, err := ws.LookUp(ip, addon, lang)
+
+ if err != nil {
+ fmt.Print(err)
+ return
+ }
+ if res.Response != "OK" {
+ fmt.Printf("Error: %s\n", res.Response)
+ } else {
+ // standard results
+ fmt.Printf("Response: %s\n", res.Response)
+ fmt.Printf("CountryCode: %s\n", res.CountryCode)
+ fmt.Printf("CountryName: %s\n", res.CountryName)
+ fmt.Printf("RegionName: %s\n", res.RegionName)
+ fmt.Printf("CityName: %s\n", res.CityName)
+ fmt.Printf("Latitude: %f\n", res.Latitude)
+ fmt.Printf("Longitude: %f\n", res.Longitude)
+ fmt.Printf("ZipCode: %s\n", res.ZipCode)
+ fmt.Printf("TimeZone: %s\n", res.TimeZone)
+ fmt.Printf("Isp: %s\n", res.Isp)
+ fmt.Printf("Domain: %s\n", res.Domain)
+ fmt.Printf("NetSpeed: %s\n", res.NetSpeed)
+ fmt.Printf("IddCode: %s\n", res.IddCode)
+ fmt.Printf("AreaCode: %s\n", res.AreaCode)
+ fmt.Printf("WeatherStationCode: %s\n", res.WeatherStationCode)
+ fmt.Printf("WeatherStationName: %s\n", res.WeatherStationName)
+ fmt.Printf("Mcc: %s\n", res.Mcc)
+ fmt.Printf("Mnc: %s\n", res.Mnc)
+ fmt.Printf("MobileBrand: %s\n", res.MobileBrand)
+ fmt.Printf("Elevation: %d\n", res.Elevation)
+ fmt.Printf("UsageType: %s\n", res.UsageType)
+ fmt.Printf("AddressType: %s\n", res.AddressType)
+ fmt.Printf("Category: %s\n", res.Category)
+ fmt.Printf("CategoryName: %s\n", res.CategoryName)
+ fmt.Printf("CreditsConsumed: %d\n", res.CreditsConsumed)
+
+ // continent addon
+ fmt.Printf("Continent => Name: %s\n", res.Continent.Name)
+ fmt.Printf("Continent => Code: %s\n", res.Continent.Code)
+ fmt.Printf("Continent => Hemisphere: %+v\n", res.Continent.Hemisphere)
+
+ // country addon
+ fmt.Printf("Country => Name: %s\n", res.Country.Name)
+ fmt.Printf("Country => Alpha3Code: %s\n", res.Country.Alpha3Code)
+ fmt.Printf("Country => NumericCode: %s\n", res.Country.NumericCode)
+ fmt.Printf("Country => Demonym: %s\n", res.Country.Demonym)
+ fmt.Printf("Country => Flag: %s\n", res.Country.Flag)
+ fmt.Printf("Country => Capital: %s\n", res.Country.Capital)
+ fmt.Printf("Country => TotalArea: %s\n", res.Country.TotalArea)
+ fmt.Printf("Country => Population: %s\n", res.Country.Population)
+ fmt.Printf("Country => IddCode: %s\n", res.Country.IddCode)
+ fmt.Printf("Country => Tld: %s\n", res.Country.Tld)
+ fmt.Printf("Country => IsEu: %t\n", res.Country.IsEu)
+
+ fmt.Printf("Country => Currency => Code: %s\n", res.Country.Currency.Code)
+ fmt.Printf("Country => Currency => Name: %s\n", res.Country.Currency.Name)
+ fmt.Printf("Country => Currency => Symbol: %s\n", res.Country.Currency.Symbol)
+
+ fmt.Printf("Country => Language => Code: %s\n", res.Country.Language.Code)
+ fmt.Printf("Country => Language => Name: %s\n", res.Country.Language.Name)
+
+ // region addon
+ fmt.Printf("Region => Name: %s\n", res.Region.Name)
+ fmt.Printf("Region => Code: %s\n", res.Region.Code)
+
+ // city addon
+ fmt.Printf("City => Name: %s\n", res.City.Name)
+
+ // geotargeting addon
+ fmt.Printf("Geotargeting => Metro: %s\n", res.Geotargeting.Metro)
+
+ // country_groupings addon
+ for i, s := range res.CountryGroupings {
+ fmt.Printf("CountryGroupings => #%d => Acronym: %s\n", i, s.Acronym)
+ fmt.Printf("CountryGroupings => #%d => Name: %s\n", i, s.Name)
+ }
+
+ // time_zone_info addon
+ fmt.Printf("TimeZoneInfo => Olson: %s\n", res.TimeZoneInfo.Olson)
+ fmt.Printf("TimeZoneInfo => CurrentTime: %s\n", res.TimeZoneInfo.CurrentTime)
+ fmt.Printf("TimeZoneInfo => GmtOffset: %d\n", res.TimeZoneInfo.GmtOffset)
+ fmt.Printf("TimeZoneInfo => IsDst: %s\n", res.TimeZoneInfo.IsDst)
+ fmt.Printf("TimeZoneInfo => Sunrise: %s\n", res.TimeZoneInfo.Sunrise)
+ fmt.Printf("TimeZoneInfo => Sunset: %s\n", res.TimeZoneInfo.Sunset)
+ }
-Copyright
-=========
+ res2, err := ws.GetCredit()
-Copyright (C) 2021 by IP2Location.com, support@ip2location.com
+ if err != nil {
+ fmt.Print(err)
+ return
+ }
+
+ fmt.Printf("Credit Balance: %d\n", res2.Response)
+}
+```
\ No newline at end of file
diff --git a/ip2location.go b/ip2location.go
index 5a03a22..319cb65 100644
--- a/ip2location.go
+++ b/ip2location.go
@@ -142,7 +142,7 @@ var usagetype_position = [26]uint8{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
var addresstype_position = [26]uint8{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21}
var category_position = [26]uint8{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 22}
-const api_version string = "9.1.0"
+const api_version string = "9.2.0"
var max_ipv4_range = big.NewInt(4294967295)
var max_ipv6_range = big.NewInt(0)
diff --git a/ip2locationwebservice.go b/ip2locationwebservice.go
new file mode 100644
index 0000000..4d8c5db
--- /dev/null
+++ b/ip2locationwebservice.go
@@ -0,0 +1,227 @@
+package ip2location
+
+import (
+ "encoding/json"
+ "errors"
+ "io/ioutil"
+ "net/http"
+ "net/url"
+ "regexp"
+ "strconv"
+)
+
+// The IP2LocationResult struct stores all of the available
+// geolocation info found in the IP2Location Web Service.
+type IP2LocationResult struct {
+ Response string `json:"response"`
+ CountryCode string `json:"country_code"`
+ CountryName string `json:"country_name"`
+ RegionName string `json:"region_name"`
+ CityName string `json:"city_name"`
+ Latitude float64 `json:"latitude"`
+ Longitude float64 `json:"longitude"`
+ ZipCode string `json:"zip_code"`
+ TimeZone string `json:"time_zone"`
+ Isp string `json:"isp"`
+ Domain string `json:"domain"`
+ NetSpeed string `json:"net_speed"`
+ IddCode string `json:"idd_code"`
+ AreaCode string `json:"area_code"`
+ WeatherStationCode string `json:"weather_station_code"`
+ WeatherStationName string `json:"weather_station_name"`
+ Mcc string `json:"mcc"`
+ Mnc string `json:"mnc"`
+ MobileBrand string `json:"mobile_brand"`
+ Elevation int `json:"elevation"`
+ UsageType string `json:"usage_type"`
+ AddressType string `json:"address_type"`
+ Category string `json:"category"`
+ CategoryName string `json:"category_name"`
+ Geotargeting struct {
+ Metro string `json:"metro"`
+ } `json:"geotargeting"`
+ Continent struct {
+ Name string `json:"name"`
+ Code string `json:"code"`
+ Hemisphere []string `json:"hemisphere"`
+ } `json:"continent"`
+ Country struct {
+ Name string `json:"name"`
+ Alpha3Code string `json:"alpha3_code"`
+ NumericCode string `json:"numeric_code"`
+ Demonym string `json:"demonym"`
+ Flag string `json:"flag"`
+ Capital string `json:"capital"`
+ TotalArea string `json:"total_area"`
+ Population string `json:"population"`
+ Currency struct {
+ Code string `json:"code"`
+ Name string `json:"name"`
+ Symbol string `json:"symbol"`
+ } `json:"currency"`
+ Language struct {
+ Code string `json:"code"`
+ Name string `json:"name"`
+ } `json:"language"`
+ IddCode string `json:"idd_code"`
+ Tld string `json:"tld"`
+ IsEu bool `json:"is_eu"`
+ } `json:"country"`
+ CountryGroupings []struct {
+ Acronym string `json:"acronym"`
+ Name string `json:"name"`
+ } `json:"country_groupings"`
+ Region struct {
+ Name string `json:"name"`
+ Code string `json:"code"`
+ } `json:"region"`
+ City struct {
+ Name string `json:"name"`
+ } `json:"city"`
+ TimeZoneInfo struct {
+ Olson string `json:"olson"`
+ CurrentTime string `json:"current_time"`
+ GmtOffset int `json:"gmt_offset"`
+ IsDst string `json:"is_dst"`
+ Sunrise string `json:"sunrise"`
+ Sunset string `json:"sunset"`
+ } `json:"time_zone_info"`
+ CreditsConsumed int `json:"credits_consumed"`
+}
+
+// The IP2LocationCreditResult struct stores the
+// credit balance for the IP2Location Web Service.
+type IP2LocationCreditResult struct {
+ Response int `json:"response"`
+}
+
+// The WS struct is the main object used to query the IP2Location Web Service.
+type WS struct {
+ apiKey string
+ apiPackage string
+ useSSL bool
+}
+
+var regexAPIKey = regexp.MustCompile(`^[\dA-Z]{10}$`)
+var regexAPIPackage = regexp.MustCompile(`^WS\d+$`)
+
+const baseURL = "api.ip2location.com/v2/"
+const msgInvalidAPIKey = "Invalid API key."
+const msgInvalidAPIPackage = "Invalid package name."
+
+// OpenWS initializes with the web service API key, API package and whether to use SSL
+func OpenWS(apikey string, apipackage string, usessl bool) (*WS, error) {
+ var ws = &WS{}
+ ws.apiKey = apikey
+ ws.apiPackage = apipackage
+ ws.useSSL = usessl
+
+ err := ws.checkParams()
+
+ if err != nil {
+ return nil, err
+ }
+
+ return ws, nil
+}
+
+func (w *WS) checkParams() error {
+ if !regexAPIKey.MatchString(w.apiKey) {
+ return errors.New(msgInvalidAPIKey)
+ }
+
+ if !regexAPIPackage.MatchString(w.apiPackage) {
+ return errors.New(msgInvalidAPIPackage)
+ }
+
+ return nil
+}
+
+// LookUp will return all geolocation fields based on the queried IP address, addon, lang
+func (w *WS) LookUp(ipAddress string, addOn string, lang string) (IP2LocationResult, error) {
+ var res IP2LocationResult
+ err := w.checkParams()
+
+ if err != nil {
+ return res, err
+ }
+
+ protocol := "https"
+
+ if !w.useSSL {
+ protocol = "http"
+ }
+
+ // lang param not supported yet due to the inconsistent data type being returned by the API
+ myUrl := protocol + "://" + baseURL + "?key=" + w.apiKey + "&package=" + w.apiPackage + "&ip=" + url.QueryEscape(ipAddress) + "&addon=" + url.QueryEscape(addOn)
+
+ resp, err := http.Get(myUrl)
+
+ if err != nil {
+ return res, err
+ }
+
+ defer resp.Body.Close()
+
+ if resp.StatusCode == http.StatusOK {
+ bodyBytes, err := ioutil.ReadAll(resp.Body)
+
+ if err != nil {
+ return res, err
+ }
+
+ err = json.Unmarshal(bodyBytes, &res)
+
+ if err != nil {
+ return res, err
+ }
+
+ return res, nil
+ }
+
+ return res, errors.New("Error HTTP " + strconv.Itoa(int(resp.StatusCode)))
+}
+
+// GetCredit will return the web service credit balance.
+func (w *WS) GetCredit() (IP2LocationCreditResult, error) {
+ var res IP2LocationCreditResult
+ err := w.checkParams()
+
+ if err != nil {
+ return res, err
+ }
+
+ protocol := "https"
+
+ if !w.useSSL {
+ protocol = "http"
+ }
+
+ myUrl := protocol + "://" + baseURL + "?key=" + w.apiKey + "&check=true"
+
+ resp, err := http.Get(myUrl)
+
+ if err != nil {
+ return res, err
+ }
+
+ defer resp.Body.Close()
+
+ if resp.StatusCode == http.StatusOK {
+ bodyBytes, err := ioutil.ReadAll(resp.Body)
+
+ if err != nil {
+ return res, err
+ }
+
+ err = json.Unmarshal(bodyBytes, &res)
+
+ if err != nil {
+ return res, err
+ }
+
+ return res, nil
+ }
+
+ return res, errors.New("Error HTTP " + strconv.Itoa(int(resp.StatusCode)))
+}