Skip to content
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

Fix ServerCallContext.Peer with IPv6 #824

Merged
merged 2 commits into from
Mar 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,25 @@ protected override string? PeerCore
{
get
{
// Follows the standard at https://github.com/grpc/grpc/blob/master/doc/naming.md
if (_peer == null)
{
var connection = HttpContext.Connection;
if (connection.RemoteIpAddress != null)
{
_peer = (connection.RemoteIpAddress.AddressFamily == AddressFamily.InterNetwork ? "ipv4:" : "ipv6:") + connection.RemoteIpAddress + ":" + connection.RemotePort;
switch (connection.RemoteIpAddress.AddressFamily)
{
case AddressFamily.InterNetwork:
_peer = "ipv4:" + connection.RemoteIpAddress + ":" + connection.RemotePort;
break;
case AddressFamily.InterNetworkV6:
_peer = "ipv6:[" + connection.RemoteIpAddress + "]:" + connection.RemotePort;
break;
default:
// TODO(JamesNK) - Test what should be output when used with UDS and named pipes
_peer = "unknown:" + connection.RemoteIpAddress + ":" + connection.RemotePort;
break;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ namespace Grpc.AspNetCore.Server.Tests
public class HttpContextServerCallContextTests
{
[TestCase("127.0.0.1", 50051, "ipv4:127.0.0.1:50051")]
[TestCase("::1", 50051, "ipv6:::1:50051")]
[TestCase("::1", 50051, "ipv6:[::1]:50051")]
public void Peer_FormatsRemoteAddressCorrectly(string ipAddress, int port, string expected)
{
// Arrange
Expand Down