-
-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathhelpers.go
More file actions
74 lines (68 loc) · 1.61 KB
/
helpers.go
File metadata and controls
74 lines (68 loc) · 1.61 KB
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
package caddywaf
import (
"net"
"net/http"
"os"
"strings"
)
// fileExists checks if a file exists and is readable.
func fileExists(path string) bool {
if path == "" {
return false
}
info, err := os.Stat(path)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}
// isIPv4 - checks if input IP is of type v4
//
//nolint:unused
func isIPv4(addr string) bool {
return strings.Count(addr, ":") < 2
}
// appendCIDR - appends CIDR for a single IP
func appendCIDR(ip string) string {
// IPv4
if strings.Count(ip, ":") < 2 {
ip += "/32"
// IPv6
} else {
ip += "/128"
}
return ip
}
// extractIP extracts the IP address from a remote address string.
// Returns the original input if parsing fails, which allows upstream
// code to handle invalid IPs gracefully.
func extractIP(remoteAddr string) string {
if remoteAddr == "" {
return ""
}
host, _, err := net.SplitHostPort(remoteAddr)
if err != nil {
// Could be an IP without port, validate it
if ip := net.ParseIP(remoteAddr); ip != nil {
return remoteAddr
}
// Return as-is for upstream handling
return remoteAddr
}
return host
}
// getClientIP returns the real client IP, checking X-Forwarded-For header first.
// Falls back to RemoteAddr if X-Forwarded-For is empty or contains invalid data.
func getClientIP(r *http.Request) string {
if xff := r.Header.Get("X-Forwarded-For"); xff != "" {
ips := strings.Split(xff, ",")
if len(ips) > 0 {
clientIP := strings.TrimSpace(ips[0])
// Validate the IP before returning
if clientIP != "" && net.ParseIP(extractIP(clientIP)) != nil {
return clientIP
}
}
}
return r.RemoteAddr
}