muxedsocket

package module
v0.0.0-...-409b1ef Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2022 License: Apache-2.0 Imports: 8 Imported by: 0

README

muxedsocket

This package enables use of multiple multiplexer and "stream-over-datagram" implementations with drop-in replacement ability.

Supports...?

Supported implementations of multiplexer are:

  • SPDY
  • smux
  • yamux

Supported stream connection implementations are:

  • Stream-oriented protocols:
    • TCP
  • Stream-over-packets:
    • KCP
    • KCP Secure (KCP with TLS)

Complete solutions:

  • QUIC

When using anything other than TCP, you may use a custom PacketConn implementation such as ICMPChannel to transport traffic over some exotic channel of your choice.

Design Choices

There are a number of design choices that I rather explain here.

A note on contexts

I would like to use Contexts but the inconsistencies led to the decision to drop them in all signatures. As we already have Close for signaling the end of listening or connection lifetime, I think there is no real compensation for trying to implement them.

License

Apache License 2.0 - See LICENSE

Documentation

Index

Constants

This section is empty.

Variables

View Source
var SchemeNotSupported = errors.New("scheme not supported")

Functions

func DialUDP

func DialUDP(addr string) (net.PacketConn, error)

func ListenUDP

func ListenUDP(addr string) (net.PacketConn, error)

Types

type AddrMuxDialer

type AddrMuxDialer func(addr string, config *tls.Config) (MuxedSocket, error)

AddrMuxDialer is another all-in-one solution: It takes an address, returns the muxed socket connection.

func CreateDialer

func CreateDialer(scheme string) (AddrMuxDialer, error)

CreateDialer enables to create a dialer function for the provided scheme and cache it. If you dial a scheme frequently, then you probably should use it and cache its result.

func CreateDialerWithRegistry

func CreateDialerWithRegistry(creators *Creators, scheme string) (AddrMuxDialer, error)

CreateDialerWithRegistry is the same as CreateDialer but with 2 arguments: it takes your registry of creators instead of default.

type AddrMuxListener

type AddrMuxListener func(addr string, config *tls.Config) (MuxedListener, error)

AddrMuxListener is another all-in-one solution, with the difference being: it takes an address instead of PacketConn.

func CreateListener

func CreateListener(scheme string) (AddrMuxListener, error)

func CreateListenerWithRegistry

func CreateListenerWithRegistry(creators *Creators, scheme string) (AddrMuxListener, error)

type ClientMuxer

type ClientMuxer func(net.Conn, *ClientParams) (MuxedSocket, error)

ClientMuxer is used for example by smux, yamux, SPDY, etc.

type ClientParams

type ClientParams struct {
	CommonParams
	// used with "WriteTo" method. a PacketConn only has "LocalAddr" of the local socket.
	RemoteAddr net.Addr
}

func GetClientParamsFromURL

func GetClientParamsFromURL(transportKey string, addr *url.URL) (*ClientParams, error)

type ClientStreamAdapter

type ClientStreamAdapter func(conn net.PacketConn, remoteAddr string) (net.Conn, error)

ClientStreamAdapter is used by packet-to-stream connection adapters. for example by KCP, eNet, etc.

type ClientStreamDialer

type ClientStreamDialer func(addr string) (net.Conn, error)

ClientStreamDialer is used by packet-to-stream connection adapters. for example by KCP, eNet, etc.

type CommonParams

type CommonParams struct {
	KeepalivePeriod time.Duration
	MaxIdleTimeout  time.Duration
}

func GetCommonParamsFromURL

func GetCommonParamsFromURL(addr *url.URL) CommonParams

type Creators

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

func GlobalCreators

func GlobalCreators() *Creators

func NewCreators

func NewCreators() *Creators

func (*Creators) AddrMuxDialers

func (c *Creators) AddrMuxDialers() *Registry[AddrMuxDialer]

func (*Creators) AddrMuxListeners

func (c *Creators) AddrMuxListeners() *Registry[AddrMuxListener]

func (*Creators) ChannelDialers

func (c *Creators) ChannelDialers() *Registry[PacketChannelDialer]

func (*Creators) ChannelListeners

func (c *Creators) ChannelListeners() *Registry[PacketChannelListener]

func (*Creators) ClientMuxers

func (c *Creators) ClientMuxers() *Registry[ClientMuxer]

func (*Creators) ClientStreamAdapters

func (c *Creators) ClientStreamAdapters() *Registry[ClientStreamAdapter]

func (*Creators) MuxDialers

func (c *Creators) MuxDialers() *Registry[MuxDialer]

func (*Creators) MuxListeners

func (c *Creators) MuxListeners() *Registry[MuxListener]

func (*Creators) ServerMuxers

func (c *Creators) ServerMuxers() *Registry[ServerMuxer]

func (*Creators) ServerStreamAdapters

func (c *Creators) ServerStreamAdapters() *Registry[ServerStreamAdapter]

type DatagramCapableMuxedSocket

type DatagramCapableMuxedSocket interface {
	MuxedSocket
	DatagramChannel
}

type DatagramChannel

type DatagramChannel interface {
	SendDatagram(b []byte) error
	ReceiveDatagram() ([]byte, error)
}

type ListeningSocket

type ListeningSocket interface {
	// Accept waits for and returns the next connection to the listener.
	Accept() (socket Socket, err error)

	// Close closes the listener.
	// Any blocked Accept operations will be unblocked and return errors.
	Close() error

	// Addr returns the listener's network address.
	Addr() net.Addr
}

type MuxDialer

type MuxDialer func(conn net.PacketConn, config *tls.Config, params *ClientParams) (MuxedSocket, error)

MuxDialer is the all-in-one solution: It takes a packet connection, returns a multiplexed secure streaming socket. Its sole purpose is for QUIC. (the only all-in-one solution we have at the moment!)

type MuxListener

type MuxListener func(conn net.PacketConn, config *tls.Config, params *ServerParams) (MuxedListener, error)

MuxListener is the all-in-one solution: It takes a packet connection, returns a multiplexed secure streaming socket. Its sole purpose is for QUIC. (the only all-in-one solution we have at the moment!)

type MuxStream

type MuxStream interface {
	Socket
	net.Conn
	StreamID() int
}

type MuxedAddr

type MuxedAddr struct {
	Original  net.Addr
	MuxStream int
}

func WrapAddr

func WrapAddr(addr net.Addr, id int) *MuxedAddr

func (MuxedAddr) Network

func (addr MuxedAddr) Network() string

Network returns network name, such as "icmp", "tcp", "udp".

func (MuxedAddr) String

func (addr MuxedAddr) String() string

String returns a serialized string representation of struct.

type MuxedListener

type MuxedListener interface {
	ListeningSocket
}

func ListenURI

func ListenURI(uri string, config *tls.Config) (MuxedListener, error)

func ListenURIWithRegistry

func ListenURIWithRegistry(creators *Creators, uri string, config *tls.Config) (MuxedListener, error)

type MuxedSocket

type MuxedSocket interface {
	Socket
	AcceptStream() (stream MuxStream, err error)
	OpenStream() (stream MuxStream, err error)
}

func DialURI

func DialURI(uri string, config *tls.Config) (MuxedSocket, error)

DialURI provides a standard way of dialing an address.

func DialURIWithRegistry

func DialURIWithRegistry(creators *Creators, uri string, config *tls.Config) (MuxedSocket, error)

DialURIWithRegistry is the same as DialURI except it supports providing custom registry of creators.

type NetAddrPort

type NetAddrPort interface {
	net.Addr
	AddrPort() netip.AddrPort
}

func GetAddrByTransportType

func GetAddrByTransportType(transportKey string, addr string) (NetAddrPort, error)

type PacketChannelDialer

type PacketChannelDialer func(addr string) (net.PacketConn, error)

PacketChannelDialer is used for example by UDP, ICMPChannel, etc.

type PacketChannelListener

type PacketChannelListener func(addr string) (net.PacketConn, error)

PacketChannelListener is used for example by UDP, ICMPChannel, etc.

type Registry

type Registry[T any] struct {
	// contains filtered or unexported fields
}

func (*Registry[T]) Get

func (r *Registry[T]) Get(name string) (T, bool)

func (*Registry[T]) Keys

func (r *Registry[T]) Keys() []string

func (*Registry[T]) Register

func (r *Registry[T]) Register(name string, value T)

type ServerMuxer

type ServerMuxer func(net.Listener, *ServerParams) (MuxedListener, error)

ServerMuxer is used for example by smux, yamux, SPDY, etc.

type ServerParams

type ServerParams struct {
	CommonParams
}

func GetServerParamsFromURL

func GetServerParamsFromURL(_ string, addr *url.URL) (*ServerParams, error)

type ServerStreamAdapter

type ServerStreamAdapter func(conn net.PacketConn) (net.Listener, error)

ServerStreamAdapter is used by packet-to-stream connection adapters. for example by KCP, eNet, etc.

type Socket

type Socket interface {
	// Close closes the connection.
	// Any blocked operations on the socket will be unblocked and return errors.
	Close() error

	// LocalAddr returns the local network address, if known.
	LocalAddr() net.Addr

	// RemoteAddr returns the remote network address, if known.
	RemoteAddr() net.Addr
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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