netwrap

package module
v0.1.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 8, 2021 License: MIT Imports: 3 Imported by: 15

Documentation

Overview

Package netwrap allows for stacking multiple layers of address onto each other and wrapping connections accordingly to these. Imagine multiple hops of a proxied connection or adding a public key to an expected endpoint.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Dial

func Dial(addr net.Addr, wrappers ...ConnWrapper) (net.Conn, error)

Dial first opens a network connection to the supplied addr, and then applies all the passed connection wrappers.

func GetAddr

func GetAddr(a net.Addr, netw string) net.Addr

GetAddr returns, if available, the concrete address addr with netw == addr.Network() from inside the stack. If no such address exists, it returns nil.

Example
tcpAddr := &net.TCPAddr{
	IP:   net.IPv4(127, 0, 0, 1),
	Port: 8008,
}

ta := testAddr{
	net: "foo", str: "some-info",
}

wrappedAddr := WrapAddr(tcpAddr, ta)
fmt.Println("wrapped:", wrappedAddr.String())

unwrappedFoo := GetAddr(wrappedAddr, "foo")
fmt.Println("foo:", unwrappedFoo.String())
unwrappedTCP := GetAddr(wrappedAddr, "tcp")
fmt.Println("tcp:", unwrappedTCP.String())
Output:

wrapped: 127.0.0.1:8008|some-info
foo: some-info
tcp: 127.0.0.1:8008

func Listen

func Listen(addr net.Addr, wrappers ...ListenerWrapper) (net.Listener, error)

Listen first listens on the supplied address and then wraps that listener with all the supplied wrappers.

func WrapAddr

func WrapAddr(inner, head net.Addr) net.Addr

WrapAddr wraps the inner address with head. If inner is nil, WrapAddr returns nil.

Types

type Addr

type Addr interface {
	net.Addr

	// Head returns the address of the highest element of the protocol stack.
	// This should not return a stack address
	Head() net.Addr

	// Inner returns everything below the highest element. Might be stack address itself.
	Inner() net.Addr
}

Addr is a stack address, representing the protocol stack.

type ConnWrapper

type ConnWrapper func(net.Conn) (net.Conn, error)

ConnWrapper wraps a network connection, e.g. to encrypt the transmitted content.

type Dialer

type Dialer func(net.Addr, ...ConnWrapper) (net.Conn, error)

Dialer is Dial() but as a function type for alternative dialers (like using a socks proxy)

type ListenerWrapper

type ListenerWrapper func(net.Listener) (net.Listener, error)

ListenerWrapper wraps a network listener.

func NewListenerWrapper

func NewListenerWrapper(addr net.Addr, cws ...ConnWrapper) ListenerWrapper

NewListenerWrapper creates a new ListenerWrapper by wrapping all accepted connections with the supplied connection wrapper and wrapping the listener's address with the supplied address.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL