Update config_api.py

pull/583/head
Mr Chen 12 months ago
parent 54e11700c2
commit e56606ac61
  1. 253
      class/core/config_api.py

@ -465,55 +465,55 @@ class config_api:
return mw.returnJson(True, '证书已保存!') return mw.returnJson(True, '证书已保存!')
# 设置面板SSL证书设置 # 设置面板SSL证书设置
def setPanelHttpToHttpsApi(self): # def setPanelHttpToHttpsApi(self):
bind_domain = self.__file['bind_domain'] # bind_domain = self.__file['bind_domain']
if not os.path.exists(bind_domain): # if not os.path.exists(bind_domain):
return mw.returnJson(False, '先要绑定域名!') # return mw.returnJson(False, '先要绑定域名!')
choose_file = self.__file['ssl'] # choose_file = self.__file['ssl']
choose = mw.readFile(choose_file) # choose = mw.readFile(choose_file)
if choose == 'local': # if choose == 'local':
return mw.returnJson(False, '本地SSL无法使用!') # return mw.returnJson(False, '本地SSL无法使用!')
keyPath = 'ssl/nginx/private.pem' # keyPath = 'ssl/nginx/private.pem'
if not os.path.exists(keyPath): # if not os.path.exists(keyPath):
return mw.returnJson(False, '未申请SSL证书!') # return mw.returnJson(False, '未申请SSL证书!')
is_https = request.form.get('https', '').strip() # is_https = request.form.get('https', '').strip()
panel_ssl = mw.getServerDir() + "/web_conf/nginx/vhost/panel.conf" # panel_ssl = mw.getServerDir() + "/web_conf/nginx/vhost/panel.conf"
if not os.path.exists(panel_ssl): # if not os.path.exists(panel_ssl):
return mw.returnJson(False, '未开启面板SSL!') # return mw.returnJson(False, '未开启面板SSL!')
if is_https == 'false': # if is_https == 'false':
conf = mw.readFile(panel_ssl) # conf = mw.readFile(panel_ssl)
if conf: # if conf:
if conf.find('ssl_certificate') == -1: # if conf.find('ssl_certificate') == -1:
return mw.returnJson(False, '当前未开启SSL') # return mw.returnJson(False, '当前未开启SSL')
to = "#error_page 404/404.html;\n\ # to = "#error_page 404/404.html;\n\
#HTTP_TO_HTTPS_START\n\ # #HTTP_TO_HTTPS_START\n\
if ($server_port !~ 443){\n\ # if ($server_port !~ 443){\n\
rewrite ^(/.*)$ https://$host$1 permanent;\n\ # rewrite ^(/.*)$ https://$host$1 permanent;\n\
}\n\ # }\n\
#HTTP_TO_HTTPS_END" # #HTTP_TO_HTTPS_END"
conf = conf.replace('#error_page 404/404.html;', to) # conf = conf.replace('#error_page 404/404.html;', to)
mw.writeFile(panel_ssl, conf) # mw.writeFile(panel_ssl, conf)
else: # else:
conf = mw.readFile(panel_ssl) # conf = mw.readFile(panel_ssl)
if conf: # if conf:
rep = "\n\\s*#HTTP_TO_HTTPS_START(.|\n){1,300}#HTTP_TO_HTTPS_END" # rep = "\n\\s*#HTTP_TO_HTTPS_START(.|\n){1,300}#HTTP_TO_HTTPS_END"
conf = re.sub(rep, '', conf) # conf = re.sub(rep, '', conf)
rep = "\\s+if.+server_port.+\n.+\n\\s+\\s*}" # rep = "\\s+if.+server_port.+\n.+\n\\s+\\s*}"
conf = re.sub(rep, '', conf) # conf = re.sub(rep, '', conf)
mw.writeFile(panel_ssl, conf) # mw.writeFile(panel_ssl, conf)
mw.restartNginx() # mw.restartNginx()
action = '开启' # action = '开启'
if is_https == 'true': # if is_https == 'true':
action = '关闭' # action = '关闭'
return mw.returnJson(True, action + 'HTTPS跳转成功!') # return mw.returnJson(True, action + 'HTTPS跳转成功!')
# 删除面板证书 # 删除面板证书
def delPanelSslApi(self): def delPanelSslApi(self):
@ -571,84 +571,83 @@ class config_api:
return mw.returnJson(False, '未知类型!') return mw.returnJson(False, '未知类型!')
# 申请面板let证书 # 申请面板let证书
def applyPanelAcmeSslApi(self): # def applyPanelAcmeSslApi(self):
# check domain is bind? # bind_domain = self.__file['bind_domain']
bind_domain = self.__file['bind_domain'] # if not os.path.exists(bind_domain):
if not os.path.exists(bind_domain): # return mw.returnJson(False, '先要绑定域名!')
return mw.returnJson(False, '先要绑定域名!')
# # 生成nginx配置
# 生成nginx配置 # domain = mw.readFile(bind_domain)
domain = mw.readFile(bind_domain) # panel_tpl = mw.getRunDir() + "/data/tpl/nginx_panel.conf"
panel_tpl = mw.getRunDir() + "/data/tpl/nginx_panel.conf" # dst_panel_path = mw.getServerDir() + "/web_conf/nginx/vhost/panel.conf"
dst_panel_path = mw.getServerDir() + "/web_conf/nginx/vhost/panel.conf" # if not os.path.exists(dst_panel_path):
if not os.path.exists(dst_panel_path): # reg = r"^([\w\-\*]{1,100}\.){1,4}(\w{1,10}|\w{1,10}\.\w{1,10})$"
reg = r"^([\w\-\*]{1,100}\.){1,4}(\w{1,10}|\w{1,10}\.\w{1,10})$" # if not re.match(reg, domain):
if not re.match(reg, domain): # return mw.returnJson(False, '主域名格式不正确')
return mw.returnJson(False, '主域名格式不正确')
# op_dir = mw.getServerDir() + "/openresty"
op_dir = mw.getServerDir() + "/openresty" # if not os.path.exists(op_dir):
if not os.path.exists(op_dir): # return mw.returnJson(False, '依赖OpenResty,先安装启动它!')
return mw.returnJson(False, '依赖OpenResty,先安装启动它!')
# content = mw.readFile(panel_tpl)
content = mw.readFile(panel_tpl) # content = content.replace("{$PORT}", "80")
content = content.replace("{$PORT}", "80") # content = content.replace("{$SERVER_NAME}", domain)
content = content.replace("{$SERVER_NAME}", domain) # content = content.replace("{$PANAL_PORT}", mw.readFile('data/port.pl'))
content = content.replace("{$PANAL_PORT}", mw.readFile('data/port.pl')) # content = content.replace("{$LOGPATH}", mw.getRunDir() + '/logs')
content = content.replace("{$LOGPATH}", mw.getRunDir() + '/logs') # content = content.replace("{$PANAL_ADDR}", mw.getRunDir())
content = content.replace("{$PANAL_ADDR}", mw.getRunDir()) # mw.writeFile(dst_panel_path, content)
mw.writeFile(dst_panel_path, content) # mw.restartNginx()
mw.restartNginx()
# siteName = mw.readFile(bind_domain).strip()
siteName = mw.readFile(bind_domain).strip() # auth_to = mw.getRunDir() + "/tmp"
auth_to = mw.getRunDir() + "/tmp" # to_args = {
to_args = { # 'domains': [siteName],
'domains': [siteName], # 'auth_type': 'http',
'auth_type': 'http', # 'auth_to': auth_to,
'auth_to': auth_to, # }
}
# src_path = mw.getServerDir() + '/web_conf/letsencrypt/' + siteName
src_path = mw.getServerDir() + '/web_conf/letsencrypt/' + siteName # src_csrpath = src_path + "/fullchain.pem" # 生成证书路径
src_csrpath = src_path + "/fullchain.pem" # 生成证书路径 # src_keypath = src_path + "/privkey.pem" # 密钥文件路径
src_keypath = src_path + "/privkey.pem" # 密钥文件路径
# dst_path = mw.getRunDir() + '/ssl/nginx'
dst_path = mw.getRunDir() + '/ssl/nginx' # dst_csrpath = dst_path + '/cert.pem'
dst_csrpath = dst_path + '/cert.pem' # dst_keypath = dst_path + '/private.pem'
dst_keypath = dst_path + '/private.pem'
# is_already_apply = False
is_already_apply = False
# if not os.path.exists(src_path):
if not os.path.exists(src_path): # import cert_api
import cert_api # data = cert_api.cert_api().applyCertApi(to_args)
data = cert_api.cert_api().applyCertApi(to_args) # if not data['status']:
if not data['status']: # msg = data['msg']
msg = data['msg'] # if type(data['msg']) != str:
if type(data['msg']) != str: # msg = data['msg'][0]
msg = data['msg'][0] # emsg = data['msg'][1]['challenges'][0]['error']
emsg = data['msg'][1]['challenges'][0]['error'] # msg = msg + '<p><span>响应状态:</span>' + str(emsg['status']) + '</p><p><span>错误类型:</span>' + emsg[
msg = msg + '<p><span>响应状态:</span>' + str(emsg['status']) + '</p><p><span>错误类型:</span>' + emsg[ # 'type'] + '</p><p><span>错误代码:</span>' + emsg['detail'] + '</p>'
'type'] + '</p><p><span>错误代码:</span>' + emsg['detail'] + '</p>' # return mw.returnJson(data['status'], msg, data['msg'])
return mw.returnJson(data['status'], msg, data['msg']) # else:
else: # is_already_apply = True
is_already_apply = True
# mw.buildSoftLink(src_csrpath, dst_csrpath, True)
mw.buildSoftLink(src_csrpath, dst_csrpath, True) # mw.buildSoftLink(src_keypath, dst_keypath, True)
mw.buildSoftLink(src_keypath, dst_keypath, True) # mw.execShell('echo "acme" > "' + dst_path + '/README"')
mw.execShell('echo "acme" > "' + dst_path + '/README"')
# tmp_well_know = auth_to + '/.well-known'
tmp_well_know = auth_to + '/.well-known' # if os.path.exists(tmp_well_know):
if os.path.exists(tmp_well_know): # mw.execShell('rm -rf ' + tmp_well_know)
mw.execShell('rm -rf ' + tmp_well_know)
# if os.path.exists(dst_path):
if os.path.exists(dst_path): # choose_file = self.__file['ssl']
choose_file = self.__file['ssl'] # mw.writeFile(choose_file, 'nginx')
mw.writeFile(choose_file, 'nginx')
# data = self.getPanelSslData()
data = self.getPanelSslData()
# if is_already_apply:
if is_already_apply: # return mw.returnJson(True, '重复申请!', data)
return mw.returnJson(True, '重复申请!', data) # return mw.returnJson(True, '申请成功!', data)
return mw.returnJson(True, '申请成功!', data)
def setPanelDomainApi(self): def setPanelDomainApi(self):
domain = request.form.get('domain', '') domain = request.form.get('domain', '')

Loading…
Cancel
Save