mirror of https://github.com/mpolden/echoip
use big int for decimal IP rather than uint64
This commit is contained in:
parent
a9c0587f87
commit
6d915d0f96
|
@ -11,6 +11,7 @@ import (
|
||||||
"github.com/mpolden/ipd/iputil/geo"
|
"github.com/mpolden/ipd/iputil/geo"
|
||||||
"github.com/mpolden/ipd/useragent"
|
"github.com/mpolden/ipd/useragent"
|
||||||
|
|
||||||
|
"math/big"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
@ -31,7 +32,7 @@ type Server struct {
|
||||||
|
|
||||||
type Response struct {
|
type Response struct {
|
||||||
IP net.IP `json:"ip"`
|
IP net.IP `json:"ip"`
|
||||||
IPDecimal uint64 `json:"ip_decimal"`
|
IPDecimal *big.Int `json:"ip_decimal"`
|
||||||
Country string `json:"country,omitempty"`
|
Country string `json:"country,omitempty"`
|
||||||
CountryISO string `json:"country_iso,omitempty"`
|
CountryISO string `json:"country_iso,omitempty"`
|
||||||
City string `json:"city,omitempty"`
|
City string `json:"city,omitempty"`
|
||||||
|
|
|
@ -27,12 +27,12 @@ func LookupPort(ip net.IP, port uint64) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func ToDecimal(ip net.IP) uint64 {
|
func ToDecimal(ip net.IP) *big.Int {
|
||||||
i := big.NewInt(0)
|
i := big.NewInt(0)
|
||||||
if to4 := ip.To4(); to4 != nil {
|
if to4 := ip.To4(); to4 != nil {
|
||||||
i.SetBytes(to4)
|
i.SetBytes(to4)
|
||||||
} else {
|
} else {
|
||||||
i.SetBytes(ip)
|
i.SetBytes(ip)
|
||||||
}
|
}
|
||||||
return i.Uint64()
|
return i
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,21 +1,26 @@
|
||||||
package iputil
|
package iputil
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"math/big"
|
||||||
"net"
|
"net"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestToDecimal(t *testing.T) {
|
func TestToDecimal(t *testing.T) {
|
||||||
|
var msb = new(big.Int)
|
||||||
|
msb, _ = msb.SetString("80000000000000000000000000000000", 16)
|
||||||
|
|
||||||
var tests = []struct {
|
var tests = []struct {
|
||||||
in string
|
in string
|
||||||
out uint64
|
out *big.Int
|
||||||
}{
|
}{
|
||||||
{"127.0.0.1", 2130706433},
|
{"127.0.0.1", big.NewInt(2130706433)},
|
||||||
{"::1", 1},
|
{"::1", big.NewInt(1)},
|
||||||
|
{"8000::", msb},
|
||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
i := ToDecimal(net.ParseIP(tt.in))
|
i := ToDecimal(net.ParseIP(tt.in))
|
||||||
if tt.out != i {
|
if tt.out.Cmp(i) != 0 {
|
||||||
t.Errorf("Expected %d, got %d for IP %s", tt.out, i, tt.in)
|
t.Errorf("Expected %d, got %d for IP %s", tt.out, i, tt.in)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue