network

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 5, 2024 License: Apache-2.0 Imports: 13 Imported by: 1

README

Network

Go Reference License Release Go Test Go Report Card

This package provides factory and wrapper of native net listener and dialer for configuration maintaining and continuous portability.

Concepts

Address

Methods:

  • Network() string get network type.
  • String() string get real address.
Server

Maintains a copy of Listener config, including desired address & port. New listener can be easily created for error retrying or dynamic loading.

Methods:

  • Type() string - Get network type
  • Addr() Addr - Get the listening(local) address
  • Config() any - Get the listener's configuration
  • Upstream() Server - Get the upstream wrapped server
  • ListenPacket(), ListenContext() - Create listener

By calling ListenPacket or ListenContext, we can get stateless or stateful listener as following.

Listener

Wrapper of net.Listener, typically for stateful sessions like tcp connection. you can directly call its Accept() for handling incoming connection.

Accept() (Conn, error)

Cancelable accept operation may be provided by some external package, like quic, which implements ListenerContext:

AcceptContext(ctx context.Context) (net.Conn, error)

if one Listener is built on top of another Listener, like tls, it may implement SubListener, which provide method to get upstream Listener:

Upstream() Listener

Underlying object and parent server is available through both interface's Underlying() and Server() method.

Underlying() any
Server() Server
PacketListener

Since stateless connection has no session concept, to maintain consistency with the stateful listener creation process, here made this interface aliasing Server. Call ListenPacket to execute real operation.

ListenPacket(ctx context.Context) (packetConn PacketConn, err error)
Client

Like the Server interface, Maintains a copy of Dialer config, but address is not included. Though Dialer can be easily created, it's recommend to limit dialer amount for multiplexed connection.

Methods:

  • Type() string - Get network type
  • Config() any - Get the dialer's configuration
  • Upstream() Client - Get the upstream wrapped dialer
  • Dialer(ctx context.Context) (Dialer, error) - Create Dialer
  • Resolve(network, address string) (net.Addr, error)- Parse target address
Dialer

Abstraction of native *net.Dialer, with parent client and underlying provided.

  • Client() Client
  • Underlying() any
  • Dial(network string, address string) (net.Conn, error)
  • DialContext(ctx context.Context, network string, address string) (net.Conn, error)
Multiplexing

For some protocol like QUIC, multiple logical connection on top of one physical connection, the Listener and Dialer can be asserted to following for enabling Multiplexing.

ListenerMultiplexed

  • Listener
  • AcceptMux() (Listener, error)
  • AcceptMuxContext(ctx context.Context) (Listener, error)

DialerMultiplexed

  • Dialer
  • DialMux(network string, address string) (DialerCloser, error)
  • DialMuxContext(ctx context.Context, network string, address string) (DialerCloser, error)

Contributing

For convenience of PM, please commit all issue to Document Repo.

License

This project is licensed under the Apache License Version 2.0.

Use and contributions signify your agreement to honor the terms of this LICENSE.

Commercial support or licensing is conditionally available through organization email.

Documentation

Index

Constants

View Source
const (
	ClientAuthOptNone uint8 = 1 << iota >> 1
	ClientAuthOptRequest
	ClientAuthOptOptional
	ClientAuthOptVerify
)
View Source
const ContextKeyTLSCfgFilter contextKey = "network.tls.cfg_filter"
View Source
const TypeTCP = "tcp"
View Source
const TypeTls = "tls"
View Source
const TypeUDP = "udp"
View Source
const TypeUnix = "unix"

Variables

View Source
var (
	ErrUnsupportedProtocol = errors.New("unsupported network")
	ErrUnsupportedUpstream = errors.New("unsupported upstream")
)

Functions

func DefaultTlsConfig

func DefaultTlsConfig() *tls.Config

func TLSConfigFilterContext

func TLSConfigFilterContext(ctx context.Context, filter TLSConfigFilter) context.Context

func TLSConfigOf

func TLSConfigOf(source any) *tls.Config

Types

type Addr

type Addr struct {
	NetworkStr string `json:"network"`
	AddressStr string `json:"address"`
}

func NewAddr

func NewAddr(network, address string) Addr

func (Addr) Network

func (a Addr) Network() string

func (Addr) String

func (a Addr) String() string

type Client

type Client interface {
	Type() string
	Config() any
	Upstream() Client
	Dialer(ctx context.Context) (Dialer, error)
	Resolve(network, address string) (net.Addr, error)
}

func NewTCPClient

func NewTCPClient(cfg TCPClientConfig) Client

func NewTLSClient

func NewTLSClient(config TlsConfig, upstream Client) Client

type Dialer

type Dialer interface {
	Client() Client
	Underlying() any
	NativeDialer
}

type DialerCloser

type DialerCloser interface {
	Dialer
	io.Closer
}

type DialerMultiplexed

type DialerMultiplexed interface {
	Dialer
	DialMux(network string, address string) (DialerCloser, error)
	DialMuxContext(ctx context.Context, network string, address string) (DialerCloser, error)
}

type Duration

type Duration int64

func (*Duration) Duration

func (d *Duration) Duration() time.Duration

func (*Duration) UnmarshalJSON

func (d *Duration) UnmarshalJSON(rawBytes []byte) (err error)

func (*Duration) UnmarshalYAML

func (d *Duration) UnmarshalYAML(unmarshal func(any) error) (err error)

type Listener

type Listener interface {
	Server() Server
	Underlying() any
	net.Listener
}

type ListenerContext

type ListenerContext interface {
	Listener
	AcceptContext(ctx context.Context) (net.Conn, error)
}

type ListenerMultiplexed

