mdserver-web/class/core/common.py

87 lines
1.9 KiB
Python
Raw Normal View History

2018-12-27 21:49:15 -05:00
# coding: utf-8
import os
import sys
import time
import string
import json
import hashlib
import shlex
import datetime
import subprocess
import re
import hashlib
2018-12-27 22:31:08 -05:00
from random import Random
2018-12-27 21:49:15 -05:00
2020-07-10 03:57:25 -04:00
import mw
2018-12-27 21:49:15 -05:00
import db
2019-02-21 10:05:09 -05:00
from flask import redirect
2018-12-27 22:31:08 -05:00
def init():
2019-02-21 10:05:09 -05:00
2018-12-27 22:31:08 -05:00
initDB()
initInitD()
2019-01-15 02:38:00 -05:00
initUserInfo()
2018-12-27 22:31:08 -05:00
2019-02-21 22:47:56 -05:00
def local():
result = checkClose()
if result:
return result
2019-02-21 10:05:09 -05:00
# 检查面板是否关闭
def checkClose():
if os.path.exists('data/close.pl'):
return redirect('/close')
2018-12-27 22:31:08 -05:00
def initDB():
try:
sql = db.Sql().dbfile('default')
2020-07-10 03:57:25 -04:00
csql = mw.readFile('data/sql/default.sql')
2018-12-27 22:31:08 -05:00
csql_list = csql.split(';')
for index in range(len(csql_list)):
sql.execute(csql_list[index], ())
2018-12-28 01:08:49 -05:00
2021-04-30 23:45:29 -04:00
except Exception as ex:
print(str(ex))
2018-12-27 22:31:08 -05:00
def initInitD():
2020-07-10 03:57:25 -04:00
script = mw.getRunDir() + '/scripts/init.d/mw.tpl'
script_bin = mw.getRunDir() + '/scripts/init.d/mw'
2019-01-19 02:12:44 -05:00
# if os.path.exists(script_bin):
# return
2018-12-27 22:31:08 -05:00
2020-07-10 03:57:25 -04:00
content = mw.readFile(script)
content = content.replace("{$SERVER_PATH}", mw.getRunDir())
2018-12-27 22:31:08 -05:00
2020-07-10 03:57:25 -04:00
mw.writeFile(script_bin, content)
mw.execShell('chmod +x ' + script_bin)
2018-12-27 22:31:08 -05:00
2020-07-14 22:43:10 -04:00
mw.setHostAddr(mw.getLocalIp())
2020-07-10 03:57:25 -04:00
if not mw.isAppleSystem():
2018-12-27 22:31:08 -05:00
initd_bin = '/etc/init.d/mw'
if not os.path.exists(initd_bin):
2019-01-19 02:20:51 -05:00
import shutil
2018-12-27 22:31:08 -05:00
shutil.copyfile(script_bin, initd_bin)
2020-07-10 03:57:25 -04:00
mw.execShell('chmod +x ' + initd_bin)
# 加入自启动
mw.execShell('chkconfig --add mw')
2022-06-12 13:17:36 -04:00
mw.execShell('systemctl enable mw')
2019-01-12 08:29:07 -05:00
def initUserInfo():
2020-07-10 03:57:25 -04:00
data = mw.M('users').where('id=?', (1,)).getField('password')
2019-01-15 02:38:00 -05:00
if data == '21232f297a57a5a743894a0e4a801fc3':
2020-07-10 03:57:25 -04:00
pwd = mw.getRandomString(8).lower()
file_pw = mw.getRunDir() + '/data/default.pl'
mw.writeFile(file_pw, pwd)
mw.M('users').where('id=?', (1,)).setField(
'password', mw.md5(pwd))