Documentation ¶
Index ¶
- Constants
- Variables
- func IsPrivateAddress(ip net.IP) bool
- func ListenUDP(network string, laddr string, fwmark int) (conn *net.UDPConn, err error, serr error)
- func NewAddr(address, network string) net.Addr
- func ParseFlagsForError(flags int) error
- func Relay(leftConn, rightConn DuplexConn) (int64, int64, error)
- func UpdateOobCache(oobCache, oob []byte, logger *zap.Logger) ([]byte, error)
- type ConnectionError
- type DuplexConn
- type TargetIPValidator
- type UDPPacketConn
Constants ¶
const UDPOOBBufferSize = 128
UDPOOBBufferSize specifies the size of buffer to allocate for receiving OOB data when calling the ReadMsgUDP method on a *net.UDPConn returned by this package's ListenUDP function.
Variables ¶
var ( ErrMessageTruncated = errors.New("the packet is larger than the supplied buffer") ErrControlMessageTruncated = errors.New("the control message is larger than the supplied buffer") )
Functions ¶
func IsPrivateAddress ¶
IsPrivateAddress returns whether an IP address belongs to the LAN.
func ListenUDP ¶ added in v0.2.0
ListenUDP wraps Go's net.ListenConfig.ListenPacket and sets socket options on supported platforms.
On Linux and Windows, IP_PKTINFO and IPV6_RECVPKTINFO are set to 1; IP_MTU_DISCOVER, IPV6_MTU_DISCOVER are set to IP_PMTUDISC_DO to disable IP fragmentation to encourage correct MTU settings.
On Linux, SO_MARK is set to user-specified value.
On macOS and FreeBSD, IP_DONTFRAG, IPV6_DONTFRAG are set to 1 (Don't Fragment).
func NewAddr ¶
NewAddr returns a net.Addr that holds an address of the form `host:port` with a domain name or IP as host. Used for SOCKS addressing.
func ParseFlagsForError ¶ added in v0.3.5
ParseFlagsForError parses the message flags returned by the ReadMsgUDPAddrPort method and returns an error if MSG_TRUNC is set, indicating that the returned packet was truncated.
The check is skipped on Windows, because an error (WSAEMSGSIZE) is also returned when MSG_PARTIAL is set.
func Relay ¶
func Relay(leftConn, rightConn DuplexConn) (int64, int64, error)
Relay copies between left and right bidirectionally. Returns number of bytes copied from right to left, from left to right, and any error occurred. Relay allows for half-closed connections: if one side is done writing, it can still read all remaining data from its peer.
func UpdateOobCache ¶ added in v0.3.5
UpdateOobCache filters out irrelevant OOB messages, saves IP_PKTINFO or IPV6_PKTINFO socket control messages to the OOB cache, and returns the updated OOB cache slice.
IP_PKTINFO and IPV6_PKTINFO socket control messages are only supported on Linux and Windows.
The returned OOB cache is unchanged if no relevant control messages are found.
Errors returned by this function can be safely ignored, or printed as debug logs.
Types ¶
type ConnectionError ¶
type ConnectionError struct { // TODO: create status enums and move to metrics.go Status string Message string Cause error }
func NewConnectionError ¶
func NewConnectionError(status, message string, cause error) *ConnectionError
func RequirePublicIP ¶
func RequirePublicIP(ip net.IP) *ConnectionError
RequirePublicIP returns an error if the destination IP is not a standard public IP.
type DuplexConn ¶
type DuplexConn interface { net.Conn // Closes the Read end of the connection, allowing for the release of resources. // No more reads should happen. CloseRead() error // Closes the Write end of the connection. An EOF or FIN signal may be // sent to the connection target. CloseWrite() error }
DuplexConn is a net.Conn that allows for closing only the reader or writer end of it, supporting half-open state.
func WrapDuplexConn ¶
func WrapDuplexConn(c DuplexConn, r io.Reader, w io.Writer) DuplexConn
WrapDuplexConn wraps an existing DuplexConn with new Reader and Writer, but preserving the original CloseRead() and CloseWrite().
type TargetIPValidator ¶
type TargetIPValidator = func(net.IP) *ConnectionError
TargetIPValidator is a type alias for checking if an IP is allowed.
type UDPPacketConn ¶
type UDPPacketConn interface { net.PacketConn ReadFromUDP(b []byte) (n int, addr *net.UDPAddr, err error) WriteToUDP(b []byte, addr *net.UDPAddr) (int, error) ReadMsgUDP(b, oob []byte) (n, oobn, flags int, addr *net.UDPAddr, err error) WriteMsgUDP(b, oob []byte, addr *net.UDPAddr) (n, oobn int, err error) }