-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat: support forwarded headers for websocket #3235
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jclab-joseph
wants to merge
4
commits into
libp2p:master
Choose a base branch
from
jclab-joseph:feat/websocket-support-forwarded-headers
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,67 @@ | ||
package websocket | ||
|
||
import ( | ||
"github.com/stretchr/testify/require" | ||
"testing" | ||
) | ||
|
||
func TestIsProxyTrusted(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
trustedProxies []string | ||
remoteAddr string | ||
want bool | ||
}{ | ||
{ | ||
name: "Single IP trusted", | ||
trustedProxies: []string{"192.168.1.1"}, | ||
remoteAddr: "192.168.1.1:1234", | ||
want: true, | ||
}, | ||
{ | ||
name: "IP not in trusted list", | ||
trustedProxies: []string{"192.168.1.1"}, | ||
remoteAddr: "192.168.1.2:1234", | ||
want: false, | ||
}, | ||
{ | ||
name: "CIDR range trusted", | ||
trustedProxies: []string{"192.168.1.0/24"}, | ||
remoteAddr: "192.168.1.100:1234", | ||
want: true, | ||
}, | ||
{ | ||
name: "IPv6 address trusted", | ||
trustedProxies: []string{"2001:db8::1"}, | ||
remoteAddr: "[2001:db8::1]:1234", | ||
want: true, | ||
}, | ||
{ | ||
name: "IPv6 CIDR range trusted", | ||
trustedProxies: []string{"2001:db8::/32"}, | ||
remoteAddr: "[2001:db8:1:2:3:4:5:6]:1234", | ||
want: true, | ||
}, | ||
{ | ||
name: "Empty trusted proxies list", | ||
trustedProxies: []string{}, | ||
remoteAddr: "192.168.1.1:1234", | ||
want: true, // Everything is trusted when list is empty | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
transport := &WebsocketTransport{} | ||
err := WithAllowForwardedHeader(tt.trustedProxies)(transport) | ||
require.NoError(t, err) | ||
l := &listener{ | ||
trustedProxies: transport.trustedProxies, | ||
} | ||
got := l.isProxyTrusted(tt.remoteAddr) | ||
if got != tt.want { | ||
t.Errorf("isProxyTrusted() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains hidden or 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,79 @@ | ||
package websocket | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"net/http" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
func GetRealIP(addr net.Addr, h http.Header) string { | ||
remoteAddr := addr.String() | ||
remoteIp := GetRealIPFromHeader(h) | ||
if remoteIp != nil { | ||
remoteTcpAddr, ok := addr.(*net.TCPAddr) | ||
if ok { | ||
remoteAddr = IpPort(remoteIp, strconv.Itoa(remoteTcpAddr.Port)) | ||
} else { | ||
_, port, err := net.SplitHostPort(remoteAddr) | ||
if err == nil { | ||
remoteAddr = IpPort(remoteIp, port) | ||
} | ||
} | ||
} | ||
return remoteAddr | ||
} | ||
|
||
// GetRealIPFromHeader extracts the client's real IP address from HTTP request header. | ||
// It checks various proxy header to find the actual IP. | ||
func GetRealIPFromHeader(h http.Header) net.IP { | ||
// Check X-Real-IP header (used by Nginx and others) | ||
ipStr := h.Get("X-Real-IP") | ||
jclab-joseph marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
if ip := validateIp(ipStr); ip != nil { | ||
return ip | ||
} | ||
|
||
// Check X-Forwarded-For header (used by most proxies) | ||
// Format: client, proxy1, proxy2, ... | ||
ipStr = h.Get("X-Forwarded-For") | ||
if ipStr != "" { | ||
// Extract the first IP from the comma-separated list | ||
ips := strings.Split(ipStr, ",") | ||
for _, ipItem := range ips { | ||
ipItem = strings.TrimSpace(ipItem) | ||
if ip := validateIp(ipItem); ip != nil { | ||
return ip | ||
} | ||
} | ||
} | ||
|
||
// Check CF-Connecting-IP header (used by Cloudflare) | ||
ipStr = h.Get("CF-Connecting-IP") | ||
if ip := validateIp(ipStr); ip != nil { | ||
return ip | ||
} | ||
|
||
// Check True-Client-IP header (used by Akamai, Cloudflare, etc.) | ||
ipStr = h.Get("True-Client-IP") | ||
if ip := validateIp(ipStr); ip != nil { | ||
return ip | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// validateIp checks if a string is a valid IP address | ||
func validateIp(ip string) net.IP { | ||
if ip == "" { | ||
return nil | ||
} | ||
return net.ParseIP(ip) | ||
} | ||
|
||
func IpPort(ip net.IP, port string) string { | ||
if ip.To4() == nil { | ||
return fmt.Sprintf("[%s]:%s", ip.String(), port) | ||
} | ||
return fmt.Sprintf("%s:%s", ip.String(), port) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.