mirror of
https://github.com/midoks/mdserver-web.git
synced 2026-10-20 08:25:50 +08:00
update
This commit is contained in:
@@ -93,8 +93,10 @@ with app.app_context():
|
||||
if setup_db_required:
|
||||
setup.init_admin_user()
|
||||
setup.init_option()
|
||||
setup.init_cmd()
|
||||
setup.init_db_system()
|
||||
|
||||
|
||||
app.config['BASIC_AUTH_OPEN'] = False
|
||||
with app.app_context():
|
||||
basic_auth = model.getOptionByJson('basic_auth', default={'open':False})
|
||||
@@ -155,6 +157,7 @@ def page_unauthorized(error):
|
||||
# 设置模板全局变量
|
||||
@app.context_processor
|
||||
def inject_global_variables():
|
||||
start_t = time.time()
|
||||
ver = config.APP_VERSION;
|
||||
if mw.isDebugMode():
|
||||
ver = ver + str(time.time())
|
||||
@@ -165,6 +168,9 @@ def inject_global_variables():
|
||||
'title' : 'MW面板',
|
||||
'ip' : '127.0.0.1'
|
||||
}
|
||||
|
||||
end_t = time.time()
|
||||
print("cos:"+str(end_t-start_t))
|
||||
return dict(config=g_config, data=data)
|
||||
|
||||
|
||||
|
||||
@@ -36,6 +36,10 @@ def index():
|
||||
@blueprint.route('/<path>',endpoint='admin_safe_path',methods=['GET'])
|
||||
def admin_safe_path(path):
|
||||
db_path = model.getOption('admin_path')
|
||||
|
||||
if isLogined():
|
||||
return redirect('/')
|
||||
|
||||
if db_path == path:
|
||||
return render_template('default/login.html')
|
||||
|
||||
@@ -63,7 +67,7 @@ def webhook():
|
||||
|
||||
wh_install_path = mw.getServerDir() + '/webhook'
|
||||
if not os.path.exists(wh_install_path):
|
||||
return mw.returnJson(False, '请先安装WebHook插件!')
|
||||
return mw.returnData(False, '请先安装WebHook插件!')
|
||||
|
||||
package = mw.getPanelDir() + "/plugins/webhook"
|
||||
if not package in sys.path:
|
||||
|
||||
@@ -19,7 +19,7 @@ from .option import setOption
|
||||
from .sites import getSitesCount
|
||||
|
||||
from .task import addTask
|
||||
from .task import getTaskCount,getTaskUnexecutedCount,getTaskList,getTaskFirstByRun
|
||||
from .task import getTaskCount,getTaskUnexecutedCount,getTaskList,getTaskFirstByRun,getTaskRunList
|
||||
from .task import setTaskStatus,setTaskData
|
||||
|
||||
from .user import isLoginCheck
|
||||
|
||||
@@ -71,5 +71,4 @@ def setOption(name, value,
|
||||
|
||||
db.session.query(Option).filter_by(name=name, type=type).update({"value":value})
|
||||
db.session.commit()
|
||||
db.session.close()
|
||||
return True
|
||||
@@ -88,6 +88,26 @@ def getTaskList(
|
||||
rows.append(t)
|
||||
return rows
|
||||
|
||||
def getTaskRunList(
|
||||
page: int | None = 1,
|
||||
size: int | None = 10,
|
||||
):
|
||||
pagination = Tasks.query.filter(Tasks.status!=1).order_by(Tasks.id.asc()).paginate(page=int(page), per_page=int(size))
|
||||
|
||||
rows = []
|
||||
for item in pagination.items:
|
||||
t = {}
|
||||
t['id'] = item.id
|
||||
t['name'] = item.name
|
||||
t['type'] = item.type
|
||||
t['cmd'] = item.cmd
|
||||
t['start'] = item.start
|
||||
t['end'] = item.end
|
||||
t['status'] = item.status
|
||||
t['add_time'] = item.add_time
|
||||
rows.append(t)
|
||||
return rows
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -11,4 +11,5 @@
|
||||
from .user import init_admin_user
|
||||
from .option import init_option
|
||||
from .init_db_system import init_db_system
|
||||
from .init_cmd import init_cmd
|
||||
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
# coding:utf-8
|
||||
|
||||
# ---------------------------------------------------------------------------------
|
||||
# MW-Linux面板
|
||||
# ---------------------------------------------------------------------------------
|
||||
# copyright (c) 2018-∞(https://github.com/midoks/mdserver-web) All rights reserved.
|
||||
# ---------------------------------------------------------------------------------
|
||||
# Author: midoks <midoks@163.com>
|
||||
# ---------------------------------------------------------------------------------
|
||||
|
||||
import os
|
||||
import shutil
|
||||
|
||||
import core.mw as mw
|
||||
|
||||
def contentReplace(src, dst):
|
||||
content = mw.readFile(src)
|
||||
content = content.replace("{$SERVER_PATH}", mw.getFatherDir())
|
||||
|
||||
content += "\n# make:{0}".format(mw.formatDate())
|
||||
mw.writeFile(dst, content)
|
||||
|
||||
|
||||
def init_cmd():
|
||||
script = mw.getPanelDir() + '/scripts/init.d/mw.tpl'
|
||||
script_bin = mw.getPanelDir() + '/scripts/init.d/mw'
|
||||
contentReplace(script, script_bin)
|
||||
mw.execShell('chmod +x ' + script_bin)
|
||||
|
||||
# 在linux系统中,确保/etc/init.d存在
|
||||
if not mw.isAppleSystem() and not os.path.exists("/etc/rc.d/init.d"):
|
||||
mw.execShell('mkdir -p /etc/rc.d/init.d')
|
||||
|
||||
if not mw.isAppleSystem() and not os.path.exists("/etc/init.d"):
|
||||
mw.execShell('mkdir -p /etc/init.d')
|
||||
|
||||
# initd
|
||||
if os.path.exists('/etc/rc.d/init.d'):
|
||||
initd_bin = '/etc/rc.d/init.d/mw'
|
||||
if not os.path.exists(initd_bin):
|
||||
shutil.copyfile(script_bin, initd_bin)
|
||||
mw.execShell('chmod +x ' + initd_bin)
|
||||
# 加入自启动
|
||||
mw.execShell('which chkconfig && chkconfig --add mw')
|
||||
|
||||
if os.path.exists('/etc/init.d'):
|
||||
initd_bin = '/etc/init.d/mw'
|
||||
if not os.path.exists(initd_bin):
|
||||
import shutil
|
||||
shutil.copyfile(script_bin, initd_bin)
|
||||
mw.execShell('chmod +x ' + initd_bin)
|
||||
# 加入自启动
|
||||
mw.execShell('which update-rc.d && update-rc.d -f mw defaults')
|
||||
return True
|
||||
@@ -34,9 +34,6 @@ def init_option():
|
||||
# basic auth 配置
|
||||
model.setOption('basic_auth', json.dumps({'open':False}))
|
||||
|
||||
|
||||
|
||||
|
||||
# 开启后台任务
|
||||
# model.setOption('run_bg_task', 'close')
|
||||
|
||||
@@ -50,7 +47,10 @@ def init_option():
|
||||
|
||||
# 初始化安全路径
|
||||
model.setOption('admin_path', mw.getRandomString(8))
|
||||
model.setOption('server_ip', '127.0.0.1')
|
||||
|
||||
|
||||
ip = mw.getLocalIp()
|
||||
model.setOption('server_ip', ip)
|
||||
|
||||
# 默认备份目录
|
||||
model.setOption('backup_path', mw.getFatherDir()+'/backup')
|
||||
|
||||
@@ -17,6 +17,7 @@ from admin.model import db,Tasks
|
||||
from admin.user_login_check import panel_login_required
|
||||
|
||||
import core.mw as mw
|
||||
import utils.task as MwTasks
|
||||
|
||||
blueprint = Blueprint('task', __name__, url_prefix='/task', template_folder='../../templates/default')
|
||||
|
||||
@@ -98,9 +99,16 @@ def get_task_speed():
|
||||
data['isDownload'] = False
|
||||
|
||||
|
||||
data['task'] = model.getTaskList(status=-1)
|
||||
data['task'] = model.getTaskRunList()
|
||||
return data
|
||||
|
||||
|
||||
@blueprint.route('/remove_task', endpoint='remove_task', methods=['POST'])
|
||||
@panel_login_required
|
||||
def remove_task():
|
||||
task_id = request.form.get('id', '')
|
||||
if task_id == '':
|
||||
return mw.returnData(False, '任务ID不能为空!')
|
||||
return MwTasks.removeTask(task_id)
|
||||
|
||||
|
||||
|
||||
+55
-12
@@ -199,10 +199,6 @@ def writeFile(filename, content, mode='w+'):
|
||||
except Exception as e:
|
||||
return False
|
||||
|
||||
def triggerTask():
|
||||
isTask = getPanelDir() + '/tmp/panelTask.pl'
|
||||
writeFile(isTask, 'True')
|
||||
|
||||
|
||||
def systemdCfgDir():
|
||||
# ubuntu
|
||||
@@ -263,6 +259,39 @@ def getClientIp():
|
||||
from flask import request
|
||||
return request.remote_addr.replace('::ffff:', '')
|
||||
|
||||
def getLocalIp():
|
||||
filename = getPanelDir()+'/data/iplist.txt'
|
||||
try:
|
||||
ipaddress = readFile(filename)
|
||||
if not ipaddress or ipaddress == '127.0.0.1':
|
||||
cmd = "curl --insecure -4 -sS --connect-timeout 5 -m 60 https://v6r.ipip.net/?format=text"
|
||||
ip = execShell(cmd)
|
||||
result = ip[0].strip()
|
||||
if result == '':
|
||||
raise Exception("ipv4 is empty!")
|
||||
writeFile(filename, result)
|
||||
return result
|
||||
return ipaddress
|
||||
except Exception as e:
|
||||
cmd = "curl --insecure -6 -sS --connect-timeout 5 -m 60 https://v6r.ipip.net/?format=text"
|
||||
ip = execShell(cmd)
|
||||
result = ip[0].strip()
|
||||
if result == '':
|
||||
return '127.0.0.1'
|
||||
writeFile(filename, result)
|
||||
return result
|
||||
finally:
|
||||
pass
|
||||
return '127.0.0.1'
|
||||
|
||||
|
||||
def inArray(arrays, searchStr):
|
||||
# 搜索数据中是否存在
|
||||
for key in arrays:
|
||||
if key == searchStr:
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
def getJson(data):
|
||||
import json
|
||||
@@ -541,13 +570,6 @@ def isNumber(s):
|
||||
|
||||
return False
|
||||
|
||||
def isRestart():
|
||||
# 检查是否允许重启
|
||||
num = M('tasks').where('status!=?', ('1',)).count()
|
||||
if num > 0:
|
||||
return False
|
||||
return True
|
||||
|
||||
# 检查端口是否占用
|
||||
def isOpenPort(port):
|
||||
import socket
|
||||
@@ -633,13 +655,33 @@ def deDoubleCrypt(key, strings):
|
||||
writeFileLog(getTracebackInfo())
|
||||
return strings
|
||||
|
||||
# ------------------------------ openresty start -----------------------------
|
||||
# ------------------------------ panel start -----------------------------
|
||||
|
||||
def isRestart():
|
||||
# 检查是否允许重启
|
||||
num = M('tasks').where('status!=?', ('1',)).count()
|
||||
if num > 0:
|
||||
return False
|
||||
return True
|
||||
|
||||
def triggerTask():
|
||||
isTask = getPanelDir() + '/tmp/panelTask.pl'
|
||||
writeFile(isTask, 'True')
|
||||
|
||||
def restartTask():
|
||||
initd = getPanelDir() + '/scripts/init.d/mw'
|
||||
if os.path.exists(initd):
|
||||
cmd = initd + ' ' + 'restart_task'
|
||||
os.system(cmd)
|
||||
return True
|
||||
|
||||
def restartMw():
|
||||
restart_file = getPanelDir()+'/data/restart.pl'
|
||||
writeFile(restart_file, 'True')
|
||||
return True
|
||||
# ------------------------------ panel end -----------------------------
|
||||
|
||||
# ------------------------------ openresty start -----------------------------
|
||||
def restartWeb():
|
||||
return opWeb("reload")
|
||||
|
||||
@@ -667,6 +709,7 @@ def opWeb(method):
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
def opLuaMake(cmd_name):
|
||||
path = getServerDir() + '/web_conf/nginx/lua/lua.conf'
|
||||
root_dir = getServerDir() + '/web_conf/nginx/lua/' + cmd_name
|
||||
|
||||
@@ -941,7 +941,7 @@ function removeTask(b) {
|
||||
time: 0,
|
||||
shade: [0.3, "#000"]
|
||||
});
|
||||
$.post("/files/remove_task", "id=" + b, function(c) {
|
||||
$.post("/task/remove_task", "id=" + b, function(c) {
|
||||
layer.close(a);
|
||||
layer.msg(c.msg, {
|
||||
icon: c.status ? 1 : 5
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
# coding:utf-8
|
||||
|
||||
# ---------------------------------------------------------------------------------
|
||||
# MW-Linux面板
|
||||
# ---------------------------------------------------------------------------------
|
||||
# copyright (c) 2018-∞(https://github.com/midoks/mdserver-web) All rights reserved.
|
||||
# ---------------------------------------------------------------------------------
|
||||
# Author: midoks <midoks@163.com>
|
||||
# ---------------------------------------------------------------------------------
|
||||
|
||||
from .option import *
|
||||
@@ -0,0 +1,28 @@
|
||||
# coding:utf-8
|
||||
|
||||
# ---------------------------------------------------------------------------------
|
||||
# MW-Linux面板
|
||||
# ---------------------------------------------------------------------------------
|
||||
# copyright (c) 2018-∞(https://github.com/midoks/mdserver-web) All rights reserved.
|
||||
# ---------------------------------------------------------------------------------
|
||||
# Author: midoks <midoks@163.com>
|
||||
# ---------------------------------------------------------------------------------
|
||||
|
||||
|
||||
import core.mw as mw
|
||||
|
||||
def getOption(name,
|
||||
type: str | None = 'common',
|
||||
default : str | None = None
|
||||
) -> str:
|
||||
'''
|
||||
获取配置的值
|
||||
:name -> str 名称 (必填)
|
||||
:type -> str 类型 (可选|默认common)
|
||||
:default -> str 默认值 (可选)
|
||||
'''
|
||||
data = mw.M('option').field('name').where('name=? and type=?',(name, type,)).getField('value')
|
||||
|
||||
if len(data) == 0:
|
||||
return default
|
||||
return data
|
||||
+1
-1
@@ -397,4 +397,4 @@ def setMode(path):
|
||||
s_path = os.path.dirname(path)
|
||||
p_stat = os.stat(s_path)
|
||||
os.chown(path, p_stat.st_uid, p_stat.st_gid)
|
||||
os.chmod(path, p_stat.st_mode)
|
||||
os.chmod(path, p_stat.st_mode)
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
# coding:utf-8
|
||||
|
||||
# ---------------------------------------------------------------------------------
|
||||
# MW-Linux面板
|
||||
# ---------------------------------------------------------------------------------
|
||||
# copyright (c) 2018-∞(https://github.com/midoks/mdserver-web) All rights reserved.
|
||||
# ---------------------------------------------------------------------------------
|
||||
# Author: midoks <midoks@163.com>
|
||||
# ---------------------------------------------------------------------------------
|
||||
|
||||
import os
|
||||
import pwd
|
||||
import time
|
||||
|
||||
import core.mw as mw
|
||||
|
||||
# 删除进程下的所有进程
|
||||
def removeTaskRecursion(pid):
|
||||
cmd = "ps -ef|grep %s | grep -v grep |sed -n '2,1p' | awk '{print $2}'" % pid
|
||||
sub_pid = mw.execShell(cmd)
|
||||
if sub_pid[0].strip() == '':
|
||||
return 'ok'
|
||||
|
||||
if sub_pid[0].strip() == pid :
|
||||
return 'ok'
|
||||
|
||||
removeTaskRecursion(sub_pid[0].strip())
|
||||
mw.execShell('kill -9 ' + sub_pid[0].strip())
|
||||
return sub_pid[0].strip()
|
||||
|
||||
# 删除任务
|
||||
def removeTask(task_id):
|
||||
try:
|
||||
name = mw.M('tasks').where('id=?', (task_id,)).getField('name')
|
||||
status = mw.M('tasks').where('id=?', (task_id,)).getField('status')
|
||||
mw.M('tasks').delete(task_id)
|
||||
if str(status) == '-1':
|
||||
cmd = "ps aux | grep 'panel_task.py' | grep -v grep |awk '{print $2}'"
|
||||
task_pid = mw.execShell(cmd)
|
||||
task_list = task_pid[0].strip().split("\n")
|
||||
for i in range(len(task_list)):
|
||||
removeTaskRecursion(task_list[i])
|
||||
t = mw.execShell('kill -9 ' + task_list[i])
|
||||
print(t)
|
||||
mw.triggerTask()
|
||||
mw.restartTask()
|
||||
except Exception as e:
|
||||
mw.restartTask()
|
||||
|
||||
# 删除日志
|
||||
task_log = mw.getPanelDir() + "/tmp/panelTask.pl"
|
||||
if os.path.exists(task_log):
|
||||
os.remove(task_log)
|
||||
return mw.returnData(True, '任务已删除!')
|
||||
Reference in New Issue
Block a user