mirror of https://github.com/v2ray/v2ray-core
				
				
				
			special handling for IPv6 from URL
							parent
							
								
									b0ac43a8fc
								
							
						
					
					
						commit
						dd159cce6c
					
				| 
						 | 
					@ -73,6 +73,12 @@ type Address interface {
 | 
				
			||||||
// ParseAddress parses a string into an Address. The return value will be an IPAddress when
 | 
					// ParseAddress parses a string into an Address. The return value will be an IPAddress when
 | 
				
			||||||
// the string is in the form of IPv4 or IPv6 address, or a DomainAddress otherwise.
 | 
					// the string is in the form of IPv4 or IPv6 address, or a DomainAddress otherwise.
 | 
				
			||||||
func ParseAddress(addr string) Address {
 | 
					func ParseAddress(addr string) Address {
 | 
				
			||||||
 | 
						// Handle IPv6 address in form as "[2001:4860:0:2001::68]"
 | 
				
			||||||
 | 
						lenAddr := len(addr)
 | 
				
			||||||
 | 
						if lenAddr > 0 && addr[0] == '[' && addr[lenAddr-1] == ']' {
 | 
				
			||||||
 | 
							addr = addr[1 : lenAddr-1]
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	ip := net.ParseIP(addr)
 | 
						ip := net.ParseIP(addr)
 | 
				
			||||||
	if ip != nil {
 | 
						if ip != nil {
 | 
				
			||||||
		return IPAddress(ip)
 | 
							return IPAddress(ip)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -73,3 +73,15 @@ func TestNetIPv4Address(t *testing.T) {
 | 
				
			||||||
	assert(addr, IsIPv4)
 | 
						assert(addr, IsIPv4)
 | 
				
			||||||
	assert(addr.String(), Equals, "1.2.3.4")
 | 
						assert(addr.String(), Equals, "1.2.3.4")
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestParseIPv6Address(t *testing.T) {
 | 
				
			||||||
 | 
						assert := With(t)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						ip := ParseAddress("[2001:4860:0:2001::68]")
 | 
				
			||||||
 | 
						assert(ip, IsIPv6)
 | 
				
			||||||
 | 
						assert(ip.String(), Equals, "[2001:4860:0:2001::68]")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						ip = ParseAddress("[::ffff:123.151.71.143]")
 | 
				
			||||||
 | 
						assert(ip, IsIPv4)
 | 
				
			||||||
 | 
						assert(ip.String(), Equals, "123.151.71.143")
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue