Skip to content

Commit

Permalink
remove: maxmind database embeddings
Browse files Browse the repository at this point in the history
  • Loading branch information
dennis-tra committed Nov 28, 2023
1 parent 0e6c5f7 commit 3b8ad31
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 25 deletions.
34 changes: 24 additions & 10 deletions cmd/nebula/cmd_resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ import (
)

var resolveConfig = &config.Resolve{
Root: rootConfig,
FilePathUdgerDB: "",
BatchSize: 1000,
Root: rootConfig,
BatchSize: 1000,
FilePathUdgerDB: "",
FilePathMaxmindCountry: "",
FilePathMaxmindASN: "",
}

// ResolveCommand contains the monitor sub-command configuration.
Expand All @@ -32,19 +34,31 @@ var ResolveCommand = &cli.Command{
Usage: "Resolves all multi addresses to their IP addresses and geo location information",
Action: ResolveAction,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "udger-db",
Usage: "Location of the Udger database v3",
EnvVars: []string{"NEBULA_RESOLVE_UDGER_DB"},
Destination: &resolveConfig.FilePathUdgerDB,
},
&cli.IntFlag{
Name: "batch-size",
Usage: "How many database entries should be fetched at each iteration",
EnvVars: []string{"NEBULA_RESOLVE_BATCH_SIZE"},
Value: resolveConfig.BatchSize,
Destination: &resolveConfig.BatchSize,
},
&cli.StringFlag{
Name: "udger-db",
Usage: "Location of the Udger database v3",
EnvVars: []string{"NEBULA_RESOLVE_UDGER_DB"},
Destination: &resolveConfig.FilePathUdgerDB,
},
&cli.StringFlag{
Name: "maxmind-asn",
Usage: "Location of the Maxmind ASN database",
EnvVars: []string{"NEBULA_RESOLVE_MAXMIND_ASN"},
Destination: &resolveConfig.FilePathMaxmindASN,
},
&cli.StringFlag{
Name: "maxmind-country",
Usage: "Location of the Maxmind Country database",
EnvVars: []string{"NEBULA_RESOLVE_MAXMIND_COUNTRY"},
Destination: &resolveConfig.FilePathMaxmindCountry,
},
},
}

Expand All @@ -63,7 +77,7 @@ func ResolveAction(c *cli.Context) error {
dbh := dbc.Handle()

// Initialize new maxmind client to interact with the country database.
mmc, err := maxmind.NewClient()
mmc, err := maxmind.NewClient(resolveConfig.FilePathMaxmindASN, resolveConfig.FilePathMaxmindCountry)
if err != nil {
return err
}
Expand Down
Binary file removed maxmind/GeoLite2-ASN.mmdb
Binary file not shown.
Binary file removed maxmind/GeoLite2-Country.mmdb
Binary file not shown.
25 changes: 13 additions & 12 deletions maxmind/maxmind.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,38 @@ import (
_ "embed"
"fmt"
"net"
"os"

"github.com/friendsofgo/errors"
ma "github.com/multiformats/go-multiaddr"
madns "github.com/multiformats/go-multiaddr-dns"
"github.com/oschwald/geoip2-golang"
log "github.com/sirupsen/logrus"
)

//go:embed GeoLite2-Country.mmdb
var geoLite2Country []byte

//go:embed GeoLite2-ASN.mmdb
var geoLite2ASN []byte

type Client struct {
countryReader *geoip2.Reader
asnReader *geoip2.Reader
}

// NewClient initializes a new maxmind database client from the embedded database
func NewClient() (*Client, error) {
countryReader, err := geoip2.FromBytes(geoLite2Country)
func NewClient(asnDB string, countryDB string) (*Client, error) {
asnData, err := os.ReadFile(asnDB)
if err != nil {
return nil, fmt.Errorf("read asn file %s: %w", asnDB, err)
}

asnReader, err := geoip2.FromBytes(asnData)
if err != nil {
return nil, errors.Wrap(err, "geoip from bytes")
return nil, fmt.Errorf("asn geoip from bytes: %w", err)
}

asnReader, err := geoip2.FromBytes(geoLite2ASN)
countryData, err := os.ReadFile(countryDB)
if err != nil {
return nil, errors.Wrap(err, "geoip from bytes")
return nil, fmt.Errorf("read country file %s: %w", asnDB, err)
}

countryReader, err := geoip2.FromBytes(countryData)
return nil, fmt.Errorf("country geoip from bytes: %w", err)
return &Client{
countryReader: countryReader,
asnReader: asnReader,
Expand Down
6 changes: 3 additions & 3 deletions maxmind/maxmind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

func TestClient_AddrCountry(t *testing.T) {
client, err := NewClient()
client, err := NewClient("", "")
require.NoError(t, err)

tests := []struct {
Expand Down Expand Up @@ -40,7 +40,7 @@ func TestClient_AddrCountry(t *testing.T) {
}

func TestClient_AddrASN(t *testing.T) {
client, err := NewClient()
client, err := NewClient("", "")
require.NoError(t, err)

tests := []struct {
Expand Down Expand Up @@ -69,7 +69,7 @@ func TestClient_AddrASN(t *testing.T) {
}

func TestClient_MaddrCountry(t *testing.T) {
client, err := NewClient()
client, err := NewClient("", "")
require.NoError(t, err)

tests := []struct {
Expand Down

0 comments on commit 3b8ad31

Please sign in to comment.