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/pureftp/index.py

469 lines
11 KiB

6 years ago
# coding:utf-8
import sys
import io
import os
import time
6 years ago
import shutil
6 years ago
sys.path.append(os.getcwd() + "/class/core")
import mw
6 years ago
app_debug = False
if mw.isAppleSystem():
6 years ago
app_debug = True
def getPluginName():
return 'pureftp'
def getPluginDir():
return mw.getPluginDir() + '/' + getPluginName()
6 years ago
def getServerDir():
return mw.getServerDir() + '/' + getPluginName()
6 years ago
def getInitDFile():
if app_debug:
return '/tmp/' + getPluginName()
return '/etc/init.d/' + getPluginName()
def getConf():
path = getServerDir() + "/etc/pure-ftpd.conf"
return path
def getInitDTpl():
path = getPluginDir() + "/init.d/pure-ftpd.tpl"
return path
def getArgs():
6 years ago
args = sys.argv[2:]
6 years ago
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
3 years ago
def checkArgs(data, ck=[]):
for i in range(len(ck)):
if not ck[i] in data:
return (False, mw.returnJson(False, '参数:(' + ck[i] + ')没有!'))
return (True, mw.returnJson(True, 'ok'))
6 years ago
def status():
6 years ago
cmd = "ps -ef|grep pure-ftpd |grep -v grep | grep -v python | awk '{print $2}'"
data = mw.execShell(cmd)
6 years ago
if data[0] == '':
return 'stop'
return 'start'
def contentReplace(content):
service_path = mw.getServerDir()
content = content.replace('{$ROOT_PATH}', mw.getRootDir())
6 years ago
content = content.replace('{$SERVER_PATH}', service_path)
return content
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
6 years ago
if not os.path.exists(file_bin):
content = mw.readFile(file_tpl)
6 years ago
content = contentReplace(content)
mw.writeFile(file_bin, content)
mw.execShell('chmod +x ' + file_bin)
6 years ago
6 years ago
pureSbinConfig = getServerDir() + "/sbin/pure-config.pl"
6 years ago
if not os.path.exists(pureSbinConfig):
pureTplConfig = getPluginDir() + "/init.d/pure-config.pl"
content = mw.readFile(pureTplConfig)
6 years ago
content = contentReplace(content)
mw.writeFile(pureSbinConfig, content)
mw.execShell('chmod +x ' + pureSbinConfig)
6 years ago
pureFtpdConfig = getServerDir() + "/etc/pure-ftpd.conf"
pureFtpdConfigBak = getServerDir() + "/etc/pure-ftpd.bak.conf"
pureFtpdConfigTpl = getPluginDir() + "/conf/pure-ftpd.conf"
if not os.path.exists(pureFtpdConfigBak):
shutil.copyfile(pureFtpdConfig, pureFtpdConfigBak)
content = mw.readFile(pureFtpdConfigTpl)
6 years ago
content = contentReplace(content)
mw.writeFile(pureFtpdConfig, content)
6 years ago
# systemd
systemDir = '/lib/systemd/system'
systemService = systemDir + '/pureftp.service'
systemServiceTpl = getPluginDir() + '/init.d/pureftp.service.tpl'
if os.path.exists(systemDir) and not os.path.exists(systemService):
se_content = mw.readFile(systemServiceTpl)
se_content = se_content.replace('{$SERVER_PATH}', service_path)
mw.writeFile(systemService, se_content)
mw.execShell('systemctl daemon-reload')
6 years ago
return file_bin
3 years ago
def pfOp(method):
6 years ago
file = initDreplace()
3 years ago
if not mw.isAppleSystem():
data = mw.execShell('systemctl ' + method + ' pureftp')
if data[1] == '':
return 'ok'
return 'fail'
data = mw.execShell(file + ' ' + method)
6 years ago
if data[1] == '':
return 'ok'
6 years ago
return data[1]
6 years ago
3 years ago
def start():
return pfOp('start')
6 years ago
def stop():
3 years ago
return pfOp('stop')
6 years ago
def restart():
3 years ago
return pfOp('restart')
6 years ago
def reload():
3 years ago
return pfOp('reload')
6 years ago
def initdStatus():
3 years ago
if mw.isAppleSystem():
return "Apple Computer does not support"
shell_cmd = 'systemctl status pureftp | grep loaded | grep "enabled;"'
data = mw.execShell(shell_cmd)
if data[0] == '':
return 'fail'
return 'ok'
6 years ago
def initdInstall():
3 years ago
if mw.isAppleSystem():
return "Apple Computer does not support"
mw.execShell('systemctl start pureftp')
6 years ago
return 'ok'
def initdUinstall():
3 years ago
if mw.isAppleSystem():
return "Apple Computer does not support"
mw.execShell('systemctl stop pureftp')
6 years ago
return 'ok'
6 years ago
6 years ago
def pftpDB():
file = getServerDir() + '/ftps.db'
if not os.path.exists(file):
conn = mw.M('ftps').dbPos(getServerDir(), 'ftps')
csql = mw.readFile(getPluginDir() + '/conf/ftps.sql')
6 years ago
csql_list = csql.split(';')
for index in range(len(csql_list)):
conn.execute(csql_list[index], ())
else:
conn = mw.M('ftps').dbPos(getServerDir(), 'ftps')
6 years ago
return conn
6 years ago
def pftpUser():
if mw.isAppleSystem():
user = mw.execShell(
6 years ago
"who | sed -n '2, 1p' |awk '{print $1}'")[0].strip()
return user
return 'www'
6 years ago
def pftpAdd(username, password, path):
6 years ago
user = pftpUser()
6 years ago
if not os.path.exists(path):
6 years ago
os.makedirs(path)
if mw.isAppleSystem():
6 years ago
os.system('chown ' + user + '.staff ' + path)
else:
os.system('chown www.www ' + path)
6 years ago
cmd = getServerDir() + '/bin/pure-pw useradd ' + username + ' -u ' + user + ' -d ' + \
path + '<<EOF \n' + password + '\n' + password + '\nEOF'
return mw.execShell(cmd)
6 years ago
def pftpMod(username, password):
user = pftpUser()
6 years ago
cmd = getServerDir() + '/bin/pure-pw passwd ' + username + \
'<<EOF \n' + password + '\n' + password + '\nEOF'
return mw.execShell(cmd)
6 years ago
6 years ago
def pftpStop(username):
cmd = getServerDir() + '/bin/pure-pw usermod ' + username + ' -r 1'
return mw.execShell(cmd)
6 years ago
def pftpStart(username):
cmd = getServerDir() + '/bin/pure-pw usermod ' + username + " -r ''"
return mw.execShell(cmd)
6 years ago
6 years ago
def pftpReload():
3 years ago
cmd = getServerDir() + '/bin/pure-pw mkdb ' +
getServerDir() + '/etc/pureftpd.pdb'
mw.execShell(cmd)
6 years ago
def getWwwDir():
path = mw.getWwwDir()
6 years ago
return path
6 years ago
def getFtpPort():
import re
try:
file = getServerDir() + '/etc/pure-ftpd.conf'
conf = mw.readFile(file)
6 years ago
rep = "\n#?\s*Bind\s+[0-9]+\.[0-9]+\.[0-9]+\.+[0-9]+,([0-9]+)"
port = re.search(rep, conf).groups()[0]
except:
port = '21'
return port
def getFtpList():
6 years ago
args = getArgs()
page = 1
6 years ago
page_size = 10
search = ''
6 years ago
if 'page' in args:
page = int(args['page'])
if 'page_size' in args:
page_size = int(args['page_size'])
6 years ago
if 'search' in args:
search = args['search']
6 years ago
data = {}
6 years ago
conn = pftpDB()
6 years ago
limit = str((page - 1) * page_size) + ',' + str(page_size)
# print limit, search
condition = ''
if not search == '':
condition = "name like '%" + search + "%'"
field = 'id,pid,name,password,path,status,ps,addtime'
clist = conn.where(condition, ()).field(
field).limit(limit).order('id desc').select()
count = conn.where(condition, ()).count()
6 years ago
_page = {}
_page['count'] = count
_page['p'] = page
_page['row'] = page_size
_page['tojs'] = 'ftpList'
data['page'] = mw.getPage(_page)
6 years ago
6 years ago
info = {}
info['ip'] = mw.getLocalIp()
6 years ago
info['port'] = getFtpPort()
data['info'] = info
6 years ago
data['data'] = clist
6 years ago
return mw.getJson(data)
6 years ago
def addFtp():
6 years ago
import urllib
args = getArgs()
3 years ago
data = checkArgs(args, ['ftp_username', 'ftp_password', 'path', 'ps'])
if not data[0]:
return data[1]
6 years ago
3 years ago
path = urllib.request.unquote(args['path'])
6 years ago
user = args['ftp_username']
pwd = args['ftp_password']
ps = args['ps']
addtime = time.strftime('%Y-%m-%d %X', time.localtime())
data = pftpAdd(user, pwd, path)
conn = pftpDB()
conn.add('pid,name,password,path,status,ps,addtime',
(0, user, pwd, path, 1, ps, addtime))
pftpReload()
if data[1] == '':
return 'ok'
return data[0]
6 years ago
6 years ago
def delFtp():
args = getArgs()
3 years ago
data = checkArgs(args, ['id', 'username'])
if not data[0]:
return data[1]
6 years ago
mw.execShell(getServerDir() +
'/bin/pure-pw userdel ' + args['username'])
6 years ago
pftpReload()
conn = pftpDB()
conn.where("id=?", (args['id'],)).delete()
mw.writeLog('TYPE_FTP', 'FTP_DEL_SUCCESS', (args['username'],))
6 years ago
return 'ok'
6 years ago
def modFtp():
args = getArgs()
3 years ago
data = checkArgs(args, ['id', 'name', 'password'])
if not data[0]:
return data[1]
6 years ago
6 years ago
conn = pftpDB()
data = pftpMod(args['name'], args['password'])
6 years ago
pftpReload()
6 years ago
conn.where('id=?', (int(args['id']),)).save(
'password', (args['password'],))
# print data
6 years ago
if data[1] == '':
return 'ok'
return data[0]
6 years ago
def modFtpPort():
import re
args = getArgs()
if not 'port' in args:
return 'port missing'
try:
port = args['port']
if int(port) < 1 or int(port) > 65535:
return '端口范围不正确!'
file = file = getServerDir() + '/etc/pure-ftpd.conf'
conf = mw.readFile(file)
6 years ago
rep = u"\n#?\s*Bind\s+[0-9]+\.[0-9]+\.[0-9]+\.+[0-9]+,([0-9]+)"
# preg_match(rep,conf,tmp)
6 years ago
conf = re.sub(
rep, "\nBind 0.0.0.0," + port, conf)
mw.writeFile(file, conf)
6 years ago
restart()
return 'ok'
except Exception as ex:
return str(ex)
6 years ago
def stopPort():
args = getArgs()
if not 'id' in args:
return 'id missing'
if not 'username' in args:
return 'username missing'
if not 'status' in args:
return 'status missing'
data = pftpStop(args['username'])
6 years ago
pftpReload()
6 years ago
conn = pftpDB()
conn.where('id=?', (int(args['id']),)).save(
'status', (args['status'],))
if data[1] == '':
return 'ok'
return data[0]
def startPort():
args = getArgs()
if not 'id' in args:
return 'id missing'
if not 'username' in args:
return 'username missing'
if not 'status' in args:
return 'status missing'
data = pftpStart(args['username'])
6 years ago
pftpReload()
6 years ago
conn = pftpDB()
conn.where('id=?', (int(args['id']),)).save(
'status', (args['status'],))
if data[1] == '':
return 'ok'
return data[0]
6 years ago
if __name__ == "__main__":
func = sys.argv[1]
if func == 'status':
3 years ago
print(status())
6 years ago
elif func == 'start':
3 years ago
print(start())
6 years ago
elif func == 'stop':
3 years ago
print(stop())
6 years ago
elif func == 'restart':
3 years ago
print(restart())
6 years ago
elif func == 'reload':
3 years ago
print(reload())
6 years ago
elif func == 'initd_status':
3 years ago
print(initdStatus())
6 years ago
elif func == 'initd_install':
3 years ago
print(initdInstall())
6 years ago
elif func == 'initd_uninstall':
3 years ago
print(initdUinstall())
6 years ago
elif func == 'conf':
3 years ago
print(getConf())
6 years ago
elif func == 'get_www_dir':
3 years ago
print(getWwwDir())
6 years ago
elif func == 'get_ftp_list':
3 years ago
print(getFtpList())
6 years ago
elif func == 'add_ftp':
3 years ago
print(addFtp())
6 years ago
elif func == 'del_ftp':
3 years ago
print(delFtp())
6 years ago
elif func == 'mod_ftp':
3 years ago
print(modFtp())
6 years ago
elif func == 'mod_ftp_port':
3 years ago
print(modFtpPort())
6 years ago
elif func == 'stop_ftp':
3 years ago
print(stopPort())
6 years ago
elif func == 'start_ftp':
3 years ago
print(startPort())
6 years ago
else:
3 years ago
print('error')