client_ip.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package request // import "miniflux.app/v2/internal/http/request"
  4. import (
  5. "net"
  6. "net/http"
  7. "strings"
  8. )
  9. // IsTrustedIP reports whether the given remote IP address belongs to one of the trusted networks.
  10. func IsTrustedIP(remoteIP string, trustedNetworks []string) bool {
  11. if len(trustedNetworks) == 0 {
  12. return false
  13. }
  14. ip := net.ParseIP(remoteIP)
  15. if ip == nil {
  16. return false
  17. }
  18. for _, cidr := range trustedNetworks {
  19. _, network, err := net.ParseCIDR(cidr)
  20. if err != nil {
  21. continue
  22. }
  23. if network.Contains(ip) {
  24. return true
  25. }
  26. }
  27. return false
  28. }
  29. // FindClientIP returns the real client IP address using trusted reverse-proxy headers when allowed.
  30. func FindClientIP(r *http.Request, isTrustedProxyClient bool) string {
  31. if isTrustedProxyClient {
  32. headers := [...]string{"X-Forwarded-For", "X-Real-Ip"}
  33. for _, header := range headers {
  34. value := r.Header.Get(header)
  35. if value != "" {
  36. addresses := strings.Split(value, ",")
  37. address := strings.TrimSpace(addresses[0])
  38. address = dropIPv6zone(address)
  39. if net.ParseIP(address) != nil {
  40. return address
  41. }
  42. }
  43. }
  44. }
  45. // Fallback to TCP/IP source IP address.
  46. return FindRemoteIP(r)
  47. }
  48. // FindRemoteIP returns the parsed remote IP address from the request,
  49. // falling back to 127.0.0.1 if the address is empty, a unix socket, or invalid.
  50. func FindRemoteIP(r *http.Request) string {
  51. if r.RemoteAddr == "@" || r.RemoteAddr == "" {
  52. return "127.0.0.1"
  53. }
  54. // If it looks like it has a port (IPv4:port or [IPv6]:port), try to split it.
  55. ip, _, err := net.SplitHostPort(r.RemoteAddr)
  56. if err != nil {
  57. // No port — could be a bare IPv4, IPv6, or IPv6 with zone.
  58. ip = r.RemoteAddr
  59. }
  60. // Strip IPv6 zone identifier if present (e.g., %eth0).
  61. ip = dropIPv6zone(ip)
  62. // Validate the IP address.
  63. if net.ParseIP(ip) == nil {
  64. return "127.0.0.1"
  65. }
  66. return ip
  67. }
  68. func dropIPv6zone(address string) string {
  69. idx := strings.IndexByte(address, '%')
  70. if idx != -1 {
  71. address = address[:idx]
  72. }
  73. return address
  74. }