net

package
v0.28.15 Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2024 License: Apache-2.0 Imports: 26 Imported by: 4,360

Documentation

Index

Constants

View Source
const (
	// LoopbackInterfaceName is the default name of the loopback interface
	LoopbackInterfaceName = "lo"
)

Variables

This section is empty.

Functions

func AppendForwardedForHeader

func AppendForwardedForHeader(req *http.Request)

Prepares the X-Forwarded-For header for another forwarding hop by appending the previous sender's IP address to the X-Forwarded-For chain.

func ChooseBindAddressForInterface added in v0.17.0

func ChooseBindAddressForInterface(intfName string) (net.IP, error)

ChooseBindAddressForInterface choose a global IP for a specific interface, with priority given to IPv4. This is required in case of network setups where default routes are present, but network interfaces use only link-local addresses (e.g. as described in RFC5549). e.g when using BGP to announce a host IP over link-local ip addresses and this ip address is attached to the lo interface.

func ChooseHostInterface

func ChooseHostInterface() (net.IP, error)

ChooseHostInterface is a method used fetch an IP for a daemon. If there is no routing info file, it will choose a global IP from the system interfaces. Otherwise, it will use IPv4 and IPv6 route information to return the IP of the interface with a gateway on it (with priority given to IPv4). For a node with no internet connection, it returns error.

func CloneHeader

func CloneHeader(in http.Header) http.Header

CloneHeader creates a deep copy of an http.Header.

func CloneRequest

func CloneRequest(req *http.Request) *http.Request

CloneRequest creates a shallow copy of the request along with a deep copy of the Headers.

func CloseIdleConnectionsFor added in v0.23.0

func CloseIdleConnectionsFor(transport http.RoundTripper)

CloseIdleConnectionsFor close idles connections for the Transport. If the Transport is wrapped it iterates over the wrapped round trippers until it finds one that implements the CloseIdleConnections method. If the Transport does not have a CloseIdleConnections method then this function does nothing.

func FormatURL

func FormatURL(scheme string, host string, port int, path string) *url.URL

func GetClientIP

func GetClientIP(req *http.Request) net.IP

Extracts and returns the clients IP from the given request. Looks at X-Forwarded-For header, X-Real-Ip header and request.RemoteAddr in that order. Returns nil if none of them are set or is set to an invalid value.

func GetHTTPClient

func GetHTTPClient(req *http.Request) string

func IPNetEqual

func IPNetEqual(ipnet1, ipnet2 *net.IPNet) bool

IPNetEqual checks if the two input IPNets are representing the same subnet. For example,

10.0.0.1/24 and 10.0.0.0/24 are the same subnet.
10.0.0.1/24 and 10.0.0.0/25 are not the same subnet.

func IsConnectionRefused added in v0.16.4

func IsConnectionRefused(err error) bool

Returns if the given err is "connection refused" error

func IsConnectionReset

func IsConnectionReset(err error) bool

Returns if the given err is "connection reset by peer" error.

func IsHTTP2ConnectionLost added in v0.28.0

func IsHTTP2ConnectionLost(err error) bool

Returns if the given err is "http2: client connection lost" error.

func IsNoRoutesError

func IsNoRoutesError(err error) bool

IsNoRoutesError checks if an error is of type noRoutesError

func IsProbableEOF

func IsProbableEOF(err error) bool

IsProbableEOF returns true if the given error resembles a connection termination scenario that would justify assuming that the watch is empty. These errors are what the Go http stack returns back to us which are general connection closure errors (strongly correlated) and callers that need to differentiate probable errors in connection behavior between normal "this is disconnected" should use the method.

func IsTimeout added in v0.15.12

func IsTimeout(err error) bool

IsTimeout returns true if the given error is a network timeout error

func JoinPreservingTrailingSlash

func JoinPreservingTrailingSlash(elem ...string) string

JoinPreservingTrailingSlash does a path.Join of the specified elements, preserving any trailing slash on the last non-empty segment

func JoinSchemeNamePort

func JoinSchemeNamePort(scheme, name, port string) string

JoinSchemeNamePort returns a string that specifies the scheme, name, and port:

  • "<name>"
  • "<name>:<port>"
  • "<scheme>:<name>:<port>"

None of the parameters may contain a ':' character Name is required Scheme must be "", "http", or "https"

func NewProxierWithNoProxyCIDR

func NewProxierWithNoProxyCIDR(delegate func(req *http.Request) (*url.URL, error)) func(req *http.Request) (*url.URL, error)

NewProxierWithNoProxyCIDR constructs a Proxier function that respects CIDRs in NO_PROXY and delegates if no matching CIDRs are found

func NewWarningHeader added in v0.19.0

func NewWarningHeader(code int, agent, text string) (string, error)

func ResolveBindAddress added in v0.17.0

func ResolveBindAddress(bindAddress net.IP) (net.IP, error)

ResolveBindAddress returns the IP address of a daemon, based on the given bindAddress: If bindAddress is unset, it returns the host's default IP, as with ChooseHostInterface(). If bindAddress is unspecified or loopback, it returns the default IP of the same address family as bindAddress. Otherwise, it just returns bindAddress.

