pull/27/head
V2 Ray 9 years ago
parent e4b6d5e0f0
commit 313aec4097

@ -7,15 +7,16 @@ import (
"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.
// 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
Port() uint16 // Port of this Address
IP() net.IP // IP of this Address
Domain() string // Domain of this Address
Port() uint16 // Port of this Address
PortBytes() []byte // Port in bytes, network byte order
IsIPv4() bool // True if this Address is an IPv4 address
IsIPv6() bool // True if this Address is an IPv6 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
@ -32,10 +33,12 @@ func IPAddress(ip []byte, port uint16) Address {
case net.IPv6len:
return IPv6Address{
PortAddress: PortAddress{port: port},
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]},
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:
panic(log.Error("Unknown IP format: %v", ip))

@ -1,18 +1,21 @@
package net
// Destination represents a network destination including address and protocol (tcp / udp).
type Destination interface {
Network() string
Address() Address
String() string
Network() string // Protocol of communication (tcp / udp)
Address() Address // Address of destination
String() string // String representation of the destination
IsTCP() bool
IsUDP() bool
IsTCP() bool // True if destination is reachable via TCP
IsUDP() bool // True if destination is reachable via UDP
}
// NewTCPDestination creates a TCP destination with given address
func NewTCPDestination(address Address) Destination {
return TCPDestination{address: address}
}
// NewUDPDestination creates a UDP destination with given address
func NewUDPDestination(address Address) Destination {
return UDPDestination{address: address}
}

@ -8,6 +8,7 @@ const (
bufferSize = 32 * 1024
)
// ReaderToChan dumps all content from a given reader to a chan by constantly reading it until EOF.
func ReaderToChan(stream chan<- []byte, reader io.Reader) error {
for {
buffer := make([]byte, bufferSize)
@ -21,6 +22,7 @@ func ReaderToChan(stream chan<- []byte, reader io.Reader) error {
}
}
// ChanToWriter dumps all content from a given chan to a writer until the chan is closed.
func ChanToWriter(writer io.Writer, stream <-chan []byte) error {
for buffer := range stream {
_, err := writer.Write(buffer)

Loading…
Cancel
Save