client_ip.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 checks if the given remote IP belongs to one of the trusted networks.
  10. func IsTrustedIP(remoteIP string, trustedNetworks []string) bool {
  11. if remoteIP == "@" || strings.HasPrefix(remoteIP, "/") {
  12. return true
  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 client real IP address based on trusted Reverse-Proxy HTTP headers.
  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 remote client IP address without considering HTTP headers.
  49. func FindRemoteIP(r *http.Request) string {
  50. remoteIP, _, err := net.SplitHostPort(r.RemoteAddr)
  51. if err != nil {
  52. remoteIP = r.RemoteAddr
  53. }
  54. return dropIPv6zone(remoteIP)
  55. }
  56. func dropIPv6zone(address string) string {
  57. i := strings.IndexByte(address, '%')
  58. if i != -1 {
  59. address = address[:i]
  60. }
  61. return address
  62. }