Documentation
¶
Index ¶
- Constants
- Variables
- func Control(network, address string, c syscall.RawConn) (err error)
- func GenerateLocalhostCertPEM(validFor time.Duration) (certPEM, keyPEM []byte, err error)
- func GetIPAddress(ipAddresses []string, privateRanges []IPRange) (string, bool)
- func GetLoopbackSubdomain(s string) string
- func IPInRange(r IPRange, ipAddress net.IP) bool
- func IPIsPrivateSubnet(ipAddress net.IP, privateRanges []IPRange) bool
- func IPRangeEqual(a, b []IPRange) bool
- func IsTLS(srv *http.Server) bool
- func JoinURL(scheme, host string, port int) string
- func ResolveAddr(addr string) string
- func ResolveHostname(addr string) string
- func ResolvePort(addr string) int
- func ResolveScheme(isTLS bool) string
- func ResolveSchemeFromServer(srv *http.Server) string
- func ResolveSchemeFromVHost(vhost string) string
- func ResolveURL(scheme string, addr string) string
- func ResolveURLFromServer(srv *http.Server) string
- func ResolveVHost(addr string) string
- func SplitURL(inputURL string) (scheme, host string, port int, err error)
- type IPRange
Constants ¶
const ( // SchemeHTTPS the "https" url scheme. SchemeHTTPS = "https" // SchemeHTTP the "http" url scheme. SchemeHTTP = "http" )
Variables ¶
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).
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.
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 GenerateLocalhostCertPEM ¶
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 ¶
GetIPAddress returns a valid public IP Address from a collection of IP Addresses and a range of private subnets.
The collection is the value of a header such as X-Forwarded-For split on the comma. Each proxy appends the address it got the request from, so the entry on the right is the closest to the server and the entries on the left are whatever the client sent. The walk goes from right to left, and an entry is:
- a public address: the result. A port, as in "8.8.4.4:1234" or "[2001:db8::1]:443", is left out of it.
- an address in "privateRanges", or one that is not a global unicast address (a loopback one for example), with or without a port: one of our own proxies, skipped.
- empty (the header was not sent, or a comma has nothing next to it): not an entry, skipped.
- anything else: the end of the walk, with no result. An entry that is not an address cannot be taken as one of our proxies, so it is never skipped for an entry on its left, which the client may have written.
Reports whether a valid IP was found.
func GetLoopbackSubdomain ¶
GetLoopbackSubdomain returns the part of the loopback subdomain.
func IPIsPrivateSubnet ¶
IPIsPrivateSubnet reports whether this "ipAddress" is in a private subnet.
func IPRangeEqual ¶
IPRangeEqual reports whether two IPRange slices are equal.
func IsTLS ¶
IsTLS returns true if the "srv" contains any certificates or a get certificate function, meaning that is secure.
func ResolveAddr ¶
ResolveAddr tries to convert a given string to an address which is compatible with net.Listener and server
func ResolveHostname ¶
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 ¶
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 ¶
ResolveScheme returns "https" if "isTLS" receiver is true, otherwise "http".
func ResolveSchemeFromServer ¶
ResolveSchemeFromServer tries to resolve a url scheme based on the server's configuration. Returns "https" on secure server, otherwise "http".
func ResolveSchemeFromVHost ¶
ResolveSchemeFromVHost returns the scheme based on the "vhost".
func ResolveURL ¶
ResolveURL takes the scheme and an address and returns its URL, pure implementation but it does the job.
func ResolveURLFromServer ¶
ResolveURLFromServer returns the scheme+host from a server.
func ResolveVHost ¶
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.