Code Monkey home page Code Monkey logo

Comments (4)

j-nanaji avatar j-nanaji commented on July 23, 2024 1

from mmdb-from-go-blogpost.

nchelluri avatar nchelluri commented on July 23, 2024

Hi, I think you're probably running into an issue with the private IP range. If you set the IncludeReservedNetworks option to true, it should work.

Here is some sample code to do what you've asked:

package main

import (
	"log"
	"net"
	"os"

	"github.com/maxmind/mmdbwriter"
	"github.com/maxmind/mmdbwriter/inserter"
	"github.com/maxmind/mmdbwriter/mmdbtype"
)

func main() {
	// Load the database we wish to enrich.
	writer, err := mmdbwriter.Load("GeoLite2-City.mmdb", mmdbwriter.Options{IncludeReservedNetworks: true})
	if err != nil {
		log.Fatal(err)
	}

	// Define and insert the new data.
	data := mmdbtype.Map{
		"country": mmdbtype.Map{
			"geoname_id": mmdbtype.Uint32(1880251),
			"iso_code":   mmdbtype.String("SG"),
			"names": mmdbtype.Map{
				"en": mmdbtype.String("Singapore"),
			},
		},
	}
	_, network1, err := net.ParseCIDR("10.200.11.0/28")
	if err != nil {
		log.Fatal(err)
	}
	if err := writer.InsertFunc(network1, inserter.TopLevelMergeWith(data)); err != nil {
		log.Fatal(err)
	}

	_, network2, err := net.ParseCIDR("10.200.11.16/30")
	if err != nil {
		log.Fatal(err)
	}
	if err := writer.InsertFunc(network2, inserter.TopLevelMergeWith(data)); err != nil {
		log.Fatal(err)
	}

	_, network3, err := net.ParseCIDR("10.200.11.20/32")
	if err != nil {
		log.Fatal(err)
	}
	if err := writer.InsertFunc(network3, inserter.TopLevelMergeWith(data)); err != nil {
		log.Fatal(err)
	}

	// Write the newly enriched DB to the filesystem.
	fh, err := os.Create("GeoLite2-City-with-Reserved-SG-Data.mmdb")
	if err != nil {
		log.Fatal(err)
	}
	_, err = writer.WriteTo(fh)
	if err != nil {
		log.Fatal(err)
	}
}

I adapted the example from the blogpost, switching to GeoLite2-City.mmdb, changing the data we use to the data you supplied, using a CIDR calculator to determine the ranges for 10.200.11.0 - 10.200.11.20, and using the IncludeReservedNetworks as discussed above.

from mmdb-from-go-blogpost.

j-nanaji avatar j-nanaji commented on July 23, 2024

from mmdb-from-go-blogpost.

nchelluri avatar nchelluri commented on July 23, 2024

Hi Nanaji, glad it worked out for you. The source database was originally generated by https://github.com/maxmind/MaxMind-DB-Reader-perl and the implementation is slightly different than using this library. It is safe to ignore the difference in file size, as it is an implementation detail. Databases generated with this library will be slightly smaller than those generated with the Perl one, generally, due to a difference in the logic on when to use a pointer.

from mmdb-from-go-blogpost.

Related Issues (11)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.