1212public class ParsedFirewallLists {
1313 private final List <IPEntry > blockedIps = new ArrayList <>();
1414 private final List <IPEntry > allowedIps = new ArrayList <>();
15- private final List <BlockedUAEntry > blockedUserAgents = new ArrayList <>();
15+ private final List <UADetailsEntry > uaDetails = new ArrayList <>();
16+ private Pattern blockedUserAgents = null ;
17+ private Pattern monitoredUserAgents = null ;
1618
1719 public ParsedFirewallLists () {
1820
@@ -41,20 +43,35 @@ public boolean matchesAllowedIps(String ip) {
4143 return false ;
4244 }
4345
44- public List <Match > matchBlockedUserAgents (String userAgent ) {
45- List <Match > matches = new ArrayList <>();
46- for (BlockedUAEntry entry : this .blockedUserAgents ) {
46+ public UABlockedResult matchBlockedUserAgents (String userAgent ) {
47+ boolean isBlocked = false ;
48+ if (blockedUserAgents != null )
49+ isBlocked = blockedUserAgents .matcher (userAgent ).find ();
50+
51+ boolean isMonitored = false ;
52+ if (monitoredUserAgents != null )
53+ isMonitored = monitoredUserAgents .matcher (userAgent ).find ();
54+
55+ if (!isMonitored && !isBlocked )
56+ // only run the more detailed matches if it's an actual attack/monitored.
57+ return new UABlockedResult (false , List .of ());
58+
59+ List <String > matchedUAKeys = new ArrayList <>();
60+ for (UADetailsEntry entry : this .uaDetails ) {
4761 if (entry .pattern ().matcher (userAgent ).find ()) {
48- matches .add (new Match ( entry .key (), ! entry . monitor (), null ));
62+ matchedUAKeys .add (entry .key ());
4963 }
5064 }
51- return matches ;
65+ return new UABlockedResult ( isBlocked , matchedUAKeys ) ;
5266 }
5367
5468 public void update (ReportingApi .APIListsResponse response ) {
5569 updateBlockedIps (response .blockedIPAddresses ());
70+ updateMonitoredIps (response .monitoredIPAddresses ());
5671 updateAllowedIps (response .allowedIPAddresses ());
57- updateBlockedUserAgents (response .blockedUserAgents ());
72+
73+ updateBlockedAndMonitoredUAs (response .blockedUserAgents (), response .monitoredUserAgents ());
74+ updateUADetails (response .userAgentDetails ());
5875 }
5976
6077 public void updateBlockedIps (List <ReportingApi .ListsResponseEntry > blockedIpsList ) {
@@ -63,7 +80,16 @@ public void updateBlockedIps(List<ReportingApi.ListsResponseEntry> blockedIpsLis
6380 return ;
6481 for (ReportingApi .ListsResponseEntry entry : blockedIpsList ) {
6582 IPList ipList = createIPList (entry .ips ());
66- blockedIps .add (new IPEntry (entry .monitor (), entry .key (), entry .source (), entry .description (), ipList ));
83+ blockedIps .add (new IPEntry (/* monitor */ false , entry .key (), entry .source (), entry .description (), ipList ));
84+ }
85+ }
86+
87+ public void updateMonitoredIps (List <ReportingApi .ListsResponseEntry > monitoredIpsList ) {
88+ if (monitoredIpsList == null )
89+ return ;
90+ for (ReportingApi .ListsResponseEntry entry : monitoredIpsList ) {
91+ IPList ipList = createIPList (entry .ips ());
92+ blockedIps .add (new IPEntry (/* monitor */ true , entry .key (), entry .source (), entry .description (), ipList ));
6793 }
6894 }
6995
@@ -73,26 +99,43 @@ public void updateAllowedIps(List<ReportingApi.ListsResponseEntry> allowedIpsLis
7399 return ;
74100 for (ReportingApi .ListsResponseEntry entry : allowedIpsList ) {
75101 IPList ipList = createIPList (entry .ips ());
76- allowedIps .add (new IPEntry (entry .monitor (), entry .key (), entry .source (), entry .description (), ipList ));
102+ boolean shouldMonitor = false ; // we don't monitor allowed ips
103+ allowedIps .add (new IPEntry (shouldMonitor , entry .key (), entry .source (), entry .description (), ipList ));
77104 }
78105 }
79106
80- public void updateBlockedUserAgents (List <ReportingApi .BotBlocklist > blockedUserAgentsList ) {
81- blockedUserAgents .clear ();
82- if (blockedUserAgentsList == null )
107+ public void updateUADetails (List <ReportingApi .UserAgentDetail > userAgentDetails ) {
108+ this . uaDetails .clear ();
109+ if (userAgentDetails == null )
83110 return ;
84- for (ReportingApi .BotBlocklist entry : blockedUserAgentsList ) {
111+ for (ReportingApi .UserAgentDetail entry : userAgentDetails ) {
85112 Pattern pattern = Pattern .compile (entry .pattern (), Pattern .CASE_INSENSITIVE );
86- blockedUserAgents . add (new BlockedUAEntry ( entry . monitor (), entry .key (), pattern ));
113+ this . uaDetails . add (new UADetailsEntry ( entry .key (), pattern ));
87114 }
88115 }
89116
117+ public void updateBlockedAndMonitoredUAs (String blockedUAs , String monitoredUAs ) {
118+ this .blockedUserAgents = null ;
119+ if (blockedUAs != null && !blockedUAs .isEmpty ()) {
120+ this .blockedUserAgents = Pattern .compile (blockedUAs , Pattern .CASE_INSENSITIVE );
121+ }
122+
123+ this .monitoredUserAgents = null ;
124+ if (monitoredUAs != null && !monitoredUAs .isEmpty ()) {
125+ this .monitoredUserAgents = Pattern .compile (monitoredUAs , Pattern .CASE_INSENSITIVE );
126+ }
127+ }
128+
129+
90130 public record Match (String key , boolean block , String description ) {
91131 }
92132
133+ public record UABlockedResult (boolean block , List <String > matchedKeys ) {
134+ }
135+
93136 private record IPEntry (boolean monitor , String key , String source , String description , IPList ips ) {
94137 }
95138
96- private record BlockedUAEntry ( boolean monitor , String key , Pattern pattern ) {
139+ private record UADetailsEntry ( String key , Pattern pattern ) {
97140 }
98141}
0 commit comments