Jelajahi Sumber

Fix for IPv4 address

V2Ray 10 tahun lalu
induk
melakukan
2a00e2c6e9
2 mengubah file dengan 23 tambahan dan 0 penghapusan
  1. 12 0
      common/net/address.go
  2. 11 0
      common/net/address_test.go

+ 12 - 0
common/net/address.go

@@ -22,6 +22,15 @@ type Address interface {
 	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.
 func IPAddress(ip []byte, port uint16) Address {
 	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]},
 		}
 	case net.IPv6len:
+		if allZeros(ip[0:10]) && ip[10] == 0xff && ip[11] == 0xff {
+			return IPAddress(ip[12:16], port)
+		}
 		return IPv6Address{
 			PortAddress: PortAddress{port: port},
 			ip: [16]byte{

+ 11 - 0
common/net/address_test.go

@@ -1,6 +1,7 @@
 package net
 
 import (
+	"net"
 	"testing"
 
 	"github.com/v2ray/v2ray-core/testing/unit"
@@ -55,3 +56,13 @@ func TestDomainAddress(t *testing.T) {
 	assert.Uint16(addr.Port()).Equals(port)
 	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")
+}