func SetOldTransportDefaults

func SetOldTransportDefaults(t *http.Transport) *http.Transport

SetOldTransportDefaults applies the defaults from http.DefaultTransport for the Proxy, Dial, and TLSHandshakeTimeout fields if unset

func SetTransportDefaults

func SetTransportDefaults(t *http.Transport) *http.Transport

SetTransportDefaults applies the defaults from http.DefaultTransport for the Proxy, Dial, and TLSHandshakeTimeout fields if unset

func SourceIPs

func SourceIPs(req *http.Request) []net.IP

SourceIPs splits the comma separated X-Forwarded-For header and joins it with the X-Real-Ip header and/or req.RemoteAddr, ignoring invalid IPs. The X-Real-Ip is omitted if it's already present in the X-Forwarded-For chain. The req.RemoteAddr is always the last IP in the returned list. It returns nil if all of these are empty or invalid.

func SplitSchemeNamePort

func SplitSchemeNamePort(id string) (scheme, name, port string, valid bool)

SplitSchemeNamePort takes a string of the following forms:

  • "<name>", returns "", "<name>","", true
  • "<name>:<port>", returns "", "<name>","<port>",true
  • "<scheme>:<name>:<port>", returns "<scheme>","<name>","<port>",true

Name must be non-empty or valid will be returned false. Scheme must be "http" or "https" if specified Port is returned as a string, and it is not required to be numeric (could be used for a named port, for example).

func TLSClientConfig

func TLSClientConfig(transport http.RoundTripper) (*tls.Config, error)

Types

type AddressFamily

type AddressFamily uint

type AddressFamilyPreference added in v0.17.0

type AddressFamilyPreference []AddressFamily

type DialFunc

type DialFunc func(ctx context.Context, net, addr string) (net.Conn, error)

func DialerFor

func DialerFor(transport http.RoundTripper) (DialFunc, error)

type Dialer

type Dialer interface {
	// Dial connects to the host specified by req's URL, writes the request to the connection, and
	// returns the opened net.Conn.
	Dial(req *http.Request) (net.Conn, error)
}

Dialer dials a host and writes a request to it.

type DialerFunc

type DialerFunc func(req *http.Request) (net.Conn, error)

DialerFunc implements Dialer for the provided function.

func (DialerFunc) Dial

func (fn DialerFunc) Dial(req *http.Request) (net.Conn, error)

type PortRange

type PortRange struct {
	Base int
	Size int
}

PortRange represents a range of TCP/UDP ports. To represent a single port, set Size to 1.

func ParsePortRange

func ParsePortRange(value string) (*PortRange, error)

ParsePortRange parses a string of the form "min-max", inclusive at both ends, and initializes a new PortRange from it.

func ParsePortRangeOrDie

func ParsePortRangeOrDie(value string) *PortRange

func (*PortRange) Contains

func (pr *PortRange) Contains(p int) bool

Contains tests whether a given port falls within the PortRange.

func (*PortRange) Set

func (pr *PortRange) Set(value string) error

Set parses a string of the form "value", "min-max", or "min+offset", inclusive at both ends, and sets the PortRange from it. This is part of the flag.Value and pflag.Value interfaces.

func (PortRange) String

func (pr PortRange) String() string

String converts the PortRange to a string representation, which can be parsed by PortRange.Set or ParsePortRange.

func (*PortRange) Type

func (*PortRange) Type() string

Type returns a descriptive string about this type. This is part of the pflag.Value interface.

type RoundTripperWrapper

type RoundTripperWrapper interface {
	http.RoundTripper
	WrappedRoundTripper() http.RoundTripper
}

type Route

type Route struct {
	Interface   string
	Destination net.IP
	Gateway     net.IP
	Family      AddressFamily
}

type RouteFile

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

type TLSClientConfigHolder

type TLSClientConfigHolder interface {
	TLSClientConfig() *tls.Config
}

type WarningHeader added in v0.19.0

type WarningHeader struct {
	// Codeindicates the type of warning. 299 is a miscellaneous persistent warning
	Code int
	// Agent contains the name or pseudonym of the server adding the Warning header.
	// A single "-" is recommended when agent is unknown.
	Agent string
	// Warning text
	Text string
}

WarningHeader contains a single RFC2616 14.46 warnings header

func ParseWarningHeader added in v0.19.0

func ParseWarningHeader(header string) (result WarningHeader, remainder string, err error)

ParseWarningHeader extracts one RFC2616 14.46 warning from the specified header, returning an error if the header does not contain a correctly formatted warning. Any remaining content in the header is returned.

func ParseWarningHeaders added in v0.19.0

func ParseWarningHeaders(headers []string) ([]WarningHeader, []error)

ParseWarningHeaders extract RFC2616 14.46 warnings headers from the specified set of header values. Multiple comma-separated warnings per header are supported. If errors are encountered on a header, the remainder of that header are skipped and subsequent headers are parsed. Returns successfully parsed warnings and any errors encountered.

Directories

Path Synopsis
Package nettesting contains utilities for testing networking functionality.
Package nettesting contains utilities for testing networking functionality.

Jump to

Keyboard shortcuts

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