| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- package net
- import (
- "net"
- "github.com/v2ray/v2ray-core/common/log"
- )
- // Address represents a network address to be communicated with. It may be an IP address or domain
- // address, not both. This interface doesn't resolve IP address for a given domain.
- type Address interface {
- IP() net.IP // IP of this Address
- Domain() string // Domain of this Address
- IsIPv4() bool // True if this Address is an IPv4 address
- IsIPv6() bool // True if this Address is an IPv6 address
- IsDomain() bool // True if this Address is an domain 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.
- func IPAddress(ip []byte) Address {
- switch len(ip) {
- case net.IPv4len:
- return &IPv4Address{
- 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])
- }
- return &IPv6Address{
- ip: [16]byte{
- ip[0], ip[1], ip[2], ip[3],
- ip[4], ip[5], ip[6], ip[7],
- ip[8], ip[9], ip[10], ip[11],
- ip[12], ip[13], ip[14], ip[15],
- },
- }
- default:
- log.Error("Invalid IP format: %v", ip)
- return nil
- }
- }
- // DomainAddress creates an Address with given domain and port.
- func DomainAddress(domain string) Address {
- return &DomainAddressImpl{
- domain: domain,
- }
- }
- type address struct {
- }
- type IPv4Address struct {
- ip [4]byte
- }
- func (addr *IPv4Address) IP() net.IP {
- return net.IP(addr.ip[:])
- }
- func (addr *IPv4Address) Domain() string {
- panic("Calling Domain() on an IPv4Address.")
- }
- func (addr *IPv4Address) IsIPv4() bool {
- return true
- }
- func (addr *IPv4Address) IsIPv6() bool {
- return false
- }
- func (addr *IPv4Address) IsDomain() bool {
- return false
- }
- func (this *IPv4Address) String() string {
- return this.IP().String()
- }
- type IPv6Address struct {
- ip [16]byte
- }
- func (addr *IPv6Address) IP() net.IP {
- return net.IP(addr.ip[:])
- }
- func (addr *IPv6Address) Domain() string {
- panic("Calling Domain() on an IPv6Address.")
- }
- func (addr *IPv6Address) IsIPv4() bool {
- return false
- }
- func (addr *IPv6Address) IsIPv6() bool {
- return true
- }
- func (addr *IPv6Address) IsDomain() bool {
- return false
- }
- func (this *IPv6Address) String() string {
- return "[" + this.IP().String() + "]"
- }
- type DomainAddressImpl struct {
- domain string
- }
- func (addr *DomainAddressImpl) IP() net.IP {
- panic("Calling IP() on a DomainAddress.")
- }
- func (addr *DomainAddressImpl) Domain() string {
- return addr.domain
- }
- func (addr *DomainAddressImpl) IsIPv4() bool {
- return false
- }
- func (addr *DomainAddressImpl) IsIPv6() bool {
- return false
- }
- func (addr *DomainAddressImpl) IsDomain() bool {
- return true
- }
- func (this *DomainAddressImpl) String() string {
- return this.domain
- }
|