Skip to content

Commit

Permalink
Fixed conversion for MMDB using IP2Location LITE CSV
Browse files Browse the repository at this point in the history
  • Loading branch information
ip2location committed May 29, 2024
1 parent cafe97d commit 208079d
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 46 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ $GOPATH/bin/ip2convert
#### Debian/Ubuntu (amd64)

```bash
curl -LO https://github.com/ip2location/ip2convert/releases/download/v1.2.0/ip2convert-1.2.0.deb
sudo dpkg -i ip2convert-1.2.0.deb
curl -LO https://github.com/ip2location/ip2convert/releases/download/v1.2.1/ip2convert-1.2.1.deb
sudo dpkg -i ip2convert-1.2.1.deb
```


Expand Down Expand Up @@ -89,12 +89,12 @@ After choosing a platform `PLAT` from above, run:

```bash
# for Windows, use ".zip" instead of ".tar.gz"
curl -LO https://github.com/ip2location/ip2convert/releases/download/v1.2.0/ip2convert_1.2.0_${PLAT}.tar.gz
curl -LO https://github.com/ip2location/ip2convert/releases/download/v1.2.1/ip2convert_1.2.1_${PLAT}.tar.gz
# OR
wget https://github.com/ip2location/ip2convert/releases/download/v1.2.0/ip2convert_1.2.0_${PLAT}.tar.gz
wget https://github.com/ip2location/ip2convert/releases/download/v1.2.1/ip2convert_1.2.1_${PLAT}.tar.gz

tar -xvf ip2convert_1.2.0_${PLAT}.tar.gz
mv ip2convert_1.2.0_${PLAT} /usr/local/bin/ip2convert
tar -xvf ip2convert_1.2.1_${PLAT}.tar.gz
mv ip2convert_1.2.1_${PLAT} /usr/local/bin/ip2convert
```


Expand Down
6 changes: 5 additions & 1 deletion debian/changelog
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
ip2convert (1.2.0) jammy; urgency=medium
ip2convert (1.2.1) jammy; urgency=medium

* 1.2.1 fixed conversion for MMDB with IP2Location LITE CSV.

-- IP2Location <[email protected]> Wed, 29 May 2024 15:04:19 +0800

* 1.2.0 added support for IP2Location BIN format.

Expand Down
2 changes: 1 addition & 1 deletion debian/control
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ Vcs-browser: https://github.com/ip2location/ip2convert
Homepage: https://www.ip2location.com
XS-Go-Import-Path: github.com/ip2location/ip2convert
Package: ip2convert
Version: 1.2.0
Version: 1.2.1
Architecture: amd64
Description: This is the official CLI for converting IP2Location CSV to MMDB.
2 changes: 1 addition & 1 deletion ip2convert/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var cmdCSV2BINDBPackage string
var cmdCSV2BINInput string
var cmdCSV2BINOutput string

const version string = "1.2.0"
const version string = "1.2.1"
const programName string = "ip2convert Geolocation File Format Converter"

var showVer bool = false
Expand Down
90 changes: 54 additions & 36 deletions ip2convert/csv2mmdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,13 @@ func AppendDB1CSVRecord(delim rune, parts []string, tree *mmdbwriter.Tree) error
oriStartNum := parts[0]
oriEndNum := parts[1]

skipSpecialCase := false

if oriEndNum == "281474976710655" {
// should be LITE CSV which does not have range with IPv4 start and IPv6 end
skipSpecialCase = true
}

startNum := new(big.Int)
startNum, _ = startNum.SetString(parts[0], 10)

Expand Down Expand Up @@ -161,26 +168,28 @@ func AppendDB1CSVRecord(delim rune, parts []string, tree *mmdbwriter.Tree) error
record["country"] = country

