iwla/plugins/display/visitor_ip.py

86 lines
2.3 KiB
Python
Raw Normal View History

2023-05-21 11:06:16 +02:00
# -*- coding: utf-8 -*-
#
# Copyright Grégory Soutadé 2023
# 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 ipaddress import ip_address
from iwla import IWLA
from iplugin import IPlugin
from display import *
"""
Display hook
Display IP below visitor name
Plugin requirements :
None
Conf values needed :
compact_ip*
Output files :
OUTPUT_ROOT/year/month/index.html
Statistics creation :
None
Statistics update :
None
Statistics deletion :
None
"""
class IWLADisplayVisitorIP(IPlugin):
def load(self):
display = self.iwla.getDisplay()
display.addColumnFilter(self.iwla._(u'Host'), self.IPFilter, {'self':self})
self.compact_ip = self.iwla.getConfValue('compact_ip', False)
return True
def processIP(self, host_name, ip):
host_name = host_name.replace(ip, 'IP')
# IPv4
2023-05-21 11:06:16 +02:00
ip = ip.replace('.', '-')
# IPv6
2023-05-21 11:06:16 +02:00
ip = ip.replace(':', '-')
host_name = host_name.replace(ip, 'IP')
ip = ip.replace('-', '')
host_name = host_name.replace(ip, 'IP')
return host_name
@staticmethod # Needed to have unbound method
def IPFilter(host, remote_ip, self):
if remote_ip is None or not remote_ip in self.visitors.keys(): return None
visitor = self.visitors[remote_ip]
if remote_ip == visitor['remote_addr']: return None
host_name = host
if self.compact_ip:
host_name = self.processIP(host_name, visitor['remote_ip'])
host_name = self.processIP(host_name,
ip_address(visitor['remote_ip']).exploded)
return '%s [%s]' % (host_name, visitor['remote_ip'])
def hook(self):
self.visitors = self.iwla.getCurrentVisits()