Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
IncSW committed Aug 31, 2020
0 parents commit 62c4ade
Show file tree
Hide file tree
Showing 18 changed files with 1,614 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
testdata/GeoIP2-City.mmdb
testdata/GeoIP2-Connection-Type.mmdb
testdata/GeoIP2-Country.mmdb
testdata/GeoIP2-ISP.mmdb
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2017 Aleksey Lin <[email protected]> (https://incsw.in)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
113 changes: 113 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE)
[![Go Report Card](https://goreportcard.com/badge/github.com/IncSW/geoip2?style=flat-square)](https://goreportcard.com/report/github.com/IncSW/geoip2)

# GeoIP2 Reader for Go

This library reads MaxMind GeoIP2 databases.

## Installation

`go get github.com/IncSW/geoip2`

## Quick Start

```go
import "github.com/IncSW/geoip2"

reader, err := geoip2.NewReaderFromFile("path/to/GeoIP2-City.mmdb")
if err != nil {
panic(err)
}
record, err := reader.LookupCity(net.ParseIP("1.1.1.1"))
if err != nil {
panic(err)
}
data, err := json.Marshal(record)
if err != nil {
panic(err)
}
println(string(data))
// {
// "Continent": {
// "GeoNameID": 6255151,
// "Code": "OC",
// "Names": {
// "de": "Ozeanien",
// "en": "Oceania",
// "es": "Oceanía",
// "fr": "Océanie",
// "ja": "オセアニア",
// "pt-BR": "Oceania",
// "ru": "Океания",
// "zh-CN": "大洋洲"
// }
// },
// "City": {
// "GeoNameID": 0,
// "Names": null
// },
// "Country": {
// "GeoNameID": 2077456,
// "ISOCode": "AU",
// "IsInEuropeanEnion": false,
// "Names": {
// "de": "Australien",
// "en": "Australia",
// "es": "Australia",
// "fr": "Australie",
// "ja": "オーストラリア",
// "pt-BR": "Austrália",
// "ru": "Австралия",
// "zh-CN": "澳大利亚"
// },
// "Type": ""
// },
// "Subdivisions": null,
// "Location": {
// "AccuracyRadius": 1000,
// "MetroCode": 0,
// "Latitude": -33.494,
// "Longitude": 143.2104,
// "TimeZone": "Australia/Sydney"
// },
// "Postal": {
// "Code": ""
// },
// "RegisteredCountry": {
// "GeoNameID": 2077456,
// "ISOCode": "AU",
// "IsInEuropeanEnion": false,
// "Names": {
// "de": "Australien",
// "en": "Australia",
// "es": "Australia",
// "fr": "Australie",
// "ja": "オーストラリア",
// "pt-BR": "Austrália",
// "ru": "Австралия",
// "zh-CN": "澳大利亚"
// },
// "Type": ""
// },
// "RepresentedCountry": {
// "GeoNameID": 0,
// "ISOCode": "",
// "IsInEuropeanEnion": false,
// "Names": null,
// "Type": ""
// },
// "Traits": {
// "IsAnonymousProxy": false,
// "IsSatelliteProvider": false,
// "StaticIPScore": 0
// }
// }
```

## Performance

TODO

## License

[MIT License](LICENSE).
130 changes: 130 additions & 0 deletions city.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package geoip2

import (
"errors"
"strconv"
)

func readCityResponse(buffer []byte, offset uint) (*CityResponse, error) {
dataType, citySize, offset, err := readControl(buffer, offset)
if err != nil {
return nil, err
}
if dataType != dataTypeMap {
return nil, errors.New("invalid city type: " + strconv.Itoa(int(dataType)))
}
var key []byte
response := &CityResponse{}
for i := uint(0); i < citySize; i++ {
key, offset, err = readMapKey(buffer, offset)
if err != nil {
return nil, err
}
switch b2s(key) {
case "city":
offset, err = readCity(&response.City, buffer, offset)
if err != nil {
return nil, err
}
case "continent":
offset, err = readContinent(&response.Continent, buffer, offset)
if err != nil {
return nil, err
}
case "country":
offset, err = readCountry(&response.Country, buffer, offset)
if err != nil {
return nil, err
}
case "location":
offset, err = readLocation(&response.Location, buffer, offset)
if err != nil {
return nil, err
}
case "postal":
offset, err = readPostal(&response.Postal, buffer, offset)
if err != nil {
return nil, err
}
case "registered_country":
offset, err = readCountry(&response.RegisteredCountry, buffer, offset)
if err != nil {
return nil, err
}
case "represented_country":
offset, err = readCountry(&response.RepresentedCountry, buffer, offset)
if err != nil {
return nil, err
}
case "subdivisions":
response.Subdivisions, offset, err = readSubdivisions(buffer, offset)
if err != nil {
return nil, err
}
case "traits":
offset, err = readTraits(&response.Traits, buffer, offset)
if err != nil {
return nil, err
}
default:
return nil, errors.New("unknown city response key: " + string(key) + ", type: " + strconv.Itoa(int(dataType)))
}
}
return response, nil
}

func readCity(city *City, buffer []byte, offset uint) (uint, error) {
dataType, size, offset, err := readControl(buffer, offset)
if err != nil {
return 0, err
}
switch dataType {
case dataTypeMap:
return readCityMap(city, buffer, size, offset)
case dataTypePointer:
pointer, newOffset, err := readPointer(buffer, size, offset)
if err != nil {
return 0, err
}
dataType, size, offset, err := readControl(buffer, pointer)
if err != nil {
return 0, err
}
if dataType != dataTypeMap {
return 0, errors.New("invalid city pointer type: " + strconv.Itoa(int(dataType)))
}
_, err = readCityMap(city, buffer, size, offset)
if err != nil {
return 0, err
}
return newOffset, nil
default:
return 0, errors.New("invalid city type: " + strconv.Itoa(int(dataType)))
}
}

func readCityMap(city *City, buffer []byte, mapSize uint, offset uint) (uint, error) {
var key []byte
var err error
for i := uint(0); i < mapSize; i++ {
key, offset, err = readMapKey(buffer, offset)
if err != nil {
return 0, err
}
switch b2s(key) {
case "geoname_id":
city.GeoNameID, offset, err = readUInt32(buffer, offset)
if err != nil {
return 0, err
}
case "names":
city.Names, offset, err = readStringMap(buffer, offset)
if err != nil {
return 0, err
}
default:
return 0, errors.New("unknown city key: " + string(key))
}
}
return offset, nil
}
Loading

0 comments on commit 62c4ade

Please sign in to comment.