mirror of https://github.com/v2ray/v2ray-core
Fix for IPv4 address
parent
8408b7735c
commit
2a00e2c6e9
|
@ -22,6 +22,15 @@ type Address interface {
|
||||||
String() string // String representation of this Address
|
String() string // String representation of this Address
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func allZeros(data []byte) bool {
|
||||||
|
for _, v := range data {
|
||||||
|
if v != 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
// IPAddress creates an Address with given IP and port.
|
// IPAddress creates an Address with given IP and port.
|
||||||
func IPAddress(ip []byte, port uint16) Address {
|
func IPAddress(ip []byte, port uint16) Address {
|
||||||
switch len(ip) {
|
switch len(ip) {
|
||||||
|
@ -31,6 +40,9 @@ func IPAddress(ip []byte, port uint16) Address {
|
||||||
ip: [4]byte{ip[0], ip[1], ip[2], ip[3]},
|
ip: [4]byte{ip[0], ip[1], ip[2], ip[3]},
|
||||||
}
|
}
|
||||||
case net.IPv6len:
|
case net.IPv6len:
|
||||||
|
if allZeros(ip[0:10]) && ip[10] == 0xff && ip[11] == 0xff {
|
||||||
|
return IPAddress(ip[12:16], port)
|
||||||
|
}
|
||||||
return IPv6Address{
|
return IPv6Address{
|
||||||
PortAddress: PortAddress{port: port},
|
PortAddress: PortAddress{port: port},
|
||||||
ip: [16]byte{
|
ip: [16]byte{
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package net
|
package net
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"net"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/v2ray/v2ray-core/testing/unit"
|
"github.com/v2ray/v2ray-core/testing/unit"
|
||||||
|
@ -55,3 +56,13 @@ func TestDomainAddress(t *testing.T) {
|
||||||
assert.Uint16(addr.Port()).Equals(port)
|
assert.Uint16(addr.Port()).Equals(port)
|
||||||
assert.String(addr.String()).Equals("v2ray.com:443")
|
assert.String(addr.String()).Equals("v2ray.com:443")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestNetIPv4Address(t *testing.T) {
|
||||||
|
assert := unit.Assert(t)
|
||||||
|
|
||||||
|
ip := net.IPv4(1, 2, 3, 4)
|
||||||
|
port := uint16(80)
|
||||||
|
addr := IPAddress(ip, port)
|
||||||
|
assert.Bool(addr.IsIPv4()).IsTrue()
|
||||||
|
assert.String(addr.String()).Equals("1.2.3.4:80")
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue