|
@@ -14,6 +14,15 @@ var (
|
|
|
defaultBinder = &DefaultBinder{}
|
|
|
)
|
|
|
|
|
|
+var (
|
|
|
+ realIPHeaders = []string{
|
|
|
+ "Cf-Connecting-Ip",
|
|
|
+ "True-Client-IP",
|
|
|
+ "X-Forwarded-For",
|
|
|
+ "X-Real-Ip",
|
|
|
+ }
|
|
|
+)
|
|
|
+
|
|
|
type Context struct {
|
|
|
ctx context.Context
|
|
|
req *http.Request
|
|
@@ -28,18 +37,21 @@ func (ctx *Context) reset(req *http.Request, res http.ResponseWriter, ps map[str
|
|
|
}
|
|
|
|
|
|
func (ctx *Context) RealIp() string {
|
|
|
- if ip := ctx.Request().Header.Get("X-Forwarded-For"); ip != "" {
|
|
|
- i := strings.IndexAny(ip, ",")
|
|
|
- if i > 0 {
|
|
|
- return strings.TrimSpace(ip[:i])
|
|
|
+ var (
|
|
|
+ pos int
|
|
|
+ ipaddr string
|
|
|
+ )
|
|
|
+ for _, h := range realIPHeaders {
|
|
|
+ if ipaddr = ctx.Request().Header.Get(h); ipaddr != "" {
|
|
|
+ goto __end
|
|
|
}
|
|
|
- return ip
|
|
|
}
|
|
|
- if ip := ctx.Request().Header.Get("X-Real-IP"); ip != "" {
|
|
|
- return ip
|
|
|
+ ipaddr, _, _ = net.SplitHostPort(ctx.Request().RemoteAddr)
|
|
|
+__end:
|
|
|
+ if pos = strings.LastIndexByte(ipaddr, ','); pos > -1 {
|
|
|
+ ipaddr = ipaddr[:pos]
|
|
|
}
|
|
|
- ra, _, _ := net.SplitHostPort(ctx.Request().RemoteAddr)
|
|
|
- return ra
|
|
|
+ return ipaddr
|
|
|
}
|
|
|
|
|
|
func (ctx *Context) Request() *http.Request {
|
|
@@ -76,7 +88,7 @@ func (ctx *Context) Param(k string) string {
|
|
|
return ctx.Request().FormValue(k)
|
|
|
}
|
|
|
|
|
|
-func (ctx *Context) send(res responsePayload) (err error) {
|
|
|
+func (ctx *Context) json(res responsePayload) (err error) {
|
|
|
ctx.Response().Header().Set("Content-Type", "application/json")
|
|
|
encoder := json.NewEncoder(ctx.Response())
|
|
|
if strings.HasPrefix(ctx.Request().Header.Get("User-Agent"), "curl") {
|
|
@@ -86,7 +98,7 @@ func (ctx *Context) send(res responsePayload) (err error) {
|
|
|
}
|
|
|
|
|
|
func (ctx *Context) Success(v any) (err error) {
|
|
|
- return ctx.send(responsePayload{Data: v})
|
|
|
+ return ctx.json(responsePayload{Data: v})
|
|
|
}
|
|
|
|
|
|
func (ctx *Context) Status(code int) {
|
|
@@ -94,7 +106,7 @@ func (ctx *Context) Status(code int) {
|
|
|
}
|
|
|
|
|
|
func (ctx *Context) Error(code int, reason string) (err error) {
|
|
|
- return ctx.send(responsePayload{Code: code, Reason: reason})
|
|
|
+ return ctx.json(responsePayload{Code: code, Reason: reason})
|
|
|
}
|
|
|
|
|
|
func (ctx *Context) Redirect(url string, code int) {
|