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