Filter users filter can now be a function defined in conf

This commit is contained in:
Gregory Soutade 2022-11-04 20:03:33 +01:00
parent 0e7b6c9131
commit c84373b2d7

View File

@ -37,8 +37,8 @@ Conf values needed :
filtered_ip : list of ip (string) filtered_ip : list of ip (string)
create_filtered_page* create_filtered_page*
Filter is a list of filter description combined by AND operator Filter can be a function or a list of filter description combined by AND operator
Filter description is a list of 3 elements : Filter description can be a function or a list of 3 elements :
* Field to match in visits * Field to match in visits
* Operator '=', '==', '!=', '>', '>=', '<', '<=' for int value * Operator '=', '==', '!=', '>', '>=', '<', '<=' for int value
@ -47,6 +47,19 @@ Filter description is a list of 3 elements :
For easiest config, you can indicate both 'remote_addr' or 'ip' in field element For easiest config, you can indicate both 'remote_addr' or 'ip' in field element
function prototype is func(iwla, hit) and must return True or False
Example :
def my_filter(iwla, hit):
return True
filtered_users = [
[['viewed_pages', '>=', '5'], ['viewed_hits', '>=', '5']],
[['viewed_hits', '>=', '5'], my_filter],
my_filter,
]
Output files : Output files :
None None
@ -91,8 +104,14 @@ class IWLAPostAnalysisFilterUsers(IPlugin):
self.filters = self.iwla.getConfValue('filtered_users', []) self.filters = self.iwla.getConfValue('filtered_users', [])
self.ip_filters = self.iwla.getConfValue('filtered_ip', []) self.ip_filters = self.iwla.getConfValue('filtered_ip', [])
for _filter in self.filters: for _filter in self.filters:
if type(_filter) == list:
for sub_filter in _filter: for sub_filter in _filter:
if not callable(sub_filter):
self._check_filter(sub_filter) self._check_filter(sub_filter)
elif callable(_filter):
continue
else:
raise Exception(f'Invalid filter {_filter}')
return True return True
def __do_filter(self, hit, _filter): def __do_filter(self, hit, _filter):
@ -124,9 +143,18 @@ class IWLAPostAnalysisFilterUsers(IPlugin):
return True return True
for _filter in self.filters: for _filter in self.filters:
if callable(_filter):
if _filter(self.iwla, hits[ip]):
return True
continue
# Must match all sub filters
filtered = True filtered = True
for sub_filter in _filter: for sub_filter in _filter:
if not self.__do_filter(hits[ip], sub_filter): if callable(sub_filter):
if not sub_filter(self.iwla, hits[ip]):
filtered = False
break
elif not self.__do_filter(hits[ip], sub_filter):
filtered = False filtered = False
break break
if filtered: if filtered:
@ -134,6 +162,9 @@ class IWLAPostAnalysisFilterUsers(IPlugin):
return False return False
def hook(self): def hook(self):
if not len(self.filters) and not len(self.ip_filters):
return
hits = self.iwla.getValidVisitors() hits = self.iwla.getValidVisitors()
if len(self.filters) or len(self.ip_filters): if len(self.filters) or len(self.ip_filters):