<s> import os <EOL> import json <EOL> import logging <EOL> import subprocess <EOL> from ajenti . api import * <EOL> from ajenti . plugins . main . api import SectionPlugin <EOL> from ajenti . ui import on <EOL> from ajenti . ui . binder import Binder <EOL> from templates import config_templates <EOL> config_dirs = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> config_file_types = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> class f2b_Config ( object ) : <EOL> def __init__ ( self , name , path ) : <EOL> self . name = name <EOL> self . path = path <EOL> self . configfile = os . path . join ( path , name ) <EOL> self . config = '<STR_LIT>' <EOL> def __repr__ ( self ) : <EOL> return json . dumps ( { '<STR_LIT:name>' : self . name , '<STR_LIT>' : self . configfile , '<STR_LIT>' : self . config } ) <EOL> def save ( self ) : <EOL> self . configfile = os . path . join ( self . path , self . name ) <EOL> try : <EOL> open ( self . configfile , '<STR_LIT:w>' ) . write ( self . config ) <EOL> return self <EOL> except IOError as e : <EOL> logging . error ( e . message ) <EOL> def update ( self ) : <EOL> self . configfile = os . path . join ( self . path , self . name ) <EOL> self . config = '<STR_LIT>' . join ( ( open ( self . configfile ) . readlines ( ) ) ) <EOL> return self <EOL> class f2b_Configs ( object ) : <EOL> def __init__ ( self , name , path ) : <EOL> self . name = name <EOL> self . path = path <EOL> self . configlist = f2b_list ( path = path , type = name ) <EOL> self . configlist . path = self . path <EOL> def update ( self ) : <EOL> for filename in os . listdir ( self . path ) : <EOL> try : <EOL> cf = os . path . join ( self . path , filename ) <EOL> if os . path . isfile ( cf ) and ( os . path . splitext ( filename ) [ - <NUM_LIT:1> ] in config_file_types ) : <EOL> self . configlist . append ( f2b_Config ( filename , self . path ) . update ( ) ) <EOL> except IOError as e : <EOL> logging . error ( '<STR_LIT>' . format ( filename , self . path ) ) <EOL> self . configlist . sort ( key = lambda k : k . name ) <EOL> return self <EOL> class f2b_list ( list ) : <EOL> def __init__ ( self , path = '<STR_LIT>' , type = '<STR_LIT>' ) : <EOL> self . path = path <EOL> self . type = type <EOL> @ plugin <EOL> class fail2ban ( SectionPlugin ) : <EOL> def init ( self ) : <EOL> self . title = '<STR_LIT>' <EOL> self . icon = '<STR_LIT>' <EOL> self . category = _ ( '<STR_LIT>' ) <EOL> self . f2b_v = subprocess . check_output ( [ '<STR_LIT>' , '<STR_LIT>' ] ) . splitlines ( ) [ <NUM_LIT:0> ] <EOL> self . append ( self . ui . inflate ( '<STR_LIT>' ) ) <EOL> self . binder = Binder ( self , self ) <EOL> self . update_status = True <EOL> def on_config_update ( o , c , config , u ) : <EOL> if config . __old_name != config . name : <EOL> if os . path . exists ( os . path . join ( c . path , config . name ) ) : <EOL> self . context . notify ( '<STR_LIT:error>' , _ ( <EOL> '<STR_LIT>' ) . format ( config . name , c . path ) ) <EOL> self . update_status = False <EOL> config . name = config . __old_name <EOL> return <EOL> logging . debug ( '<STR_LIT>' % ( config . __old_name , config . name ) ) <EOL> os . unlink ( config . configfile ) <EOL> config . save ( ) <EOL> self . update_status = True <EOL> def on_config_bind ( o , c , config , u ) : <EOL> config . __old_name = config . name <EOL> def new_config ( c ) : <EOL> new_fn = '<STR_LIT>' <EOL> s_fn = new_fn . format ( '<STR_LIT>' ) <EOL> filename = os . path . join ( c . path , s_fn ) <EOL> i = <NUM_LIT:1> <EOL> while os . path . exists ( filename ) : <EOL> s_fn = new_fn . format ( '<STR_LIT:_>' + str ( i ) ) <EOL> filename = os . path . join ( c . path , s_fn ) <EOL> i += <NUM_LIT:1> <EOL> try : <EOL> logging . debug ( '<STR_LIT>' % filename ) <EOL> conf = f2b_Config ( s_fn , c . path ) <EOL> conf . config = config_templates [ c . type ] <EOL> return conf . save ( ) <EOL> except IOError as e : <EOL> print ( '<STR_LIT>' . format ( filename , c . path ) ) <EOL> def delete_config ( config , c ) : <EOL> filename = config . configfile <EOL> logging . debug ( '<STR_LIT>' % config . configfile ) <EOL> c . remove ( config ) <EOL> os . unlink ( filename ) <EOL> self . find ( '<STR_LIT>' ) . post_item_update = on_config_update <EOL> self . find ( '<STR_LIT>' ) . post_item_bind = on_config_bind <EOL> self . find ( '<STR_LIT>' ) . new_item = new_config <EOL> self . find ( '<STR_LIT>' ) . delete_item = delete_config <EOL> def on_page_load ( self ) : <EOL> self . refresh ( ) <EOL> def refresh ( self ) : <EOL> self . configurations = [ <EOL> f2b_Configs ( k , d ) . update ( ) <EOL> for k , d in config_dirs . iteritems ( ) <EOL> if os . path . isdir ( d ) <EOL> ] <EOL> self . binder . setup ( self ) . populate ( ) <EOL> @ on ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def save ( self ) : <EOL> self . binder . update ( ) <EOL> if self . update_status : <EOL> self . context . notify ( '<STR_LIT:info>' , _ ( '<STR_LIT>' ) ) <EOL> self . update_status = True <EOL> self . binder . setup ( self ) . populate ( ) <EOL> @ on ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def check_regex ( self ) : <EOL> self . find ( '<STR_LIT>' ) . text = '<STR_LIT>' <EOL> log_fname = self . find ( '<STR_LIT>' ) . value <EOL> filter_fname = self . find ( '<STR_LIT>' ) . value <EOL> log_as_text = self . find ( '<STR_LIT>' ) . value <EOL> filter_as_text = self . find ( '<STR_LIT>' ) . value <EOL> if not ( log_fname or log_as_text ) or not ( filter_fname or filter_as_text ) : <EOL> logging . debug ( '<STR_LIT>' ) <EOL> self . context . notify ( '<STR_LIT:error>' , _ ( '<STR_LIT>' ) ) <EOL> return <EOL> with open ( filter_fname + '<STR_LIT>' , '<STR_LIT:w>' ) as rt : <EOL> rt . write ( self . find ( '<STR_LIT>' ) . value ) <EOL> rt_name = rt . name <EOL> rt . close ( ) <EOL> p = subprocess . Popen ( [ '<STR_LIT>' , '<STR_LIT>' , log_fname , rt_name ] , stdout = subprocess . PIPE , <EOL> stderr = subprocess . STDOUT ) <EOL> out , err = p . communicate ( ) <EOL> self . find ( '<STR_LIT>' ) . text = out <EOL> os . unlink ( rt_name ) <EOL> @ on ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def open_filter ( self ) : <EOL> self . find ( '<STR_LIT>' ) . show ( ) <EOL> @ on ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def on_open_filter_dialog ( self , button ) : <EOL> self . find ( '<STR_LIT>' ) . visible = False <EOL> @ on ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def on_filter_file_select ( self , path = None ) : <EOL> if not path : <EOL> return <EOL> self . find ( '<STR_LIT>' ) . visible = False <EOL> self . find ( '<STR_LIT>' ) . value = path <EOL> self . find ( '<STR_LIT>' ) . value = '<STR_LIT>' . join ( open ( path ) . readlines ( ) ) <EOL> @ on ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def open_log ( self ) : <EOL> self . find ( '<STR_LIT>' ) . show ( ) <EOL> @ on ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def on_open_log_dialog ( self , button ) : <EOL> self . find ( '<STR_LIT>' ) . visible = False <EOL> @ on ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def on_log_file_select ( self , path = None ) : <EOL> if not path : <EOL> return <EOL> self . find ( '<STR_LIT>' ) . visible = False <EOL> self . find ( '<STR_LIT>' ) . value = path <EOL> self . find ( '<STR_LIT>' ) . value = '<STR_LIT>' . join ( open ( path ) . readlines ( ) ) <EOL> @ on ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def save_filter ( self ) : <EOL> try : <EOL> open ( self . find ( '<STR_LIT>' ) . value , '<STR_LIT:w>' ) . write ( self . find ( '<STR_LIT>' ) . value ) <EOL> except IOError as e : <EOL> self . context . notify ( '<STR_LIT:error>' , _ ( '<STR_LIT>' ) % str ( e ) ) <EOL> logging . error ( e . message ) </s>
