Skip to content

Commit

Permalink
Fixed BIN conversion using LITE CSV
Browse files Browse the repository at this point in the history
  • Loading branch information
ip2location committed Jun 13, 2024
1 parent 208079d commit 456ed87
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 15 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.1/ip2convert-1.2.1.deb
sudo dpkg -i ip2convert-1.2.1.deb
curl -LO https://github.com/ip2location/ip2convert/releases/download/v1.2.2/ip2convert-1.2.2.deb
sudo dpkg -i ip2convert-1.2.2.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.1/ip2convert_1.2.1_${PLAT}.tar.gz
curl -LO https://github.com/ip2location/ip2convert/releases/download/v1.2.2/ip2convert_1.2.2_${PLAT}.tar.gz
# OR
wget https://github.com/ip2location/ip2convert/releases/download/v1.2.1/ip2convert_1.2.1_${PLAT}.tar.gz
wget https://github.com/ip2location/ip2convert/releases/download/v1.2.2/ip2convert_1.2.2_${PLAT}.tar.gz

tar -xvf ip2convert_1.2.1_${PLAT}.tar.gz
mv ip2convert_1.2.1_${PLAT} /usr/local/bin/ip2convert
tar -xvf ip2convert_1.2.2_${PLAT}.tar.gz
mv ip2convert_1.2.2_${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.1) jammy; urgency=medium
ip2convert (1.2.2) jammy; urgency=medium

* 1.2.2 fixed conversion for BIN with IP2Location LITE CSV.

-- IP2Location <[email protected]> Thu, 13 Jun 2024 12:17:19 +0800

* 1.2.1 fixed conversion for MMDB with IP2Location LITE CSV.

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.1
Version: 1.2.2
Architecture: amd64
Description: This is the official CLI for converting IP2Location CSV to MMDB.
6 changes: 3 additions & 3 deletions 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.1"
const version string = "1.2.2"
const programName string = "ip2convert Geolocation File Format Converter"

var showVer bool = false
Expand Down Expand Up @@ -155,7 +155,7 @@ NOTE:
OR download the free LITE DB9 from https://lite.ip2location.com
To convert IP2Location DB CSV to IP2Location BIN
To convert IP2Location DB IPv6 CSV to IP2Location BIN
Usage: EXE csv2bin [OPTION]
Expand All @@ -168,7 +168,7 @@ To convert IP2Location DB CSV to IP2Location BIN
NOTE:
The conversion requires the IP2Location DB CSV file.
The conversion requires the IP2Location DB IPv6 CSV file.
You can either subscribe to the commercial DB at https://www.ip2location.com
OR download the free LITE DB from https://lite.ip2location.com
Expand Down
45 changes: 42 additions & 3 deletions ip2convert/csv2bin.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func WriteBIN(input string, output string, dbPackage string) {
var longSize uint32 = 4
dbColl := columnSize[dbType]
var ipv6Count uint32 = 0
var ipv6Base uint32 = 0
var ipv6Base uint32

// cannot have initial size as we won't know the total elements for each until we read the CSV
country := map[string]*countryType{}
Expand Down Expand Up @@ -177,6 +177,20 @@ func WriteBIN(input string, output string, dbPackage string) {
return
}

if parts[0] == "0" && parts[1] == "281470681743359" {
continue // just skip the uncompressed line at the start of LITE
}
if parts[0] == "281470681743360" && parts[1] == "1470698520575" {
parts[0] = "0" // compress the 2 lines at the start of LITE into 1 line
}

if parts[0] == "281474439839744" && parts[1] == "281474976710655" {
continue // just skip the uncompressed line in the LITE for the IPv4/IPv6 boundary
}
if parts[0] == "281474976710656" && parts[1] == "42540528726795050063891204319802818559" {
parts[0] = "281474439839744" // compress the 2 lines in the LITE for the IPv4/IPv6 boundary into 1 line
}

lines++

if parts[2] == "UK" {
Expand Down Expand Up @@ -264,6 +278,18 @@ func WriteBIN(input string, output string, dbPackage string) {
endNum := new(big.Int)
endNum, _ = endNum.SetString(parts[1], 10)

// need to detect if is IPv4 CSV and show error
// check line 10 because some IPv6 CSV has IPv4 ranges at the start
if lines == 10 {
endOfIPV4 := new(big.Int)
endOfIPV4, _ = endOfIPV4.SetString("4294967295", 10) // 255.255.255.255

if startNum.Cmp(endOfIPV4) <= 0 || endNum.Cmp(endOfIPV4) <= 0 {
fmt.Println("Please use IP2Location IPv6 CSV.")
return
}
}

var startIP net.IP
var endIP net.IP

Expand Down Expand Up @@ -336,7 +362,6 @@ func WriteBIN(input string, output string, dbPackage string) {
lastIPv4To = "16777215"
ipv4Count++
}

} else { // normal case where ISP field not present or is IPv4 CSV or rows not covered by the above criteria
if startIP, err = DecimalToIPv4(startNum); err != nil {
if startIP, err = DecimalToIPv6(startNum); err != nil {
Expand Down Expand Up @@ -738,6 +763,20 @@ func WriteBIN(input string, output string, dbPackage string) {
return
}

if parts[0] == "0" && parts[1] == "281470681743359" {
continue // just skip the uncompressed line at the start of LITE
}
if parts[0] == "281470681743360" && parts[1] == "1470698520575" {
parts[0] = "0" // compress the 2 lines at the start of LITE into 1 line
}

if parts[0] == "281474439839744" && parts[1] == "281474976710655" {
continue // just skip the uncompressed line in the LITE for the IPv4/IPv6 boundary
}
if parts[0] == "281474976710656" && parts[1] == "42540528726795050063891204319802818559" {
parts[0] = "281474439839744" // compress the 2 lines in the LITE for the IPv4/IPv6 boundary into 1 line
}

lines++

startNum := new(big.Int)
Expand Down Expand Up @@ -1291,9 +1330,9 @@ func WriteBIN(input string, output string, dbPackage string) {
ipv4boundary = false

var err error
var v4Bytes []byte
var v6Bytes []byte

var v4Bytes []byte
// IPv4 part
v4Bytes, err = IPv4ToBytes("255.255.255.255")
if err != 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.1"
VERSION="1.2.2"

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

0 comments on commit 456ed87

Please sign in to comment.