2016-02-06 14:41:41 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2016-02-06 14:47:47 +01:00
|
|
|
# Copyright Grégory Soutadé 2016
|
2016-02-06 14:41:41 +01:00
|
|
|
|
|
|
|
# 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
|
|
|
|
|
|
|
|
from iwla import IWLA
|
|
|
|
from iplugin import IPlugin
|
|
|
|
from display import *
|
|
|
|
|
|
|
|
"""
|
|
|
|
Display hook
|
|
|
|
|
|
|
|
Add geo statistics
|
|
|
|
|
|
|
|
Plugin requirements :
|
|
|
|
post_analysis/ip_to_geo
|
|
|
|
|
|
|
|
Conf values needed :
|
|
|
|
create_geo_page*
|
|
|
|
|
|
|
|
Output files :
|
|
|
|
OUTPUT_ROOT/year/month/index.html
|
|
|
|
|
|
|
|
Statistics creation :
|
|
|
|
None
|
|
|
|
|
|
|
|
Statistics update :
|
|
|
|
None
|
|
|
|
|
|
|
|
Statistics deletion :
|
|
|
|
None
|
|
|
|
"""
|
|
|
|
|
2022-11-10 20:04:45 +01:00
|
|
|
class IWLADisplayIPToGeo(IPlugin):
|
2016-02-06 14:41:41 +01:00
|
|
|
def __init__(self, iwla):
|
2022-11-10 20:04:45 +01:00
|
|
|
super(IWLADisplayIPToGeo, self).__init__(iwla)
|
2016-02-06 14:41:41 +01:00
|
|
|
self.API_VERSION = 1
|
2022-11-10 20:04:45 +01:00
|
|
|
self.requires = ['IWLAPostAnalysisIPToGeo']
|
2016-02-06 14:41:41 +01:00
|
|
|
|
|
|
|
def load(self):
|
|
|
|
self.icon_path = self.iwla.getConfValue('icon_path', '/')
|
|
|
|
self.create_geo_page = self.iwla.getConfValue('create_geo_page_page', True)
|
|
|
|
|
|
|
|
display = self.iwla.getDisplay()
|
|
|
|
display.addColumnFilter(self.iwla._(u'Host'), self.FlagFilter, {'self':self})
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
2016-04-12 08:17:34 +02:00
|
|
|
@staticmethod # Needed to have unbound method
|
2016-02-06 14:41:41 +01:00
|
|
|
def FlagFilter(host, self):
|
|
|
|
cc = None
|
|
|
|
host_name = host.split(' ')[0] # hostname or ip
|
2017-09-06 07:43:55 +02:00
|
|
|
if host_name in self.visitors.keys():
|
|
|
|
cc = self.visitors[host_name].get('country_code', None)
|
2016-02-06 14:41:41 +01:00
|
|
|
else:
|
2017-09-06 07:43:55 +02:00
|
|
|
for visitor in self.visitors.values():
|
2016-02-06 14:41:41 +01:00
|
|
|
if visitor['remote_addr'] == host_name:
|
2016-02-07 10:17:32 +01:00
|
|
|
cc = visitor.get('country_code', None)
|
2016-02-06 14:41:41 +01:00
|
|
|
break
|
|
|
|
if not cc or cc == 'ip': return None
|
2016-04-12 09:35:03 +02:00
|
|
|
icon = '<img alt="%s flag" src="/%s/flags/%s.png"/>' % (cc, self.icon_path, cc)
|
2016-02-06 14:41:41 +01:00
|
|
|
return '%s %s' % (icon ,host)
|
|
|
|
|
|
|
|
def hook(self):
|
|
|
|
display = self.iwla.getDisplay()
|
2017-08-23 20:10:15 +02:00
|
|
|
geo = self.iwla.getMonthStats().get('geo', {})
|
2016-02-06 14:41:41 +01:00
|
|
|
geo = sorted(geo.items(), key=lambda t: t[1], reverse=True)
|
2017-09-06 07:43:55 +02:00
|
|
|
self.visitors = self.iwla.getCurrentVisits()
|
2016-02-06 14:41:41 +01:00
|
|
|
|
|
|
|
# All in a page
|
|
|
|
if self.create_geo_page:
|
|
|
|
title = createCurTitle(self.iwla, u'All Coutries')
|
|
|
|
filename = 'geo.html'
|
|
|
|
path = self.iwla.getCurDisplayPath(filename)
|
|
|
|
|
|
|
|
page = display.createPage(title, path, self.iwla.getConfValue('css_path', []))
|
|
|
|
table = display.createBlock(DisplayHTMLBlockTable, self.iwla._(u'Countries'), ['', self.iwla._(u'Country'), self.iwla._(u'Visitors')])
|
|
|
|
table.setColsCSSClass(['', '', 'iwla_hit'])
|
|
|
|
for (cc, visitors) in geo:
|
2016-04-12 09:35:03 +02:00
|
|
|
icon = '<img alt="%s flag" src="/%s/flags/%s.png"/>' % (cc, self.icon_path, cc)
|
2016-02-06 14:41:41 +01:00
|
|
|
table.appendRow([icon, cc, visitors])
|
|
|
|
table.computeRatio(2)
|
|
|
|
page.appendBlock(table)
|
|
|
|
|
|
|
|
display.addPage(page)
|
|
|
|
|
|
|
|
# Countries in index
|
|
|
|
title = self.iwla._(u'Countries')
|
|
|
|
if self.create_geo_page:
|
2022-11-10 20:04:45 +01:00
|
|
|
link = '<a href=\'%s\'>%s</a>' % (filename, self.iwla._(u'All countries'))
|
2016-02-06 14:41:41 +01:00
|
|
|
title = '%s - %s' % (title, link)
|
|
|
|
|
|
|
|
index = self.iwla.getDisplayIndex()
|
|
|
|
|
|
|
|
table = display.createBlock(DisplayHTMLBlockTable, title, ['', self.iwla._(u'Countries'), self.iwla._(u'Visitors')])
|
|
|
|
table.setColsCSSClass(['', '', 'iwla_hit'])
|
|
|
|
for (cc, visitors) in geo[:10]:
|
2016-04-12 09:35:03 +02:00
|
|
|
icon = '<img alt="%s flag" src="/%s/flags/%s.png"/>' % (cc, self.icon_path, cc)
|
2016-02-06 14:41:41 +01:00
|
|
|
table.appendRow([icon, cc, visitors])
|
|
|
|
table.computeRatio(2)
|
|
|
|
index.appendBlock(table)
|