mdserver-web/plugins/l2tp/index.py

234 lines
5.5 KiB
Python
Raw Normal View History

2019-07-22 04:43:40 -04:00
# coding:utf-8
import sys
import io
import os
import time
import shutil
sys.path.append(os.getcwd() + "/class/core")
2020-07-10 03:57:25 -04:00
import mw
2019-07-22 04:43:40 -04:00
app_debug = False
2020-07-10 03:57:25 -04:00
if mw.isAppleSystem():
2019-07-22 04:43:40 -04:00
app_debug = True
def getPluginName():
return 'l2tp'
def getPluginDir():
2020-07-10 03:57:25 -04:00
return mw.getPluginDir() + '/' + getPluginName()
2019-07-22 04:43:40 -04:00
def getServerDir():
2020-07-10 03:57:25 -04:00
return mw.getServerDir() + '/' + getPluginName()
2019-07-22 04:43:40 -04:00
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 checkArgs(data, ck=[]):
for i in range(len(ck)):
if not ck[i] in data:
2020-07-10 03:57:25 -04:00
return (False, mw.returnJson(False, '参数:(' + ck[i] + ')没有!'))
return (True, mw.returnJson(True, 'ok'))
2019-07-22 04:43:40 -04:00
def status():
cmd = "ps -ef|grep xl2tpd |grep -v grep | grep -v python | awk '{print $2}'"
2020-07-10 03:57:25 -04:00
data = mw.execShell(cmd)
2019-07-22 04:43:40 -04:00
if data[0] == '':
return 'stop'
return 'start'
def initConf():
l2tp_cs = getServerDir() + '/chap-secrets'
if not os.path.exists(l2tp_cs):
2020-07-10 03:57:25 -04:00
mw.execShell('cp -rf ' + getPluginDir() +
'/tmp/chap-secrets' + ' ' + getServerDir())
2019-07-22 04:43:40 -04:00
l2tp_is = getServerDir() + '/ipsec.secrets'
if not os.path.exists(l2tp_is):
2020-07-10 03:57:25 -04:00
mw.execShell('cp -rf ' + getPluginDir() +
'/tmp/ipsec.secrets' + ' ' + getServerDir())
2019-07-22 04:43:40 -04:00
def start():
initConf()
2020-07-10 03:57:25 -04:00
if mw.isAppleSystem():
2019-07-22 04:43:40 -04:00
return "Apple Computer does not support"
2020-07-10 03:57:25 -04:00
data = mw.execShell('service xl2tpd start')
2019-07-22 04:43:40 -04:00
if data[0] == '':
return 'ok'
return data[1]
def stop():
2020-07-10 03:57:25 -04:00
if mw.isAppleSystem():
2019-07-22 04:43:40 -04:00
return "Apple Computer does not support"
2020-07-10 03:57:25 -04:00
data = mw.execShell('service xl2tpd stop')
2019-07-22 04:43:40 -04:00
if data[0] == '':
return 'ok'
return data[1]
def restart():
2020-07-10 03:57:25 -04:00
if mw.isAppleSystem():
2019-07-22 04:43:40 -04:00
return "Apple Computer does not support"
2020-07-10 03:57:25 -04:00
data = mw.execShell('service xl2tpd restart')
2019-07-22 04:43:40 -04:00
if data[0] == '':
return 'ok'
return data[1]
def reload():
2020-07-10 03:57:25 -04:00
data = mw.execShell('service xl2tpd reload')
2019-07-22 04:43:40 -04:00
if data[0] == '':
return 'ok'
return data[1]
def getPathFile():
2020-07-10 03:57:25 -04:00
if mw.isAppleSystem():
2019-07-22 04:43:40 -04:00
return getServerDir() + '/chap-secrets'
return '/etc/ppp/chap-secrets'
def getPathFilePsk():
2020-07-10 03:57:25 -04:00
if mw.isAppleSystem():
2019-07-22 04:43:40 -04:00
return getServerDir() + '/ipsec.secrets'
return '/etc/ipsec.secrets'
def getUserList():
import re
path = getPathFile()
if not os.path.exists(path):
2020-07-10 03:57:25 -04:00
return mw.returnJson(False, '密码配置文件不存在!')
conf = mw.readFile(path)
2019-07-22 04:43:40 -04:00
conf = re.sub('#(.*)\n', '', conf)
if conf.strip() == '':
2020-07-10 03:57:25 -04:00
return mw.returnJson(True, 'ok', [])
2019-07-22 04:43:40 -04:00
ulist = conf.strip().split('\n')
user = []
for line in ulist:
line_info = {}
line = re.match(r'(\w*)\s*(\w*)\s*(\w*)\s*(.*)',
line.strip(), re.M | re.I).groups()
line_info['user'] = line[0]
line_info['pwd'] = line[2]
line_info['type'] = line[1]
line_info['ip'] = line[3]
user.append(line_info)
2020-07-10 03:57:25 -04:00
return mw.returnJson(True, 'ok', user)
2019-07-22 04:43:40 -04:00
def addUser():
2020-07-10 03:57:25 -04:00
if mw.isAppleSystem():
return mw.returnJson(False, "Apple Computer does not support")
2019-07-22 04:43:40 -04:00
args = getArgs()
data = checkArgs(args, ['username'])
if not data[0]:
return data[1]
2020-07-10 03:57:25 -04:00
ret = mw.execShell('echo ' + args['username'] + '|l2tp -a')
2019-07-22 04:43:40 -04:00
if ret[1] == '':
2020-07-10 03:57:25 -04:00
return mw.returnJson(True, '添加成功!:' + ret[0])
return mw.returnJson(False, '添加失败:' + ret[0])
2019-07-22 04:43:40 -04:00
def delUser():
2020-07-10 03:57:25 -04:00
if mw.isAppleSystem():
return mw.returnJson(False, "Apple Computer does not support")
2019-07-22 04:43:40 -04:00
args = getArgs()
data = checkArgs(args, ['username'])
if not data[0]:
return data[1]
2020-07-10 03:57:25 -04:00
ret = mw.execShell('echo ' + args['username'] + '|l2tp -d')
2019-07-22 04:43:40 -04:00
if ret[1] == '':
2020-07-10 03:57:25 -04:00
return mw.returnJson(True, '删除成功!:' + ret[0])
return mw.returnJson(False, '删除失败:' + ret[0])
2019-07-22 04:43:40 -04:00
def modUser():
args = getArgs()
data = checkArgs(args, ['username', 'password'])
if not data[0]:
return data[1]
path = getPathFile()
username = args['username']
password = args['password']
# sed -i "/^\<${user}\>/d" /etc/ppp/chap-secrets
# echo "${user} l2tpd ${pass} *" >> /etc/ppp/chap-secrets
2020-07-10 03:57:25 -04:00
if mw.isAppleSystem():
mw.execShell("sed -i .bak '/^\(" + username + "\)/d' " + path)
2019-07-22 04:43:40 -04:00
else:
2020-07-10 03:57:25 -04:00
mw.execShell("sed -i '/^\(" + username + "\)/d' " + path)
2019-07-22 04:43:40 -04:00
# print 'echo "' + username + " l2tpd " + password + " *\" >>"
# + path
2020-07-10 03:57:25 -04:00
ret = mw.execShell("echo \"" + username +
" l2tpd " + password + " *\" >>" + path)
2019-07-22 04:43:40 -04:00
if ret[1] == '':
2020-07-10 03:57:25 -04:00
return mw.returnJson(True, '修改成功!')
return mw.returnJson(False, '修改失败')
2019-07-22 04:43:40 -04:00
if __name__ == "__main__":
func = sys.argv[1]
if func == 'status':
2021-05-08 13:11:18 -04:00
print(status())
2019-07-22 04:43:40 -04:00
elif func == 'start':
2021-05-08 13:11:18 -04:00
print(start())
2019-07-22 04:43:40 -04:00
elif func == 'stop':
2021-05-08 13:11:18 -04:00
print(stop())
2019-07-22 04:43:40 -04:00
elif func == 'restart':
2021-05-08 13:11:18 -04:00
print(restart())
2019-07-22 04:43:40 -04:00
elif func == 'reload':
2021-05-08 13:11:18 -04:00
print(reload())
2019-07-22 04:43:40 -04:00
elif func == 'conf':
2021-05-08 13:11:18 -04:00
print(getPathFile())
2019-07-22 04:43:40 -04:00
elif func == 'conf_psk':
2021-05-08 13:11:18 -04:00
print(getPathFilePsk())
2019-07-22 04:43:40 -04:00
elif func == 'user_list':
2021-05-08 13:11:18 -04:00
print(getUserList())
2019-07-22 04:43:40 -04:00
elif func == 'add_user':
2021-05-08 13:11:18 -04:00
print(addUser())
2019-07-22 04:43:40 -04:00
elif func == 'del_user':
2021-05-08 13:11:18 -04:00
print(delUser())
2019-07-22 04:43:40 -04:00
elif func == 'mod_user':
2021-05-08 13:11:18 -04:00
print(modUser())
2019-07-22 04:43:40 -04:00
else:
2021-05-08 13:11:18 -04:00
print('error')