Hi SmarterTools
Have a look at the following description on how to implement support for proxies and the X-Forwarded-For header in the newest version of SmarterMail (.NET 8), so logging, displaying etc. of the reel clients IP address are correct whenever the client are jumping through Cloudflare, HA Proxies etc:
Configure services for Mailservice.exe:
services.Configure<ForwardedHeadersOptions>(options =>
{
var knownProxyNetworks = configuration.GetValue("KnownProxyNetworks", "").Split(new[]{',',';'}, StringSplitOptions.RemoveEmptyEntries);
var networkList = knownProxyNetworks.Select(x => x.ToIpNetwork());
options.ForwardedHeaders = ForwardedHeaders.All;
options.KnownNetworks.AddRange(networkList);
});
Add middleware:
app.UseForwardedHeaders();
Make use of the following extension method:
public static class NetworkExtensions
{
public static IPNetwork ToIpNetwork(this string cidr)
{
try
{
var delimiterIndex = cidr.IndexOf('/');
var ipSubnet = cidr.Substring(0, delimiterIndex);
var mask = cidr.Substring(delimiterIndex + 1);
var prefixLength = int.Parse(mask);
var subnetAddress = IPAddress.Parse(ipSubnet);
return new IPNetwork(subnetAddress, prefixLength);
}
catch
{
return new IPNetwork(IPAddress.Parse("127.0.0.1"), 32);
}
}
}
Configure the Known Proxy Networks:
This is needed so not everyone can inject a fake IP in the X-Forwarded-For header.
So now you just need to pull the "KnownProxyNetworks" value from the system configuration :)
Eg. KnownProxyNetworks = "192.168.10.5/32;192.168.10.13/32"