package libproxy

import "github.com/moby/vpnkit/go/pkg/libproxy"

Package libproxy provides a network Proxy interface and implementations for TCP and UDP.

Index

Constants

const (
	// UDPConnTrackTimeout is the timeout used for UDP connection tracking
	UDPConnTrackTimeout = 90 * time.Second
	// UDPBufSize is the buffer size for the UDP proxy
	UDPBufSize = 65507
)

Functions

func ExposePort

func ExposePort(host net.Addr, container net.Addr) (*os.File, error)

ExposePort exposes a port using 9p

func Forward

func Forward(conn Conn, destination Destination, quit chan struct{})

Forward runs the TCP/UDP forwarder over a sub-connection

func HandleTCPConnection

func HandleTCPConnection(client Conn, backendAddr *net.TCPAddr, quit chan struct{}) error

HandleTCPConnection forwards the TCP traffic to a specified backend address

func HandleUnixConnection

func HandleUnixConnection(client Conn, backendAddr *net.UnixAddr, quit chan struct{}) error

HandleUnixConnection forwards the Unix traffic to a specified backend address

Types

type CloseFrame

type CloseFrame struct {
}

CloseFrame requests to disconnect from a proxy backend

type Command

type Command int8

Command is the action requested by a message.

const (
	// Open requests to open a connection to a backend service.
	Open Command = iota + 1
	// Close requests and then acknowledges the close of a sub-connection
	Close
	// Shutdown indicates that no more data will be written in this direction
	Shutdown
	// Data is a payload of a connection/sub-connection
	Data
	// Window is permission to send and consume buffer space
	Window
)

type Conn

type Conn interface {
	net.Conn
	CloseRead() error
	CloseWrite() error
}

Conn defines a network connection

type Connection

type Connection int8

Connection indicates whether the connection will use multiplexing or not.

const (
	// Dedicated means this connection will not use multiplexing
	Dedicated Connection = iota + 1
	// Multiplexed means this connection will contain labelled sub-connections mixed together
	Multiplexed
)

func (Connection) String

func (c Connection) String() string

type DataFrame

type DataFrame struct {
	// contains filtered or unexported fields
}

DataFrame is the header of a frame containing user data

func (*DataFrame) Size

func (d *DataFrame) Size() int

Size returns the marshalled size of the data payload header

func (*DataFrame) Write

func (d *DataFrame) Write(w io.Writer) error

type Destination

type Destination struct {
	Proto Proto
	IP    net.IP
	Port  uint16
	Path  string
}

Destination refers to a listening TCP or UDP service

func (Destination) Size

func (d Destination) Size() int

Size returns the marshalled size in bytes

func (Destination) String

func (d Destination) String() string

func (Destination) Write

func (d Destination) Write(w io.Writer) error

type Frame

type Frame struct {
	Command Command // Command is the action erquested
	ID      uint32  // Id of the sub-connection, managed by the client
	// contains filtered or unexported fields
}

Frame is the low-level message sent to the multiplexer

func NewClose

func NewClose(ID uint32) *Frame

NewClose creates a close frame

func NewData

func NewData(ID, payloadlen uint32) *Frame

NewData creates a data header frame

func NewOpen

func NewOpen(ID uint32, d Destination) *Frame

NewOpen creates an open message

func NewShutdown

func NewShutdown(ID uint32) *Frame

NewShutdown creates a shutdown frame

func NewWindow

func NewWindow(ID uint32, seq uint64) *Frame

NewWindow creates a Window message

func (*Frame) Data

func (f *Frame) Data() (*DataFrame, error)

Data returns the payload of the frame, if it has Command = Data

func (*Frame) Open

func (f *Frame) Open() (*OpenFrame, error)

Open returns the payload of the frame, if it has Command = Open

func (*Frame) Payload

func (f *Frame) Payload() interface{}

Payload returns the payload of the frame.

func (*Frame) Size

func (f *Frame) Size() int

Size returns the marshalled size of the frame

func (*Frame) String

func (f *Frame) String() string

