def setAdminPathApi(self): admin_path = request.form.get('admin_path', '').strip() admin_path_checks = ['/', '/close', '/login', '/do_login', '/site', '/sites', '/download_file', '/control', '/crontab', '/firewall', '/files', 'config', '/soft', '/system', '/code', '/ssl', '/plugins'] if admin_path == '': admin_path = '/' if admin_path != '/': if len(admin_path) < 6: return mw.returnJson(False, '安全入口地址长度不能小于6位!') if admin_path in admin_path_checks: return mw.returnJson(False, '该入口已被面板占用,请使用其它入口!') if not re.match("^/[\w\./-_]+$", admin_path): return mw.returnJson(False, '入口地址格式不正确,示例: /my_panel') else: domain = mw.readFile('data/domain.conf') if not domain: domain = '' limitip = mw.readFile('data/limitip.conf') if not limitip: limitip = '' if not domain.strip() and not limitip.strip(): return mw.returnJson(False, '警告,关闭安全入口等于直接暴露你的后台地址在外网,十分危险,至少开启以下一种安全方式才能关闭:<a style="color:red;"><br>1、绑定访问域名<br>2、绑定授权IP</a>') admin_path_file = 'data/admin_path.pl' admin_path_old = '/' if os.path.exists(admin_path_file): admin_path_old = mw.readFile(admin_path_file).strip() if admin_path_old != admin_path: mw.writeFile(admin_path_file, admin_path) mw.restartMw() return mw.returnJson(True, '修改成功!')
def getIndexList(self): if not os.path.exists(self.__index): mw.writeFile(self.__index, '[]') indexList = json.loads(mw.readFile(self.__index)) plist = [] app = [] for i in indexList: info = i.split('-') if not info[0] in app: app.append(info[0]) path = self.__plugin_dir + '/' + info[0] if os.path.isdir(path): json_file = path + '/info.json' if os.path.exists(json_file): try: content = mw.readFile(json_file) if content: data = json.loads(content) tmp_data = self.makeList(data) for index in range(len(tmp_data)): if tmp_data[index]['versions'] == info[ 1] or info[1] in tmp_data[index][ 'versions']: tmp_data[index]['display'] = True plist.append(tmp_data[index]) continue except Exception as e: # raise e print('getIndexList:', e) # 使用gevent模式时,无法使用多进程 #plist = self.checkStatusMProcess(plist) plist = self.checkStatusMThreads(plist) return plist
def save(self, keys, param): # 更新数据 self.__GetConn() self.__DB_CONN.text_factory = str try: opt = "" for key in keys.split(','): opt += key + "=?," opt = opt[0:len(opt) - 1] sql = "UPDATE " + self.__DB_TABLE + " SET " + opt + self.__OPT_WHERE import mw mw.writeFile('/tmp/test.pl', sql) # 处理拼接WHERE与UPDATE参数 tmp = list(param) for arg in self.__OPT_PARAM: tmp.append(arg) self.__OPT_PARAM = tuple(tmp) result = self.__DB_CONN.execute(sql, self.__OPT_PARAM) self.__close() self.__DB_CONN.commit() return result.rowcount except Exception, ex: return "error: " + str(ex)
def pm2Add(): args = getArgs() data = checkArgs(args, ['path', 'run', 'pname']) if not data[0]: return data[1] path = args['path'] run = args['run'] pname = args['pname'] runFile = (path + '/' + run).replace('//', '/') if not os.path.exists(runFile): return mw.returnJson(False, '指定文件不存在!') nlist = pm2GetList() for node in nlist: if pname == node['name']: return mw.returnJson(False, '指定项目名称已经存在!') if os.path.exists(path + '/package.json' ) and not os.path.exists(path + '/package-lock.json'): mw.execShell(__SR + "cd " + path + ' && npm install -s') mw.execShell(__SR + 'cd ' + path + ' && pm2 start ' + runFile + ' --name "' + pname + '"|grep ' + pname) mw.execShell(__SR + 'pm2 save && pm2 startup') if not os.path.exists(__path): mw.execShell('mkdir -p ' + __path) mw.writeFile(__path + '/' + pname, path) return mw.returnJson(True, '添加成功!')
def modifyRule(): args = getArgs() data = checkArgs(args, ['index', 'ruleName', 'ruleBody', 'rulePs']) if not data[0]: return data[1] index = int(args['index']) ruleName = args['ruleName'] ruleBody = args['ruleBody'] rulePs = args['rulePs'] fpath = getRuleJsonPath(ruleName) content = mw.readFile(fpath) content = json.loads(content) tmp = content[index] tmp_k = [] tmp_k.append(tmp[0]) tmp_k.append(ruleBody) tmp_k.append(rulePs) tmp_k.append(tmp[3]) content[index] = tmp_k cjson = mw.getJson(content) mw.writeFile(fpath, cjson) return mw.returnJson(True, '设置成功!', content)
def siteEdate(): # 网站到期处理 global oldEdate try: if not oldEdate: oldEdate = mw.readFile('data/edate.pl') if not oldEdate: oldEdate = '0000-00-00' mEdate = time.strftime('%Y-%m-%d', time.localtime()) if oldEdate == mEdate: return False edateSites = mw.M('sites').where( 'edate>? AND edate<? AND (status=? OR status=?)', ('0000-00-00', mEdate, 1, u'正在运行')).field('id,name').select() import panelSite siteObject = panelSite.panelSite() for site in edateSites: get = MyBad('') get.id = site['id'] get.name = site['name'] siteObject.SiteStop(get) oldEdate = mEdate mw.writeFile('data/edate.pl', mEdate) except: pass
def addRule(): args = getArgs() data = checkArgs(args, ['ruleName', 'ruleValue', 'ps']) if not data[0]: return data[1] ruleValue = args['ruleValue'] ruleName = args['ruleName'] ps = args['ps'] fpath = getRuleJsonPath(ruleName) content = mw.readFile(fpath) content = json.loads(content) tmp_k = [] tmp_k.append(1) tmp_k.append(ruleValue) tmp_k.append(ps) tmp_k.append(1) content.append(tmp_k) cjson = mw.getJson(content) mw.writeFile(fpath, cjson) return mw.returnJson(True, '设置成功!', content)
def getIndexList(self): if not os.path.exists(self.__index): mw.writeFile(self.__index, '[]') indexList = json.loads(mw.readFile(self.__index)) plist = [] app = [] for i in indexList: info = i.split('-') if not info[0] in app: app.append(info[0]) path = self.__plugin_dir + '/' + info[0] if os.path.isdir(path): json_file = path + '/info.json' if os.path.exists(json_file): try: data = json.loads(mw.readFile(json_file)) tmp_data = self.makeList(data) for index in range(len(tmp_data)): if tmp_data[index]['versions'] == info[1] or info[ 1] in tmp_data[index]['versions']: tmp_data[index]['display'] = True plist.append(tmp_data[index]) continue except Exception, e: print 'getIndexList:', e
def getTaskSpeedApi(self): tempFile = os.getcwd() + '/tmp/panelExec.log' freshFile = os.getcwd() + '/tmp/panelFresh' find = mw.M('tasks').where('status=? OR status=?', ('-1', '0')).field('id,type,name,execstr').find() if not len(find): return mw.returnJson(False, '当前没有任务队列在执行-2!') isTask = os.getcwd() + '/tmp/panelTask.pl' mw.writeFile(isTask, 'True') echoMsg = {} echoMsg['name'] = find['name'] echoMsg['execstr'] = find['execstr'] if find['type'] == 'download': import json try: tmp = mw.readFile(tempFile) if len(tmp) < 10: return mw.returnJson(False, '当前没有任务队列在执行-3!') echoMsg['msg'] = json.loads(tmp) echoMsg['isDownload'] = True except: mw.M('tasks').where( "id=?", (find['id'],)).save('status', ('0',)) return mw.returnJson(False, '当前没有任务队列在执行-4!') else: echoMsg['msg'] = mw.getLastLine(tempFile, 10) echoMsg['isDownload'] = False echoMsg['task'] = mw.M('tasks').where("status!=?", ('1',)).field( 'id,status,name,type').order("id asc").select() return mw.getJson(echoMsg)
def setSiteObjOpen(): args = getArgs() data = checkArgs(args, ['siteName', 'obj']) if not data[0]: return data[1] siteName = args['siteName'] obj = args['obj'] path = getJsonPath('site') content = mw.readFile(path) content = json.loads(content) if type(content[siteName][obj]) == bool: if content[siteName][obj]: content[siteName][obj] = False else: content[siteName][obj] = True else: if content[siteName][obj]['open']: content[siteName][obj]['open'] = False else: content[siteName][obj]['open'] = True cjson = mw.getJson(content) mw.writeFile(path, cjson) return mw.returnJson(True, '设置成功!')
def initDreplace(): initd_tpl = getInitdTpl() initD_path = getServerDir() + '/init.d' if not os.path.exists(initD_path): os.mkdir(initD_path) file_bin = initD_path + '/' + getPluginName() if not os.path.exists(file_bin): content = mw.readFile(initd_tpl) content = contentReplace(content) mw.writeFile(file_bin, content) mw.execShell('chmod +x ' + file_bin) mysql_conf_dir = getServerDir() + '/etc' if not os.path.exists(mysql_conf_dir): os.mkdir(mysql_conf_dir) mysql_conf = mysql_conf_dir + '/my.cnf' if not os.path.exists(mysql_conf): mysql_conf_tpl = getPluginDir() + '/conf/my.cnf' content = mw.readFile(mysql_conf_tpl) content = contentReplace(content) mw.writeFile(mysql_conf, content) return file_bin
def addRec(): args = getArgs() data = checkArgs(args, ['name', 'path', 'pwd', 'ps']) if not data[0]: return data[1] args_name = args['name'] args_pwd = args['pwd'] args_path = args['path'] args_ps = args['ps'] pwd_path = appConfPwd() pwd_content = mw.readFile(pwd_path) pwd_content += args_name + ':' + args_pwd + "\n" mw.writeFile(pwd_path, pwd_content) path = appConf() content = mw.readFile(path) con = "\n\n" + '[' + args_name + ']' + "\n" con += 'path = ' + args_path + "\n" con += 'comment = ' + args_ps + "\n" con += 'auth users = ' + args_name + "\n" con += 'read only = false' content = content + con mw.writeFile(path, content) return mw.returnJson(True, '添加成功')
def projectScriptLoad(): args = getArgs() if not 'pname' in args: return 'project name missing' post_commit_tpl = getPluginDir() + '/hook/post-commit.tpl' hook_path = getServerDir() + '/data/repositories/' + \ args['pname'] + '/hooks' post_commit_file = hook_path + '/post-commit' pct_content = mw.readFile(post_commit_tpl) mw.writeFile(post_commit_file, pct_content) mw.execShell('chmod 777 ' + post_commit_file) commit_tpl = getPluginDir() + '/hook/commit.tpl' commit_file = hook_path + '/commit' ct_content = mw.readFile(commit_tpl) ct_content = ct_content.replace('{$PROJECT_DIR}', mw.getRootDir()) ct_content = ct_content.replace('{$PORT}', getHttpPort()) ct_content = ct_content.replace('{$CSVN_USER}', getCsvnUser()) ct_content = ct_content.replace('{$CSVN_PWD}', getCsvnPwd(getCsvnUser())) mw.writeFile(commit_file, ct_content) mw.execShell('chmod 777 ' + commit_file) return 'ok'
def phpPrependFile(version): app_start = getServerDir() + '/app_start.php' if not os.path.exists(app_start): tpl = getPluginDir() + '/conf/app_start.php' content = mw.readFile(tpl) content = contentReplace(content, version) mw.writeFile(app_start, content)
def initDreplace(): ddir = getServerDir() + '/workers' if not os.path.exists(ddir): sdir = getPluginDir() + '/workers' mw.execShell('cp -rf ' + sdir + ' ' + getServerDir()) cfg = getServerDir() + '/db.cfg' if not os.path.exists(cfg): cfg_tpl = getPluginDir() + '/workers/db.cfg' content = mw.readFile(cfg_tpl) mw.writeFile(cfg, content) file_tpl = getInitDTpl() service_path = os.path.dirname(os.getcwd()) initD_path = getServerDir() + '/init.d' if not os.path.exists(initD_path): os.mkdir(initD_path) file_bin = initD_path + '/' + getPluginName() # initd replace content = mw.readFile(file_tpl) content = content.replace('{$SERVER_PATH}', service_path) mw.writeFile(file_bin, content) mw.execShell('chmod +x ' + file_bin) return file_bin
def setSshPortApi(self): port = request.form.get('port', '1').strip() if int(port) < 22 or int(port) > 65535: return mw.returnJson(False, '端口范围必需在22-65535之间!') ports = ['21', '25', '80', '443', '7200', '8080', '888', '8888'] if port in ports: return mw.returnJson(False, '(' + port + ')' + '特殊端口不可设置!') file = '/etc/ssh/sshd_config' conf = mw.readFile(file) rep = "#*Port\s+([0-9]+)\s*\n" conf = re.sub(rep, "Port " + port + "\n", conf) mw.writeFile(file, conf) if self.__isFirewalld: mw.execShell('setenforce 0') mw.execShell( 'sed -i "s#SELINUX=enforcing#SELINUX=disabled#" /etc/selinux/config') mw.execShell("systemctl restart sshd.service") elif self.__isUfw: mw.execShell('ufw allow ' + port + '/tcp') mw.execShell("service ssh restart") else: mw.execShell( 'iptables -I INPUT -p tcp -m state --state NEW -m tcp --dport ' + port + ' -j ACCEPT') mw.execShell("/etc/init.d/sshd restart") self.firewallReload() # mw.M('firewall').where( # "ps=?", ('SSH远程管理服务',)).setField('port', port) msg = mw.getInfo('改SSH端口为[{1}]成功!', port) mw.writeLog("防火墙管理", msg) return mw.returnJson(True, '修改成功!')
def removeTaskApi(self): mid = request.form.get('id', '').encode('utf-8') try: name = mw.M('tasks').where('id=?', (mid, )).getField('name') status = mw.M('tasks').where('id=?', (mid, )).getField('status') mw.M('tasks').delete(mid) if status == '-1': os.system( "kill `ps -ef |grep 'python panelSafe.pyc'|grep -v grep|grep -v panelExec|awk '{print $2}'`" ) os.system( "kill `ps aux | grep 'python task.pyc$'|awk '{print $2}'`") os.system(''' pids=`ps aux | grep 'sh'|grep -v grep|grep install|awk '{print $2}'` arr=($pids) for p in ${arr[@]} do kill -9 $p done ''') os.system('rm -f ' + name.replace('扫描目录[', '').replace(']', '') + '/scan.pl') isTask = mw.getRootDir() + '/tmp/panelTask.pl' mw.writeFile(isTask, 'True') os.system('/etc/init.d/mw start') except: os.system('/etc/init.d/mw start') return mw.returnJson(True, '任务已删除!')
def initDreplace(): file_tpl = getInitDTpl() service_path = os.path.dirname(os.getcwd()) initD_path = getServerDir() + '/init.d' if not os.path.exists(initD_path): os.mkdir(initD_path) file_bin = initD_path + '/' + getPluginName() # initd replace if not os.path.exists(file_bin): content = mw.readFile(file_tpl) content = contentReplace(content) mw.writeFile(file_bin, content) mw.execShell('chmod +x ' + file_bin) # config replace conf_bin = getConf() if not os.path.exists(conf_bin): conf_content = mw.readFile(getConfTpl()) conf_content = contentReplace(conf_content) mw.writeFile(getServerDir() + '/sphinx.conf', conf_content) mkdirAll() return file_bin
def addIpBlack(): args = getArgs() data = checkArgs(args, ['start_ip', 'end_ip']) if not data[0]: return data[1] start_ip = args['start_ip'] end_ip = args['end_ip'] path = getRuleJsonPath('ip_black') content = mw.readFile(path) content = json.loads(content) data = [] start_ip_list = start_ip.split('.') tmp = [] for x in range(len(start_ip_list)): tmp.append(int(start_ip_list[x])) end_ip_list = end_ip.split('.') tmp2 = [] for x in range(len(end_ip_list)): tmp2.append(int(end_ip_list[x])) data.append(tmp) data.append(tmp2) content.append(data) cjson = mw.getJson(content) mw.writeFile(path, cjson) return mw.returnJson(True, '设置成功!')
def initDreplace(): file_tpl = getInitdConfTpl() service_path = mw.getServerDir() initD_path = getServerDir() + '/init.d' if not os.path.exists(initD_path): os.mkdir(initD_path) file_bin = initD_path + '/' + getPluginName() if not os.path.exists(file_bin): content = mw.readFile(file_tpl) content = contentReplace(content) mw.writeFile(file_bin, content) mw.execShell('chmod +x ' + file_bin) conf_bin = getConf() if not os.path.exists(conf_bin): mw.execShell('mkdir -p ' + getServerDir() + '/custom/conf') conf_tpl = getConfTpl() content = mw.readFile(conf_tpl) content = contentReplace(content) mw.writeFile(conf_bin, content) log_path = getServerDir() + '/log' if not os.path.exists(log_path): os.mkdir(log_path) return file_bin
def binLog(): args = getArgs() conf = getConf() con = mw.readFile(conf) if con.find('#log-bin=mysql-bin') != -1: if args.has_key('status'): return mw.returnJson(False, '0') con = con.replace('#log-bin=mysql-bin', 'log-bin=mysql-bin') con = con.replace('#binlog_format=mixed', 'binlog_format=mixed') mw.execShell('sync') restart() else: path = getDataDir() if args.has_key('status'): dsize = 0 for n in os.listdir(path): if len(n) < 9: continue if n[0:9] == 'mysql-bin': dsize += os.path.getsize(path + '/' + n) return mw.returnJson(True, dsize) con = con.replace('log-bin=mysql-bin', '#log-bin=mysql-bin') con = con.replace('binlog_format=mixed', '#binlog_format=mixed') mw.execShell('sync') restart() mw.execShell('rm -f ' + path + '/mysql-bin.*') mw.writeFile(conf, con) return mw.returnJson(True, '设置成功!')
def initDreplace(): file_tpl = getInitDTpl() service_path = os.path.dirname(os.getcwd()) initD_path = getServerDir() + '/init.d' file_bin = initD_path + '/' + getPluginName() if not os.path.exists(initD_path): os.mkdir(initD_path) # initd replace content = mw.readFile(file_tpl) content = content.replace('{$SERVER_PATH}', service_path) mw.writeFile(file_bin, content) mw.execShell('chmod +x ' + file_bin) aria2_path = getServerDir() + '/aria2' mw.execShell('mkdir -p /tmp/download') if not os.path.exists(aria2_path): mw.execShell('mkdir -p ' + aria2_path) mw.execShell('touch ' + aria2_path + '/aria2.session') path = getPluginDir() + "/conf/aria2.conf" content = mw.readFile(path) content = content.replace('{$SERVER_PATH}', aria2_path) mw.writeFile(aria2_path + '/aria2.conf', content) return file_bin
def initDreplace(): file_tpl = getInitDTpl() service_path = os.path.dirname(os.getcwd()) initD_path = getServerDir() + '/init.d' # Openresty is not installed if not os.path.exists(getServerDir()): print("ok") exit(0) file_bin = initD_path + '/' + getPluginName() if not os.path.exists(initD_path): os.mkdir(initD_path) # initd replace content = mw.readFile(file_tpl) content = content.replace('{$SERVER_PATH}', service_path) mw.writeFile(file_bin, content) mw.execShell('chmod +x ' + file_bin) # config replace confReplace() # make nginx vhost or other makeConf() return file_bin
def initTotalInfo(): data = [] path_domains = getJsonPath('domains') path_total = getJsonPath('total') domain_contents = mw.readFile(path_domains) domain_contents = json.loads(domain_contents) try: total_contents = mw.readFile(path_total) except Exception as e: total_contents = "{}" total_contents = json.loads(total_contents) total_contents_new = {} for x in range(len(domain_contents)): name = domain_contents[x]['name'] if 'sites' in total_contents and name in total_contents['sites']: pass else: tmp = {} tmp['cdn'] = 0 tmp['log'] = 0 tmp['get'] = 0 tmp['post'] = 0 tmp['total'] = 0 _name = {} _name[name] = tmp total_contents['sites'] = _name cjson = mw.getJson(total_contents) mw.writeFile(path_total, cjson)
def initUserInfo(): data = mw.M('users').where('id=?', (1, )).getField('password') if data == '21232f297a57a5a743894a0e4a801fc3': 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))
def closePanelApi(self): filename = 'data/close.pl' if os.path.exists(filename): os.remove(filename) return mw.returnJson(True, '开启成功') mw.writeFile(filename, 'True') mw.execShell("chmod 600 " + filename) mw.execShell("chown root.root " + filename) return mw.returnJson(True, '面板已关闭!')
def getCachePhpVer(): cacheFile = getServerDir() + '/php.pl' v = '' if os.path.exists(cacheFile): v = mw.readFile(cacheFile) else: v = getPhpVer() mw.writeFile(cacheFile, v) return v
def importData(): args = getArgs() data = checkArgs(args, ['s_Name', 'pdata']) if not data[0]: return data[1] path = getRuleJsonPath(args['s_Name']) mw.writeFile(path, args['pdata']) return mw.returnJson(True, '设置成功!')
def makePhpIni(version): d_ini = mw.getServerDir() + '/php/' + version + '/etc/php.ini' if not os.path.exists(d_ini): s_ini = getPluginDir() + '/conf/php' + version[0:1] + '.ini' # shutil.copyfile(s_ini, d_ini) content = mw.readFile(s_ini) if version == '52': content = content + "auto_prepend_file=/www/server/php/app_start.php" mw.writeFile(d_ini, content)
def reload(): file = initDreplace() data = runShell(file + ' reload') solr_log = getServerDir() + "/code/logs/walle.log" mw.writeFile(solr_log, "") if data[1] == '': return 'ok' return 'fail'