Skip to content

Commit

Permalink
lib iso ok
Browse files Browse the repository at this point in the history
  • Loading branch information
Thiago Tognoli committed Oct 30, 2024
1 parent b9ab8c6 commit 8953bca
Show file tree
Hide file tree
Showing 51 changed files with 2,450 additions and 143 deletions.
16 changes: 8 additions & 8 deletions .traefik.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ summary: 'Getting data from GeoIP databases and pass it downstream via HTTP requ


testData:
cityDbPath: 'GeoLite2-Country.mmdb'
asnDbPath: 'GeoLite2-ASN.mmdb'
countryDbPath: 'GeoLite2-Country.mmdb'
preferXForwardedForHeader: false
ipHeader: 'X-IP'
failInError: true
debug: false
iso88591: true
cityDbPath: 'data/mmdb/GeoLite2-City.mmdb'
# asnDbPath: 'GeoLite2-ASN.mmdb'
# countryDbPath: 'GeoLite2-Country.mmdb'
# preferXForwardedForHeader: false
# ipHeader: 'X-IP'
# failInError: true
# debug: false
# iso88591: true
16 changes: 0 additions & 16 deletions geoip2/.gitignore

This file was deleted.

2 changes: 1 addition & 1 deletion geoip2/anonymous_ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func readAnonymousIPMap(result *AnonymousIP, buffer []byte, mapSize, offset uint
if err != nil {
return 0, err
}
switch bytesToString(key) {
switch bytesToKeyString(key) {
case "is_anonymous":
result.IsAnonymous, offset, err = readBool(buffer, offset)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion geoip2/asn.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func readASNMap(result *ASN, buffer []byte, mapSize, offset uint) (uint, error)
if err != nil {
return 0, err
}
switch bytesToString(key) {
switch bytesToKeyString(key) {
case "autonomous_system_number":
result.AutonomousSystemNumber, offset, err = readUInt32(buffer, offset)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion geoip2/city.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func readCityMap(city *City, buffer []byte, mapSize, offset uint) (uint, error)
if err != nil {
return 0, err
}
switch bytesToString(key) {
switch bytesToKeyString(key) {
case "geoname_id":
city.GeoNameID, offset, err = readUInt32(buffer, offset)
if err != nil {
Expand Down
31 changes: 29 additions & 2 deletions geoip2/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,12 +252,12 @@ func readStringMapMap(buffer []byte, mapSize, offset uint) (map[string]string, u
return nil, 0, errors.New("map key must be a string, got: " + strconv.Itoa(int(dataType)))
}
offset = newOffset
result[bytesToString(key)] = bytesToString(buffer[valueOffset : valueOffset+size])
result[bytesToKeyString(key)] = bytesToString(buffer[valueOffset : valueOffset+size])
case dataTypeString:
newOffset := offset + size
value := bytesToString(buffer[offset:newOffset])
offset = newOffset
result[bytesToString(key)] = value
result[bytesToKeyString(key)] = value
default:
return nil, 0, errors.New("invalid data type of key " + string(key) + ": " + strconv.Itoa(int(dataType)))
}
Expand Down Expand Up @@ -364,6 +364,33 @@ func bytesToFloat64(buffer []byte) float64 {
return math.Float64frombits(bits)
}

func bytesToKeyString(value []byte) string {
return string(value)
}

func bytesToString(value []byte) string {
// return string(bytesUtf8ToIso88591(value))
return string(value)
}

// bytesUtf8ToIso88591 convert a UTF-8 string in a sequence of bytes ISO-8859-1.
func bytesUtf8ToIso88591(value []byte) []byte {
var isoOutput []byte
for i := 0; i < len(value); i++ {
b := value[i]
switch {
case b < 0x80: //nolint:mnd
// ASCII compatible with ISO-8859-1
isoOutput = append(isoOutput, b)
case (b&0xE0) == 0xC0 && i+1 < len(value) && (value[i+1]&0xC0) == 0x80: //nolint:mnd
// converts from UTF-8 to ISO-8859-1: two bytes (110xxxxx 10xxxxxx)
isoByte := ((b & 0x1F) << 6) | (value[i+1] & 0x3F) //nolint:mnd
isoOutput = append(isoOutput, isoByte)
i++ // jump to next byte
default:
// characters without of ISO-8859-1 cannot be converted, ignore and replace by '?'
isoOutput = append(isoOutput, '?')
}
}
return isoOutput
}
2 changes: 1 addition & 1 deletion geoip2/connection_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func readConnectionTypeMap(result *ConnectionType, buffer []byte, mapSize, offse
if err != nil {
return 0, err
}
switch bytesToString(key) {
switch bytesToKeyString(key) {
case "connection_type":
result.ConnectionType, offset, err = readString(buffer, offset)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion geoip2/continent.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func readContinentMap(continent *Continent, buffer []byte, mapSize, offset uint)
if err != nil {
return 0, err
}
switch bytesToString(key) {
switch bytesToKeyString(key) {
case "geoname_id":
continent.GeoNameID, offset, err = readUInt32(buffer, offset)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion geoip2/country.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func readCountryMap(country *Country, buffer []byte, mapSize, offset uint) (uint
if err != nil {
return 0, err
}
switch bytesToString(key) {
switch bytesToKeyString(key) {
case "geoname_id":
country.GeoNameID, offset, err = readUInt32(buffer, offset)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion geoip2/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func readDomainMap(result *Domain, buffer []byte, mapSize, offset uint) (uint, e
if err != nil {
return 0, err
}
switch bytesToString(key) {
switch bytesToKeyString(key) {
case "domain":
result.Domain, offset, err = readString(buffer, offset)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion geoip2/isp.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func readISPMap(result *ISP, buffer []byte, mapSize, offset uint) (uint, error)
if err != nil {
return 0, err
}
switch bytesToString(key) {
switch bytesToKeyString(key) {
case "autonomous_system_number":
result.AutonomousSystemNumber, offset, err = readUInt32(buffer, offset)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion geoip2/location.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func readLocationMap(location *Location, buffer []byte, mapSize, offset uint) (u
if err != nil {
return 0, err
}
switch bytesToString(key) {
switch bytesToKeyString(key) {
case "latitude":
location.Latitude, offset, err = readFloat64(buffer, offset)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions geoip2/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func readMetadata(buffer []byte) (*Metadata, error) {
return nil, err
}
newOffset := uint(0)
switch bytesToString(key) {
switch bytesToKeyString(key) {
case "binary_format_major_version":
if dataType != dataTypeUint16 {
return nil, errors.New("invalid binary_format_major_version type: " + strconv.Itoa(int(dataType)))
Expand All @@ -64,7 +64,7 @@ func readMetadata(buffer []byte) (*Metadata, error) {
return nil, errors.New("invalid database_type type: " + strconv.Itoa(int(dataType)))
}
newOffset = offset + size
metadata.DatabaseType = bytesToString(buffer[offset:newOffset])
metadata.DatabaseType = bytesToKeyString(buffer[offset:newOffset])
case "description":
if dataType != dataTypeMap {
return nil, errors.New("invalid description type: " + strconv.Itoa(int(dataType)))
Expand Down
2 changes: 1 addition & 1 deletion geoip2/postal.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func readPostalMap(postal *Postal, buffer []byte, mapSize, offset uint) (uint, e
if err != nil {
return 0, err
}
switch bytesToString(key) {
switch bytesToKeyString(key) {
case "code":
postal.Code, offset, err = readString(buffer, offset)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion geoip2/reader_city.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (r *CityReader) Lookup(ip net.IP) (*CityResult, error) {
if err != nil {
return nil, err
}
switch bytesToString(key) {
switch bytesToKeyString(key) {
case "city":
offset, err = readCity(&result.City, r.decoderBuffer, offset)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion geoip2/reader_country.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (r *CountryReader) Lookup(ip net.IP) (*CountryResult, error) {
if err != nil {
return nil, err
}
switch bytesToString(key) {
switch bytesToKeyString(key) {
case "continent":
offset, err = readContinent(&result.Continent, r.decoderBuffer, offset)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion geoip2/subdivision.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func readSubdivisionMap(subdivision *Subdivision, buffer []byte, mapSize, offset
if err != nil {
return 0, err
}
switch bytesToString(key) {
switch bytesToKeyString(key) {
case "geoname_id":
subdivision.GeoNameID, offset, err = readUInt32(buffer, offset)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion geoip2/traits.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func readTraitsMap(traits *Traits, buffer []byte, mapSize, offset uint) (uint, e
if err != nil {
return 0, err
}
switch bytesToString(key) {
switch bytesToKeyString(key) {
case "is_anonymous_proxy":
traits.IsAnonymousProxy, offset, err = readBool(buffer, offset)
if err != nil {
Expand Down
21 changes: 21 additions & 0 deletions geoip2_iso88591/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.
96 changes: 96 additions & 0 deletions geoip2_iso88591/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
[Forked from](https://github.com/IncSW/geoip2) to support yaegi with new features

[![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)
[![Go Doc](https://img.shields.io/badge/godoc-reference-blue.svg?style=flat-square)](https://pkg.go.dev/github.com/IncSW/geoip2?tab=doc)

# GeoIP2 Reader for Go

This library reads MaxMind GeoIP2 databases.

Inspired by [oschwald/geoip2-golang](https://github.com/oschwald/geoip2-golang).

## Installation

`go get github.com/IncSW/geoip2`

## Quick Start

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

reader, err := geoip2.NewCityReaderFromFile("path/to/GeoIP2-City.mmdb")
if err != nil {
panic(err)
}
record, err := reader.Lookup(net.ParseIP("81.2.69.142"))
if err != nil {
panic(err)
}
println(record.Continent.Names["zh-CN"]) // 欧洲
println(record.City.Names["pt-BR"]) // Wimbledon
if len(record.Subdivisions) != 0 {
println(record.Subdivisions[0].Names["en"]) // England
}
println(record.Country.Names["ru"]) // Великобритания
println(record.Country.ISOCode) // GB
println(record.Location.TimeZone) // Europe/London
println(record.Country.GeoNameID) // 2635167, https://www.geonames.org/2635167
```

## Performance

### [IncSW/geoip2](https://github.com/IncSW/geoip2)
```
city-24 342847 2981 ns/op 2032 B/op 12 allocs/op
city_parallel-24 4477626 269 ns/op 2032 B/op 12 allocs/op
isp-24 3539738 336 ns/op 64 B/op 1 allocs/op
isp_parallel-24 46938070 25.7 ns/op 64 B/op 1 allocs/op
connection_type-24 8759110 133 ns/op 0 B/op 0 allocs/op
connection_type_parallel-24 142261742 8.34 ns/op 0 B/op 0 allocs/op
```

### [oschwald/geoip2-golang](https://github.com/oschwald/geoip2-golang)
```
city-24 109092 10717 ns/op 2848 B/op 103 allocs/op
city_parallel-24 662510 1718 ns/op 2848 B/op 103 allocs/op
isp-24 1688287 705 ns/op 112 B/op 4 allocs/op
isp_parallel-24 14285560 84.4 ns/op 112 B/op 4 allocs/op
connection_type-24 3883234 305 ns/op 32 B/op 2 allocs/op
connection_type_parallel-24 34284831 32.1 ns/op 32 B/op 2 allocs/op
```

## Supported databases types

### Country
- GeoIP2-Country
- GeoLite2-Country
- DBIP-Country
- DBIP-Country-Lite

### City
- GeoIP2-City
- GeoLite2-City
- GeoIP2-Enterprise
- DBIP-City-Lite

### ISP
- GeoIP2-ISP

### ASN
- GeoLite2-ASN
- DBIP-ASN-Lite
- DBIP-ASN-Lite (compat=GeoLite2-ASN)

### Connection Type
- GeoIP2-Connection-Type

### Anonymous IP
- GeoIP2-Anonymous-IP

### Domain
- GeoIP2-Domain

## License

[MIT License](LICENSE).
49 changes: 49 additions & 0 deletions geoip2_iso88591/anonymous_ip.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package geoip2_iso88591

import "errors"

func readAnonymousIPMap(result *AnonymousIP, buffer []byte, mapSize, 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 bytesToKeyString(key) {
case "is_anonymous":
result.IsAnonymous, offset, err = readBool(buffer, offset)
if err != nil {
return 0, err
}
case "is_anonymous_vpn":
result.IsAnonymousVPN, offset, err = readBool(buffer, offset)
if err != nil {
return 0, err
}
case "is_hosting_provider":
result.IsHostingProvider, offset, err = readBool(buffer, offset)
if err != nil {
return 0, err
}
case "is_public_proxy":
result.IsPublicProxy, offset, err = readBool(buffer, offset)
if err != nil {
return 0, err
}
case "is_tor_exit_node":
result.IsTorExitNode, offset, err = readBool(buffer, offset)
if err != nil {
return 0, err
}
case "is_residential_proxy":
result.IsResidentialProxy, offset, err = readBool(buffer, offset)
if err != nil {
return 0, err
}
default:
return 0, errors.New("unknown anonymous ip key: " + string(key))
}
}
return offset, nil
}
Loading

0 comments on commit 8953bca

Please sign in to comment.