if err = tree.InsertRange(startIp, endIp, record); err != nil {
if strings.Contains(err.Error(), "start & end IPs did not give valid range") { // special case where start IP is IPv4-mapped IPv6 (converted by Go into plain IPv4)
// need to split into 2 ranges
splitIPv4 := make([]string, len(parts))
splitIPv6 := make([]string, len(parts))
copy(splitIPv4, parts)
copy(splitIPv6, parts)

splitIPv4[0] = oriStartNum
splitIPv4[1] = "281474976710655"
splitIPv6[0] = "281474976710656"
splitIPv6[1] = oriEndNum

if err = AppendDB1CSVRecord(delim, splitIPv4, tree); err != nil {
if skipSpecialCase {
if strings.Contains(err.Error(), "start & end IPs did not give valid range") { // special case where start IP is IPv4-mapped IPv6 (converted by Go into plain IPv4)
// need to split into 2 ranges
splitIPv4 := make([]string, len(parts))
splitIPv6 := make([]string, len(parts))
copy(splitIPv4, parts)
copy(splitIPv6, parts)

splitIPv4[0] = oriStartNum
splitIPv4[1] = "281474976710655"
splitIPv6[0] = "281474976710656"
splitIPv6[1] = oriEndNum

if err = AppendDB1CSVRecord(delim, splitIPv4, tree); err != nil {
return err
}
if err = AppendDB1CSVRecord(delim, splitIPv6, tree); err != nil {
return err
}
} else if !strings.Contains(err.Error(), "which is in an aliased network") {
return err
}
if err = AppendDB1CSVRecord(delim, splitIPv6, tree); err != nil {
return err
}
} else if !strings.Contains(err.Error(), "which is in an aliased network") {
return err
}
}
return nil
Expand All @@ -193,6 +202,13 @@ func AppendDB9CSVRecord(delim rune, parts []string, tree *mmdbwriter.Tree) error
oriStartNum := parts[0]
oriEndNum := parts[1]

skipSpecialCase := false

if oriEndNum == "281474976710655" {
// should be LITE CSV which does not have range with IPv4 start and IPv6 end
skipSpecialCase = true
}

startNum := new(big.Int)
startNum, _ = startNum.SetString(parts[0], 10)

Expand Down Expand Up @@ -258,26 +274,28 @@ func AppendDB9CSVRecord(delim rune, parts []string, tree *mmdbwriter.Tree) error
record["subdivisions"] = subdivisions

if err = tree.InsertRange(startIp, endIp, record); err != nil {
if strings.Contains(err.Error(), "start & end IPs did not give valid range") { // special case where start IP is IPv4-mapped IPv6 (converted by Go into plain IPv4)
// need to split into 2 ranges
splitIPv4 := make([]string, len(parts))
splitIPv6 := make([]string, len(parts))
copy(splitIPv4, parts)
copy(splitIPv6, parts)

splitIPv4[0] = oriStartNum
splitIPv4[1] = "281474976710655"
splitIPv6[0] = "281474976710656"
splitIPv6[1] = oriEndNum

if err = AppendDB9CSVRecord(delim, splitIPv4, tree); err != nil {
if skipSpecialCase {
if strings.Contains(err.Error(), "start & end IPs did not give valid range") { // special case where start IP is IPv4-mapped IPv6 (converted by Go into plain IPv4)
// need to split into 2 ranges
splitIPv4 := make([]string, len(parts))
splitIPv6 := make([]string, len(parts))
copy(splitIPv4, parts)
copy(splitIPv6, parts)

splitIPv4[0] = oriStartNum
splitIPv4[1] = "281474976710655"
splitIPv6[0] = "281474976710656"
splitIPv6[1] = oriEndNum

if err = AppendDB9CSVRecord(delim, splitIPv4, tree); err != nil {
return err
}
if err = AppendDB9CSVRecord(delim, splitIPv6, tree); err != nil {
return err
}
} else if !strings.Contains(err.Error(), "which is in an aliased network") {
return err
}
if err = AppendDB9CSVRecord(delim, splitIPv6, tree); err != nil {
return err
}
} else if !strings.Contains(err.Error(), "which is in an aliased network") {
return err
}
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion scripts/build-deb.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash

VERSION="1.2.0"
VERSION="1.2.1"

rm -rf ../dist
mkdir -p ../dist/DEBIAN/
Expand Down

0 comments on commit 208079d

Please sign in to comment.