networking

package
v0.7.1 Latest Latest
Warning

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

Go to latest
Published: Jan 15, 2026 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Overview

Package networking provides utilities for network operations, such as finding available ports and checking network connectivity.

Index

Constants

View Source
const (
	// MinPort is the minimum port number to use
	MinPort = 10000
	// MaxPort is the maximum port number to use
	MaxPort = 65535
	// MaxAttempts is the maximum number of attempts to find an available port
	MaxAttempts = 10
)
View Source
const (
	// ErrPrivateIpAddress is the error returned when the provided URL redirects to a private IP address
	ErrPrivateIpAddress = "the provided registry URL redirects to a private IP address, which is not allowed; " +
		"to override this, reset the registry URL using the --allow-private-ip (-p) flag"
)
View Source
const HttpScheme = "http"

HttpScheme is the HTTP scheme

View Source
const HttpTimeout = 30 * time.Second

HttpTimeout is the timeout for outgoing HTTP requests

View Source
const HttpsScheme = "https"

HttpsScheme is the HTTPS scheme

Variables

This section is empty.

Functions

func AddressReferencesPrivateIp added in v0.1.1

func AddressReferencesPrivateIp(address string) error

AddressReferencesPrivateIp returns an error if the address references a private IP address

func FindAvailable

func FindAvailable() int

FindAvailable finds an available port

func FindOrUsePort

func FindOrUsePort(port int) (int, error)

FindOrUsePort checks if the provided port is available or finds an available port if none is provided. If port is 0, it will find an available port. If port is not 0, it will check if the port is available. Returns the selected port and an error if any.

func IsAvailable

func IsAvailable(port int) bool

IsAvailable checks if a port is available

func IsHTTPError added in v0.7.0

func IsHTTPError(err error, statusCode int) bool

IsHTTPError checks if an error is an HTTPError with the specified status code. If statusCode is 0, it matches any HTTPError.

func IsIPv6Available

func IsIPv6Available() bool

IsIPv6Available checks if IPv6 is available on the system by looking for IPv6 addresses on network interfaces

func IsLocalhost added in v0.2.4

func IsLocalhost(host string) bool

IsLocalhost checks if a host is localhost (for development)

func IsPreRegisteredClient added in v0.3.9

func IsPreRegisteredClient(clientID string) bool

IsPreRegisteredClient determines if the OAuth client is pre-registered (has client ID)

func IsURL added in v0.2.10

func IsURL(input string) bool

IsURL checks if the input is a valid HTTP or HTTPS URL

func NewHTTPError added in v0.7.0

func NewHTTPError(statusCode int, url, message string) error

NewHTTPError creates a new HTTP error.

func ValidateCallbackPort added in v0.3.9

func ValidateCallbackPort(callbackPort int, clientID string) error

ValidateCallbackPort validates that the specified callback port is valid and available. It checks that the port is within the valid range (1-65535) and, for pre-registered clients (with clientID), it returns an error if the port is not available.

func ValidateEndpointURL added in v0.2.4

func ValidateEndpointURL(endpoint string) error

ValidateEndpointURL validates that an endpoint URL is secure

func ValidateEndpointURLWithInsecure added in v0.4.2

func ValidateEndpointURLWithInsecure(endpoint string, insecureAllowHTTP bool) error

ValidateEndpointURLWithInsecure validates that an endpoint URL is secure, allowing HTTP if insecureAllowHTTP is true WARNING: This is insecure and should NEVER be used in production

Types

type FetchOption added in v0.7.0

type FetchOption func(*fetchOptions)

FetchOption configures a fetch request.

func WithBody added in v0.7.0

func WithBody(body io.Reader) FetchOption

WithBody sets the request body.

func WithErrorHandler added in v0.7.0

func WithErrorHandler(handler func(*http.Response, []byte) error) FetchOption

WithErrorHandler sets a custom error handler for non-200 responses. The handler receives the response and body, and should return an error. If the handler returns nil, the default HTTPError will be returned. This is useful for parsing structured error responses (e.g., OAuth error responses).

func WithHeader added in v0.7.0

func WithHeader(key, value string) FetchOption

WithHeader adds a single header to the request.

func WithMethod added in v0.7.0

func WithMethod(method string) FetchOption

WithMethod sets the HTTP method for the request.

type FetchResult added in v0.7.0

type FetchResult[T any] struct {
	// Data is the parsed JSON response body.
	Data T

	// Headers are the response headers.
	Headers http.Header
}

FetchResult contains the result of a successful JSON fetch operation.

func FetchJSON added in v0.7.0

func FetchJSON[T any](
	ctx context.Context,
	client HTTPClient,
	requestURL string,
	opts ...FetchOption,
) (*FetchResult[T], error)

FetchJSON performs an HTTP request and parses the JSON response body. It sets the Accept header to application/json by default. For non-200 responses, it returns an HTTPError or the result of a custom error handler.

func FetchJSONWithForm added in v0.7.0

func FetchJSONWithForm[T any](
	ctx context.Context,
	client HTTPClient,
	requestURL string,
	formData url.Values,
	opts ...FetchOption,
) (*FetchResult[T], error)

FetchJSONWithForm performs a POST request with form-urlencoded body and parses JSON response. This is a convenience wrapper around FetchJSON for token endpoints and similar APIs. It sets Content-Type to application/x-www-form-urlencoded and Accept to application/json.

type HTTPClient added in v0.7.0

type HTTPClient interface {
	Do(req *http.Request) (*http.Response, error)
}

HTTPClient is an interface for making HTTP requests. This interface is satisfied by *http.Client and allows for dependency injection in testing.

type HTTPError added in v0.7.0

type HTTPError struct {
	// StatusCode is the HTTP status code.
	StatusCode int

	// Message is a description of the error (may be a preview of the response body).
	Message string

	// URL is the requested URL.
	URL string
}

HTTPError represents an HTTP error response with status code, URL, and message.

func (*HTTPError) Error added in v0.7.0

func (e *HTTPError) Error() string

Error implements the error interface.

type HttpClientBuilder added in v0.1.8

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

HttpClientBuilder provides a fluent interface for building HTTP clients

func NewHttpClientBuilder added in v0.1.8

func NewHttpClientBuilder() *HttpClientBuilder

NewHttpClientBuilder returns a new HttpClientBuilder

func (*HttpClientBuilder) Build added in v0.1.8

func (b *HttpClientBuilder) Build() (*http.Client, error)

Build creates the configured HTTP client

func (*HttpClientBuilder) WithCABundle added in v0.1.8

func (b *HttpClientBuilder) WithCABundle(path string) *HttpClientBuilder

WithCABundle sets the CA certificate bundle path

func (*HttpClientBuilder) WithInsecureAllowHTTP added in v0.4.2

func (b *HttpClientBuilder) WithInsecureAllowHTTP(allow bool) *HttpClientBuilder

WithInsecureAllowHTTP allows HTTP (non-HTTPS) URLs WARNING: This is insecure and should NEVER be used in production

func (*HttpClientBuilder) WithPrivateIPs added in v0.1.8

func (b *HttpClientBuilder) WithPrivateIPs(allow bool) *HttpClientBuilder

WithPrivateIPs allows connections to private IP addresses

func (*HttpClientBuilder) WithTokenFromFile added in v0.1.8

func (b *HttpClientBuilder) WithTokenFromFile(path string) *HttpClientBuilder

WithTokenFromFile sets the auth token file path

type ValidatingTransport added in v0.0.37

type ValidatingTransport struct {
	Transport         http.RoundTripper
	InsecureAllowHTTP bool
}

ValidatingTransport is for validating URLs prior to request

func (*ValidatingTransport) RoundTrip added in v0.0.37

func (t *ValidatingTransport) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip validates the request URL prior to forwarding

Jump to

Keyboard shortcuts

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