func (*Frame) Window

func (f *Frame) Window() (*WindowFrame, error)

Window returns the payload of the frame, if it has Command = Window.

func (*Frame) Write

func (f *Frame) Write(w io.Writer) error

type Multiplexer

type Multiplexer struct {
	// contains filtered or unexported fields
}

Multiplexer muxes and demuxes sub-connections over a single connection

func NewMultiplexer

func NewMultiplexer(label string, conn io.ReadWriteCloser) *Multiplexer

NewMultiplexer constructs a multiplexer from a channel

func (*Multiplexer) Accept

func (m *Multiplexer) Accept() (Conn, *Destination, error)

Accept returns the next client connection

func (*Multiplexer) Close

func (m *Multiplexer) Close() error

Close the underlying transport.

func (*Multiplexer) Dial

func (m *Multiplexer) Dial(d Destination) (Conn, error)

Dial opens a connection to the given destination

func (*Multiplexer) IsRunning

func (m *Multiplexer) IsRunning() bool

IsRunning returns whether the multiplexer is running or not

func (*Multiplexer) Run

func (m *Multiplexer) Run()

Run starts handling the requests from the other side

type OpenFrame

type OpenFrame struct {
	Connection  Connection // Connection describes whether the opened connection should be dedicated or multiplexed
	Destination Destination
}

OpenFrame requests to connect to a proxy backend

func (*OpenFrame) Size

func (o *OpenFrame) Size() int

Size returns the marshalled size of the Open message

func (*OpenFrame) Write

func (o *OpenFrame) Write(w io.Writer) error

type Proto

type Proto uint8

Proto is the protocol of the flow

const (
	// TCP flow
	TCP Proto = 1
	// UDP flow
	UDP Proto = 2
	// Unix domain socket flow
	Unix Proto = 3
)

type Proxy

type Proxy interface {
	// Run starts forwarding traffic back and forth between the front
	// and back-end addresses.
	Run()
	// Close stops forwarding traffic and close both ends of the Proxy.
	Close()
	// FrontendAddr returns the address on which the proxy is listening.
	FrontendAddr() net.Addr
	// BackendAddr returns the proxied address.
	BackendAddr() net.Addr
}

Proxy defines the behavior of a proxy. It forwards traffic back and forth between two endpoints : the frontend and the backend. It can be used to do software port-mapping between two addresses. e.g. forward all traffic between the frontend (host) 127.0.0.1:3000 to the backend (container) at 172.17.42.108:4000.

func NewBestEffortIPProxy

func NewBestEffortIPProxy(host net.Addr, container net.Addr) (Proxy, error)

NewBestEffortIPProxy Best-effort attempt to listen on the address in the VM. This is for backwards compatibility with software that expects to be able to listen on 0.0.0.0 and then connect from within a container to the external port. If the address doesn't exist in the VM (i.e. it exists only on the host) then this is not a hard failure.

func NewIPProxy

func NewIPProxy(frontendAddr, backendAddr net.Addr) (Proxy, error)

NewIPProxy creates a Proxy according to the specified frontendAddr and backendAddr.

func NewStubProxy

func NewStubProxy(frontendAddr, backendAddr net.Addr) (Proxy, error)

NewStubProxy creates a new StubProxy

type ShutdownFrame

type ShutdownFrame struct {
}

ShutdownFrame requests to close the write channel to a proxy backend

type StubProxy

type StubProxy struct {
	// contains filtered or unexported fields
}

StubProxy is a proxy that is a stub (does nothing).

func (*StubProxy) BackendAddr

func (p *StubProxy) BackendAddr() net.Addr

BackendAddr returns the backend address.

func (*StubProxy) Close

func (p *StubProxy) Close()

Close does nothing.

func (*StubProxy) FrontendAddr

func (p *StubProxy) FrontendAddr() net.Addr

FrontendAddr returns the frontend address.

func (*StubProxy) Run

func (p *StubProxy) Run()

Run does nothing.

type TCPProxy

type TCPProxy struct {
	// contains filtered or unexported fields
}

