Test UDPPing country parsing.
This commit is contained in:
parent
ee5fcaef14
commit
4e6b47c2a3
3 changed files with 29 additions and 18 deletions
|
@ -73,7 +73,7 @@ func (s *Server) getNumSubs() int64 {
|
||||||
// pings it, so we can determine our own external IP address.
|
// pings it, so we can determine our own external IP address.
|
||||||
func (s *Server) getAndSetExternalIp(msg *pb.ServerMessage) error {
|
func (s *Server) getAndSetExternalIp(msg *pb.ServerMessage) error {
|
||||||
log.Println(msg)
|
log.Println(msg)
|
||||||
myIp, err := UDPPing(msg.Address, msg.Port)
|
myIp, _, err := UDPPing(msg.Address, msg.Port)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -149,47 +149,52 @@ func (pong *SPVPong) DecodeAddress() net.IP {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (pong *SPVPong) DecodeCountry() string {
|
||||||
|
return pb.Location_Country_name[int32(pong.country)]
|
||||||
|
}
|
||||||
|
|
||||||
// UDPPing sends a ping over udp to another hub and returns the ip address of
|
// UDPPing sends a ping over udp to another hub and returns the ip address of
|
||||||
// this hub.
|
// this hub.
|
||||||
func UDPPing(ip, port string) (net.IP, error) {
|
func UDPPing(ip, port string) (net.IP, string, error) {
|
||||||
address := ip + ":" + port
|
address := ip + ":" + port
|
||||||
addr, err := net.ResolveUDPAddr("udp", address)
|
addr, err := net.ResolveUDPAddr("udp", address)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return net.IP{}, err
|
return net.IP{}, "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
conn, err := net.DialUDP("udp", nil, addr)
|
conn, err := net.DialUDP("udp", nil, addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return net.IP{}, err
|
return net.IP{}, "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
|
|
||||||
_, err = conn.Write(encodeSPVPing())
|
_, err = conn.Write(encodeSPVPing())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return net.IP{}, err
|
return net.IP{}, "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
buffer := make([]byte, maxBufferSize)
|
buffer := make([]byte, maxBufferSize)
|
||||||
deadline := time.Now().Add(time.Second)
|
deadline := time.Now().Add(time.Second)
|
||||||
err = conn.SetReadDeadline(deadline)
|
err = conn.SetReadDeadline(deadline)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return net.IP{}, err
|
return net.IP{}, "", err
|
||||||
}
|
}
|
||||||
n, _, err := conn.ReadFromUDP(buffer)
|
n, _, err := conn.ReadFromUDP(buffer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return net.IP{}, err
|
return net.IP{}, "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
pong := decodeSPVPong(buffer[:n])
|
pong := decodeSPVPong(buffer[:n])
|
||||||
|
|
||||||
if pong == nil {
|
if pong == nil {
|
||||||
return net.IP{}, errors.Base("Pong decoding failed")
|
return net.IP{}, "", errors.Base("Pong decoding failed")
|
||||||
}
|
}
|
||||||
|
|
||||||
myAddr := pong.DecodeAddress()
|
myAddr := pong.DecodeAddress()
|
||||||
|
country := pong.DecodeCountry()
|
||||||
|
|
||||||
return myAddr, nil
|
return myAddr, country, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// UDPServer is a goroutine that starts an udp server that implements the hubs
|
// UDPServer is a goroutine that starts an udp server that implements the hubs
|
||||||
|
|
|
@ -13,12 +13,14 @@ func TestUDPPing(t *testing.T) {
|
||||||
args.StartUDP = false
|
args.StartUDP = false
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
want string
|
wantIP string
|
||||||
|
wantCountry string
|
||||||
} {
|
} {
|
||||||
{
|
{
|
||||||
name: "Get the right ip from production server.",
|
name: "Get the right ip from production server.",
|
||||||
want: "SETME",
|
wantIP: "SETME",
|
||||||
|
wantCountry: "US",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -28,7 +30,8 @@ func TestUDPPing(t *testing.T) {
|
||||||
toAddr := "spv16.lbry.com"
|
toAddr := "spv16.lbry.com"
|
||||||
toPort := "50001"
|
toPort := "50001"
|
||||||
|
|
||||||
ip, err := UDPPing(toAddr, toPort)
|
ip, country, err := UDPPing(toAddr, toPort)
|
||||||
|
gotCountry := country
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
}
|
}
|
||||||
|
@ -41,11 +44,14 @@ func TestUDPPing(t *testing.T) {
|
||||||
|
|
||||||
digIP := strings.TrimSpace(string(res))
|
digIP := strings.TrimSpace(string(res))
|
||||||
udpIP := ip.String()
|
udpIP := ip.String()
|
||||||
tt.want = digIP
|
tt.wantIP = digIP
|
||||||
|
|
||||||
got1 := udpIP
|
gotIP := udpIP
|
||||||
if got1 != tt.want {
|
if gotIP != tt.wantIP {
|
||||||
t.Errorf("got: '%s', want: '%s'\n", got1, tt.want)
|
t.Errorf("got: '%s', want: '%s'\n", gotIP, tt.wantIP)
|
||||||
|
}
|
||||||
|
if gotCountry != tt.wantCountry {
|
||||||
|
t.Errorf("got: '%s', want: '%s'\n", gotCountry, tt.wantCountry)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue