fix real ip
This commit is contained in:
parent
4199b81b5f
commit
d24f7efb0b
|
@ -38,6 +38,7 @@ func (ctx *Context) reset(req *http.Request, res http.ResponseWriter, ps map[str
|
||||||
|
|
||||||
func (ctx *Context) RealIp() string {
|
func (ctx *Context) RealIp() string {
|
||||||
var (
|
var (
|
||||||
|
s string
|
||||||
pos int
|
pos int
|
||||||
ipaddr string
|
ipaddr string
|
||||||
)
|
)
|
||||||
|
@ -48,8 +49,16 @@ func (ctx *Context) RealIp() string {
|
||||||
}
|
}
|
||||||
ipaddr, _, _ = net.SplitHostPort(ctx.Request().RemoteAddr)
|
ipaddr, _, _ = net.SplitHostPort(ctx.Request().RemoteAddr)
|
||||||
__end:
|
__end:
|
||||||
if pos = strings.LastIndexByte(ipaddr, ','); pos > -1 {
|
for {
|
||||||
ipaddr = ipaddr[:pos]
|
if pos = strings.IndexByte(ipaddr, ','); pos > -1 {
|
||||||
|
s = strings.TrimSpace(ipaddr[:pos])
|
||||||
|
if netip := net.ParseIP(s); netip != nil && !netip.IsPrivate() {
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
ipaddr = ipaddr[pos+1:]
|
||||||
|
} else {
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return ipaddr
|
return ipaddr
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,28 @@
|
||||||
|
package http
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestContext_RealIp(t *testing.T) {
|
||||||
|
var (
|
||||||
|
pos int
|
||||||
|
s string
|
||||||
|
)
|
||||||
|
ipaddr := "192.168.6.76, 192.168.6.76, 116.24.65.173,192.168.6.76"
|
||||||
|
for {
|
||||||
|
if pos = strings.IndexByte(ipaddr, ','); pos > -1 {
|
||||||
|
s = strings.TrimSpace(ipaddr[:pos])
|
||||||
|
if netip := net.ParseIP(s); netip != nil && !netip.IsPrivate() {
|
||||||
|
t.Log(s)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
ipaddr = ipaddr[pos+1:]
|
||||||
|
} else {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
t.Log(ipaddr)
|
||||||
|
}
|
Loading…
Reference in New Issue