diff --git a/build.sh b/build.sh old mode 100644 new mode 100755 index 54e3f16..dabdbea --- a/build.sh +++ b/build.sh @@ -1,66 +1,37 @@ -#! /bin/bash +#!/bin/bash -sudo git clone https://github.com/alegarsan11/nftables-gui.git /var/www/flask +# Configuración de Matplotlib +export MPLCONFIGDIR=/tmp/matplotlib +sudo mkdir -p $MPLCONFIGDIR +sudo chown www-data:www-data $MPLCONFIGDIR +sudo chmod 777 $MPLCONFIGDIR -# Variables -PROJECT_DIR=/var/www/flask/nftables-frontend -PROJECT_NAME=nftables-gui -WSGI_FILE=wsgi.py -VENV_DIR=$PROJECT_DIR/venv -PYTHON_VERSION=python3.8 +# Instalación de dependencias +sudo apt-get install -y libfreetype6-dev libxft-dev -# Actualizar los repositorios de paquetes -sudo apt update +# Copiar la aplicación +sudo cp -r nftables-frontend /usr/share -# Instalar Apache y el módulo mod_wsgi para Python 3 si no están instalados -sudo apt install -y apache2 libapache2-mod-wsgi-py3 - -# Crear un archivo WSGI para que Apache pueda interactuar con la aplicación Flask -cat < $PROJECT_DIR/$WSGI_FILE -import sys -import logging - -# Configurar el logging -logging.basicConfig(stream=sys.stderr) - -# Añadir el directorio del proyecto al path -sys.path.insert(0, "$PROJECT_DIR") - -from app import app as application # Ajusta según tu archivo principal de Flask -EOT - -# Configurar Apache para servir la aplicación Flask -sudo tee /etc/apache2/sites-available/$PROJECT_NAME.conf > /dev/null < - ServerName localhost - - WSGIDaemonProcess $PROJECT_NAME python-path=$VENV_DIR/lib/$PYTHON_VERSION/site-packages - WSGIScriptAlias / $PROJECT_DIR/$WSGI_FILE - - - Require all granted - - - Alias /static $PROJECT_DIR/static - - Require all granted - - - ErrorLog \${APACHE_LOG_DIR}/$PROJECT_NAME_error.log - CustomLog \${APACHE_LOG_DIR}/$PROJECT_NAME_access.log combined - -EOT +# Instalación y configuración de Apache con WSGI +sudo apt-get install -y libapache2-mod-wsgi-py3 +sudo a2enmod wsgi -# Actualizar los repositorios de paquetes -sudo apt update +# Configuración del sitio de Apache +sudo cp nftables-frontend/nftables-config.conf /etc/apache2/sites-available/nftables-config.conf +sudo a2ensite nftables-config.conf -# Instalar Apache y el módulo mod_wsgi para Python 3 si no están instalados -sudo apt install -y apache2 libapache2-mod-wsgi-py3 +# Ajuste de permisos +sudo chmod 664 /usr/share/nftables-frontend/instance/nftables.db +sudo chown www-data:www-data /usr/share/nftables-frontend/instance/nftables.db +sudo chown -R www-data:www-data /usr/share/nftables-frontend +sudo chmod -R 775 /usr/share/nftables-frontend +sudo chmod -R 775 /usr/share/nftables-frontend/instance +sudo chmod -R 775 /usr/share/nftables-frontend/static +sudo chown -R www-data:www-data /usr/share/nftables-frontend/static -# Habilitar el sitio y el módulo wsgi -sudo a2ensite $PROJECT_NAME.conf -sudo a2enmod wsgi -#Reiniciar Apache para aplicar los cambios +# Reiniciar Apache sudo systemctl restart apache2 -echo "La aplicación Flask ahora debería estar disponible en http://localhost" \ No newline at end of file +# Iniciar el parser de nftables +cd nftables-parser +sudo hug -f main.py diff --git a/nftables-frontend/app.py b/nftables-frontend/app.py index 2c353fa..fe4cda2 100644 --- a/nftables-frontend/app.py +++ b/nftables-frontend/app.py @@ -6,6 +6,38 @@ import os from service import create_default_user, login_manager +ERROR = 'error.html' + +app = Flask(__name__) +app.register_blueprint(visualization_bp) +app.register_blueprint(creation_bp) +dir_path = os.path.dirname(os.path.realpath(__file__)) +app.config['SECRET_KEY'] = 'hfds732klejds90ahg' +app.config['SQLALCHEMY_DATABASE_URI'] = f'sqlite:///{dir_path}/instance/nftables.db' +app.config['SESSION_COOKIE_SAMESITE'] = 'Strict' +app.config['SESSION_COOKIE_SECURE'] = True +login_manager.init_app(app) +db.init_app(app) + +with app.app_context(): + db.create_all() + create_default_user() + +migrate = Migrate(app, db) +Bootstrap(app) + +@app.errorhandler(404) +def page_not_found(e): + return render_template(ERROR, message='Page not found'), 404 + +@app.errorhandler(500) +def internal_error(e): + return render_template(ERROR, message="Internal server error"), 500 + +@app.route('/favicon.ico') +def favicon(): + return app.send_static_file('favicon.ico') + def create_app(): app = Flask(__name__) app.register_blueprint(visualization_bp) @@ -27,11 +59,11 @@ def create_app(): @app.errorhandler(404) def page_not_found(e): - return render_template('error.html', message='Page not found'), 404 + return render_template(ERROR, message='Page not found'), 404 @app.errorhandler(500) def internal_error(e): - return render_template('error.html', message="Internal server error"), 500 + return render_template(ERROR, message="Internal server error"), 500 @app.route('/favicon.ico') def favicon(): @@ -40,5 +72,4 @@ def favicon(): return app if __name__ == '__main__': - app = create_app() app.run(debug=False) \ No newline at end of file diff --git a/nftables-frontend/instance/nftables.db b/nftables-frontend/instance/nftables.db new file mode 100644 index 0000000..5753389 Binary files /dev/null and b/nftables-frontend/instance/nftables.db differ diff --git a/nftables-frontend/nftables-config.conf b/nftables-frontend/nftables-config.conf new file mode 100644 index 0000000..a30a50b --- /dev/null +++ b/nftables-frontend/nftables-config.conf @@ -0,0 +1,24 @@ + + ServerName www.example.com + ServerAlias example.com + DocumentRoot /usr/share/nftables-frontend + WSGIDaemonProcess nftables-frontend user=www-data group=www-data threads=5 + WSGIScriptAlias / /usr/share/nftables-frontend/run.wsgi + + WSGIProcessGroup nftables-frontend + WSGIApplicationGroup %{GLOBAL} + Options Indexes FollowSymLinks MultiViews + AllowOverride All + Require all granted + + Alias /static /usr/share/nftables-frontend/static + + Require all granted + + Alias /templates /usr/share/nftables-frontend/templates + + Require all granted + + ErrorLog ${APACHE_LOG_DIR}/error.log + CustomLog ${APACHE_LOG_DIR}/access.log combined + diff --git a/nftables-frontend/run.wsgi b/nftables-frontend/run.wsgi new file mode 100644 index 0000000..9e5bf17 --- /dev/null +++ b/nftables-frontend/run.wsgi @@ -0,0 +1,13 @@ +import os +import sys +import matplotlib +matplotlib.use('Agg') # Configurar el backend de Matplotlib + +# Establece la variable de entorno MPLCONFIGDIR +os.environ['MPLCONFIGDIR'] = '/tmp/matplotlib' + +# Añade tu aplicación al path de Python +sys.path.insert(0,'/usr/share/nftables-frontend') + +# Importa tu aplicación +from app import app as application \ No newline at end of file diff --git a/nftables-frontend/static/img/nftables_info.png b/nftables-frontend/static/img/nftables_info.png new file mode 100644 index 0000000..d490f16 Binary files /dev/null and b/nftables-frontend/static/img/nftables_info.png differ diff --git a/nftables-frontend/templates/chains/chains.html b/nftables-frontend/templates/chains/chains.html index 7c762cc..b81114a 100644 --- a/nftables-frontend/templates/chains/chains.html +++ b/nftables-frontend/templates/chains/chains.html @@ -34,7 +34,7 @@

Chains:

{{ chain.policy }} {% endif %} - Delete + Delete {% endfor %} diff --git a/nftables-frontend/views.py b/nftables-frontend/views.py index 5e725e9..0cfff47 100644 --- a/nftables-frontend/views.py +++ b/nftables-frontend/views.py @@ -36,7 +36,11 @@ def main_view(): categories = ['Rules', 'Chains', 'Tables'] # Get the number of rules, chains and tables values = service.load_data(False) - image_path = 'static/img/nftables_info.png' + # Obtén la ruta del directorio actual + dir_path = os.path.dirname(os.path.realpath(__file__)) + + # Construye la ruta absoluta al archivo + image_path = os.path.join(dir_path, 'static/img/nftables_info.png') plt.figure(figsize=(8, 6)) plt.bar(categories, values, color=['blue', 'green', 'orange']) plt.xlabel('nftables elements') @@ -605,7 +609,7 @@ def add_list_post(): form = AddListForm() lista = request.files['list'].read().decode('utf-8').split("\n") form.element.data = lista - if 'txt' not in request.files['list'].filename: + if '.txt' not in request.files['list'].filename: flash('Error adding list.') tables = service.get_tables()