This repository has been archived by the owner on Aug 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
objectmap: Add objectmap library code
Change-Id: I71e16b48d974cbe165e6d75b0ba3f416a8eb6c7a
- Loading branch information
Showing
4 changed files
with
177 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// Copyright (C) 2020 Storj Labs, Inc. | ||
// See LICENSE for copying information. | ||
|
||
package objectmap | ||
|
||
import ( | ||
"net" | ||
|
||
"github.com/oschwald/maxminddb-golang" | ||
"github.com/zeebo/errs" | ||
) | ||
|
||
// Error is the default error class for objectmap. | ||
var Error = errs.Class("objectmap error") | ||
|
||
// IPInfo represents the geolocation data from maxmind db. | ||
type IPInfo struct { | ||
Location struct { | ||
Latitude float64 `maxminddb:"latitude"` | ||
Longitude float64 `maxminddb:"longitude"` | ||
} `maxminddb:"location"` | ||
Postal struct { | ||
Code string `maxminddb:"code"` | ||
} `maxminddb:"postal"` | ||
Country struct { | ||
IsoCode string `maxminddb:"iso_code"` | ||
} `maxminddb:"country"` | ||
} | ||
|
||
// Reader is a maxmind database reader interface. | ||
type Reader interface { | ||
Lookup(ip net.IP, result interface{}) error | ||
Close() error | ||
} | ||
|
||
// IPDB holds the database file path and its reader. | ||
type IPDB struct { | ||
reader Reader | ||
} | ||
|
||
// NewIPDB creates a new IPMapper instance. | ||
func NewIPDB(dbPath string) (*IPDB, error) { | ||
reader, err := maxminddb.Open(dbPath) | ||
if err != nil { | ||
return nil, Error.Wrap(err) | ||
} | ||
|
||
return &IPDB{ | ||
reader: reader, | ||
}, nil | ||
} | ||
|
||
// Close closes the IPMapper reader. | ||
func (mapper *IPDB) Close() (err error) { | ||
if mapper.reader != nil { | ||
return mapper.reader.Close() | ||
} | ||
return nil | ||
} | ||
|
||
// ValidateIP validate and remove port from IP address. | ||
func ValidateIP(ipAddress string) (net.IP, error) { | ||
|
||
ip, _, err := net.SplitHostPort(ipAddress) | ||
if err != nil { | ||
ip = ipAddress // assume it had no port | ||
} | ||
|
||
parsed := net.ParseIP(ip) | ||
if parsed == nil { | ||
return nil, errs.New("invalid IP address: %s", ip) | ||
} | ||
return parsed, nil | ||
} | ||
|
||
// GetIPInfos returns the geolocation information from an IP address. | ||
func (mapper *IPDB) GetIPInfos(ipAddress string) (_ *IPInfo, err error) { | ||
|
||
var record IPInfo | ||
parsed, err := ValidateIP(ipAddress) | ||
if err != nil { | ||
return nil, Error.Wrap(err) | ||
} | ||
|
||
err = mapper.reader.Lookup(parsed, &record) | ||
|
||
return &record, Error.Wrap(err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Copyright (C) 2020 Storj Labs, Inc. | ||
// See LICENSE for copying information. | ||
|
||
package objectmap | ||
|
||
import ( | ||
"errors" | ||
"net" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
type MockReader struct{} | ||
|
||
func (mr *MockReader) Lookup(ip net.IP, result interface{}) error { | ||
|
||
// Valid geolocation case | ||
if ip.Equal(net.IPv4(172, 146, 10, 1)) { | ||
result.(*IPInfo).Location = mockIPInfo(-19.456, 20.123).Location | ||
return nil | ||
} | ||
// Location not found | ||
if ip.Equal(net.IPv4(1, 1, 1, 1)) { | ||
return errors.New("Not found") | ||
} | ||
return nil | ||
} | ||
|
||
func (mr *MockReader) Close() error { | ||
return nil | ||
} | ||
|
||
func mockIPInfo(latitude, longitude float64) *IPInfo { | ||
return &IPInfo{ | ||
Location: struct { | ||
Latitude float64 `maxminddb:"latitude"` | ||
Longitude float64 `maxminddb:"longitude"` | ||
}{ | ||
Latitude: latitude, | ||
Longitude: longitude, | ||
}, | ||
} | ||
} | ||
func TestIPDB_GetIPInfos(t *testing.T) { | ||
|
||
mockReader := &MockReader{} | ||
|
||
tests := []struct { | ||
name string | ||
reader *MockReader | ||
ipAddress string | ||
expected *IPInfo | ||
expectedErr bool | ||
}{ | ||
{"invalid IP", mockReader, "999.999.999.999", nil, true}, | ||
{"invalid (IP:PORT)", mockReader, "999.999.999.999:42", nil, true}, | ||
{"valid IP found geolocation", mockReader, "172.146.10.1", mockIPInfo(-19.456, 20.123), false}, | ||
{"valid (IP:PORT) found geolocation", mockReader, "172.146.10.1:4545", mockIPInfo(-19.456, 20.123), false}, | ||
{"valid IP geolocation not found", mockReader, "1.1.1.1", &IPInfo{}, true}, | ||
{"valid (IP:PORT) geolocation not found", mockReader, "1.1.1.1:1000", &IPInfo{}, true}, | ||
} | ||
for _, tt := range tests { | ||
mapper := &IPDB{ | ||
reader: tt.reader, | ||
} | ||
testCase := tt | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := mapper.GetIPInfos(testCase.ipAddress) | ||
|
||
if testCase.expectedErr { | ||
require.Error(t, err) | ||
return | ||
} | ||
|
||
require.NoError(t, err) | ||
require.EqualValues(t, testCase.expected, got) | ||
}) | ||
} | ||
} |