Commit 1ddb9a90 authored by Pieter-Jan Moreels's avatar Pieter-Jan Moreels
Browse files

bugfix and some updates in control panel

parent 1cf745a9
Loading
Loading
Loading
Loading
+22 −39
Original line number Diff line number Diff line
@@ -19,7 +19,7 @@ sys.path.append(os.path.join(_runPath, ".."))
from tornado.wsgi import WSGIContainer
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop
from flask import Flask, render_template, request, redirect, jsonify
from flask import Flask, render_template, request, redirect, jsonify, send_file
from flask.ext.login import LoginManager, current_user, login_user, logout_user, login_required
from werkzeug import secure_filename
from passlib.hash import pbkdf2_sha256
@@ -34,7 +34,7 @@ import random
import signal
import logging
import subprocess
from io import TextIOWrapper
from io import TextIOWrapper, BytesIO
from logging.handlers import RotatingFileHandler

from lib.User import User
@@ -490,7 +490,7 @@ def link(vFeedMap=None,field=None,value=None):
@app.route('/admin')
@app.route('/admin/')
def admin():
    status = ["default", "none"]
    status = "default"
    if Configuration.loginRequired():
        if not current_user.is_authenticated():
            return render_template('login.html', status=status)
@@ -512,9 +512,7 @@ def updatedb():
    process = subprocess.Popen(["python3", os.path.join(_runPath, "../sbin/db_updater.py"), "-civ"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    out, err = process.communicate()
    output="%s\n\nErrors:\n%s"%(str(out,'utf-8'),str(err,'utf-8')) if err else str(out,'utf-8')
    status = ["db_updated", "success"]
    return render_template('admin.html', status=status, stats=adminStats(), updateOutput=filterUpdateField(output))

    return jsonify({"updateOutput": output, "status": "db_updated"})

@app.route('/admin/whitelist/import', methods=['POST'])
@login_required
@@ -525,32 +523,25 @@ def whitelistImport(force=None, path=None):
    if (count == 0) | (not count) | (force == "f"):
        dropWhitelist()
        importWhitelist(TextIOWrapper(file.stream))
        status = ["wl_imported", "success"]
        status = "wl_imported"
    else:
        status = ["wl_already_filled", "info"]
        status = "wl_already_filled"
    return render_template('admin.html', status=status, stats=adminStats())

@app.route('/admin/whitelist/export', methods=['POST'])
@app.route('/admin/whitelist/export')
@login_required
def whitelistExport(force=None, path=None):
    file = request.files['file']
    filename = secure_filename(file.filename)
    force = request.form.get('force')
    if (force == "df") and (os.path.isfile(filename)):
        status = ["wl_file_already_exists", "warning"]
    else:
        if(os.path.isfile(filename)):
            os.remove(filename)
        exportWhitelist(filename)
        status = ["wl_exported", "success"]
    return render_template('admin.html', status=status, stats=adminStats())
    bytIO = BytesIO()
    bytIO.write(bytes(exportWhitelist(), "utf-8"))
    bytIO.seek(0)
    return send_file(bytIO, as_attachment=True, attachment_filename="whitelist.txt")


@app.route('/admin/whitelist/drop')
@login_required
def whitelistDrop():
    dropWhitelist()
    return render_template('admin.html', status=["wl_dropped", "success"], stats=adminStats())
    return jsonify({"status":"wl_dropped"})


@app.route('/admin/whitelist')
@@ -606,7 +597,7 @@ def listEdit():
    return jsonify({"rules":returnList, "status":status, "listType":lst})


@app.route('/admin/blacklist/import', methods=['POST'])
@app.route('/admin/blacklist/import')
@login_required
def blacklistImport():
    file = request.files['file']
@@ -615,32 +606,24 @@ def blacklistImport():
    if (count == 0) | (not count) | (force == "f"):
        dropBlacklist()
        importBlacklist(TextIOWrapper(file.stream))
        status = ["bl_imported", "success"]
        status = "bl_imported"
    else:
        status = ["bl_already_filled", "info"]
        status = "bl_already_filled"
    return render_template('admin.html', status=status, stats=adminStats())

@app.route('/admin/blacklist/export', methods=['POST'])
@app.route('/admin/blacklist/export')
@login_required
def blacklistExport():
    file = request.files['file']
    filename = secure_filename(file.filename)
    force = request.form.get('force')
    if (force == "df") and (os.path.isfile(filename)):
        status = ["bl_file_already_exists", "warning"]
    else:
        if(os.path.isfile(filename)):
            os.remove(filename)
        exportBlacklist(filename)
        status = ["bl_exported", "success"]
    return render_template('admin.html', status=status, stats=adminStats())

    bytIO = BytesIO()
    bytIO.write(bytes(exportBlacklist(), "utf-8"))
    bytIO.seek(0)
    return send_file(bytIO, as_attachment=True, attachment_filename="blacklist.txt")

@app.route('/admin/blacklist/drop')
@login_required
def blacklistDrop():
    dropBlacklist()
    return render_template('admin.html', status=["bl_dropped", "success"], stats=adminStats())
    return jsonify({status:"bl_dropped"})


@app.route('/admin/blacklist')
@@ -740,7 +723,7 @@ def login_check():
    try:
        if person and pbkdf2_sha256.verify(password, person.password):
            login_user(person)
            return render_template('admin.html', status=["logged_in", "success"], stats=adminStats())
            return render_template('admin.html', status="logged_in", stats=adminStats())
        else:
            return render_template('login.html', status=["wrong_combination", "warning"])
    except:
+22 −23
Original line number Diff line number Diff line
function updateDB(){
  setStatus("Database update started", "info")
  $.getJSON('/admin/updatedb', {}, function(data){ parseStatus(data) })
}
function whitelistImport(){ listURLBuilder("/admin/whitelist/import", 'wl');}
function blacklistImport(){ listURLBuilder("/admin/blacklist/import", 'bl');}
function whitelistExport(){ window.location = '/admin/whitelist/export';}
function blacklistExport(){ window.location = '/admin/blacklist/export';}
function dropWhitelist(){
  if(confirm("You are about to drop the whitelist. Are you sure?")){
    $.getJSON('/admin/whitelist/drop', {}, function(data){
      if (parseStatus(data)){$("#wl_rules").text("Whitelist: 0 rules");}
    })
  }
}
function dropBlacklist(){
  if(confirm("You are about to drop the whitelist. Are you sure?")){
    $.getJSON('/admin/blacklist/drop', {}, function(data){
      if (parseStatus(data)){$("#bl_rules").text("Blacklist: 0 rules");}
    })
  }
}
function listURLBuilder(url, list){
  var file = list+"_Import";
  var force = "";
@@ -12,29 +34,6 @@ function listURLBuilder(url, list){
    alert('Please select a file');
  }
}
function whitelistImport(){
  listURLBuilder("/admin/whitelist/import", 'wl');
}
function whitelistExport(){
  listURLBuilder("/admin/whitelist/export", 'wl');
}
function dropWhitelist(){
  if(confirm("You are about to drop the whitelist. Are you sure?")){
    var url = "/admin/whitelist/drop";window.location = url;
  }
}
function blacklistImport(){ 
  listURLBuilder("/admin/blacklist/import", 'bl');
}
function blacklistExport(){
  listURLBuilder("/admin/blacklist/export", 'bl');
}
function dropBlacklist(){
  if(confirm("You are about to drop the whitelist. Are you sure?")){
    var url = "/admin/blacklist/drop";window.location = url;
  }
}

function postURL(url, force, file) {
  var form = document.createElement("FORM");
  form.enctype="multipart/form-data";
+40 −0
Original line number Diff line number Diff line
function parseStatus(data){
  _ok = false;
  if (data['status'] != undefined){
    list = "";
    if       (data['status'].startsWith('wl_')){ list = "Whitelist";
    }else if (data['status'].startsWith('bl_')){ list = "Blacklist";}

    switch(data['status']){
      case "default":
        if (data['updateOutput'] != undefined){
          setStatus("Last update info <div class='well'><pre>"+data['updateOutput']+"</pre></div>", "success"); break;
        }
        _ok=true;break;
      case "logged_in":
        setStatus("Logged in successfully", "success"); _ok=true; break;
      case "db_updated":
        setStatus("Database update finished <div class='well'><pre>"+data['updateOutput']+"</pre></div>", "success"); break;
        _ok=true;break;
      case "wl_imported":
      case "bl_imported":
        setStatus(list+" import finished"); _ok=true; break;
      case "wl_already_filled":
      case "bl_already_filled":
        setStatus(list+" is already filled. You can force to drop the database", "info"); break;
      case "wl_dropped":
      case "bl_dropped":
        setStatus(list+" dropped", "success"); _ok=true; _ok=true; break;
      default:
        setStatus("A problem occurred with the server!", "danger");
    }
  }
  return _ok;
}

function setStatus(text, status){
  $("#status-box").empty();
  $("#status-box").removeClass();
  $("#status-box").addClass("alert alert-"+status);
  $("#status-box").append(text);
}
+10 −61
Original line number Diff line number Diff line
@@ -6,63 +6,14 @@

  <!-- javascript -->
  <script type="text/javascript" src="/static/js/custom/admin.js"></script>
  <script>
    {% if status %}
      data = {status: "{{status}}"}
      parseStatus(data);
    {% endif %}
  </script>
{% endblock %}
{% block content %}
  <!-- Status -->
  <div>
    <!-- type -->
    {% if status[1] == 'success' %}
      <div class="alert alert-success">
        <span class="glyphicon glyphicon-ok-sign"></span>
    {% elif status[1] == 'info' %}
      <div class="alert alert-info">
        <span class="glyphicon glyphicon-info-sign"></span>
    {% elif status[1] == 'warning' %}
      <div class="alert alert-warning">
        <span class="glyphicon glyphicon-warning-sign"></span>
    {% elif status[1] == 'error' %}
      <div class="alert alert-danger">
        <span class="glyphicon glyphicon-remove-sign"></span>
    {% else %}
      <div>
    {% endif %}
      <!-- content -->
      {% if status[0].startswith('wl_') %}
          {% set list = 'Whitelist' %}
      {% elif status[0].startswith('bl_') %}
          {% set list = 'Blacklist' %}
      {% endif %}

      {% if status[0] == 'logged_in' %}
        Logged in succesfully
      {% elif status[0] == 'default' %}
        {% if updateOutput %}
          Last update info
          <div class=well"><pre>{{updateOutput}}</pre></div>
        {% endif %}
      {% elif status[0] == 'db_updated' %}
        Database update finished
        <div class=well"><pre>{{updateOutput}}</pre></div>
      {% elif (status[0] == 'wl_imported') or (status[0] == 'bl_imported') %}
        {{ list }} import finished
      {% elif (status[0] == 'wl_already_filled') or (status[0] == 'bl_already_filled') %}
        {{ list }} is already filled. You can force to drop the database
      {% elif (status[0] == 'wl_exported') or (status[0] == 'bl_exported') %}
        {{ list }} export finished
      {% elif (status[0] == 'wl_file_already_exists') or (status[0] == 'bl_file_already_exists') %}
        A file with that name already exists. The {{ list|lower }} was not exported
      {% elif (status[0] == 'wl_dropped') or (status[0] == 'bl_dropped') %}
        {{ list }} dropped
      {% elif status[0] == 'invalid_path_format' %}
        Invalid path format!
      {% elif status[0] == 'invalid_path' %}
        Invalid path!
      {% endif %}
      {% if (status[0] != 'default') %}
        <br /><br /><a href="/admin"><span class="glyphicon glyphicon-remove"></span> close</a>
      {% endif %}
    </div>
  </div>
  <div id='stats' class="well well-small">
    <div>
      <span>Database info for database <b>{{stats['dbName']}}</b></span>
@@ -85,18 +36,16 @@
        <tr><td>vFeed info</td>       <td>{{stats['vfeedA']}}</td>
            <td>{% if stats['vfeedU'] is not none %}{{stats['vfeedU']|currentTime}} {% else %}Not updated{% endif %}</td><tr>
      </table>
      <span>Whitelist: {{stats['wlA']}} rules</span><br />
      <span>Blacklist: {{stats['blA']}} rules</span><br /><br />
      <span id="wl_rules">Whitelist: {{stats['wlA']}} rules</span><br />
      <span id="bl_rules">Blacklist: {{stats['blA']}} rules</span><br /><br />
      <span>Database size: {{'%0.2f' % (stats['dbSize']/1024**2)}}MB ({{'%0.2f' % (stats['dbSize']/1024**3)}}GB)</span><br />
      <span>Database size on disk: {{'%0.2f' % (stats['dbOnDisk']/1024**2)}}MB ({{'%0.2f' % (stats['dbOnDisk']/1024**3)}}GB)</span>
    </div>
  </div>
  <!-- Database update -->
  <div class="well well-small tab">
    <form action="/admin/updatedb">
    <strong>Update the database</strong> <br />
      <input type="submit" value="Update"/>
    </form>
    <button onclick="updateDB()">Update</button>
  </div>
  <!-- Whitelist import -->
  <div class="well well-small tab">
+1 −1
Original line number Diff line number Diff line
@@ -38,7 +38,7 @@
          {% include 'subpages/menu.html' %}
        {% endif %}
        <!-- End Nav -->
        <!-- To be added later <div id="status"></div> -->
        <div id="status-box"></div>
        <!-- Body Start -->
        <div id="content">
          {% block content %}{% endblock %}