2015-02-17 19:11:04 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# Copyright Grégory Soutadé 2015
|
|
|
|
|
|
|
|
# This file is part of iwla
|
|
|
|
|
|
|
|
# iwla is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# iwla is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with iwla. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
|
|
|
|
import re
|
2024-07-28 09:25:06 +02:00
|
|
|
import time
|
2015-02-17 19:11:04 +01:00
|
|
|
|
|
|
|
from iwla import IWLA
|
|
|
|
from iplugin import IPlugin
|
|
|
|
|
|
|
|
"""
|
|
|
|
Post analysis hook
|
|
|
|
|
|
|
|
Find feeds parsers (first hit in feeds conf value and no viewed pages if it's a robot)
|
2022-11-12 19:00:14 +01:00
|
|
|
If merge_feeds_parsers is set to True, merge feeds parsers with the same user agent
|
2015-02-18 20:32:04 +01:00
|
|
|
as it must be the same person with a different IP address.
|
2015-02-17 19:11:04 +01:00
|
|
|
|
2023-01-28 09:36:48 +01:00
|
|
|
Warning : When merge_feeds_parsers is activated, last access display date is the more
|
|
|
|
recent date of all merged parsers found
|
|
|
|
|
2015-02-17 19:11:04 +01:00
|
|
|
Plugin requirements :
|
|
|
|
None
|
|
|
|
|
|
|
|
Conf values needed :
|
|
|
|
feeds
|
2023-03-11 20:42:56 +01:00
|
|
|
feeds_agents*
|
2022-11-12 19:00:14 +01:00
|
|
|
merge_feeds_parsers*
|
2015-02-17 19:11:04 +01:00
|
|
|
|
|
|
|
Output files :
|
|
|
|
None
|
|
|
|
|
|
|
|
Statistics creation :
|
2023-05-21 11:04:40 +02:00
|
|
|
remote_ip =>
|
2015-02-17 19:11:04 +01:00
|
|
|
feed_parser
|
2024-07-28 09:25:06 +02:00
|
|
|
feed_name_analyzed
|
2023-03-11 20:42:56 +01:00
|
|
|
feed_parser_last_access (for merged parser)
|
2024-07-28 09:25:06 +02:00
|
|
|
feed_domain
|
|
|
|
feed_uri
|
|
|
|
feed_subscribers
|
2015-02-17 19:11:04 +01:00
|
|
|
|
|
|
|
Statistics update :
|
|
|
|
None
|
|
|
|
|
|
|
|
Statistics deletion :
|
|
|
|
None
|
|
|
|
"""
|
|
|
|
|
|
|
|
class IWLAPostAnalysisFeeds(IPlugin):
|
2015-02-18 20:56:03 +01:00
|
|
|
NOT_A_FEED_PARSER = 0
|
|
|
|
FEED_PARSER = 1
|
|
|
|
MERGED_FEED_PARSER = 2
|
2016-08-20 13:08:02 +02:00
|
|
|
BAD_FEED_PARSER = 3
|
2015-02-18 20:56:03 +01:00
|
|
|
|
2015-02-17 19:11:04 +01:00
|
|
|
def __init__(self, iwla):
|
|
|
|
super(IWLAPostAnalysisFeeds, self).__init__(iwla)
|
|
|
|
self.API_VERSION = 1
|
2015-02-18 20:56:03 +01:00
|
|
|
self.conf_requires = ['feeds']
|
2015-02-17 19:11:04 +01:00
|
|
|
|
|
|
|
def load(self):
|
2021-06-03 08:50:46 +02:00
|
|
|
feeds = self.iwla.getConfValue('feeds', [])
|
2023-03-11 20:42:56 +01:00
|
|
|
feeds_agents = self.iwla.getConfValue('feeds_agents', [])
|
2022-11-12 19:00:14 +01:00
|
|
|
self.merge_feeds_parsers = self.iwla.getConfValue('merge_feeds_parsers', False)
|
2022-11-16 21:09:50 +01:00
|
|
|
_merge_feeds_parsers_list = self.iwla.getConfValue('merge_feeds_parsers_list', [])
|
2015-02-17 19:11:04 +01:00
|
|
|
|
|
|
|
if feeds is None: return False
|
|
|
|
|
|
|
|
self.feeds_re = []
|
|
|
|
for f in feeds:
|
2021-06-03 08:50:46 +02:00
|
|
|
self.feeds_re.append(re.compile(f))
|
2015-02-17 19:11:04 +01:00
|
|
|
|
2016-08-20 13:08:02 +02:00
|
|
|
self.bad_feeds_re = []
|
|
|
|
self.bad_feeds_re.append(re.compile(r'.*crawl.*'))
|
|
|
|
|
2021-04-01 08:22:52 +02:00
|
|
|
self.user_agents_re = []
|
|
|
|
self.user_agents_re.append(re.compile(r'.*rss.*'))
|
|
|
|
self.user_agents_re.append(re.compile(r'.*atom.*'))
|
|
|
|
self.user_agents_re.append(re.compile(r'.*feed.*'))
|
|
|
|
|
2023-03-11 20:42:56 +01:00
|
|
|
for f in feeds_agents:
|
|
|
|
self.user_agents_re.append(re.compile(f))
|
2021-06-03 08:50:46 +02:00
|
|
|
|
2024-07-28 09:25:06 +02:00
|
|
|
self.bad_user_agents_re = []
|
|
|
|
self.bad_user_agents_re.append(re.compile(r'.*feedback.*'))
|
|
|
|
|
|
|
|
self.subscribers_re = re.compile(r'.* ([0-9]+) subscriber.*')
|
|
|
|
|
2022-11-16 21:09:50 +01:00
|
|
|
self.merge_feeds_parsers_list = []
|
|
|
|
for f in _merge_feeds_parsers_list:
|
|
|
|
self.merge_feeds_parsers_list.append(re.compile(f))
|
2024-07-28 09:25:06 +02:00
|
|
|
|
2022-11-16 21:09:50 +01:00
|
|
|
self.merged_feeds = {}
|
|
|
|
|
2015-02-17 19:11:04 +01:00
|
|
|
return True
|
2022-11-16 21:09:50 +01:00
|
|
|
|
|
|
|
def _appendToMergeCache(self, isFeedParser, key, hit):
|
2024-07-28 09:25:06 +02:00
|
|
|
hit['feed_parser'] = isFeedParser
|
2022-11-16 21:09:50 +01:00
|
|
|
# First time, register into dict
|
|
|
|
if self.merged_feeds.get(key, None) is None:
|
|
|
|
# Merged
|
|
|
|
self.merged_feeds[key] = hit
|
2023-01-28 09:36:48 +01:00
|
|
|
elif hit['remote_ip'] != self.merged_feeds[key]['remote_ip']:
|
2022-11-16 21:09:50 +01:00
|
|
|
# Next time
|
|
|
|
# Current must be ignored
|
|
|
|
hit['feed_parser'] = self.NOT_A_FEED_PARSER
|
2024-07-28 09:25:06 +02:00
|
|
|
merged_hit = hit
|
2023-01-28 09:36:48 +01:00
|
|
|
last_access = hit['last_access']
|
2022-11-16 21:09:50 +01:00
|
|
|
# Previous matched hit must be set as merged
|
|
|
|
hit = self.merged_feeds[key]
|
2024-07-28 09:25:06 +02:00
|
|
|
hit['feed_parser'] = self.MERGED_FEED_PARSER
|
|
|
|
hit['viewed_pages'][0] += merged_hit['viewed_pages'][0]
|
|
|
|
hit['viewed_hits'][0] += merged_hit['viewed_hits'][0]
|
|
|
|
hit['not_viewed_pages'][0] += merged_hit['not_viewed_pages'][0]
|
|
|
|
hit['not_viewed_hits'][0] += merged_hit['not_viewed_hits'][0]
|
|
|
|
if hit['last_access'] < merged_hit['last_access']:
|
|
|
|
hit['feed_parser_last_access'] = merged_hit['last_access']
|
2023-01-28 09:36:48 +01:00
|
|
|
else:
|
|
|
|
hit['feed_parser_last_access'] = hit['last_access']
|
2022-11-16 21:09:50 +01:00
|
|
|
|
|
|
|
def mergeFeedsParsers(self, isFeedParser, hit):
|
2024-07-28 09:25:06 +02:00
|
|
|
if isFeedParser in (self.FEED_PARSER, self.MERGED_FEED_PARSER):
|
2023-01-28 09:36:48 +01:00
|
|
|
for r in self.merge_feeds_parsers_list:
|
|
|
|
if r.match(hit['remote_addr']) or r.match(hit['remote_ip']):
|
2024-07-28 09:25:06 +02:00
|
|
|
# One group can view multiple different feeds
|
|
|
|
key = r.pattern + hit.get('feed_domain', '') + hit.get('feed_uri', '')
|
|
|
|
self._appendToMergeCache(isFeedParser, key, hit)
|
2023-01-28 09:36:48 +01:00
|
|
|
return
|
2022-11-16 21:09:50 +01:00
|
|
|
#print("No match for %s : %d" % (hit['remote_addr'], hit['viewed_hits'][0] + hit['not_viewed_hits'][0]))
|
|
|
|
# Other cases, look for user agent
|
|
|
|
user_agent = hit['requests'][0]['http_user_agent'].lower()
|
|
|
|
self._appendToMergeCache(isFeedParser, user_agent, hit)
|
2015-02-18 20:32:04 +01:00
|
|
|
|
2015-02-17 19:11:04 +01:00
|
|
|
def hook(self):
|
2016-02-04 20:44:36 +01:00
|
|
|
hits = self.iwla.getCurrentVisits()
|
2015-02-17 19:11:04 +01:00
|
|
|
for hit in hits.values():
|
2015-02-18 20:32:04 +01:00
|
|
|
isFeedParser = hit.get('feed_parser', None)
|
|
|
|
|
2024-07-28 09:25:06 +02:00
|
|
|
if isFeedParser == self.NOT_A_FEED_PARSER:
|
2022-11-12 19:00:14 +01:00
|
|
|
continue
|
2024-07-28 09:25:06 +02:00
|
|
|
|
|
|
|
# Second time
|
2016-08-20 13:08:02 +02:00
|
|
|
if isFeedParser:
|
2024-07-28 09:25:06 +02:00
|
|
|
# Update last access time
|
|
|
|
if hit['last_access'] > hit.get('feed_parser_last_access', time.gmtime(0)):
|
|
|
|
hit['feed_parser_last_access'] = hit['last_access']
|
|
|
|
|
|
|
|
if not hit.get('feed_name_analyzed', False) and\
|
2016-08-20 13:08:02 +02:00
|
|
|
hit.get('dns_name_replaced', False):
|
2024-07-28 09:25:06 +02:00
|
|
|
hit['feed_name_analyzed'] = True
|
2016-08-20 13:08:02 +02:00
|
|
|
addr = hit.get('remote_addr', None)
|
|
|
|
for r in self.bad_feeds_re:
|
|
|
|
if r.match(addr):
|
2024-07-28 09:25:06 +02:00
|
|
|
hit['feed_parser'] = self.NOT_A_FEED_PARSER
|
2022-11-12 19:00:14 +01:00
|
|
|
break
|
2024-07-28 09:25:06 +02:00
|
|
|
|
|
|
|
# Register already tagged feed parser in merged_feeds
|
|
|
|
if self.merge_feeds_parsers:
|
|
|
|
self.mergeFeedsParsers(isFeedParser, hit)
|
2022-11-12 19:00:14 +01:00
|
|
|
continue
|
2015-02-18 20:32:04 +01:00
|
|
|
|
2021-06-03 08:50:46 +02:00
|
|
|
request = hit['requests'][0]
|
2015-02-18 20:56:03 +01:00
|
|
|
isFeedParser = self.NOT_A_FEED_PARSER
|
2021-06-03 08:50:46 +02:00
|
|
|
uri = request['extract_request']['extract_uri'].lower()
|
2015-02-17 19:11:04 +01:00
|
|
|
for regexp in self.feeds_re:
|
|
|
|
if regexp.match(uri):
|
2015-02-18 20:56:03 +01:00
|
|
|
isFeedParser = self.FEED_PARSER
|
2015-02-17 19:11:04 +01:00
|
|
|
# Robot that views pages -> bot
|
|
|
|
if hit['robot']:
|
2017-08-24 07:55:53 +02:00
|
|
|
if hit['not_viewed_pages'][0]:
|
2015-02-18 20:56:03 +01:00
|
|
|
isFeedParser = self.NOT_A_FEED_PARSER
|
2015-02-17 19:11:04 +01:00
|
|
|
break
|
2021-04-01 08:22:52 +02:00
|
|
|
|
2024-07-28 09:25:06 +02:00
|
|
|
user_agent = request['http_user_agent'].lower()
|
|
|
|
|
2021-04-01 08:22:52 +02:00
|
|
|
if isFeedParser == self.NOT_A_FEED_PARSER:
|
|
|
|
for regexp in self.user_agents_re:
|
|
|
|
if regexp.match(user_agent):
|
|
|
|
isFeedParser = self.FEED_PARSER
|
|
|
|
break
|
2024-07-28 09:25:06 +02:00
|
|
|
|
|
|
|
if isFeedParser == self.FEED_PARSER:
|
|
|
|
for regexp in self.bad_user_agents_re:
|
|
|
|
if regexp.match(user_agent):
|
|
|
|
isFeedParser = self.NOT_A_FEED_PARSER
|
|
|
|
break
|
|
|
|
|
|
|
|
if not hit.get('feed_name_analyzed', False) and\
|
|
|
|
hit.get('dns_name_replaced', False):
|
|
|
|
hit['feed_name_analyzed'] = True
|
|
|
|
addr = hit.get('remote_addr', None)
|
|
|
|
for r in self.bad_feeds_re:
|
|
|
|
if r.match(addr):
|
|
|
|
isFeedParser = hit['feed_parser'] = self.NOT_A_FEED_PARSER
|
|
|
|
break
|
|
|
|
|
|
|
|
if isFeedParser == self.FEED_PARSER:
|
|
|
|
hit['feed_domain'] = request['server_name']
|
|
|
|
hit['feed_uri'] = uri
|
|
|
|
hit['feed_subscribers'] = 0
|
|
|
|
|
|
|
|
subscribers = self.subscribers_re.match(user_agent)
|
|
|
|
if subscribers:
|
|
|
|
hit['feed_subscribers'] = int(subscribers.groups()[0])
|
|
|
|
|
|
|
|
hit['feed_parser'] = isFeedParser
|
2022-11-12 19:00:14 +01:00
|
|
|
if self.merge_feeds_parsers:
|
2022-11-16 21:09:50 +01:00
|
|
|
self.mergeFeedsParsers(isFeedParser, hit)
|