diff --git a/class/core/firewall_api.py b/class/core/firewall_api.py new file mode 100755 index 000000000..d25424029 --- /dev/null +++ b/class/core/firewall_api.py @@ -0,0 +1,56 @@ +# coding: utf-8 + +import psutil +import time +import os +import sys +import public +import re +import json +import pwd + + +class firewall_api: + + def __init__(self): + pass + + def getList(self, page, limit): + + start = (page - 1) * limit + + _list = public.M('firewall').field('id,port,ps,addtime').limit( + str(start) + ',' + str(limit)).order('id desc').select() + data = {} + data['data'] = _list + + count = public.M('firewall').count() + _page = {} + _page['count'] = count + _page['tojs'] = 'showAccept' + _page['p'] = page + + data['page'] = public.getPage(_page) + return public.getJson(data) + + def getLogList(self, page, limit, search=''): + find_search = '' + if search != '': + find_search = "type like '%" + search + "%' or log like '%" + \ + search + "%' or addtime like '%" + search + "%'" + + start = (page - 1) * limit + + _list = public.M('logs').where(find_search, ()).field( + 'id,type,log,addtime').limit(str(start) + ',' + str(limit)).order('id desc').select() + data = {} + data['data'] = _list + + count = public.M('logs').where(find_search, ()).count() + _page = {} + _page['count'] = count + _page['tojs'] = 'getLogs' + _page['p'] = page + + data['page'] = public.getPage(_page) + return public.getJson(data) diff --git a/route/files.py b/route/files.py index ac63bd502..9938518a7 100644 --- a/route/files.py +++ b/route/files.py @@ -70,6 +70,17 @@ def fileAccess(): return public.getJson(data) +@files.route('/get_dir_size', methods=['POST']) +def getDirSize(): + path = request.form.get('path', '').encode('utf-8') + if public.getOs() == 'darwin': + tmp = public.execShell('du -sh ' + path) + else: + tmp = public.execShell('du -sbh ' + path) + # print tmp + return public.returnJson(True, tmp[0].split()[0]) + + @files.route('/get_dir', methods=['POST']) def getDir(): path = request.form.get('path', '').encode('utf-8') diff --git a/route/firewall.py b/route/firewall.py index 39b0b8af2..ef5b1f1a9 100644 --- a/route/firewall.py +++ b/route/firewall.py @@ -1,14 +1,18 @@ # coding:utf-8 +import sys +import os +import re + from flask import Flask from flask import Blueprint, render_template from flask import request -import sys -import os sys.path.append(os.getcwd() + "/class/core/") import db +import public +import firewall_api firewall = Blueprint('firewall', __name__, template_folder='templates') @@ -18,126 +22,34 @@ def index(): return render_template('default/firewall.html') -''' -* 取数据列表 -* @param String _GET['tab'] 数据库表名 -* @param Int _GET['count'] 每页的数据行数 -* @param Int _GET['p'] 分页号 要取第几页数据 -* @return Json page.分页数 , count.总行数 data.取回的数据 -''' +@firewall.route("/get_list", methods=['POST']) +def getList(): + p = request.form.get('p', '1').strip() + limit = request.form.get('limit', '10').strip() + return firewall_api.firewall_api().getList(int(p), int(limit)) -@firewall.route("/log_list", methods=['GET', 'POST']) -def log_list(): - try: - table = request.form.get('table', '') - data = GetSql(request.form) - SQL = public.M(table) - - if table == 'backup': - import os - for i in range(len(data['data'])): - if data['data'][i]['size'] == 0: - if os.path.exists(data['data'][i]['filename']): - data['data'][i]['size'] = os.path.getsize( - data['data'][i]['filename']) - - elif table == 'sites' or table == 'databases': - type = '0' - if table == 'databases': - type = '1' - for i in range(len(data['data'])): - data['data'][i]['backup_count'] = SQL.table('backup').where( - "pid=? AND type=?", (data['data'][i]['id'], type)).count() - if table == 'sites': - for i in range(len(data['data'])): - data['data'][i]['domain'] = SQL.table('domain').where( - "pid=?", (data['data'][i]['id'],)).count() - elif table == 'firewall': - for i in range(len(data['data'])): - if data['data'][i]['port'].find(':') != -1 or data['data'][i]['port'].find('.') != -1 or data['data'][i]['port'].find('-') != -1: - data['data'][i]['status'] = -1 - else: - data['data'][i]['status'] = CheckPort( - int(data['data'][i]['port'])) - - # 返回 - return data - except Exception as ex: - return str(ex) - -''' - * 获取数据与分页 - * @param string table 表 - * @param string where 查询条件 - * @param int limit 每页行数 - * @param mixed result 定义分页数据结构 - * @return array -''' +@firewall.route("/get_log_list", methods=['POST']) +def getLogList(): + p = request.form.get('p', '1').strip() + limit = request.form.get('limit', '10').strip() + search = request.form.get('search', '').strip() + return firewall_api.firewall_api().getLogList(int(p), int(limit), search) -def GetSql(get, result='1,2,3,4,5,8'): - # 判断前端是否传入参数 - order = "id desc" - if hasattr(get, 'order'): - order = get.order +@firewall.route('get_ssh_info', methods=['POST']) +def getSshInfo(): + file = '/etc/ssh/sshd_config' + conf = public.readFile(file) + rep = "#*Port\s+([0-9]+)\s*\n" + port = re.search(rep, conf).groups(0)[0] - limit = 20 - if hasattr(get, 'limit'): - limit = int(get.limit) - - if hasattr(get, 'result'): - result = get.result - - SQL = db.Sql() data = {} - # 取查询条件 - where = '' - if hasattr(get, 'search'): - where = self.GetWhere(get.table, get.search) - if get.table == 'backup': - where += " and type='" + get.type + "'" - - if get.table == 'sites': - pid = SQL.table('domain').where( - 'name=?', (get.search,)).getField('pid') - if pid: - where = "id=" + str(pid) - - field = self.GetField(get.table) - # 实例化数据库对象 - - # 是否直接返回所有列表 - if hasattr(get, 'list'): - data = SQL.table(get.table).where( - where, ()).field(field).order(order).select() - return data - - # 取总行数 - count = SQL.table(get.table).where(where, ()).count() - #get.uri = get - # 包含分页类 - import page - # 实例化分页类 - page = page.Page() - - info = {} - info['count'] = count - info['row'] = limit - - info['p'] = 1 - if hasattr(get, 'p'): - info['p'] = int(get['p']) - info['uri'] = get - info['return_js'] = '' - if hasattr(get, 'tojs'): - info['return_js'] = get.tojs - - data['where'] = where - - # 获取分页数据 - data['page'] = page.GetPage(info, result) - # 取出数据 - data['data'] = SQL.table(get.table).where(where, ()).order(order).field( - field).limit(str(page.SHIFT) + ',' + str(page.ROW)).select() - return data + data['port'] = port + data['status'] = True + data['ping'] = True + if public.getOs() == 'draim': + data['firewall_status'] = False + else: + data['firewall_status'] = True + return public.getJson(data) diff --git a/route/task.py b/route/task.py index 5b3108401..b6801928a 100644 --- a/route/task.py +++ b/route/task.py @@ -2,8 +2,6 @@ import os import sys -sys.path.append("/class/core") -import public from flask import Flask from flask import Blueprint, render_template @@ -11,6 +9,9 @@ from flask import Blueprint, render_template task = Blueprint('task', __name__, template_folder='templates') +sys.path.append("/class/core") +import public + @task.route("/") def index(): diff --git a/static/app/firewall.js b/static/app/firewall.js index 55cdcbbca..283064141 100755 --- a/static/app/firewall.js +++ b/static/app/firewall.js @@ -1,26 +1,27 @@ setTimeout(function(){ - GetSshInfo(); - },500); + getSshInfo(); +},500); - setTimeout(function(){ - ShowAccept(1); - },1000); - - setTimeout(function(){ - getLogs(1); - },1500); - - function CloseLogs(){ - $.post('/files?action=CloseLogs','',function(rdata){ +setTimeout(function(){ + showAccept(1); +},1000); + +setTimeout(function(){ + getLogs(1); +},1500); + + +function closeLogs(){ + $.post('/files?action=CloseLogs','',function(rdata){ $("#logSize").html(rdata); layer.msg(lan.firewall.empty,{icon:1}); - }); + },'json'); } $(function(){ - $.post('/files?action=GetDirSize','path=/www/wwwlogs',function(rdata){ + $.post('/files/get_dir_size','path=/Users/midoks/Desktop/fwww/wwwlogs', function(rdata){ $("#logSize").html(rdata); - }); + },'json'); }) $("#firewalldType").change(function(){ @@ -43,8 +44,9 @@ $("#firewalldType").change(function(){ }); -function GetSshInfo(){ - $.post('/firewall?action=GetSshInfo','',function(rdata){ +function getSshInfo(){ + $.post('/firewall/get_ssh_info', '',function(rdata){ + console.log(rdata); var SSHchecked = '' if(rdata.status){ SSHchecked = "" @@ -65,7 +67,7 @@ function GetSshInfo(){ $("#isPing").html(isPing) - }); + },'json'); } @@ -83,7 +85,7 @@ function mstsc(port) { $.post('/firewall?action=SetSshPort', data, function(ret) { layer.msg(ret.msg,{icon:ret.status?1:2}) layer.close(loadT) - GetSshInfo() + getSshInfo() }); }); } @@ -168,37 +170,38 @@ function SetMstscStatus(){ * 取回数据 * @param {Int} page 分页号 */ -function ShowAccept(page,search) { +function showAccept(page,search) { search = search == undefined ? '':search; var loadT = layer.load(); - $.post('/firewall/log_list','table=firewall&tojs=ShowAccept&limit=10&p=' + page+"&search="+search, function(data) { + $.post('/firewall/get_list','limit=10&p=' + page+"&search="+search, function(data) { + console.log(data); layer.close(loadT); - var Body = ''; + var body = ''; for (var i = 0; i < data.data.length; i++) { var status = ''; switch(data.data[i].status){ case 0: - status = lan.firewall.status_not; + status = '未使用'; break; case 1: - status = lan.firewall.status_net; + status = '外网不通'; break; default: - status = lan.firewall.status_ok; + status = '正常'; break; } - Body += "