Skip to content

Commit

Permalink
refactor: change get client ip function
Browse files Browse the repository at this point in the history
  • Loading branch information
vicanso committed Sep 10, 2020
1 parent b774464 commit a40d68e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 13 deletions.
9 changes: 6 additions & 3 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,16 @@ func GetClientIP(req *http.Request) string {
if ip != "" {
arr := sort.StringSlice(strings.Split(ip, ","))
// 从后往前找第一个非内网IP的则为客户IP
sort.Sort(sort.Reverse(arr))
for _, value := range arr {
v := strings.TrimSpace(value)
for i := len(arr) - 1; i >= 0; i-- {
v := strings.TrimSpace(arr[i])
if !intranetip.Is(net.ParseIP(v)) {
return v
}
}
// 如果所有IP都是非内网IP,则直接取第一个
if len(arr) != 0 {
return strings.TrimSpace(arr[0])
}
}
ip = h.Get(HeaderXRealIP)
if ip != "" {
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/stretchr/testify v1.6.1
github.com/tidwall/gjson v1.6.1
github.com/vicanso/fresh v1.0.0
github.com/vicanso/hes v0.2.2
github.com/vicanso/hes v0.2.3
github.com/vicanso/intranet-ip v0.0.1
github.com/vicanso/keygrip v1.0.0
github.com/vicanso/keygrip v1.0.1
)
12 changes: 4 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,20 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/tidwall/gjson v1.6.0 h1:9VEQWz6LLMUsUl6PueE49ir4Ka6CzLymOAZDxpFsTDc=
github.com/tidwall/gjson v1.6.0/go.mod h1:P256ACg0Mn+j1RXIDXoss50DeIABTYK1PULOJHhxOls=
github.com/tidwall/gjson v1.6.1 h1:LRbvNuNuvAiISWg6gxLEFuCe72UKy5hDqhxW/8183ws=
github.com/tidwall/gjson v1.6.1/go.mod h1:BaHyNc5bjzYkPqgLq7mdVzeiRtULKULXLgZFKsxEHI0=
github.com/tidwall/match v1.0.1 h1:PnKP62LPNxHKTwvHHZZzdOAOCtsJTjo6dZLCwpKm5xc=
github.com/tidwall/match v1.0.1/go.mod h1:LujAq0jyVjBy028G1WhWfIzbpQfMO8bBZ6Tyb0+pL9E=
github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4=
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
github.com/tidwall/pretty v1.0.2 h1:Z7S3cePv9Jwm1KwS0513MRaoUe3S01WPbLNV40pwWZU=
github.com/tidwall/pretty v1.0.2/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
github.com/vicanso/fresh v1.0.0 h1:u3ykbW6SYW5CbI6rx1lZCVfWKVIptvL6/2KOnrTKsTY=
github.com/vicanso/fresh v1.0.0/go.mod h1:gr1RKSFxQ1OnQHzUMBHCigifni7KrXveJjWCTlPjICA=
github.com/vicanso/hes v0.2.2 h1:asekH5sz7ekWvTHajxO0ly/Z8jrvYyx+OxzkBRw75es=
github.com/vicanso/hes v0.2.2/go.mod h1:QcxOFmFfBQMhASTaLgnFayXYCgevdSeBVprt+o+3eKo=
github.com/vicanso/hes v0.2.3 h1:q99u/6veXCJPfNEPUpRB0LpmwUbcvNjNcorZmt9Ciwk=
github.com/vicanso/hes v0.2.3/go.mod h1:QcxOFmFfBQMhASTaLgnFayXYCgevdSeBVprt+o+3eKo=
github.com/vicanso/intranet-ip v0.0.1 h1:cYS+mExFsKqewWSuHtFwAqw/CO66GsheB/P1BPmSTx0=
github.com/vicanso/intranet-ip v0.0.1/go.mod h1:bqQ6VUhxdz0ipSb1kzd6aoZStlp+pB7CTlVmVhgLAxA=
github.com/vicanso/keygrip v1.0.0 h1:qdfc/X6lSFZGIYxBnuNs+3/P/YP5K8OFbprABkWKGb0=
github.com/vicanso/keygrip v1.0.0/go.mod h1:cI05iOjY00NJ7oH2Z9Zdm9eJPUkpoex3XnEubK78nho=
github.com/vicanso/keygrip v1.0.1 h1:pbMM8jicd7Bh3SLops0b3rkDONH+XUCm5zpiwSf26DM=
github.com/vicanso/keygrip v1.0.1/go.mod h1:cI05iOjY00NJ7oH2Z9Zdm9eJPUkpoex3XnEubK78nho=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
Expand Down

0 comments on commit a40d68e

Please sign in to comment.