netutil

package
v12.1.9-0...-e9b74bf Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2020 License: BSD-3-Clause Imports: 13 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.Config.Addr. Developer may want to override this function to return always false in order to not allow different hostname from Application.Config.Addr 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" {
		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.

Functions

func CERT

func CERT(addr string, cert tls.Certificate) (net.Listener, error)

CERT returns a listener which contans tls.Config with the provided certificate, use for ssl.

func Client

func Client(timeout time.Duration) *http.Client

Client returns a new http.Client using the "timeout" for open connection.

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 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 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 LETSENCRYPT

func LETSENCRYPT(addr string, serverName string, cacheDirOptional ...string) (net.Listener, error)

LETSENCRYPT returns a new Automatic TLS Listener using letsencrypt.org service receives three parameters, the first is the host of the server, second can be the server name(domain) or empty if skip verification is the expected behavior (not recommended) and the third is optionally, the cache directory, if you skip it then the cache directory is "./certcache" if you want to disable cache directory then simple give it a value of empty string ""

does NOT supports localhost domains for testing.

this is the recommended function to use when you're ready for production state.

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'

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 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 TCP

func TCP(addr string) (net.Listener, error)

TCP returns a new tcp(ipv6 if supported by network) and an error on failure.

func TCPKeepAlive

func TCPKeepAlive(addr string) (ln net.Listener, err error)

TCPKeepAlive returns a new tcp keep alive Listener and an error on failure.

func TLS

func TLS(addr, certFile, keyFile string) (net.Listener, error)

TLS returns a new TLS Listener and an error on failure.

func UNIX

func UNIX(socketFile string, mode os.FileMode) (net.Listener, error)

UNIX returns a new unix(file) Listener.

Types

type IPRange

type IPRange struct {
	Start net.IP `json:"start" yaml:"Start" toml"Start"`
	End   net.IP `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