Skip to content

Commit

Permalink
Drop iris support.
Browse files Browse the repository at this point in the history
  • Loading branch information
vaot committed Aug 15, 2018
1 parent f1cddb3 commit b38b4b3
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 128 deletions.
152 changes: 70 additions & 82 deletions user_ip.go
Original file line number Diff line number Diff line change
@@ -1,48 +1,47 @@
package user_ip

import (
"net/http"
"net"
"strings"
"unicode"
"bytes"
"github.com/kataras/iris"
"bytes"
"net"
"net/http"
"strings"
"unicode"
)

type PrivateAddressesRange struct {
start net.IP
end net.IP
start net.IP
end net.IP
}

var privateIpAddresses = []PrivateAddressesRange{
PrivateAddressesRange{
start: net.IPv4(10, 0, 0, 0),
end: net.IPv4(10, 255, 255, 255),
},
PrivateAddressesRange{
start: net.IPv4(100, 64, 0, 0),
end: net.IPv4(100, 127, 255, 255),
},
PrivateAddressesRange{
start: net.IPv4(172, 16, 0, 0),
end: net.IPv4(172, 31, 255, 255),
},
PrivateAddressesRange{
start: net.IPv4(192, 0, 0, 0),
end: net.IPv4(192, 0, 0, 255),
},
PrivateAddressesRange{
start: net.IPv4(192, 168, 0, 0),
end: net.IPv4(192, 168, 255, 255),
},
PrivateAddressesRange{
start: net.IPv4(198, 18, 0, 0),
end: net.IPv4(198, 19, 255, 255),
},
PrivateAddressesRange {
start: net.ParseIP("fc00::"),
end: net.ParseIP("fdff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"),
},
PrivateAddressesRange{
start: net.IPv4(10, 0, 0, 0),
end: net.IPv4(10, 255, 255, 255),
},
PrivateAddressesRange{
start: net.IPv4(100, 64, 0, 0),
end: net.IPv4(100, 127, 255, 255),
},
PrivateAddressesRange{
start: net.IPv4(172, 16, 0, 0),
end: net.IPv4(172, 31, 255, 255),
},
PrivateAddressesRange{
start: net.IPv4(192, 0, 0, 0),
end: net.IPv4(192, 0, 0, 255),
},
PrivateAddressesRange{
start: net.IPv4(192, 168, 0, 0),
end: net.IPv4(192, 168, 255, 255),
},
PrivateAddressesRange{
start: net.IPv4(198, 18, 0, 0),
end: net.IPv4(198, 19, 255, 255),
},
PrivateAddressesRange{
start: net.ParseIP("fc00::"),
end: net.ParseIP("fdff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"),
},
}

var HEADER_IP_KEYS = []string{"X-Forwarded-For", "X-Real-Ip"}
Expand All @@ -52,67 +51,56 @@ const MAX_IP_SEARCH int = 4
// =====================================================================================

func removeWhiteSpaces(str string) string {
return strings.Map(func(char rune) rune {
if unicode.IsSpace(char) {
return -1
}
return char
}, str)
return strings.Map(func(char rune) rune {
if unicode.IsSpace(char) {
return -1
}
return char
}, str)
}

func isPrivateAddress(ip net.IP) bool {
for _, privateIpRange := range privateIpAddresses {
if bytes.Compare(ip, privateIpRange.start) >= 0 && bytes.Compare(ip, privateIpRange.end) <= 0 {
return true
}
}
for _, privateIpRange := range privateIpAddresses {
if bytes.Compare(ip, privateIpRange.start) >= 0 && bytes.Compare(ip, privateIpRange.end) <= 0 {
return true
}
}

return false
return false
}

func ignoreIp(ip net.IP) bool {
return !ip.IsGlobalUnicast() || isPrivateAddress(ip)
return !ip.IsGlobalUnicast() || isPrivateAddress(ip)
}

func findRealIP(ipAddress *string, availableIps []string) {
if len(availableIps) >= MAX_IP_SEARCH {
availableIps = availableIps[:MAX_IP_SEARCH]
}

for _, ip := range availableIps {
ip = removeWhiteSpaces(ip)
userIp := net.ParseIP(ip)

if ignoreIp(userIp) {
continue
} else {
*ipAddress = ip
}
}
if len(availableIps) >= MAX_IP_SEARCH {
availableIps = availableIps[:MAX_IP_SEARCH]
}

for _, ip := range availableIps {
ip = removeWhiteSpaces(ip)
userIp := net.ParseIP(ip)

if ignoreIp(userIp) {
continue
} else {
*ipAddress = ip
}
}
}

