Documentation
¶
Overview ¶
Package net provides external IP detection, multiaddr builders, and HTTP client helpers.
Index ¶
- Variables
- func AddressInfoFromString(s string) (peer.AddrInfo, error)
- func AddressInfoToString(localPeer peer.AddrInfo) string
- func BuildAdvertisedAddresses(log logger.AppLogger, publicIPV4, publicIPV6 string, listenPort int) ([]multiaddr.Multiaddr, error)
- func BuildAdvertisedQUICAddresses(log logger.AppLogger, publicIPV4, publicIPV6 string, listenPort int) ([]multiaddr.Multiaddr, error)
- func CurlWithBody[T any](ctx context.Context, method, targetURL string, payloadJSON []byte, ...) (res *T, statusCode int, err error)
- func DetectIPIfConfigTrace(traceURL, network string) (ip string, err error)
- func DetectIPViaCloudflareTrace(traceURL, network string) (ip string, err error)
- func ExternalIP() (string, error)
- func GetCurl[T any](ctx context.Context, targetURL string, headers map[string]string, ...) (res *T, statusCode int, err error)
- func GetExternalIPs() (ipV4, ipV6 string, err error)
- func GetFreePort() (int, error)
- func GetIPProtocol(ipStr string) string
- func GetInterfaceIPs() []string
- func GetOutboundIP() (string, error)
- func GetOutboundQUICP2PAddr(port int) (string, error)
- func GetOutboundTCPP2PAddr(port int) (string, error)
- func GetPrivateIPs() ([]string, error)
- func IsGlobalUnicast(ip stdnet.IP) bool
- func IsPrivateOrULA(ip stdnet.IP) bool
- func MultiAddressBuilder(ip stdnet.IP, tcpPort int) ([]multiaddr.Multiaddr, error)
- func MustBuildAdvertisedAddresses(log logger.AppLogger, publicIPV4, publicIPV6 string, listenPort int) []multiaddr.Multiaddr
- func MustBuildAdvertisedQUICAddresses(log logger.AppLogger, publicIPV4, publicIPV6 string, listenPort int) []multiaddr.Multiaddr
- func ParseCloudflareTrace(res io.Reader) (string, error)
- func PatchCurl[T any](ctx context.Context, targetURL string, payload any, headers map[string]string) (res *T, statusCode int, err error)
- func PostCurl[T any](ctx context.Context, targetURL string, payload any, headers map[string]string) (res *T, statusCode int, err error)
- func SortAddresses(ipAddrs []stdnet.IP) []stdnet.IP
- func WithDecoder[T any](decoder func(io.Reader) error) func(*CurlConf[T])
- func WithHTTPClient[T any](client *stdhttp.Client) func(*CurlConf[T])
- type CurlConf
- type CurlOpts
Constants ¶
This section is empty.
Variables ¶
var ( GetInterfaces = stdnet.Interfaces ListAddrs = func(iface stdnet.Interface) ([]stdnet.Addr, error) { return iface.Addrs() } )
Functions ¶
func AddressInfoFromString ¶
AddressInfoFromString converts a JSON representation produced by AddressInfoToString back to peer.AddrInfo
func AddressInfoToString ¶
AddressInfoToString converts a peer.AddrInfo into a JSON representation consistent with libp2p logging output
func BuildAdvertisedQUICAddresses ¶ added in v0.1.3
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 DetectIPViaCloudflareTrace ¶
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 ¶
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 ¶
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 ¶
GetFreePort reserves an available TCP port for testing purposes
func GetIPProtocol ¶
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 ¶
GetOutboundIP returns preferred outbound ip of this machine.
func GetOutboundQUICP2PAddr ¶
func GetOutboundTCPP2PAddr ¶
func GetPrivateIPs ¶
func IsGlobalUnicast ¶
func IsPrivateOrULA ¶
func MultiAddressBuilder ¶
MultiAddressBuilder takes in an IP address and port to produce libp2p-compatible multiaddresses
func MustBuildAdvertisedQUICAddresses ¶ added in v0.1.3
func ParseCloudflareTrace ¶
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 WithDecoder ¶
WithDecoder sets a custom decoder function for processing the response body. If set, the decoder is called instead of JSON unmarshaling.