blob: 14ee612b2dd8e82e9054c115f370cc51ea30805f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
package request
import (
"net"
"net/http"
"net/netip"
)
const (
// SchemeHTTP name for the HTTP scheme
SchemeHTTP = "http"
// SchemeHTTPS name for the HTTPS scheme
SchemeHTTPS = "https"
// IPV6PrefixLength is the length of the IPv6 prefix
IPV6PrefixLength = 64
)
// IsHTTPS checks whether the request originated from HTTP or HTTPS.
// It checks the value from r.URL.Scheme
func IsHTTPS(r *http.Request) bool {
return r.URL.Scheme == SchemeHTTPS
}
// GetHostWithoutPort returns a host without the port. The host(:port) comes
// from a Host: header if it is provided, otherwise it is a server name.
func GetHostWithoutPort(r *http.Request) string {
host, _, err := net.SplitHostPort(r.Host)
if err != nil {
return r.Host
}
return host
}
// GetRemoteAddrWithoutPort strips the port from the r.RemoteAddr
func GetRemoteAddrWithoutPort(r *http.Request) string {
remoteAddr, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
return r.RemoteAddr
}
return remoteAddr
}
// GetIPV4orIPV6PrefixWithoutPort strips the port from the r.RemoteAddr
// and returns IPV4 address or IPV6 Prefix.
func GetIPV4orIPV6PrefixWithoutPort(r *http.Request) string {
return GetIPV4orIPV6Prefix(r.RemoteAddr)
}
// GetIPV4orIPV6Prefix returns either the full IPv4 address or the /64 prefix
// of the IPv6 address from the provided remote address, without the port.
// For IPv4 it returns the full address. For IPv6 it returns the /64 prefix.
func GetIPV4orIPV6Prefix(remoteAddr string) string {
remoteIP, _, err := net.SplitHostPort(remoteAddr)
if err != nil {
remoteIP = remoteAddr
}
addr, err := netip.ParseAddr(remoteIP)
if err != nil {
return remoteIP
}
if addr.Is4() {
return remoteIP
} else if addr.Is6() {
ipv6Prefix, err := addr.Prefix(IPV6PrefixLength)
if err != nil {
return remoteIP
}
return ipv6Prefix.String()
}
return remoteIP
}
|