mirror of
https://github.com/Radarr/Radarr.git
synced 2024-08-18 08:19:38 +02:00
Cleanse remote IP Address from trace log file
This commit is contained in:
parent
1fcb927b3b
commit
9ca291ecaf
@ -57,5 +57,26 @@ public void should_clean_message(string message)
|
||||
cleansedMessage.Should().NotContain("mySecret");
|
||||
cleansedMessage.Should().NotContain("01233210");
|
||||
}
|
||||
|
||||
[TestCase(@"Some message (from 32.2.3.5 user agent)")]
|
||||
[TestCase(@"Auth-Invalidated ip 32.2.3.5")]
|
||||
[TestCase(@"Auth-Success ip 32.2.3.5")]
|
||||
[TestCase(@"Auth-Logout ip 32.2.3.5")]
|
||||
public void should_clean_ipaddress(string message)
|
||||
{
|
||||
var cleansedMessage = CleanseLogMessage.Cleanse(message);
|
||||
|
||||
cleansedMessage.Should().NotContain(".2.3.");
|
||||
}
|
||||
|
||||
[TestCase(@"Some message (from 10.2.3.2 user agent)")]
|
||||
[TestCase(@"Auth-Unauthorized ip 32.2.3.5")]
|
||||
[TestCase(@"Auth-Failure ip 32.2.3.5")]
|
||||
public void should_not_clean_ipaddress(string message)
|
||||
{
|
||||
var cleansedMessage = CleanseLogMessage.Cleanse(message);
|
||||
|
||||
cleansedMessage.Should().Be(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,6 @@
|
||||
using System.Linq;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text.RegularExpressions;
|
||||
using NzbDrone.Common.Extensions;
|
||||
|
||||
@ -41,6 +43,8 @@ public class CleanseLogMessage
|
||||
new Regex(@"(?<=\?|&)(authkey|torrent_pass)=(?<secret>[^&=]+?)(?=""|&|$)", RegexOptions.Compiled | RegexOptions.IgnoreCase)
|
||||
};
|
||||
|
||||
private static readonly Regex CleanseRemoteIPRegex = new Regex(@"(?:Auth-\w+(?<!Failure|Unauthorized) ip|from) (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})", RegexOptions.Compiled);
|
||||
|
||||
public static string Cleanse(string message)
|
||||
{
|
||||
if (message.IsNullOrWhiteSpace())
|
||||
@ -62,7 +66,27 @@ public static string Cleanse(string message)
|
||||
});
|
||||
}
|
||||
|
||||
message = CleanseRemoteIPRegex.Replace(message, CleanseRemoteIP);
|
||||
|
||||
return message;
|
||||
}
|
||||
|
||||
private static string CleanseRemoteIP(Match match)
|
||||
{
|
||||
var group = match.Groups[1];
|
||||
var valueAll = match.Value;
|
||||
var valueIP = group.Value;
|
||||
|
||||
if (IPAddress.TryParse(valueIP, out var address) && !address.IsLocalAddress())
|
||||
{
|
||||
var prefix = match.Value.Substring(0, group.Index - match.Index);
|
||||
var postfix = match.Value.Substring(group.Index + group.Length - match.Index);
|
||||
var items = valueIP.Split('.');
|
||||
|
||||
return $"{prefix}{items[0]}.*.*.{items[3]}{postfix}";
|
||||
}
|
||||
|
||||
return match.Value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user