From ca659eb4f18f1b47517b642f4bdb61dec7beeb8c Mon Sep 17 00:00:00 2001 From: midoks Date: Sun, 26 Jun 2022 23:25:27 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81ipv6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 + app.py | 13 ++++- class/core/system_api.py | 7 +-- route/static/app/config.js | 2 +- scripts/init.d/mw.tpl | 113 ++++++++++++++++++++++--------------- setting.py | 6 +- task.py | 77 +++++++++++++------------ 7 files changed, 127 insertions(+), 93 deletions(-) diff --git a/.gitignore b/.gitignore index ba76a1498..168553aa0 100644 --- a/.gitignore +++ b/.gitignore @@ -133,3 +133,5 @@ data/ssl.pl data/port.pl plugins/openlitespeed plugins/gdrive/ +data/ipv6.pl +data/restart.pl diff --git a/app.py b/app.py index 906f56fdc..2b70e9f08 100644 --- a/app.py +++ b/app.py @@ -14,13 +14,22 @@ from geventwebsocket.handler import WebSocketHandler try: if __name__ == "__main__": - f = open('data/port.pl') - PORT = int(f.read()) + + PORT = 7200 + if os.path.exists('data/port.pl'): + f = open('data/port.pl') + PORT = int(f.read()) + f.close() + HOST = '0.0.0.0' + if os.path.exists('data/ipv6.pl'): + HOST = "::1" http_server = WSGIServer( (HOST, PORT), app, handler_class=WebSocketHandler) + http_server.serve_forever() + socketio.run(app, host=HOST, port=PORT) except Exception as ex: print(ex) diff --git a/class/core/system_api.py b/class/core/system_api.py index 54b0c2ba2..87fb6222f 100755 --- a/class/core/system_api.py +++ b/class/core/system_api.py @@ -102,12 +102,9 @@ class system_api: return mw.returnJson(True, '正在重启服务器!') ##### ----- end ----- ### - @mw_async def restartMw(self): - sleep(0.3) - # cmd = mw.getRunDir() + '/scripts/init.d/mw restart' - # print cmd - mw.execShell('service mw restart') + mw.writeFile('data/restart.pl', 'True') + return True @mw_async def restartServer(self): diff --git a/route/static/app/config.js b/route/static/app/config.js index d7f717366..226707a3e 100755 --- a/route/static/app/config.js +++ b/route/static/app/config.js @@ -209,7 +209,7 @@ function setIPv6() { $.post('/config/set_ipv6_status', {}, function (rdata) { layer.close(loadT); layer.msg(rdata.msg, {icon:rdata.status?1:2}); - setTimeout(function(){window.location.reload();},1500); + setTimeout(function(){window.location.reload();},5000); },'json'); } diff --git a/scripts/init.d/mw.tpl b/scripts/init.d/mw.tpl index fd773551b..dddbff662 100755 --- a/scripts/init.d/mw.tpl +++ b/scripts/init.d/mw.tpl @@ -26,62 +26,71 @@ else echo "" fi - -mw_start(){ - isStart=`ps -ef|grep 'gunicorn -c setting.py app:app' |grep -v grep|awk '{print $2}'` - if [ "$isStart" == '' ];then - echo -e "Starting mw... \c" - cd $mw_path && gunicorn -c setting.py app:app - port=$(cat ${mw_path}/data/port.pl) - isStart="" - while [[ "$isStart" == "" ]]; - do - echo -e ".\c" - sleep 0.5 - isStart=$(lsof -n -P -i:$port|grep LISTEN|grep -v grep|awk '{print $2}'|xargs) - let n+=1 - if [ $n -gt 15 ];then - break; - fi - done - if [ "$isStart" == '' ];then - echo -e "\033[31mfailed\033[0m" - echo '------------------------------------------------------' - tail -n 20 ${mw_path}/logs/error.log - echo '------------------------------------------------------' - echo -e "\033[31mError: mw service startup failed.\033[0m" - return; +mw_start_panel() +{ + isStart=`ps -ef|grep 'gunicorn -c setting.py app:app' |grep -v grep|awk '{print $2}'` + if [ "$isStart" == '' ];then + echo -e "Starting mw Panel... \c" + cd $mw_path && gunicorn -c setting.py app:app + port=$(cat ${mw_path}/data/port.pl) + isStart="" + while [[ "$isStart" == "" ]]; + do + echo -e ".\c" + sleep 0.5 + isStart=$(lsof -n -P -i:$port|grep LISTEN|grep -v grep|awk '{print $2}'|xargs) + let n+=1 + if [ $n -gt 15 ];then + break; fi - echo -e "\033[32mdone\033[0m" + done + if [ "$isStart" == '' ];then + echo -e "\033[31mfailed\033[0m" + echo '------------------------------------------------------' + tail -n 20 ${mw_path}/logs/error.log + echo '------------------------------------------------------' + echo -e "\033[31mError: mw Panel service startup failed.\033[0m" + return; + fi + echo -e "\033[32mdone\033[0m" else - echo "Starting mw... mw(pid $(echo $isStart)) already running" + echo "Starting mw Panel... mw(pid $(echo $isStart)) already running" fi +} +mw_start_task() +{ isStart=$(ps aux |grep 'task.py'|grep -v grep|awk '{print $2}') if [ "$isStart" == '' ];then - echo -e "Starting mw-tasks... \c" - cd $mw_path && python3 task.py >> ${mw_path}/logs/task.log 2>&1 & - sleep 0.3 - isStart=$(ps aux |grep 'task.py'|grep -v grep|awk '{print $2}') - if [ "$isStart" == '' ];then - echo -e "\033[31mfailed\033[0m" - echo '------------------------------------------------------' - tail -n 20 $mw_path/logs/task.log - echo '------------------------------------------------------' - echo -e "\033[31mError: mw-tasks service startup failed.\033[0m" - return; - fi - echo -e "\033[32mdone\033[0m" + echo -e "Starting mw-tasks... \c" + cd $mw_path && python3 task.py >> ${mw_path}/logs/task.log 2>&1 & + sleep 0.3 + isStart=$(ps aux |grep 'task.py'|grep -v grep|awk '{print $2}') + if [ "$isStart" == '' ];then + echo -e "\033[31mfailed\033[0m" + echo '------------------------------------------------------' + tail -n 20 $mw_path/logs/task.log + echo '------------------------------------------------------' + echo -e "\033[31mError: mw-tasks service startup failed.\033[0m" + return; + fi + echo -e "\033[32mdone\033[0m" else - echo "Starting mw-tasks... mw-tasks (pid $isStart) already running" + echo "Starting mw-tasks... mw-tasks (pid $isStart) already running" fi } +mw_start() +{ + mw_start_panel + mw_start_task +} -mw_stop() + +mw_stop_task() { - echo -e "Stopping mw-tasks... \c"; + echo -e "Stopping mw-tasks... \c"; pids=$(ps aux | grep 'task.py'|grep -v grep|awk '{print $2}') arr=($pids) @@ -90,20 +99,29 @@ mw_stop() kill -9 $p done echo -e "\033[32mdone\033[0m" +} - echo -e "Stopping mw... \c"; +mw_stop_panel() +{ + echo -e "Stopping mw Panel... \c"; arr=`ps aux|grep 'gunicorn -c setting.py app:app'|grep -v grep|awk '{print $2}'` - for p in ${arr[@]} + for p in ${arr[@]} do kill -9 $p &>/dev/null done if [ -f $pidfile ];then - rm -f $pidfile + rm -f $pidfile fi echo -e "\033[32mdone\033[0m" } +mw_stop() +{ + mw_stop_panel + mw_stop_task +} + mw_status() { isStart=$(ps aux|grep 'gunicorn -c setting.py app:app'|grep -v grep|awk '{print $2}') @@ -163,6 +181,9 @@ case "$1" in 'restart') mw_stop mw_start;; + 'restart_panel') + mw_stop_panel + mw_start_panel;; 'status') mw_status;; 'logs') error_logs;; 'default') diff --git a/setting.py b/setting.py index c60fd7c9e..0e55d6b19 100755 --- a/setting.py +++ b/setting.py @@ -34,10 +34,10 @@ else: mw.writeFile('data/port.pl', mw_port) bind = [] + +bind.append('0.0.0.0:%s' % mw_port) if os.path.exists('data/ipv6.pl'): - bind.append('[0:0:0:0:0:0:0:0]:%s' % mw_port) -else: - bind.append('0.0.0.0:%s' % mw_port) + bind.append('[::1]:%s' % mw_port) if workers > 2: workers = 2 diff --git a/task.py b/task.py index 5cee97a14..5e643e21f 100755 --- a/task.py +++ b/task.py @@ -25,7 +25,6 @@ import db import psutil - global pre, timeoutCount, logPath, isTask, oldEdate, isCheck pre = 0 timeoutCount = 0 @@ -45,6 +44,18 @@ if not os.path.exists(isTask): os.system("touch " + isTask) +def service_cmd(method): + cmd = '/etc/init.d/mw' + if os.path.exists(cmd): + execShell(cmd + ' ' + method) + return + + cmd = mw.getRunDir() + '/scripts/init.d/mw' + if os.path.exists(cmd): + execShell(cmd + ' ' + method) + return + + def mw_async(f): def wrapper(*args, **kwargs): thr = threading.Thread(target=f, args=args, kwargs=kwargs) @@ -94,7 +105,7 @@ def execShell(cmdstring, cwd=None, timeout=None, shell=True): t2 = str(data[1], encoding='utf-8') return (t1, t2) except Exception as e: - return None + return (None, None) def downloadFile(url, filename): @@ -168,32 +179,12 @@ def startTask(): except: pass # siteEdate() - # mainSafe() time.sleep(2) except: time.sleep(60) startTask() -def mainSafe(): - global isCheck - try: - if isCheck < 100: - isCheck += 1 - return True - isCheck = 0 - isStart = mw.execShell( - "ps aux |grep 'python3 main.py'|grep -v grep|awk '{print $2}'")[0] - if not isStart: - os.system('/etc/init.d/mw start') - isStart = mw.execShell( - "ps aux |grep 'python main.py'|grep -v grep|awk '{print $2}'")[0] - mw.writeLog('守护程序', '面板服务程序启动成功 -> PID: ' + isStart) - except: - time.sleep(30) - mainSafe() - - def siteEdate(): # 网站到期处理 global oldEdate @@ -508,30 +499,44 @@ def openrestyAutoRestart(): # --------------------------------------OpenResty Auto Restart End --------------------------------------------- # -if __name__ == "__main__": +# --------------------------------------Panel Restart Start --------------------------------------------- # +def restartPanelService(): + restartTip = 'data/restart.pl' + while True: + if os.path.exists(restartTip): + os.remove(restartTip) + service_cmd('restart_panel') + time.sleep(1) +# --------------------------------------Panel Restart End --------------------------------------------- # - # 系统监控 - t = threading.Thread(target=systemTask) + +def setDaemon(t): if sys.version_info.major == 3 and sys.version_info.minor >= 10: t.daemon = True else: t.setDaemon(True) - t.start() + return t + +if __name__ == "__main__": + + # 系统监控 + sysTask = threading.Thread(target=systemTask) + sysTask = setDaemon(sysTask) + sysTask.start() # PHP 502错误检查线程 - p = threading.Thread(target=check502Task) - if sys.version_info.major == 3 and sys.version_info.minor >= 10: - p.daemon = True - else: - p.setDaemon(True) - p.start() + php502 = threading.Thread(target=check502Task) + php502 = setDaemon(php502) + php502.start() # OpenResty Auto Restart Start oar = threading.Thread(target=openrestyAutoRestart) - if sys.version_info.major == 3 and sys.version_info.minor >= 10: - oar.daemon = True - else: - oar.setDaemon(True) + oar = setDaemon(oar) oar.start() + # OpenResty Auto Restart Start + rps = threading.Thread(target=restartPanelService) + rps = setDaemon(rps) + rps.start() + startTask()