1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2026-06-04 02:09:09 -04:00

Massive refactoring for better code structure

This commit is contained in:
V2Ray
2015-09-19 23:54:36 +02:00
parent 5eee1b97aa
commit 075753c030
34 changed files with 107 additions and 115 deletions

68
common/net/address.go Normal file
View File

@@ -0,0 +1,68 @@
package net
import (
"net"
"strconv"
)
const (
AddrTypeIP = byte(0x01)
AddrTypeDomain = byte(0x03)
)
type Address struct {
Type byte
IP net.IP
Domain string
Port uint16
}
func IPAddress(ip []byte, port uint16) Address {
ipCopy := make([]byte, len(ip))
copy(ipCopy, ip)
// TODO: check IP length
return Address{
Type: AddrTypeIP,
IP: net.IP(ipCopy),
Domain: "",
Port: port,
}
}
func DomainAddress(domain string, port uint16) Address {
return Address{
Type: AddrTypeDomain,
IP: nil,
Domain: domain,
Port: port,
}
}
func (addr Address) IsIPv4() bool {
return addr.Type == AddrTypeIP && len(addr.IP) == net.IPv4len
}
func (addr Address) IsIPv6() bool {
return addr.Type == AddrTypeIP && len(addr.IP) == net.IPv6len
}
func (addr Address) IsDomain() bool {
return addr.Type == AddrTypeDomain
}
func (addr Address) String() string {
var host string
switch addr.Type {
case AddrTypeIP:
host = addr.IP.String()
if len(addr.IP) == net.IPv6len {
host = "[" + host + "]"
}
case AddrTypeDomain:
host = addr.Domain
default:
panic("Unknown Address Type " + strconv.Itoa(int(addr.Type)))
}
return host + ":" + strconv.Itoa(int(addr.Port))
}