diff --git a/pkg/iplibrary/.gitignore b/pkg/iplibrary/.gitignore index 893b37f..0373235 100644 --- a/pkg/iplibrary/.gitignore +++ b/pkg/iplibrary/.gitignore @@ -1 +1 @@ - ip.db \ No newline at end of file +ip.db \ No newline at end of file diff --git a/pkg/iplibrary/ip.db b/pkg/iplibrary/ip.db deleted file mode 100644 index 9dd733c..0000000 Binary files a/pkg/iplibrary/ip.db and /dev/null differ diff --git a/pkg/iplibrary/reader.go b/pkg/iplibrary/reader.go index b1bf460..fe10444 100644 --- a/pkg/iplibrary/reader.go +++ b/pkg/iplibrary/reader.go @@ -169,14 +169,15 @@ func (this *Reader) parse(data []byte) (left []byte, err error) { return nil, errors.New("invalid ip version '" + string(line) + "'") } + var ipFrom uint64 + var ipTo uint64 if len(pieces[2]) == 0 { pieces[2] = pieces[1] - } - - var ipFrom = types.Uint64(pieces[1]) - var ipTo = types.Uint64(pieces[2]) - if ipFrom > ipTo { - ipFrom, ipTo = ipTo, ipFrom + ipFrom = types.Uint64(pieces[1]) + ipTo = types.Uint64(pieces[2]) + } else { + ipFrom = types.Uint64(pieces[1]) + ipTo = types.Uint64(pieces[2]) + ipFrom } if version == "4" { diff --git a/pkg/iplibrary/writer.go b/pkg/iplibrary/writer.go index 9748b17..f5fa234 100644 --- a/pkg/iplibrary/writer.go +++ b/pkg/iplibrary/writer.go @@ -96,13 +96,20 @@ func (this *Writer) Write(ipFrom string, ipTo string, countryId int64, provinceI } // 1 - pieces = append(pieces, types.String(configutils.IP2Long(fromIP))) + var fromIPLong = configutils.IP2Long(fromIP) + var toIPLong = configutils.IP2Long(toIP) + + if toIPLong < fromIPLong { + fromIPLong, toIPLong = toIPLong, fromIPLong + } + + pieces = append(pieces, types.String(fromIPLong)) if ipFrom == ipTo { // 2 pieces = append(pieces, "") } else { // 2 - pieces = append(pieces, types.String(configutils.IP2Long(toIP))) + pieces = append(pieces, types.String(toIPLong-fromIPLong)) } // 3