-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add realip middleware * clarify comments * add more tests * fix package name * move ip get to realip package, update docs * lint: missing package comment and drop unused func
- Loading branch information
Showing
6 changed files
with
240 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Package realip extracts a real IP address from the request. | ||
package realip | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"net" | ||
"net/http" | ||
"strings" | ||
) | ||
|
||
type ipRange struct { | ||
start net.IP | ||
end net.IP | ||
} | ||
|
||
var privateRanges = []ipRange{ | ||
{start: net.ParseIP("10.0.0.0"), end: net.ParseIP("10.255.255.255")}, | ||
{start: net.ParseIP("100.64.0.0"), end: net.ParseIP("100.127.255.255")}, | ||
{start: net.ParseIP("172.16.0.0"), end: net.ParseIP("172.31.255.255")}, | ||
{start: net.ParseIP("192.0.0.0"), end: net.ParseIP("192.0.0.255")}, | ||
{start: net.ParseIP("192.168.0.0"), end: net.ParseIP("192.168.255.255")}, | ||
{start: net.ParseIP("198.18.0.0"), end: net.ParseIP("198.19.255.255")}, | ||
} | ||
|
||
// Get returns real ip from the given request | ||
func Get(r *http.Request) (string, error) { | ||
|
||
for _, h := range []string{"X-Forwarded-For", "X-Real-Ip"} { | ||
addresses := strings.Split(r.Header.Get(h), ",") | ||
// march from right to left until we get a public address | ||
// that will be the address right before our proxy. | ||
for i := len(addresses) - 1; i >= 0; i-- { | ||
ip := strings.TrimSpace(addresses[i]) | ||
realIP := net.ParseIP(ip) | ||
if !realIP.IsGlobalUnicast() || isPrivateSubnet(realIP) { | ||
continue | ||
} | ||
return ip, nil | ||
} | ||
} | ||
|
||
// X-Forwarded-For header set but parsing failed above | ||
if r.Header.Get("X-Forwarded-For") != "" { | ||
return "", fmt.Errorf("no valid ip found") | ||
} | ||
|
||
// get IP from RemoteAddr | ||
ip, _, err := net.SplitHostPort(r.RemoteAddr) | ||
if err != nil { | ||
return "", fmt.Errorf("can't parse ip %q: %w", r.RemoteAddr, err) | ||
} | ||
if netIP := net.ParseIP(ip); netIP == nil { | ||
return "", fmt.Errorf("no valid ip found") | ||
} | ||
|
||
return ip, nil | ||
} | ||
|
||
// inRange - check to see if a given ip address is within a range given | ||
func inRange(r ipRange, ipAddress net.IP) bool { | ||
// strcmp type byte comparison | ||
if bytes.Compare(ipAddress, r.start) >= 0 && bytes.Compare(ipAddress, r.end) < 0 { | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
// isPrivateSubnet - check to see if this ip is in a private subnet | ||
func isPrivateSubnet(ipAddress net.IP) bool { | ||
if ipCheck := ipAddress.To4(); ipCheck != nil { | ||
for _, r := range privateRanges { | ||
// check if this ip is in a private range | ||
if inRange(r, ipAddress) { | ||
return true | ||
} | ||
} | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package realip | ||
|
||
import ( | ||
"log" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestGetFromHeaders(t *testing.T) { | ||
{ | ||
req, err := http.NewRequest("GET", "/something", http.NoBody) | ||
assert.NoError(t, err) | ||
req.Header.Add("Something", "1234567") | ||
req.Header.Add("X-Real-IP", "8.8.8.8") | ||
adr, err := Get(req) | ||
require.NoError(t, err) | ||
assert.Equal(t, "8.8.8.8", adr) | ||
} | ||
{ | ||
req, err := http.NewRequest("GET", "/something", http.NoBody) | ||
assert.NoError(t, err) | ||
req.Header.Add("Something", "1234567") | ||
req.Header.Add("X-Forwarded-For", "8.8.8.8,1.1.1.2, 30.30.30.1") | ||
adr, err := Get(req) | ||
require.NoError(t, err) | ||
assert.Equal(t, "30.30.30.1", adr) | ||
} | ||
{ | ||
req, err := http.NewRequest("GET", "/something", http.NoBody) | ||
assert.NoError(t, err) | ||
req.Header.Add("Something", "1234567") | ||
req.Header.Add("X-Forwarded-For", "8.8.8.8,1.1.1.2,192.168.1.1,10.0.0.65") | ||
adr, err := Get(req) | ||
require.NoError(t, err) | ||
assert.Equal(t, "1.1.1.2", adr) | ||
} | ||
{ | ||
req, err := http.NewRequest("GET", "/something", http.NoBody) | ||
assert.NoError(t, err) | ||
req.Header.Add("Something", "1234567") | ||
req.Header.Add("X-Forwarded-For", "30.30.30.1") | ||
req.Header.Add("X-Real-Ip", "10.0.0.1") | ||
adr, err := Get(req) | ||
require.NoError(t, err) | ||
assert.Equal(t, "30.30.30.1", adr) | ||
} | ||
{ | ||
req, err := http.NewRequest("GET", "/something", http.NoBody) | ||
assert.NoError(t, err) | ||
req.Header.Add("Something", "1234567") | ||
req.Header.Add("X-Forwarded-For", "30.30.30.1") | ||
req.Header.Add("X-Real-Ip", "8.8.8.8") | ||
adr, err := Get(req) | ||
require.NoError(t, err) | ||
assert.Equal(t, "30.30.30.1", adr) | ||
} | ||
{ | ||
req, err := http.NewRequest("GET", "/something", http.NoBody) | ||
assert.NoError(t, err) | ||
req.Header.Add("Something", "1234567") | ||
req.Header.Add("X-Forwarded-For", "10.0.0.2,192.168.1.1") | ||
req.Header.Add("X-Real-Ip", "8.8.8.8") | ||
adr, err := Get(req) | ||
require.NoError(t, err) | ||
assert.Equal(t, "8.8.8.8", adr) | ||
} | ||
{ | ||
req, err := http.NewRequest("GET", "/something", http.NoBody) | ||
assert.NoError(t, err) | ||
ip, err := Get(req) | ||
assert.Error(t, err) | ||
assert.Equal(t, "", ip) | ||
} | ||
} | ||
|
||
func TestGetFromRemoteAddr(t *testing.T) { | ||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
log.Printf("%v", r) | ||
adr, err := Get(r) | ||
require.NoError(t, err) | ||
assert.Equal(t, "127.0.0.1", adr) | ||
})) | ||
|
||
req, err := http.NewRequest("GET", ts.URL+"/something", http.NoBody) | ||
require.NoError(t, err) | ||
client := http.Client{Timeout: time.Second} | ||
_, err = client.Do(req) | ||
require.NoError(t, err) | ||
} |