net

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Aug 13, 2026 License: BSD-3-Clause Imports: 18 Imported by: 0

Documentation

Overview

Package net provides external IP detection, multiaddr builders, and HTTP client helpers.

Index

Constants

This section is empty.

Variables

View Source
var (
	GetInterfaces = stdnet.Interfaces
	ListAddrs     = func(iface stdnet.Interface) ([]stdnet.Addr, error) {
		return iface.Addrs()
	}
)

Functions

func AddressInfoFromString

func AddressInfoFromString(s string) (peer.AddrInfo, error)

AddressInfoFromString converts a JSON representation produced by AddressInfoToString back to peer.AddrInfo

func AddressInfoToString

func AddressInfoToString(localPeer peer.AddrInfo) string

AddressInfoToString converts a peer.AddrInfo into a JSON representation consistent with libp2p logging output

func BuildAdvertisedAddresses

func BuildAdvertisedAddresses(
	log logger.AppLogger,
	publicIPV4,
	publicIPV6 string,
	listenPort int,
) ([]multiaddr.Multiaddr, error)

func BuildAdvertisedQUICAddresses added in v0.1.3

func BuildAdvertisedQUICAddresses(
	log logger.AppLogger,
	publicIPV4,
	publicIPV6 string,
	listenPort int,
) ([]multiaddr.Multiaddr, error)

func CurlWithBody

func CurlWithBody[T any](ctx context.Context, method, targetURL string, payloadJSON []byte, headers map[string]string, opts ...CurlOpts[T]) (res *T, statusCode int, err error)

CurlWithBody sends an HTTP request with the specified method, URL, JSON body, and headers. Returns the unmarshaled response of type T, the HTTP status code, and any error. Supports custom decoders and HTTP clients via options.

func DetectIPIfConfigTrace

func DetectIPIfConfigTrace(traceURL, network string) (ip string, err error)

func DetectIPViaCloudflareTrace

func DetectIPViaCloudflareTrace(traceURL, network string) (ip string, err error)

DetectIPViaCloudflareTrace fetches the Cloudflare /cdn-cgi/trace endpoint and parses the ip= field. The traceURL parameter specifies the endpoint URL, and the network parameter controls the address family ("tcp", "tcp4", or "tcp6").

func ExternalIP

func ExternalIP() (string, error)

ExternalIP returns the first non-loopback IP address available.

func GetCurl

func GetCurl[T any](ctx context.Context, targetURL string, headers map[string]string, opts ...CurlOpts[T]) (res *T, statusCode int, err error)

GetCurl is a generic function to send GET request with headers and return response expect json response T is a type of response, automatically unmarshalled from json check status code first. in some cases response can be different, so unmarshalling will fail status code return as is even in unmarshalling error

func GetExternalIPs

func GetExternalIPs() (ipV4, ipV6 string, err error)

GetExternalIPs returns the external IPv4 and IPv6 addresses of the host machine. It first attempts to probe external IPs via HTTP requests to known endpoints. If that fails, it falls back to inspecting network interfaces for non-loopback addresses.

func GetFreePort

func GetFreePort() (int, error)

GetFreePort reserves an available TCP port for testing purposes

func GetIPProtocol

func GetIPProtocol(ipStr string) string

GetIPProtocol returns the multiaddr protocol string ("ip4" or "ip6") for the given IP address string. Returns "ip4" for IPv4 addresses and "ip6" for IPv6 addresses. If the IP string cannot be parsed, it defaults to "ip4".

func GetInterfaceIPs

func GetInterfaceIPs() []string

func GetOutboundIP

func GetOutboundIP() (string, error)

GetOutboundIP returns preferred outbound ip of this machine.

func GetOutboundQUICP2PAddr

func GetOutboundQUICP2PAddr(port int) (string, error)

func GetOutboundTCPP2PAddr

func GetOutboundTCPP2PAddr(port int) (string, error)

func GetPrivateIPs

func GetPrivateIPs() ([]string, error)

func IsGlobalUnicast

func IsGlobalUnicast(ip stdnet.IP) bool

func IsPrivateOrULA

func IsPrivateOrULA(ip stdnet.IP) bool

func MultiAddressBuilder

func MultiAddressBuilder(ip stdnet.IP, tcpPort int) ([]multiaddr.Multiaddr, error)

MultiAddressBuilder takes in an IP address and port to produce libp2p-compatible multiaddresses

func MustBuildAdvertisedAddresses

func MustBuildAdvertisedAddresses(
	log logger.AppLogger,
	publicIPV4,
	publicIPV6 string,
	listenPort int,
) []multiaddr.Multiaddr

func MustBuildAdvertisedQUICAddresses added in v0.1.3

func MustBuildAdvertisedQUICAddresses(
	log logger.AppLogger,
	publicIPV4,
	publicIPV6 string,
	listenPort int,
) []multiaddr.Multiaddr

func ParseCloudflareTrace

func ParseCloudflareTrace(res io.Reader) (string, error)

ParseCloudflareTrace extracts and validates the ip= field from the Cloudflare trace response body (key=value\n format).

func PatchCurl

func PatchCurl[T any](ctx context.Context, targetURL string, payload any, headers map[string]string) (res *T, statusCode int, err error)

PatchCurl sends a PATCH request with JSON payload and returns the unmarshaled response. The payload is automatically marshaled to JSON.

func PostCurl

func PostCurl[T any](ctx context.Context, targetURL string, payload any, headers map[string]string) (res *T, statusCode int, err error)

PostCurl sends a POST request with JSON payload and returns the unmarshaled response. The payload is automatically marshaled to JSON.

func SortAddresses

func SortAddresses(ipAddrs []stdnet.IP) []stdnet.IP

func WithDecoder

func WithDecoder[T any](decoder func(io.Reader) error) func(*CurlConf[T])

WithDecoder sets a custom decoder function for processing the response body. If set, the decoder is called instead of JSON unmarshaling.

func WithHTTPClient

func WithHTTPClient[T any](client *stdhttp.Client) func(*CurlConf[T])

WithHTTPClient allows using a custom HTTP client (e.g., with keep-alive, custom timeouts)

Types

type CurlConf

type CurlConf[T any] struct {
	Decoder func(io.Reader) error
	Client  *stdhttp.Client
}

CurlConf holds configuration options for HTTP requests.

type CurlOpts

type CurlOpts[T any] func(*CurlConf[T])

CurlOpts is a functional option type for configuring HTTP requests.

Jump to

Keyboard shortcuts

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