fnet

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Jun 9, 2023 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultGRPCPort is the Fortio gRPC server default port number.
	DefaultGRPCPort = "8079"
	// StandardHTTPPort is the Standard http port number.
	StandardHTTPPort = "80"
	// StandardHTTPSPort is the Standard https port number.
	StandardHTTPSPort = "443"
	// PrefixHTTP is a constant value for representing http protocol that can be added prefix of url.
	PrefixHTTP = "http://"
	// PrefixHTTPS is a constant value for representing secure http protocol that can be added prefix of url.
	PrefixHTTPS = "https://"

	// POST is a constant value that indicates http method as post.
	POST = "POST"
	// GET is a constant value that indicates http method as get.
	GET = "GET"
	// UnixDomainSocket type for network addresses.
	UnixDomainSocket = "unix"
)
View Source
const UDPPrefix = "udp://"

UDPPrefix is the prefix that given to NetCat switches to UDP from TCP(/unix domain) socket type.

Variables

View Source
var (
	// KILOBYTE is a constant for kilobyte (ie 1024).
	KILOBYTE = 1024
	// MaxPayloadSize is the maximum size of payload to be generated by the
	// EchoHandler size= argument. In bytes.
	MaxPayloadSize = 256 * KILOBYTE
	// Payload that is returned during echo call.
	Payload []byte
	// FlagResolveIPType indicates which IP types to resolve.
	// With round robin resolution now the default, you are likely to get ipv6 which may not work if
	// use both type (`ip`). In particular some test environments like the CI do have ipv6
	// for localhost but fail to connect. So we made the default ip4 only.
	// See bincommon/commonflags.go for how an actual dflag is plugged here.
	FlagResolveIPType = dflag.New("ip4",
		"Resolve `type`: ip4 for ipv4, ip6 for ipv6 only, use ip for both")
	// FlagResolveMethod decides which method to use when multiple ips are returned for a given name
	// default assumes one gets all the ips in the first call and does round robin across these.
	// first just picks the first answer, rr rounds robin on each answer.
	FlagResolveMethod = dflag.New("cached-rr",
		"When a name resolves to multiple ip, which `method` to pick: cached-rr for cached round robin, rnd for random, "+
			"first for first answer (pre 1.30 behavior), rr for round robin.").WithValidator(dnsMethodValidator)
)
View Source
var ErrNilDestination = fmt.Errorf("nil destination")

ErrNilDestination returned when trying to proxy to a nil address.

Functions

func ChangeMaxPayloadSize

func ChangeMaxPayloadSize(newMaxPayloadSize int)

ChangeMaxPayloadSize is used to change max payload size and fill it with pseudorandom content.

func ClearResolveCache

func ClearResolveCache()

ClearResolveCache clears the DNS cache for cached-rr resolution mode. For instance in case of error, to force re-resolving to potentially changed IPs.

func Copy

func Copy(dst io.Writer, src io.Reader) (written int64, err error)

Copy is a debug version of io.Copy without the zero Copy optimizations.

func DebugSummary

func DebugSummary(buf []byte, max int) string

DebugSummary returns a string with the size and escaped first max/2 and last max/2 bytes of a buffer (or the whole escaped buffer if small enough).

func GeneratePayload

func GeneratePayload(payloadFilePath string, payloadSize int, payload string) []byte

GeneratePayload generates a payload with given inputs. First tries filePath, then random payload, at last payload. Returns nil if file read error, an empty but non nil slice if no payload is requested.

func GenerateRandomPayload

func GenerateRandomPayload(payloadSize int) []byte

GenerateRandomPayload generates a random payload with given input size.

func GetPort

func GetPort(lAddr net.Addr) string

GetPort extracts the port for TCP sockets and the path for unix domain sockets.

func GetUniqueUnixDomainPath

func GetUniqueUnixDomainPath(prefix string) string

GetUniqueUnixDomainPath returns a path to be used for unix domain socket.

func Listen

func Listen(name string, port string) (net.Listener, net.Addr)

Listen returns a listener for the port. Port can be a port or a bind address and a port (e.g. "8080" or "[::1]:8080"...). If the port component is 0 a free port will be returned by the system. If the port is a pathname (contains a /) a unix domain socket listener will be used instead of regular tcp socket. This logs critical on error and returns nil (is meant for servers that must start).

func NetCat

func NetCat(ctx context.Context, dest string, in io.Reader, out io.Writer, stopOnEOF bool) error

NetCat connects to the destination and reads from in, sends to the socket, and write what it reads from the socket to out. if the destination starts with udp:// UDP is used otherwise TCP.

func NormalizeHostPort

func NormalizeHostPort(inputPort string, addr net.Addr) string

