maxmind / mmdbwriter

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

ipv6 insert null #67

Closed Liang2580 closed 12 months ago

Liang2580 commented 12 months ago
func TestIPV6(t *testing.T) {

    fip, err := os.Open("./ipv6data.txt")
    if err != nil {
        fmt.Println("read file fail", err)
        return
    }
    defer fip.Close()
    buf := bufio.NewReader(fip)
    //一行一个
    //一行行读取
    scanner := bufio.NewScanner(buf)

    // Read each line until the end of the file
    count := 0
    for scanner.Scan() {
        line := scanner.Text()
        // Process the line as needed (e.g., print it)
        //去掉收尾空格
        line = strings.TrimSpace(line)

        count += 1
        if count == 1000 {
            break
        }

        // | 分割
        s := strings.Split(line, "|")
        //Insert(s, s[0], writer)
        ip1 := net.ParseIP(s[0])
        ip2 := net.ParseIP(s[1])

        startNetIP, ok := netipx.FromStdIP(ip1)
        if !ok {
            fmt.Println("start IP is invalid")
        }
        endNetIP, ok := netipx.FromStdIP(ip2)
        if !ok {
            fmt.Println("end IP is invalid")
        }

        r := netipx.IPRangeFrom(startNetIP, endNetIP)
        if !r.IsValid() {
            continue
        }
        //inputGeo = flag.String("i", "./GeoLite2-City.testmmdb", "Input GeoLite2-City.mmdb file path.")
        writer, _ := mmdbwriter.Load("./GeoLite2-City.testmmdb", mmdbwriter.Options{
            IncludeReservedNetworks: true,
            Description:             map[string]string{"en": fmt.Sprintf("Compiled with mmdb-editor (%v) https://github.com/iglov/mmdb-editor", Version)},
        })

        data := mmdbtype.Map{
            "country": mmdbtype.Map{
                "country":       mmdbtype.String("11"),
                "province":      mmdbtype.String("11"),
                "city":          mmdbtype.String("11"),
                "en_short_code": mmdbtype.String("11"),
            },
        }

        _, network, err := net.ParseCIDR(s[0] + "/64")
        if err != nil {
            log.Fatal(err)
        }

        err = writer.Insert(network, data)
        if err != nil {
            log.Fatal(err)
        }
        //err := writer.InsertRange(ip1, ip2, data)
        //if err != nil {
        //  fmt.Println("error1", err)
        //}
        fh, err := os.Create("./GeoLite2-ipv6.mmdb")
        if err != nil {
            fmt.Println("error2", err)
        }

        defer Check(fh.Close)

        _, err = writer.WriteTo(fh)
        if err != nil {
            fmt.Println("error3", err)
        }
    }

}

After importing 10000 rows of data, it was found that the exported database size is 2K

Liang2580 commented 12 months ago

is data GeoLite2-ASN-Blocks-IPv6.csv

Liang2580 commented 12 months ago

https://github.com/maxmind/mmdbwriter/blob/main/examples/asn-writer/main.go

I saw that the ipv6 format can be inserted in this code.

oschwald commented 12 months ago

I am sorry. I am not sure I understand the question.

Liang2580 commented 12 months ago

I have already found the reason for the attack. It's because I didn't create an ipv6 database file. The database file I used before was IPV4

Liang2580 commented 12 months ago

load ./GeoLite2-City.testmmdb is IPV4 files