mirror of
https://github.com/v2fly/v2ray-core.git
synced 2025-01-17 23:06:30 -05:00
refactor
This commit is contained in:
parent
f22e957a22
commit
78d682e4d1
@ -35,25 +35,29 @@ const (
|
||||
AddressFamilyDomain = AddressFamily(2)
|
||||
)
|
||||
|
||||
func (v AddressFamily) Either(fs ...AddressFamily) bool {
|
||||
// Either returns true if current AddressFamily matches any of the AddressFamilys provided.
|
||||
func (af AddressFamily) Either(fs ...AddressFamily) bool {
|
||||
for _, f := range fs {
|
||||
if v == f {
|
||||
if af == f {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (v AddressFamily) IsIPv4() bool {
|
||||
return v == AddressFamilyIPv4
|
||||
// IsIPv4 returns true if current AddressFamily is IPv4.
|
||||
func (af AddressFamily) IsIPv4() bool {
|
||||
return af == AddressFamilyIPv4
|
||||
}
|
||||
|
||||
func (v AddressFamily) IsIPv6() bool {
|
||||
return v == AddressFamilyIPv6
|
||||
// IsIPv6 returns true if current AddressFamily is IPv6.
|
||||
func (af AddressFamily) IsIPv6() bool {
|
||||
return af == AddressFamilyIPv6
|
||||
}
|
||||
|
||||
func (v AddressFamily) IsDomain() bool {
|
||||
return v == AddressFamilyDomain
|
||||
// IsDomain returns true if current AddressFamily is Domain.
|
||||
func (af AddressFamily) IsDomain() bool {
|
||||
return af == AddressFamilyDomain
|
||||
}
|
||||
|
||||
// Address represents a network address to be communicated with. It may be an IP address or domain
|
||||
@ -106,8 +110,8 @@ func DomainAddress(domain string) Address {
|
||||
|
||||
type ipv4Address [4]byte
|
||||
|
||||
func (v ipv4Address) IP() net.IP {
|
||||
return net.IP(v[:])
|
||||
func (a ipv4Address) IP() net.IP {
|
||||
return net.IP(a[:])
|
||||
}
|
||||
|
||||
func (ipv4Address) Domain() string {
|
||||
@ -118,14 +122,14 @@ func (ipv4Address) Family() AddressFamily {
|
||||
return AddressFamilyIPv4
|
||||
}
|
||||
|
||||
func (v ipv4Address) String() string {
|
||||
return v.IP().String()
|
||||
func (a ipv4Address) String() string {
|
||||
return a.IP().String()
|
||||
}
|
||||
|
||||
type ipv6Address [16]byte
|
||||
|
||||
func (v ipv6Address) IP() net.IP {
|
||||
return net.IP(v[:])
|
||||
func (a ipv6Address) IP() net.IP {
|
||||
return net.IP(a[:])
|
||||
}
|
||||
|
||||
func (ipv6Address) Domain() string {
|
||||
@ -136,8 +140,8 @@ func (ipv6Address) Family() AddressFamily {
|
||||
return AddressFamilyIPv6
|
||||
}
|
||||
|
||||
func (v ipv6Address) String() string {
|
||||
return "[" + v.IP().String() + "]"
|
||||
func (a ipv6Address) String() string {
|
||||
return "[" + a.IP().String() + "]"
|
||||
}
|
||||
|
||||
type domainAddress string
|
||||
@ -146,23 +150,24 @@ func (domainAddress) IP() net.IP {
|
||||
panic("Calling IP() on a DomainAddress.")
|
||||
}
|
||||
|
||||
func (v domainAddress) Domain() string {
|
||||
return string(v)
|
||||
func (a domainAddress) Domain() string {
|
||||
return string(a)
|
||||
}
|
||||
|
||||
func (domainAddress) Family() AddressFamily {
|
||||
return AddressFamilyDomain
|
||||
}
|
||||
|
||||
func (v domainAddress) String() string {
|
||||
return v.Domain()
|
||||
func (a domainAddress) String() string {
|
||||
return a.Domain()
|
||||
}
|
||||
|
||||
func (v *IPOrDomain) AsAddress() Address {
|
||||
if v == nil {
|
||||
// AsAddress translates IPOrDomain to Address.
|
||||
func (d *IPOrDomain) AsAddress() Address {
|
||||
if d == nil {
|
||||
return nil
|
||||
}
|
||||
switch addr := v.Address.(type) {
|
||||
switch addr := d.Address.(type) {
|
||||
case *IPOrDomain_Ip:
|
||||
return IPAddress(addr.Ip)
|
||||
case *IPOrDomain_Domain:
|
||||
@ -171,6 +176,7 @@ func (v *IPOrDomain) AsAddress() Address {
|
||||
panic("Common|Net: Invalid address.")
|
||||
}
|
||||
|
||||
// NewIPOrDomain translates Address to IPOrDomain
|
||||
func NewIPOrDomain(addr Address) *IPOrDomain {
|
||||
switch addr.Family() {
|
||||
case AddressFamilyDomain:
|
||||
|
Loading…
Reference in New Issue
Block a user