// =====================================================================================
func GetFromContext(ctx *iris.Context) string {
var ipAddress string

for _, headerProperty := range HEADER_IP_KEYS {
availableIps := strings.Split(string(ctx.RequestCtx.Request.Header.Peek(headerProperty)), ",")
findRealIP(&ipAddress, availableIps)
}

return ipAddress
}

func Get(req *http.Request) string {
var ipAddress string
var ipAddress string

for _, headerProperty := range HEADER_IP_KEYS {
for _, headerProperty := range HEADER_IP_KEYS {

// We will only search through up to MAX_IP_SEARCH, we should not be looking
// for all of them, since client may be playing with X-Forwarded-For
availableIps := strings.Split(req.Header.Get(headerProperty), ",")
findRealIP(&ipAddress, availableIps)
}
// We will only search through up to MAX_IP_SEARCH, we should not be looking
// for all of them, since client may be playing with X-Forwarded-For
availableIps := strings.Split(req.Header.Get(headerProperty), ",")
findRealIP(&ipAddress, availableIps)
}

return ipAddress
return ipAddress
}
60 changes: 14 additions & 46 deletions user_ip_test.go
Original file line number Diff line number Diff line change
@@ -1,63 +1,31 @@
package user_ip

import (
"testing"
"net/http"
"github.com/stretchr/testify/assert"
"github.com/kataras/iris"
"github.com/valyala/fasthttp"
"github.com/stretchr/testify/assert"
"net/http"
"testing"
)

func TestGetNoIp(t *testing.T) {
req := &http.Request{Method: "GET"}
ip := Get(req)
req := &http.Request{Method: "GET"}
ip := Get(req)

assert.Equal(t, ip, "", "Should not find an ip address.")
}

func TestGetFromContextNoIp(t *testing.T) {
var ctx iris.Context
ctx.RequestCtx = &fasthttp.RequestCtx{}
ip := GetFromContext(&ctx)

assert.Equal(t, ip, "", "Should not find an ip address.")
}

func TestGetFromContextWithPrivateForwadedIp(t *testing.T) {
var ctx iris.Context
ctx.RequestCtx = &fasthttp.RequestCtx{}
header := fasthttp.RequestHeader{}
header.Add("X-Forwarded-For", "100.64.0.0, 192.168.0.0")
ctx.RequestCtx.Request.Header = header
ip := GetFromContext(&ctx)

assert.Equal(t, ip, "", "Should not find an ip address.")
assert.Equal(t, ip, "", "Should not find an ip address.")
}

func TestGetWithPrivateForwadedIp(t *testing.T) {
req := &http.Request{Method: "GET", Header: make(http.Header)}
ip := Get(req)

req.Header.Set("X-Forwarded-For", "100.64.0.0, 192.168.0.0")

assert.Equal(t, ip, "", "Should not find an ip address.")
}
req := &http.Request{Method: "GET", Header: make(http.Header)}
ip := Get(req)

func TestGetFromContextWithGlobalAndPrivateForwardedIps(t *testing.T) {
var ctx iris.Context
ctx.RequestCtx = &fasthttp.RequestCtx{}
header := fasthttp.RequestHeader{}
header.Add("X-Forwarded-For", "65.55.37.104, 100.64.0.0, 192.168.0.0, 192.0.0.0")
ctx.RequestCtx.Request.Header = header
ip := GetFromContext(&ctx)
req.Header.Set("X-Forwarded-For", "100.64.0.0, 192.168.0.0")

assert.Equal(t, ip, "65.55.37.104", "Should be the first ip, which is the global ip address.")
assert.Equal(t, ip, "", "Should not find an ip address.")
}

func TestGetWithGlobalAndPrivateForwardedIps(t *testing.T) {
req := &http.Request{Method: "GET", Header: make(http.Header)}
req.Header.Set("X-Forwarded-For", "65.55.37.104, 100.64.0.0, 192.168.0.0, 192.0.0.0")
req := &http.Request{Method: "GET", Header: make(http.Header)}
req.Header.Set("X-Forwarded-For", "65.55.37.104, 100.64.0.0, 192.168.0.0, 192.0.0.0")

ip := Get(req)
assert.Equal(t, ip, "65.55.37.104", "Should be the first ip, which is the global ip address.")
ip := Get(req)
assert.Equal(t, ip, "65.55.37.104", "Should be the first ip, which is the global ip address.")
}

0 comments on commit b38b4b3

Please sign in to comment.