mirror of
https://github.com/labstack/echo.git
synced 2026-06-20 01:18:42 +02:00
Merge branch 'master' of https://github.com/ipfans/echo into ipfans-master
This commit is contained in:
@@ -68,6 +68,9 @@ type (
|
||||
// RemoteAddress returns the client's network address.
|
||||
RemoteAddress() string
|
||||
|
||||
// RealIP returns the client's network ip address.
|
||||
RealIP() string
|
||||
|
||||
// Method returns the request's HTTP function.
|
||||
Method() string
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"bytes"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"net"
|
||||
|
||||
"github.com/labstack/echo"
|
||||
"github.com/labstack/echo/engine"
|
||||
@@ -78,6 +79,19 @@ func (r *Request) RemoteAddress() string {
|
||||
return r.RemoteAddr().String()
|
||||
}
|
||||
|
||||
// RealIP implements `engine.Request#RealIP` function.
|
||||
func (r *Request) RealIP() string {
|
||||
ra := r.RemoteAddress()
|
||||
if ip := r.Header().Get(echo.HeaderXForwardedFor); ip != "" {
|
||||
ra = ip
|
||||
} else if ip := r.Header().Get(echo.HeaderXRealIP); ip != "" {
|
||||
ra = ip
|
||||
} else {
|
||||
ra, _, _ = net.SplitHostPort(ra)
|
||||
}
|
||||
return ra
|
||||
}
|
||||
|
||||
// Method implements `engine.Request#Method` function.
|
||||
func (r *Request) Method() string {
|
||||
return string(r.RequestCtx.Method())
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"mime/multipart"
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
@@ -98,6 +99,19 @@ func (r *Request) RemoteAddress() string {
|
||||
return r.RemoteAddr
|
||||
}
|
||||
|
||||
// RealIP implements `engine.Request#RealIP` function.
|
||||
func (r *Request) RealIP() string {
|
||||
ra := r.RemoteAddress()
|
||||
if ip := r.Header().Get(echo.HeaderXForwardedFor); ip != "" {
|
||||
ra = ip
|
||||
} else if ip := r.Header().Get(echo.HeaderXRealIP); ip != "" {
|
||||
ra = ip
|
||||
} else {
|
||||
ra, _, _ = net.SplitHostPort(ra)
|
||||
}
|
||||
return ra
|
||||
}
|
||||
|
||||
// Method implements `engine.Request#Method` function.
|
||||
func (r *Request) Method() string {
|
||||
return r.Request.Method
|
||||
|
||||
@@ -21,7 +21,7 @@ Cache-Control: no-cache
|
||||
Accept-Language: de,en;q=0.7,en-us;q=0.3
|
||||
Referer: https://github.com/
|
||||
Cookie: session=securetoken; user=123
|
||||
X-Real-IP: 127.0.0.1
|
||||
X-Real-IP: 192.168.1.1
|
||||
|
||||
--Asrf456BGe4h
|
||||
Content-Disposition: form-data; name="foo"
|
||||
@@ -47,10 +47,11 @@ func RequestTest(t *testing.T, request engine.Request) {
|
||||
|
||||
assert.Equal(t, "/labstack/echo", request.URL().Path())
|
||||
assert.Equal(t, "https://github.com/", request.Referer())
|
||||
assert.Equal(t, "127.0.0.1", request.Header().Get("X-Real-IP"))
|
||||
assert.Equal(t, "192.168.1.1", request.Header().Get("X-Real-IP"))
|
||||
assert.Equal(t, "http", request.Scheme())
|
||||
assert.Equal(t, "Mozilla/5.0 (Macintosh; U; Intel Mac OS X; de-de) AppleWebKit/523.10.3 (KHTML, like Gecko) Version/3.0.4 Safari/523.10", request.UserAgent())
|
||||
assert.Equal(t, "127.0.0.1", request.RemoteAddress())
|
||||
assert.Equal(t, "192.168.1.1", request.RealIP())
|
||||
assert.Equal(t, "POST", request.Method())
|
||||
|
||||
assert.Equal(t, int64(261), request.ContentLength())
|
||||
|
||||
@@ -3,7 +3,6 @@ package middleware
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"net"
|
||||
"os"
|
||||
"strconv"
|
||||
"sync"
|
||||
@@ -118,14 +117,7 @@ func LoggerWithConfig(config LoggerConfig) echo.MiddlewareFunc {
|
||||
case "time_rfc3339":
|
||||
return w.Write([]byte(time.Now().Format(time.RFC3339)))
|
||||
case "remote_ip":
|
||||
ra := req.RemoteAddress()
|
||||
if ip := req.Header().Get(echo.HeaderXRealIP); ip != "" {
|
||||
ra = ip
|
||||
} else if ip = req.Header().Get(echo.HeaderXForwardedFor); ip != "" {
|
||||
ra = ip
|
||||
} else {
|
||||
ra, _, _ = net.SplitHostPort(ra)
|
||||
}
|
||||
ra := req.RealIP()
|
||||
return w.Write([]byte(ra))
|
||||
case "host":
|
||||
return w.Write([]byte(req.Host()))
|
||||
|
||||
@@ -5,9 +5,11 @@ import (
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"mime/multipart"
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/ipfans/echo"
|
||||
"github.com/labstack/echo/engine"
|
||||
)
|
||||
|
||||
@@ -84,6 +86,18 @@ func (r *Request) RemoteAddress() string {
|
||||
return r.request.RemoteAddr
|
||||
}
|
||||
|
||||
func (r *Request) RealIP() string {
|
||||
ra := r.RemoteAddress()
|
||||
if ip := r.Header().Get(echo.HeaderXForwardedFor); ip != "" {
|
||||
ra = ip
|
||||
} else if ip := r.Header().Get(echo.HeaderXRealIP); ip != "" {
|
||||
ra = ip
|
||||
} else {
|
||||
ra, _, _ = net.SplitHostPort(ra)
|
||||
}
|
||||
return ra
|
||||
}
|
||||
|
||||
func (r *Request) Method() string {
|
||||
return r.request.Method
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user