Skip to content

Commit

Permalink
Merge pull request #20 from serilog-contrib/19-ipv6-address
Browse files Browse the repository at this point in the history
Fixed bug #19.
  • Loading branch information
mo-esmp authored Jun 1, 2023
2 parents 3f2d61a + b640a5a commit d1fa0e5
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 23 deletions.
18 changes: 4 additions & 14 deletions src/Serilog.Enrichers.ClientInfo/Enrichers/ClientIpEnricher.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Serilog.Core;
using Serilog.Events;
using System;
using System.Linq;
using System.Runtime.CompilerServices;

Expand Down Expand Up @@ -72,26 +71,17 @@ private string GetIpAddress()
{
var ipAddress = _contextAccessor.HttpContext?.Request?.Headers[ClinetIpConfiguration.XForwardHeaderName].FirstOrDefault();

if (!string.IsNullOrEmpty(ipAddress))
return GetIpAddressFromProxy(ipAddress);

return _contextAccessor.HttpContext?.Connection?.RemoteIpAddress?.ToString();
return !string.IsNullOrEmpty(ipAddress)
? GetIpAddressFromProxy(ipAddress)
: _contextAccessor.HttpContext?.Connection?.RemoteIpAddress?.ToString();
}
#endif

private string GetIpAddressFromProxy(string proxifiedIpList)
{
var addresses = proxifiedIpList.Split(',');

if (addresses.Length != 0)
{
// If IP contains port, it will be after the last : (IPv6 uses : as delimiter and could have more of them)
return addresses[0].Contains(":")
? addresses[0].Substring(0, addresses[0].LastIndexOf(":", StringComparison.Ordinal))
: addresses[0];
}

return string.Empty;
return addresses.Length == 0 ? string.Empty : addresses[0].Trim();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
<AssemblyName>Serilog.Enrichers.ClientInfo</AssemblyName>
<RootNamespace>Serilog</RootNamespace>
<TargetFrameworks>net462;netstandard2.0;netstandard2.1</TargetFrameworks>
<LangVersion>7.3</LangVersion>
<Version>1.3.0</Version>
<LangVersion>latest</LangVersion>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>

<PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard2.0'">
Expand Down
26 changes: 18 additions & 8 deletions test/Serilog.Enrichers.ClientInfo.Tests/ClientIpEnricherTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,16 @@ public ClientIpEnricherTests()
_contextAccessor.HttpContext.Returns(httpContext);
}

[Fact]
public void When_Enrich_Log_Event_With_IpEnricher_Should_Contain_ClientIp_Property()
[Theory]
[InlineData("::1")]
[InlineData("192.168.1.1")]
[InlineData("2001:0db8:85a3:0000:0000:8a2e:0370:7334")]
[InlineData("2001:db8:85a3:8d3:1319:8a2e:370:7348")]
public void When_Enrich_Log_Event_With_IpEnricher_Should_Contain_ClientIp_Property(string ip)
{
// Arrange
_contextAccessor.HttpContext.Connection.RemoteIpAddress = IPAddress.Parse("::1");
var ipAddress = IPAddress.Parse(ip);
_contextAccessor.HttpContext.Connection.RemoteIpAddress = ipAddress;

var ipEnricher = new ClientIpEnricher(_contextAccessor);

Expand All @@ -37,7 +42,7 @@ public void When_Enrich_Log_Event_With_IpEnricher_Should_Contain_ClientIp_Proper
// Assert
Assert.NotNull(evt);
Assert.True(evt.Properties.ContainsKey("ClientIp"));
Assert.Equal("::1", evt.Properties["ClientIp"].LiteralValue());
Assert.Equal(ipAddress.ToString(), evt.Properties["ClientIp"].LiteralValue());
}

[Fact]
Expand All @@ -63,12 +68,17 @@ public void When_Enrich_Log_Event_With_IpEnricher_And_Log_More_Than_Once_Should_
Assert.Equal(IPAddress.Loopback.ToString(), evt.Properties["ClientIp"].LiteralValue());
}

[Fact]
public void When_Enrich_Log_Event_With_IpEnricher_AndRequest_Contain_ForwardHeader_Should_Read_ClientIp_Value_From_Header_Value()
[Theory]
[InlineData("::1")]
[InlineData("192.168.1.1")]
[InlineData("2001:0db8:85a3:0000:0000:8a2e:0370:7334")]
[InlineData("2001:db8:85a3:8d3:1319:8a2e:370:7348")]
public void When_Enrich_Log_Event_With_IpEnricher_AndRequest_Contain_ForwardHeader_Should_Read_ClientIp_Value_From_Header_Value(string ip)
{
//Arrange
var ipAddress = IPAddress.Parse(ip);
_contextAccessor.HttpContext.Connection.RemoteIpAddress = IPAddress.Loopback;
_contextAccessor.HttpContext.Request.Headers.Add(ClinetIpConfiguration.XForwardHeaderName, IPAddress.Broadcast.ToString());
_contextAccessor.HttpContext.Request.Headers.Add(ClinetIpConfiguration.XForwardHeaderName, ipAddress.ToString());

var ipEnricher = new ClientIpEnricher(_contextAccessor);

Expand All @@ -84,7 +94,7 @@ public void When_Enrich_Log_Event_With_IpEnricher_AndRequest_Contain_ForwardHead
// Assert
Assert.NotNull(evt);
Assert.True(evt.Properties.ContainsKey("ClientIp"));
Assert.Equal(IPAddress.Broadcast.ToString(), evt.Properties["ClientIp"].LiteralValue());
Assert.Equal(ipAddress.ToString(), evt.Properties["ClientIp"].LiteralValue());
}

[Fact]
Expand Down

0 comments on commit d1fa0e5

Please sign in to comment.