Simple Linux Panel
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
mdserver-web/plugins/xui/index.py

224 lines
5.0 KiB

5 months ago
# coding:utf-8
import sys
import io
import os
import time
import re
web_dir = os.getcwd() + "/web"
if os.path.exists(web_dir):
sys.path.append(web_dir)
os.chdir(web_dir)
import core.mw as mw
app_debug = False
if mw.isAppleSystem():
app_debug = True
def getPluginName():
return 'xui'
def getPluginDir():
return mw.getPluginDir() + '/' + getPluginName()
def getServerDir():
return mw.getServerDir() + '/' + getPluginName()
def getArgs():
args = sys.argv[2:]
tmp = {}
args_len = len(args)
if args_len == 1:
t = args[0].strip('{').strip('}')
t = t.split(':')
tmp[t[0]] = t[1]
elif args_len > 1:
for i in range(len(args)):
t = args[i].split(':')
tmp[t[0]] = t[1]
return tmp
def status():
5 months ago
cmd = "ps -ef|grep x-ui |grep -v grep | grep -v python | awk '{print $2}'"
5 months ago
data = mw.execShell(cmd)
if data[0] == '':
return 'stop'
return 'start'
def getServiceFile():
systemDir = mw.systemdCfgDir()
5 months ago
return systemDir + '/xui.service'
5 months ago
5 months ago
def getXuiPort():
5 months ago
return '8349'
def __release_port(port):
from collections import namedtuple
try:
from utils.firewall import Firewall as MwFirewall
5 months ago
MwFirewall.instance().addAcceptPort(port, 'xui', 'port')
5 months ago
return port
except Exception as e:
return "Release failed {}".format(e)
def __delete_port(port):
from collections import namedtuple
try:
from utils.firewall import Firewall as MwFirewall
MwFirewall.instance().delAcceptPortCmd(port, 'tcp')
return port
except Exception as e:
return "Delete failed {}".format(e)
5 months ago
def pSqliteDb(dbname='databases'):
conn = mw.M(dbname).dbPos('/etc/x-ui', 'x-ui')
return conn
5 months ago
def initDreplace():
return 'ok'
5 months ago
def xuiOp(method):
5 months ago
file = initDreplace()
if not mw.isAppleSystem():
mw.execShell('systemctl daemon-reload')
5 months ago
data = mw.execShell('systemctl ' + method + ' x-ui')
5 months ago
if data[1] == '':
return 'ok'
return data[1]
return 'fail'
def start():
5 months ago
openPort()
5 months ago
return xuiOp('start')
5 months ago
def stop():
5 months ago
closePort()
5 months ago
return xuiOp('stop')
5 months ago
def restart():
5 months ago
return xuiOp('restart')
5 months ago
def reload():
return redisOp('reload')
def initdStatus():
if mw.isAppleSystem():
return "Apple Computer does not support"
5 months ago
shell_cmd = 'systemctl status x-ui | grep loaded | grep "enabled;"'
5 months ago
data = mw.execShell(shell_cmd)
if data[0] == '':
return 'fail'
return 'ok'
def initdInstall():
if mw.isAppleSystem():
return "Apple Computer does not support"
5 months ago
mw.execShell('systemctl enable x-ui')
5 months ago
return 'ok'
def initdUinstall():
if mw.isAppleSystem():
return "Apple Computer does not support"
5 months ago
mw.execShell('systemctl disable x-ui')
5 months ago
return 'ok'
5 months ago
def openPort():
5 months ago
setting = pSqliteDb('settings')
5 months ago
port_data = setting.field('id,key,value').where("key=?", ('webPort',)).find()
port = port_data['value']
__release_port(port)
def closePort():
5 months ago
setting = pSqliteDb('settings')
5 months ago
port_data = setting.field('id,key,value').where("key=?", ('webPort',)).find()
port = port_data['value']
__delete_port(port)
5 months ago
def getXuiInfo():
5 months ago
5 months ago
data = {}
5 months ago
user = pSqliteDb('users')
info = user.field('username,password').where("id=?", (1,)).find()
5 months ago
5 months ago
setting = pSqliteDb('settings')
5 months ago
port_data = setting.field('id,key,value').where("key=?", ('webPort',)).find()
path_data = setting.field('id,key,value').where("key=?", ('webBasePath',)).find()
5 months ago
if path_data is not None:
data['path'] = path_data['value']
else:
5 months ago
data['path'] = '/'
5 months ago
5 months ago
data['username'] = info['username']
data['password'] = info['password']
5 months ago
data['port'] = port_data['value']
data['ip'] = mw.getHostAddr()
5 months ago
return mw.returnJson(True, 'ok', data)
5 months ago
def installPreInspection():
sys = mw.execShell("cat /etc/*-release | grep PRETTY_NAME |awk -F = '{print $2}' | awk -F '\"' '{print $2}'| awk '{print $1}'")
if sys[1] != '':
return '不支持该系统'
sys_id = mw.execShell("cat /etc/*-release | grep VERSION_ID | awk -F = '{print $2}' | awk -F '\"' '{print $2}'")
sysName = sys[0].strip().lower()
sysId = sys_id[0].strip()
if sysName in ('opensuse'):
return '不支持该系统'
return 'ok'
if __name__ == "__main__":
func = sys.argv[1]
if func == 'status':
print(status())
elif func == 'start':
print(start())
elif func == 'stop':
print(stop())
elif func == 'restart':
print(restart())
elif func == 'reload':
print(reload())
elif func == 'initd_status':
print(initdStatus())
elif func == 'initd_install':
print(initdInstall())
elif func == 'initd_uninstall':
print(initdUinstall())
elif func == 'install_pre_inspection':
print(installPreInspection())
elif func == 'conf':
print(getServiceFile())
5 months ago
elif func == 'info':
print(getXuiInfo())
5 months ago
else:
print('error')