NormalizeHostPort generates host:port string for the address or uses localhost instead of [::] when the original port binding input didn't specify an address.

func NormalizePort

func NormalizePort(port string) string

NormalizePort parses port and returns host:port if port is in the form of host:port already or :port if port is only a port (doesn't contain :).

func Proxy

func Proxy(port string, dest net.Addr) net.Addr

Proxy starts a tcp proxy.

func ProxyToDestination

func ProxyToDestination(ctx context.Context, listenPort string, destination string) net.Addr

ProxyToDestination opens a proxy from the listenPort (or addr:port or unix domain socket path) and forwards all traffic to destination (host:port).

func ReadFileForPayload

func ReadFileForPayload(payloadFilePath string) ([]byte, error)

ReadFileForPayload reads the file from given input path. if filename is `-` then it reads from stdin.

func Resolve

func Resolve(ctx context.Context, host string, port string) (*net.TCPAddr, error)

Resolve backward compatible TCP only version of ResolveByProto.

func ResolveAll

func ResolveAll(ctx context.Context, host, resolveType string) ([]net.IP, error)

ResolveAll returns all the IPs for the host. resolveType is a filter for the IPs to use, `ip4` for ipv4, `ip6` for ipv6, or `ip` or "" for both.

func ResolveDestination

func ResolveDestination(ctx context.Context, dest string) (*net.TCPAddr, error)

ResolveDestination returns the TCP address of the "host:port" suitable for net.Dial. nil in case of errors. Backward compatible name (1.12 and prior) for TCPResolveDestination.

func SetSocketBuffers

func SetSocketBuffers(socket net.Conn, readBufferSize, writeBufferSize int)

SetSocketBuffers sets the read and write buffer size of the socket. Also sets tcp SetNoDelay().

func SmallReadUntil

func SmallReadUntil(r io.Reader, stopByte byte, max int) ([]byte, bool, error)

SmallReadUntil will read one byte at a time until stopByte is found and up to max bytes total. Returns what was read (without the stop byte when found), whether the stop byte was found, whether an error occurred (eof...). Because we read one by one directly (no buffer) this should only be used for short variable length preamble type read.

func TCPEchoServer

func TCPEchoServer(name string, port string) net.Addr

TCPEchoServer starts a TCP Echo Server on given port, name is for logging.

func TCPResolveDestination

func TCPResolveDestination(ctx context.Context, dest string) (*net.TCPAddr, error)

TCPResolveDestination returns the TCP address of the "host:port" suitable for net.Dial. nil in case of errors.

func UDPEchoServer

func UDPEchoServer(name string, port string, async bool) net.Addr

UDPEchoServer starts a UDP Echo Server on given port, name is for logging. if async flag is true will spawn go routines to reply otherwise single go routine.

func UDPListen

func UDPListen(name string, port string) (*net.UDPConn, net.Addr)

UDPListen starts server on given port. (0 for dynamic port).

func UDPNetCat

func UDPNetCat(ctx context.Context, dest string, in io.Reader, out io.Writer, stopOnEOF bool) error

UDPNetCat handles UDP part of NetCat.

func UDPResolveDestination

func UDPResolveDestination(ctx context.Context, dest string) (*net.UDPAddr, error)

UDPResolveDestination returns the UDP address of the "host:port" suitable for net.Dial. nil and the error in case of errors.

func ValidatePayloadSize

func ValidatePayloadSize(size *int)

ValidatePayloadSize compares input size with MaxPayLoadSize. If size exceeds the MaxPayloadSize size will set to MaxPayLoadSize.

Types

type HostPortAddr

type HostPortAddr struct {
	IP   net.IP
	Port int
}

HostPortAddr is the missing base. IPAddr and UDPAddr are actually the same but don't share a base (!)

func ResolveByProto

func ResolveByProto(ctx context.Context, host string, port string, proto string) (*HostPortAddr, error)

ResolveByProto returns the address of the host,port suitable for net.Dial. nil in case of errors. works for both "tcp" and "udp" proto. Limit which address type is returned using `resolve-ip` ip4/ip6/ip (for both, default). If the same host is requested, and it has more than 1 IP, returned value will first, random or roundrobin or cached roundrobin over the ips depending on the -dns-method flag value.

func ResolveDestinationInternal

func ResolveDestinationInternal(ctx context.Context, dest string, expected string, unexpected string) (*HostPortAddr, error)

ResolveDestinationInternal returns the address of the "host:port" suitable for net.Dial. nil in case of errors. Works for both TCP and UDP but proto must be passed as expected == tcp:// or udp:// and the other as unexpected.

func (*HostPortAddr) String

func (hpa *HostPortAddr) String() string

Jump to

Keyboard shortcuts

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