From b63c16eaf778af18888ee57e05c0d1c2aec4cd7e Mon Sep 17 00:00:00 2001 From: Mr Chen Date: Mon, 17 Dec 2018 15:20:30 +0800 Subject: [PATCH] update --- class/core/firewall_api.py | 56 +++++++++++++++ route/files.py | 11 +++ route/firewall.py | 150 +++++++++------------------------------- route/task.py | 5 +- static/app/firewall.js | 83 +++++++++++----------- static/app/public.js | 32 +++++---- static/css/site.css | 81 ++++++++++++---------- templates/default/firewall.html | 33 ++++++--- 8 files changed, 230 insertions(+), 221 deletions(-) create mode 100755 class/core/firewall_api.py 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 += "\ + body += "\ " + data.data[i].id + "\ " + (data.data[i].port.indexOf('.') == -1?lan.firewall.accept_port+':['+data.data[i].port+']':lan.firewall.drop_ip+':['+data.data[i].port+']') + "\ " + status + "\ " + data.data[i].addtime + "\ " + data.data[i].ps + "\ - "+lan.public.del+"\ + 删除\ "; } - $("#firewallBody").html(Body); + $("#firewallBody").html(body); $("#firewallPage").html(data.page); - }) + },'json'); } //添加放行 @@ -243,7 +246,7 @@ function AddAcceptPort(){ } //删除放行 -function DelAcceptPort(id, port) { +function delAcceptPort(id, port) { var action = "DelDropAddress"; if(port.indexOf('.') == -1){ action = "DelAcceptPort"; @@ -267,20 +270,20 @@ function DelAcceptPort(id, port) { function getLogs(page,search) { search = search == undefined ? '':search; var loadT = layer.load(); - $.post('/firewall/log_list','table=logs&tojs=getLogs&limit=10&p=' + page+"&search="+search, function(data) { + $.post('/firewall/get_log_list','limit=10&p=' + page+"&search="+search, function(data) { layer.close(loadT); - var Body = ''; + var body = ''; for (var i = 0; i < data.data.length; i++) { - Body += "\ - " + data.data[i].id + "\ - " + data.data[i].type + "\ - " + data.data[i].log + "\ - " + data.data[i].addtime + "\ - "; + body += "\ + " + data.data[i].id + "\ + " + data.data[i].type + "\ + " + data.data[i].log + "\ + " + data.data[i].addtime + "\ + "; } - $("#logsBody").html(Body); + $("#logsBody").html(body); $("#logsPage").html(data.page); - }) + },'json'); } //清理面板日志 diff --git a/static/app/public.js b/static/app/public.js index 83433de18..f88d7c7d5 100755 --- a/static/app/public.js +++ b/static/app/public.js @@ -5,6 +5,17 @@ $(document).ready(function() { }); }); +function toSize(a) { + var d = [" B", " KB", " MB", " GB", " TB", " PB"]; + var e = 1024; + for(var b = 0; b < d.length; b++) { + if(a < e) { + return(b == 0 ? a : a.toFixed(2)) + d[b] + } + a /= e + } +} + //转换单们到MB function toSizeM(byteLen) { var a = parseInt(byteLen) / 1024 / 1024; @@ -38,8 +49,13 @@ function refresh() { window.location.reload() } -var mdw = { -}; +function mwsPost(path, args, callback){ + $.post(path, args, function(rdata){ + if(typeof(callback) == 'function'){ + callback(rdata); + } + },'json'); +} function repeatPwd(a) { $("#MyPassword").val(RandomStrPwd(a)) @@ -97,16 +113,7 @@ function getLocalTime(a) { return new Date(parseInt(a) * 1000).format("yyyy/MM/dd hh:mm:ss") } -function toSize(a) { - var d = [" B", " KB", " MB", " GB", " TB", " PB"]; - var e = 1024; - for(var b = 0; b < d.length; b++) { - if(a < e) { - return(b == 0 ? a : a.toFixed(2)) + d[b] - } - a /= e - } -} + function ChangePath(d) { @@ -371,7 +378,6 @@ function openPath(a) { window.location.href = "/files" } - function onlineEditFile(k, f) { if(k != 0) { var l = $("#PathPlace input").val(); diff --git a/static/css/site.css b/static/css/site.css index fbdc167c7..fcba2812e 100755 --- a/static/css/site.css +++ b/static/css/site.css @@ -173,116 +173,121 @@ html { } .plr10 { - padding: 0 10px + padding: 0 10px; } .plr15 { - padding: 0 15px + padding: 0 15px; } .plr20 { - padding: 0 20px + padding: 0 20px; } .ptb10 { - padding: 10px 0 + padding: 10px 0; } .ptb15 { - padding: 15px 0 + padding: 15px 0; } .ptb20 { - padding: 20px 0 + padding: 20px 0; } .pd0 { - padding: 0 + padding: 0; } .pd15 { - padding: 15px + padding: 15px; } .pd20 { - padding: 20px + padding: 20px; } .pr8 { - padding-right: 8px + padding-right: 8px; } .pl7 { - padding-left: 7px + padding-left: 7px; } .pb15 { - padding-bottom: 15px + padding-bottom: 15px; } .pb55 { - padding-bottom: 55px + padding-bottom: 55px; } .pb70 { - padding-bottom: 70px + padding-bottom: 70px; } .mt10 { - margin-top: 10px + margin-top: 10px; } .mtb10 { - margin: 10px 0 + margin: 10px 0; } .mtb15 { - margin: 15px 0 + margin: 15px 0; } .mtb20 { - margin: 20px 0 + margin: 20px 0; } .mlr15 { - margin: 0 15px + margin: 0 15px; } .mlr20 { - margin: 0 20px + margin: 0 20px; } .mb15 { - margin-bottom: 15px + margin-bottom: 15px; } .mr50 { - margin-right: 50px -} -.ml33{ - margin-left: 38px -} -.ml45 { - margin-left: 50px -} - -.ml5 { - margin-left: 5px -} - -.mr5 { - margin-right: 5px + margin-right: 50px; } .mr20 { - margin-right: 20px + margin-right: 20px; +} + +.ml33{ + margin-left: 38px; +} +.ml45 { + margin-left: 50px; +} + +.ml5 { + margin-left: 5px; +} + +.mr5 { + margin-right: 5px; +} + +.mr20 { + margin-right: 20px; } .ml0{ margin-left: 0; } .mg10 { - margin: 10px + margin: 10px; } .va0 { diff --git a/templates/default/firewall.html b/templates/default/firewall.html index 501ed7f97..12fb6646f 100755 --- a/templates/default/firewall.html +++ b/templates/default/firewall.html @@ -14,7 +14,7 @@ margin: 0 30px; } .weblog span{ - margin-right:30px; + margin-right:5px; } .weblog a{ color:#20a53a; @@ -38,7 +38,7 @@
-
+
启用SSH @@ -48,7 +48,7 @@
-
+
SSH端口: @@ -57,7 +57,7 @@
-
+
启用禁ping
@@ -65,18 +65,33 @@
-
- Web日志:/www/wwwlog0KB - + +
+
+
+ 启用防火墙 +
+ +
+
+
+ +
+ Web日志:/Users/midoks/Desktop/fwww/wwwlogs0KB + +
+
-

防火墙

+

防火墙

+ +