TCPProxy is a proxy for TCP connections. It implements the Proxy interface to handle TCP traffic forwarding between the frontend and backend addresses.

func NewTCPProxy

func NewTCPProxy(listener net.Listener, backendAddr *net.TCPAddr) (*TCPProxy, error)

NewTCPProxy creates a new TCPProxy.

func (*TCPProxy) BackendAddr

func (proxy *TCPProxy) BackendAddr() net.Addr

BackendAddr returns the TCP proxied address.

func (*TCPProxy) Close

func (proxy *TCPProxy) Close()

Close stops forwarding the traffic.

func (*TCPProxy) FrontendAddr

func (proxy *TCPProxy) FrontendAddr() net.Addr

FrontendAddr returns the TCP address on which the proxy is listening.

func (*TCPProxy) Run

func (proxy *TCPProxy) Run()

Run starts forwarding the traffic using TCP.

type UDPListener

type UDPListener interface {
	ReadFromUDP(b []byte) (int, *net.UDPAddr, error)
	WriteToUDP(b []byte, addr *net.UDPAddr) (int, error)
	Close() error
}

UDPListener defines a listener interface to read, write and close a UDP connection

func NewUDPConn

func NewUDPConn(conn io.ReadWriteCloser) UDPListener

NewUDPConn initializes a new UDP connection

type UDPProxy

type UDPProxy struct {
	// contains filtered or unexported fields
}

UDPProxy is proxy for which handles UDP datagrams. It implements the Proxy interface to handle UDP traffic forwarding between the frontend and backend addresses.

func NewUDPProxy

func NewUDPProxy(frontendAddr net.Addr, listener UDPListener, backendAddr *net.UDPAddr) (*UDPProxy, error)

NewUDPProxy creates a new UDPProxy.

func (*UDPProxy) BackendAddr

func (proxy *UDPProxy) BackendAddr() net.Addr

BackendAddr returns the proxied UDP address.

func (*UDPProxy) Close

func (proxy *UDPProxy) Close()

Close stops forwarding the traffic.

func (*UDPProxy) FrontendAddr

func (proxy *UDPProxy) FrontendAddr() net.Addr

FrontendAddr returns the UDP address on which the proxy is listening.

func (*UDPProxy) Run

func (proxy *UDPProxy) Run()

Run starts forwarding the traffic using UDP.

type UnixProxy

type UnixProxy struct {
	// contains filtered or unexported fields
}

UnixProxy is a proxy for Unix connections. It implements the Proxy interface to handle Unix traffic forwarding between the frontend and backend addresses.

func NewUnixProxy

func NewUnixProxy(listener net.Listener, backendAddr *net.UnixAddr) (*UnixProxy, error)

NewUnixProxy creates a new UnixProxy.

func (*UnixProxy) BackendAddr

func (proxy *UnixProxy) BackendAddr() net.Addr

BackendAddr returns the Unix proxied address.

func (*UnixProxy) Close

func (proxy *UnixProxy) Close()

Close stops forwarding the traffic.

func (*UnixProxy) FrontendAddr

func (proxy *UnixProxy) FrontendAddr() net.Addr

FrontendAddr returns the Unix address on which the proxy is listening.

func (*UnixProxy) Run

func (proxy *UnixProxy) Run()

Run starts forwarding the traffic using Unix.

type WindowFrame

type WindowFrame struct {
	// contains filtered or unexported fields
}

WindowFrame is a window advertisement message

func (*WindowFrame) Size

func (win *WindowFrame) Size() int

Size returned the marshalled size of the Window payload

func (*WindowFrame) Write

func (win *WindowFrame) Write(w io.Writer) error

Source Files

expose_port.go frame.go loopbackconn.go multiplexed.go proxy.go stream_proxy.go stub_proxy.go tcp_proxy.go udp_encapsulation.go udp_proxy.go unix_proxy.go

Version
v0.3.0
Published
Feb 7, 2019
Platform
js/wasm
Imports
14 packages
Last checked
1 day ago

Tools for package owners.