maxmind / mmdbwriter

Go library for writing MaxMind DB (mmdb) files
Apache License 2.0
116 stars 28 forks source link

How to insert my custom data #30

Closed intfish123 closed 2 years ago

intfish123 commented 2 years ago

I rewrote my code like this, But it still doesn't work, you can try it:

writer, err := mmdbwriter.New(
    mmdbwriter.Options{
        DatabaseType: "GeoLite2-City",
            RecordSize: 24,
            IPVersion: 4,
            IncludeReservedNetworks: true,
            Languages: []string{"zh-CN"},
    },
)
if err != nil {
    return err
}

nameId := 0
for i:=0; i<len(customData); i++ {
    record := mmdbtype.Map{}
    record["continent"] = mmdbtype.Map{
        "geoname_id": mmdbtype.Uint64(nameId),
        "code": mmdbtype.String(""),
        "names":mmdbtype.Map{"zh-CN": mmdbtype.String("")},
    }
    nameId++

    record["country"] = mmdbtype.Map{
        "geoname_id": mmdbtype.Uint64(nameId),
        "iso_code": mmdbtype.String(""),
        "names":mmdbtype.Map{"zh-CN": mmdbtype.String(customData[i].Country)},
    }
    nameId++

    record["subdivisions"] = mmdbtype.Slice{
        mmdbtype.Map{
            "geoname_id": mmdbtype.Uint64(nameId),
            "iso_code": mmdbtype.String(""),
            "names":mmdbtype.Map{"zh-CN": mmdbtype.String(customData[i].Province)},
      },
    }
    nameId++

    record["city"] = mmdbtype.Map{
        "geoname_id": mmdbtype.Uint64(nameId),
        "names":mmdbtype.Map{"zh-CN": mmdbtype.String(customData[i].City)},
    }
    nameId++

    record["county"] = mmdbtype.Map{
        "geoname_id": mmdbtype.Uint64(nameId),
        "names":mmdbtype.Map{"zh-CN": mmdbtype.String(customData[i].County)},
    }
    nameId++

    record["location"] = mmdbtype.Map{
        "accuracy_radius": mmdbtype.Uint16(0),
        "latitude": mmdbtype.Float64(0),
        "longitude": mmdbtype.Float64(0),
        "metro_code": mmdbtype.Uint64(0),
        "time_zone": mmdbtype.String(""),
    }
    nameId++

    record["postal"] = mmdbtype.Map{
        "code": mmdbtype.String(""),
    }
    nameId++

    record["registered_country"] = mmdbtype.Map{
        "geoname_id": mmdbtype.Uint64(nameId),
        "is_in_european_union": mmdbtype.Bool(false),
        "iso_code": mmdbtype.String(""),
        "names": mmdbtype.Map{"zh-CN": mmdbtype.String("")},
    }
    nameId++

    record["represented_country"] = mmdbtype.Map{
        "geoname_id": mmdbtype.Uint64(nameId),
        "is_in_european_union": mmdbtype.Bool(false),
        "iso_code": mmdbtype.String(""),
        "type": mmdbtype.String(""),
        "names": mmdbtype.Map{"zh-CN": mmdbtype.String("")},
    }
    nameId++

    tmpIp := net.ParseIP(customData[i].ipVal)
    writer.Insert(&net.IPNet{IP: tmpIp, Mask: tmpIp.DefaultMask()}, record)
}
fh, err := os.Create("out/out.mmdb")
if err != nil {
    log.Fatal(err)
}
_, err = dbWriter.WriteTo(fh)
if err != nil {
    log.Fatal(err)
}
intfish123 commented 2 years ago

I read the db use this lib: github.com/oschwald/geoip2-golang v1.7.0

oschwald commented 2 years ago

Your code is not complete as is and I am not able to run it. At a glance, I don't see anything obviously wrong. I would suggest comparing the structure of records in the database you generated with a MaxMind one using mmdblookup from libmaxminddb. I am closing this as I don't see any evidence there is a problem with mmdbwriter.

intfish123 commented 2 years ago

Your code is not complete as is and I am not able to run it. At a glance, I don't see anything obviously wrong. I would suggest comparing the structure of records in the database you generated with a MaxMind one using mmdblookup from libmaxminddb. I am closing this as I don't see any evidence there is a problem with mmdbwriter.

Yep, you are right, I have found what's the my problem, &net.IPNet{IP: tmpIp, Mask: tmpIp.DefaultMask()} is not correct for *IPNet. Thanks you very much.