mirror of
https://github.com/miniflux/v2.git
synced 2025-06-27 16:36:00 +00:00
add support for ipv6 with zone index
This commit is contained in:
parent
864dd9f219
commit
05fd83bd6f
2 changed files with 36 additions and 4 deletions
|
@ -10,6 +10,14 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func dropIPv6zone(address string) string {
|
||||||
|
i := strings.IndexByte(address, '%')
|
||||||
|
if i != -1 {
|
||||||
|
address = address[:i]
|
||||||
|
}
|
||||||
|
return address
|
||||||
|
}
|
||||||
|
|
||||||
// FindClientIP returns client real IP address.
|
// FindClientIP returns client real IP address.
|
||||||
func FindClientIP(r *http.Request) string {
|
func FindClientIP(r *http.Request) string {
|
||||||
headers := []string{"X-Forwarded-For", "X-Real-Ip"}
|
headers := []string{"X-Forwarded-For", "X-Real-Ip"}
|
||||||
|
@ -19,6 +27,7 @@ func FindClientIP(r *http.Request) string {
|
||||||
if value != "" {
|
if value != "" {
|
||||||
addresses := strings.Split(value, ",")
|
addresses := strings.Split(value, ",")
|
||||||
address := strings.TrimSpace(addresses[0])
|
address := strings.TrimSpace(addresses[0])
|
||||||
|
address = dropIPv6zone(address)
|
||||||
|
|
||||||
if net.ParseIP(address) != nil {
|
if net.ParseIP(address) != nil {
|
||||||
return address
|
return address
|
||||||
|
@ -27,12 +36,11 @@ func FindClientIP(r *http.Request) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fallback to TCP/IP source IP address.
|
// Fallback to TCP/IP source IP address.
|
||||||
var remoteIP string
|
remoteIP, _, err := net.SplitHostPort(r.RemoteAddr)
|
||||||
if strings.ContainsRune(r.RemoteAddr, ':') {
|
if err != nil {
|
||||||
remoteIP, _, _ = net.SplitHostPort(r.RemoteAddr)
|
|
||||||
} else {
|
|
||||||
remoteIP = r.RemoteAddr
|
remoteIP = r.RemoteAddr
|
||||||
}
|
}
|
||||||
|
remoteIP = dropIPv6zone(remoteIP)
|
||||||
|
|
||||||
// When listening on a Unix socket, RemoteAddr is empty.
|
// When listening on a Unix socket, RemoteAddr is empty.
|
||||||
if remoteIP == "" {
|
if remoteIP == "" {
|
||||||
|
|
|
@ -19,6 +19,21 @@ func TestFindClientIPWithoutHeaders(t *testing.T) {
|
||||||
if ip := FindClientIP(r); ip != "192.168.0.1" {
|
if ip := FindClientIP(r); ip != "192.168.0.1" {
|
||||||
t.Fatalf(`Unexpected result, got: %q`, ip)
|
t.Fatalf(`Unexpected result, got: %q`, ip)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
r = &http.Request{RemoteAddr: "fe80::14c2:f039:edc7:edc7"}
|
||||||
|
if ip := FindClientIP(r); ip != "fe80::14c2:f039:edc7:edc7" {
|
||||||
|
t.Fatalf(`Unexpected result, got: %q`, ip)
|
||||||
|
}
|
||||||
|
|
||||||
|
r = &http.Request{RemoteAddr: "fe80::14c2:f039:edc7:edc7%eth0"}
|
||||||
|
if ip := FindClientIP(r); ip != "fe80::14c2:f039:edc7:edc7" {
|
||||||
|
t.Fatalf(`Unexpected result, got: %q`, ip)
|
||||||
|
}
|
||||||
|
|
||||||
|
r = &http.Request{RemoteAddr: "[fe80::14c2:f039:edc7:edc7%eth0]:4242"}
|
||||||
|
if ip := FindClientIP(r); ip != "fe80::14c2:f039:edc7:edc7" {
|
||||||
|
t.Fatalf(`Unexpected result, got: %q`, ip)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFindClientIPWithXFFHeader(t *testing.T) {
|
func TestFindClientIPWithXFFHeader(t *testing.T) {
|
||||||
|
@ -40,6 +55,15 @@ func TestFindClientIPWithXFFHeader(t *testing.T) {
|
||||||
t.Fatalf(`Unexpected result, got: %q`, ip)
|
t.Fatalf(`Unexpected result, got: %q`, ip)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Test with single IPv6 address with zone
|
||||||
|
headers = http.Header{}
|
||||||
|
headers.Set("X-Forwarded-For", "fe80::14c2:f039:edc7:edc7%eth0")
|
||||||
|
r = &http.Request{RemoteAddr: "192.168.0.1:4242", Header: headers}
|
||||||
|
|
||||||
|
if ip := FindClientIP(r); ip != "fe80::14c2:f039:edc7:edc7" {
|
||||||
|
t.Fatalf(`Unexpected result, got: %q`, ip)
|
||||||
|
}
|
||||||
|
|
||||||
// Test with single IPv4 address.
|
// Test with single IPv4 address.
|
||||||
headers = http.Header{}
|
headers = http.Header{}
|
||||||
headers.Set("X-Forwarded-For", "70.41.3.18")
|
headers.Set("X-Forwarded-For", "70.41.3.18")
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue