netutil

package
v14.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2026 License: BSD-3-Clause Imports: 20 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// SchemeHTTPS the "https" url scheme.
	SchemeHTTPS = "https"
	// SchemeHTTP the "http" url scheme.
	SchemeHTTP = "http"
)

Variables

View Source
var IsLoopbackHost = func(requestHost string) bool {

	portOrPathIdx := strings.LastIndexByte(requestHost, ':')

	if portOrPathIdx == 0 {
		return true
	}

	if portOrPathIdx == -1 {
		portOrPathIdx = strings.LastIndexByte(requestHost, '/')
		if portOrPathIdx == -1 {
			portOrPathIdx = len(requestHost)
		}
	}

	subdomainFinishIdx := strings.IndexByte(requestHost, '.') + 1
	if l := len(requestHost); l <= subdomainFinishIdx || l < portOrPathIdx {
		return false
	}

	hostname := requestHost[subdomainFinishIdx:portOrPathIdx]
	if hostname == "" {
		return false
	}

	valid := LoopbackRegex.MatchString(hostname)
	if !valid {
		valid = hostname == machineHostname
	}
	return valid
}

IsLoopbackHost tries to catch the local addresses when a developer navigates to a subdomain that its hostname differs from Application.Configuration.VHost. Developer may want to override this function to return always false in order to not allow different hostname from Application.Configuration.VHost in local environment (remote is not reached).

View Source
var IsLoopbackSubdomain = func(s string) bool {
	if strings.HasPrefix(s, "127.0.0.1:") || s == "127.0.0.1" ||
		strings.HasPrefix(s, "0.0.0.0:") || s == "0.0.0.0" {
		return true
	}

	valid := loopbackSubRegex.MatchString(s)
	if !valid {
		if !strings.Contains(machineHostname, ".") {
			valid = s == machineHostname
		}
	}
	return valid
}

IsLoopbackSubdomain checks if a string is a subdomain or a hostname.

View Source
var (
	// LoopbackRegex the regex if matched a host:port is a loopback.
	LoopbackRegex = regexp.MustCompile(`^localhost$|^127(?:\.[0-9]+){0,2}\.[0-9]+$|^(?:0*\:)*?:?0*1$`)
)

Functions

func Control

func Control(network, address string, c syscall.RawConn) (err error)

Control sets the SO_REUSEADDR and SO_REUSEPORT options on the socket.

func GenerateLocalhostCertPEM

func GenerateLocalhostCertPEM(validFor time.Duration) (certPEM, keyPEM []byte, err error)

GenerateLocalhostCertPEM builds a throwaway self-signed ECDSA P-256 certificate and private key as PEM contents, with subjectAltNames for "localhost", 127.0.0.1 and ::1, valid from an hour ago until "validFor" from now. The certificate is its own CA so a client can add it to a root pool and pass modern hostname verification against it.

It serves local development and testing (httptest and the server test suites share it); it is not for production certificates.

func GetIPAddress

func GetIPAddress(ipAddresses []string, privateRanges []IPRange) (string, bool)

GetIPAddress returns a valid public IP Address from a collection of IP Addresses and a range of private subnets.

Reports whether a valid IP was found.

func GetLoopbackSubdomain

func GetLoopbackSubdomain(s string) string

GetLoopbackSubdomain returns the part of the loopback subdomain.

func IPInRange

func IPInRange(r IPRange, ipAddress net.IP) bool

IPInRange reports whether a given IP Address is within a range given.

func IPIsPrivateSubnet

func IPIsPrivateSubnet(ipAddress net.IP, privateRanges []IPRange) bool

IPIsPrivateSubnet reports whether this "ipAddress" is in a private subnet.

func IPRangeEqual

func IPRangeEqual(a, b []IPRange) bool

IPRangeEqual reports whether two IPRange slices are equal.

func IsTLS

func IsTLS(srv *http.Server) bool

IsTLS returns true if the "srv" contains any certificates or a get certificate function, meaning that is secure.

func JoinURL

func JoinURL(scheme, host string, port int) string

JoinURL constructs a URL from the given parts.

func ResolveAddr

func ResolveAddr(addr string) string

ResolveAddr tries to convert a given string to an address which is compatible with net.Listener and server

func ResolveHostname

func ResolveHostname(addr string) string

ResolveHostname receives an addr of form host[:port] and returns the hostname part of it ex: localhost:8080 will return the `localhost`, mydomain.com:8080 will return the 'mydomain.com' and [::1]:8080 will return the '::1'.

func ResolvePort

func ResolvePort(addr string) int

ResolvePort receives an addr of form host[:port] and returns the port part of it ex: localhost:8080 and [::1]:8080 will return the `8080`, mydomain.com will return the '80'

func ResolveScheme

func ResolveScheme(isTLS bool) string

ResolveScheme returns "https" if "isTLS" receiver is true, otherwise "http".

func ResolveSchemeFromServer

func ResolveSchemeFromServer(srv *http.Server) string

ResolveSchemeFromServer tries to resolve a url scheme based on the server's configuration. Returns "https" on secure server, otherwise "http".

func ResolveSchemeFromVHost

func ResolveSchemeFromVHost(vhost string) string

ResolveSchemeFromVHost returns the scheme based on the "vhost".

func ResolveURL

func ResolveURL(scheme string, addr string) string

ResolveURL takes the scheme and an address and returns its URL, pure implementation but it does the job.

func ResolveURLFromServer

func ResolveURLFromServer(srv *http.Server) string

ResolveURLFromServer returns the scheme+host from a server.

func ResolveVHost

func ResolveVHost(addr string) string

ResolveVHost tries to get the hostname if port is no needed for Addr's usage. Addr is being used inside router->subdomains and inside {{ url }} template funcs. It should be the same as "browser's" usually they removing :80 or :443.

func SplitURL

func SplitURL(inputURL string) (scheme, host string, port int, err error)

SplitURL splits the URL into scheme, host, and port.

Types

type IPRange

type IPRange struct {
	Start string `ini:"start" json:"start" yaml:"Start" toml:"Start"`
	End   string `ini:"end" json:"end" yaml:"End" toml:"End"`
}

IPRange is a structure that holds the start and end of a range of IP Addresses.

Jump to

Keyboard shortcuts

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