mirror of
https://codeberg.org/prof_x_pvt_ltd/captive.whump.shanti-portal
synced 2024-08-14 22:46:42 +00:00
fixing config management for plugins.
This commit is contained in:
parent
f7d5aae9b3
commit
d3bf991048
3 changed files with 41 additions and 7 deletions
|
@ -1,8 +1,15 @@
|
|||
# Add an iptables rule
|
||||
|
||||
import re
|
||||
import socket
|
||||
from io import BytesIO
|
||||
from logging import getLogger, DEBUG, WARN, INFO
|
||||
|
||||
try:
|
||||
from congiparser import RawConfigParser
|
||||
except ImportError:
|
||||
from ConfigParser import RawConfigParser
|
||||
|
||||
from portal import logHandler, logFormatter
|
||||
|
||||
# Try to import arping for mac_from_ip()
|
||||
|
@ -18,12 +25,15 @@ from sh import sudo, ErrorReturnCode
|
|||
def run(arg):
|
||||
# Some info from the plugin dispatcher.
|
||||
environ = arg['environ']
|
||||
config = arg['config']
|
||||
plugin_config = arg['config']
|
||||
|
||||
config = RawConfigParser(defaults=plugin_config)
|
||||
config.add_section(plugin_config.get('__name__', 'iptables'))
|
||||
|
||||
# Setup plugin logging
|
||||
l = getLogger('plugin_iptables')
|
||||
l.addHandler(logHandler)
|
||||
if config.get('debug', False):
|
||||
if config.getboolean('iptables', 'debug', False):
|
||||
l.setLevel(DEBUG)
|
||||
|
||||
client_ip = environ.get(
|
||||
|
@ -34,11 +44,21 @@ def run(arg):
|
|||
error_msg = None
|
||||
iptables_failed = False
|
||||
|
||||
# Verify IP
|
||||
try:
|
||||
socket.inet_aton(client_ip)
|
||||
except socket.error:
|
||||
l.error('Client IP-address is invalid')
|
||||
return {
|
||||
'error': str(e),
|
||||
'failed': True
|
||||
}
|
||||
|
||||
# Attempt to get client HW address first.
|
||||
try:
|
||||
client_mac = mac_from_ip(
|
||||
l,
|
||||
config.get('arping'),
|
||||
config.get('iptables', 'arping'),
|
||||
client_ip
|
||||
)
|
||||
except Exception as e:
|
||||
|
@ -55,7 +75,7 @@ def run(arg):
|
|||
))
|
||||
|
||||
# Create tuple out of iptables command
|
||||
iptables_mac = config.get('iptables_mac').format(
|
||||
iptables_mac = config.get('iptables', 'iptables_mac').format(
|
||||
mac_address=client_mac
|
||||
)
|
||||
iptables_mac = tuple(iptables_mac.split(' '))
|
||||
|
@ -95,7 +115,7 @@ def run(arg):
|
|||
ip=client_ip
|
||||
))
|
||||
|
||||
iptables_ip = config.get('iptables_ip').format(
|
||||
iptables_ip = config.get('iptables', 'iptables_ip').format(
|
||||
ip_address=client_ip
|
||||
)
|
||||
iptables_ip = tuple(iptables_ip.split(' '))
|
||||
|
|
|
@ -2,16 +2,26 @@
|
|||
|
||||
# Sets up logging by importing from the bottle app in the parent dir.
|
||||
from logging import getLogger, DEBUG, WARN, INFO
|
||||
|
||||
try:
|
||||
from congiparser import RawConfigParser
|
||||
except ImportError:
|
||||
from ConfigParser import RawConfigParser
|
||||
|
||||
from portal import logHandler, logFormatter
|
||||
|
||||
def run(arg):
|
||||
# The WSGI environ dict should always be there, sans any special objects
|
||||
# like io streams.
|
||||
environ = arg['environ']
|
||||
plugin_config = arg['config']
|
||||
|
||||
config = RawConfigParser(defaults=plugin_config)
|
||||
config.add_section(plugin_config.get('__name__', 'sample_log'))
|
||||
|
||||
l = getLogger('plugin_log')
|
||||
l.addHandler(logHandler)
|
||||
if config.get('debug', False):
|
||||
if config.getboolean('sample_log', 'debug', False):
|
||||
l.setLevel(DEBUG)
|
||||
|
||||
log_url = '{proto}://{server}:{port}{request}'.format(
|
||||
|
|
|
@ -104,7 +104,11 @@ def dispatch_plugins():
|
|||
|
||||
# Import all the plugin configuration values as OrderedDict
|
||||
config_sections = plugin_config._sections
|
||||
arg['config'] = config_sections[plugin]
|
||||
arg['config'] = dict(config_sections[plugin])
|
||||
|
||||
# Is plugin enabled?
|
||||
if not plugin_config.getboolean(plugin, 'enabled'):
|
||||
continue
|
||||
|
||||
# Import the plugin
|
||||
try:
|
||||
|
|
Loading…
Reference in a new issue