You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

middleware.go 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright 2020 Lauris BH. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package proxy
  5. // Ported from Goji's middleware, source:
  6. // https://github.com/zenazn/goji/tree/master/web/middleware
  7. import (
  8. "net"
  9. "net/http"
  10. "strings"
  11. )
  12. var xForwardedFor = http.CanonicalHeaderKey("X-Forwarded-For")
  13. var xRealIP = http.CanonicalHeaderKey("X-Real-IP")
  14. // ForwardedHeaders is a middleware that sets a http.Request's RemoteAddr to the results
  15. // of parsing either the X-Real-IP header or the X-Forwarded-For header (in that
  16. // order).
  17. func ForwardedHeaders(options ...*ForwardedHeadersOptions) func(h http.Handler) http.Handler {
  18. opt := defaultOptions
  19. if len(options) > 0 {
  20. opt = options[0]
  21. }
  22. return func(h http.Handler) http.Handler {
  23. fn := func(w http.ResponseWriter, r *http.Request) {
  24. // Treat unix socket as 127.0.0.1
  25. if r.RemoteAddr == "@" {
  26. r.RemoteAddr = "127.0.0.1:0"
  27. }
  28. if rip := realIP(r, opt); len(rip) > 0 {
  29. r.RemoteAddr = net.JoinHostPort(rip, "0")
  30. }
  31. h.ServeHTTP(w, r)
  32. }
  33. return http.HandlerFunc(fn)
  34. }
  35. }
  36. func realIP(r *http.Request, options *ForwardedHeadersOptions) string {
  37. host, _, err := net.SplitHostPort(r.RemoteAddr)
  38. if err != nil {
  39. return ""
  40. }
  41. if !options.isTrustedProxy(net.ParseIP(host)) {
  42. return ""
  43. }
  44. var ip string
  45. if xrip := r.Header.Get(xRealIP); xrip != "" {
  46. ip = xrip
  47. } else if xff := r.Header.Get(xForwardedFor); xff != "" {
  48. p := 0
  49. for i := options.ForwardLimit; i > 0; i-- {
  50. if p > 0 {
  51. xff = xff[:p-2]
  52. }
  53. p = strings.LastIndex(xff, ", ")
  54. if p < 0 {
  55. p = 0
  56. break
  57. } else {
  58. p += 2
  59. }
  60. }
  61. ip = xff[p:]
  62. }
  63. return ip
  64. }