type ListenerMultiplexed interface {
	Listener
	AcceptMux() (Listener, error)
	AcceptMuxContext(ctx context.Context) (Listener, error)
}

type NativeDialer

type NativeDialer interface {
	Dial(network string, address string) (net.Conn, error)
	DialContext(ctx context.Context, network string, address string) (net.Conn, error)
}

type PacketConn

type PacketConn interface {
	Underlying() any
	net.PacketConn
}

type PacketListener

type PacketListener interface {
	ListenPacket(ctx context.Context) (packetConn PacketConn, err error)
}

type Server

type Server interface {
	Type() string
	Addr() Addr
	Config() any
	Upstream() Server
	ListenPacket(ctx context.Context) (packetConn PacketConn, err error)
	ListenContext(ctx context.Context) (listener Listener, err error)
}

func NewTCPServer

func NewTCPServer(cfg TCPServerConfig, addr string) Server

func NewTLSServer

func NewTLSServer(config TlsConfig, upstream Server) Server

func NewUDPServer

func NewUDPServer(cfg UDPServerConfig, addr string) Server

type SubDialer

type SubDialer interface {
	Dialer
	Upstream() Dialer
}

type SubListener

type SubListener interface {
	Listener
	Upstream() Listener
}

type TCPClientConfig

type TCPClientConfig struct {
	EnableNoDelay     bool     `json:"enable_no_delay" yaml:"enable_no_delay"`
	KeepAliveInterval Duration `json:"keep_alive_interval" yaml:"keep_alive_interval"`
	StackFallbackGap  Duration `json:"stack_fallback_gap" yaml:"stack_fallback_gap"`
	TimeoutDuration   Duration `json:"dial_timeout" yaml:"dial_timeout"`
	LocalNetwork      string   `json:"local_network" yaml:"local_network"`
	LocalAddress      string   `json:"local_address" yaml:"local_address"`
}

type TCPServerConfig

type TCPServerConfig struct {
	EnableNoDelay     bool     `json:"enable_no_delay" yaml:"enable_no_delay"`
	KeepAliveInterval Duration `json:"keep_alive_interval" yaml:"keep_alive_interval"`
}

type TLSConfigFilter

type TLSConfigFilter interface {
	Apply(config *tls.Config) (*tls.Config, error)
}

func NewTlsNextProtoFilter

func NewTlsNextProtoFilter(list []string) TLSConfigFilter

type TlsConfig

type TlsConfig struct {
	Disable        bool `json:"disable" yaml:"disable"`
	WithoutSysCA   bool `json:"without_sys_ca" yaml:"without_sys_ca"`
	SkipVerify     bool `json:"insecure_skip_verify" yaml:"insecure_skip_verify"`
	SkipVerifyHost bool `json:"skip_verify_host" yaml:"skip_verify_host"`
	ClientAuth     struct {
		Optional bool `json:"optional" yaml:"optional"`
		Request  bool `json:"request" yaml:"request"`
		Verify   bool `json:"verify" yaml:"verify"`
	} `json:"client_auth" yaml:"client_auth"`
	ServerName string            `json:"server_name" yaml:"server_name"`
	KeyLogPath string            `json:"key_log_path" yaml:"key_log_path"`
	RootCAs    []X509Cert        `json:"root_ca_list" yaml:"root_ca_list"`
	ClientCAs  []X509Cert        `json:"client_ca_list" yaml:"client_ca_list"`
	Certs      []X509CertKeyPair `json:"cert_list" yaml:"cert_list"`
}

func (*TlsConfig) Apply

func (tc *TlsConfig) Apply(tlsConfig *tls.Config) (err error)

type TlsDialer

type TlsDialer interface {
	SubDialer
	// contains filtered or unexported methods
}

type TlsListener

type TlsListener interface {
	SubListener
	// contains filtered or unexported methods
}

type UDPClientConfig

type UDPClientConfig struct {
	KeepAliveInterval Duration `json:"keep_alive_interval"`
	StackFallbackGap  Duration `json:"stack_fallback_gap"`
	TimeoutDuration   Duration `json:"dial_timeout"`
	LocalNetwork      string   `json:"local_network"`
	LocalAddress      string   `json:"local_address"`
}

type UDPServerConfig

type UDPServerConfig struct {
	KeepAliveInterval Duration `json:"keep_alive_interval"`
}

type UnixClientConfig

type UnixClientConfig struct {
	RemoveBeforeServe bool     `json:"remove_before_serve"`
	KeepAliveInterval Duration `json:"keep_alive_interval"`
	StackFallbackGap  Duration `json:"stack_fallback_gap"`
	TimeoutDuration   Duration `json:"dial_timeout"`
	LocalNetwork      string   `json:"local_network"`
	LocalAddress      string   `json:"local_address"`
}

type UnixServerConfig

type UnixServerConfig struct {
	RemoveBeforeServe bool     `json:"remove_before_serve"`
	KeepAliveInterval Duration `json:"keep_alive_interval"`
}

type X509Cert

type X509Cert struct {
	Cert     string `json:"cert"`
	CertPath string `json:"cert_path"`
}

func (*X509Cert) BuildX509Certificate

func (c *X509Cert) BuildX509Certificate() (certs []*x509.Certificate, err error)

type X509CertKeyPair

type X509CertKeyPair struct {
	Cert     string `json:"cert"`
	CertPath string `json:"cert_path"`
	Key      string `json:"key"`
	KeyPath  string `json:"key_path"`
}

func (*X509CertKeyPair) BuildTLSCertificate

func (c *X509CertKeyPair) BuildTLSCertificate() (cert tls.Certificate, err error)

Jump to

Keyboard shortcuts

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