2016-02-06 14:45:09 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# Copyright Grégory Soutadé 2016
|
|
|
|
|
|
|
|
# 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/>.
|
|
|
|
#
|
|
|
|
|
|
|
|
from iwla import IWLA
|
|
|
|
from iplugin import IPlugin
|
|
|
|
|
|
|
|
from iptogeo import IPToGeo
|
|
|
|
|
|
|
|
"""
|
|
|
|
Post analysis hook
|
|
|
|
|
|
|
|
Get country code from IP address
|
|
|
|
|
|
|
|
Plugin requirements :
|
|
|
|
None
|
|
|
|
|
|
|
|
Conf values needed :
|
|
|
|
iptogeo_remote_addr*
|
|
|
|
iptogeo_remote_port*
|
|
|
|
|
|
|
|
Output files :
|
|
|
|
None
|
|
|
|
|
|
|
|
Statistics creation :
|
|
|
|
geo =>
|
|
|
|
country_code => count
|
|
|
|
None
|
|
|
|
|
|
|
|
Statistics update :
|
|
|
|
valid_visitors:
|
|
|
|
country_code
|
|
|
|
|
|
|
|
Statistics deletion :
|
|
|
|
None
|
|
|
|
"""
|
|
|
|
|
|
|
|
class IWLAPostAnalysisIPToGeo(IPlugin):
|
|
|
|
|
|
|
|
def __init__(self, iwla):
|
|
|
|
super(IWLAPostAnalysisIPToGeo, self).__init__(iwla)
|
|
|
|
self.API_VERSION = 1
|
|
|
|
|
|
|
|
def load(self):
|
|
|
|
remote_addr = self.iwla.getConfValue('iptogeo_remote_addr',
|
|
|
|
None)
|
|
|
|
remote_port = self.iwla.getConfValue('iptogeo_remote_port',
|
|
|
|
None)
|
|
|
|
|
|
|
|
args = {}
|
|
|
|
if remote_addr: args['remote_addr'] = remote_addr
|
|
|
|
if remote_port: args['remote_port'] = remote_port
|
|
|
|
self.iptogeo = IPToGeo(**args)
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
def hook(self):
|
2017-09-06 07:43:55 +02:00
|
|
|
visitors = self.iwla.getCurrentVisits()
|
2016-02-06 14:45:09 +01:00
|
|
|
month_stats = self.iwla.getMonthStats()
|
|
|
|
|
|
|
|
geo = month_stats.get('geo', {})
|
|
|
|
|
|
|
|
for (ip, visitor) in visitors.items():
|
|
|
|
if visitor.get('country_code', False): continue
|
|
|
|
try:
|
|
|
|
(_, cc) = self.iptogeo.ip_to_geo(ip)
|
|
|
|
cc = cc and cc or 'ip'
|
|
|
|
visitor['country_code'] = cc
|
2019-08-30 07:50:54 +02:00
|
|
|
if not self.iwla.isValidVisitor(visitor):
|
|
|
|
continue
|
2016-02-06 14:45:09 +01:00
|
|
|
if cc in geo.keys():
|
|
|
|
geo[cc] += 1
|
|
|
|
else:
|
|
|
|
geo[cc] = 1
|
|
|
|
except Exception, e:
|
|
|
|
print e
|
|
|
|
|
|
|
|
month_stats['geo'] = geo
|