1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-27 09:55:22 +00:00
v2fly/net/vdest.go

65 lines
1.1 KiB
Go
Raw Normal View History

2015-09-10 22:24:18 +00:00
package core
import (
"net"
"strconv"
)
const (
AddrTypeIP = byte(0x01)
AddrTypeDomain = byte(0x03)
)
type VAddress struct {
Type byte
IP net.IP
Domain string
Port uint16
}
func IPAddress(ip []byte, port uint16) VAddress {
// TODO: check IP length
return VAddress{
AddrTypeIP,
net.IP(ip),
"",
port}
}
func DomainAddress(domain string, port uint16) VAddress {
return VAddress{
AddrTypeDomain,
nil,
domain,
port}
}
func (addr VAddress) IsIPv4() bool {
return addr.Type == AddrTypeIP && len(addr.IP) == net.IPv4len
}
func (addr VAddress) IsIPv6() bool {
return addr.Type == AddrTypeIP && len(addr.IP) == net.IPv6len
}
func (addr VAddress) IsDomain() bool {
return addr.Type == AddrTypeDomain
}
func (addr VAddress) String() string {
var host string
switch addr.Type {
case AddrTypeIP:
2015-09-11 12:12:09 +00:00
host = addr.IP.String()
if len(addr.IP) == net.IPv6len {
host = "[" + host + "]"
}
2015-09-10 22:24:18 +00:00
case AddrTypeDomain:
host = addr.Domain
default:
panic("Unknown Address Type " + strconv.Itoa(int(addr.Type)))
}
return host + ":" + strconv.Itoa(int(addr.Port))
}