Files
aaPanel/mod/base/web_conf/config_mgr.py
T
Jack ed55fa708d Update to 7.7.0
Since version 7.7.0, we recommend yours update python to 3.12.

[+] Using nginx technology to load static files improves access speed
[+] Refactor homepage, website, FTP, and database using vue3
[+] Table loading changed to skeleton screen
[+] Add Website statistics-v2 professional plug-in
[+] Add Home page - top 5 resource occupancy
[+] Add protection for Files management (requires Tamper-proof for Enterprise 3.7)
[+] Website, FTP, Databases page add program status
[+] Add FTP log analysis (only supports Centos)
[+] Add password-free login to phpMyAdmin
[+] Add Proxy Project in Website (Supported when web service uses Nginx)
[+] Add WP Toolkit (Pro version only)
[+] Redesigned Docker module
[+] Add WP Toolkit Protection
[+] Add WP Toolkit Backup and Restore
[+] Add WP Toolkit Migrated
[+] Add WP Toolkit Clone site (supports new domain and subdomain)
[+] Add WP Toolkit Create site from backup of other panel
[+] Add WP Toolkit support for Cron automatic backup (only save Local disk)
[+] Add WP Toolkit operation log
[+] Add Integrity check for WP Toolkit
[+] Add WP Toolkit plug-in management and themes management

[*] Optimize phpMyAdmin formula access method
[*] Optimize Home page PHP display problem
[*] Optimize jump to the login interface after the login expires
[*] Optimize automatic renewal of SSL at some times
[*] Optimize Let's Encrypt to increase application success rate

[-] Fix Logs Audit cannot be opened
[-] Fix apache URL rewrite issue
[-] Fix phpmyadmin installation problem
[-] Fix the problem that some servers cannot install software
[-] Fix upload file error
[-] Fix left menu hiding problem
[-] Fix aaPanel Mobile QR code display problem
[-] Fix problem that third-party plug-ins are not displayed in the App Store
[-] Fix issue where the menu bar is blank when opening new tabs
[-] Fixed panel not being accessible in some cases
[-] Fix the issue where Curl warning caused the inability to apply for SSL
[-] Fix Quota issues for Website, FTP, Databases
[-] Fix file interface display problem on mobile terminal
2024-07-19 11:25:10 +08:00

154 lines
6.3 KiB
Python

import os
import time
from hashlib import md5
from typing import Optional
from .util import service_reload, check_server_config, write_file, read_file
# 支持读取配置文件
# 保存并重启配置文件
# 历史文件记录
class ConfigMgr:
_vhost_path = "/www/server/panel/vhost"
def __init__(self, site_name: str, config_prefix: str = ""):
self.site_name = site_name
self.config_prefix = config_prefix
def _read_config(self, web_server: str) -> Optional[str]:
config_file = "{}/{}/{}{}.conf".format(self._vhost_path, web_server, self.config_prefix, self.site_name)
res = read_file(config_file)
if isinstance(res, str):
return res
return None
def nginx_config(self) -> Optional[str]:
return self._read_config("nginx")
def apache_config(self) -> Optional[str]:
return self._read_config("apache")
def save_config(self, conf_data: str, web_server: str):
config_file = "{}/{}/{}{}.conf".format(self._vhost_path, web_server, self.config_prefix, self.site_name)
old_config = self._read_config(web_server)
write_file(config_file, conf_data)
errmsg = check_server_config()
if errmsg:
write_file(config_file, old_config)
return errmsg
self._save_history(web_server)
service_reload()
def save_nginx_config(self, conf_data: str) -> Optional[str]:
return self.save_config(conf_data, "nginx")
def save_apache_config(self, conf_data: str) -> Optional[str]:
return self.save_config(conf_data, "apache")
def history_list(self):
his_path = '/www/backup/file_history'
nginx_config_file = "{}/nginx/{}{}.conf".format(self._vhost_path, self.config_prefix, self.site_name)
ng_save_path = "{}{}".format(his_path, nginx_config_file)
apache_config_file = "{}/apache/{}{}.conf".format(self._vhost_path, self.config_prefix, self.site_name)
ap_save_path = "{}{}".format(his_path, apache_config_file)
return {
"nginx": [] if not os.path.isdir(ng_save_path) else sorted(os.listdir(ng_save_path), reverse=True),
"apache": [] if not os.path.isdir(ap_save_path) else sorted(os.listdir(ap_save_path), reverse=True)
}
def history_conf(self, history_id: str) -> Optional[str]:
his_path = '/www/backup/file_history'
nginx_config_file = "{}/nginx/{}{}.conf".format(self._vhost_path, self.config_prefix, self.site_name)
ng_save_path = "{}{}".format(his_path, nginx_config_file)
if os.path.isdir(ng_save_path):
for i in os.listdir(ng_save_path):
if i == history_id:
return read_file(os.path.join(ng_save_path, i))
apache_config_file = "{}/apache/{}{}.conf".format(self._vhost_path, self.config_prefix, self.site_name)
ap_save_path = "{}{}".format(his_path, apache_config_file)
if os.path.isdir(ap_save_path):
for i in os.listdir(ap_save_path):
if i == history_id:
return read_file(os.path.join(ap_save_path, i))
return None
def remove_history_file(self, history_id: str) -> None:
his_path = '/www/backup/file_history'
nginx_config_file = "{}/nginx/{}{}.conf".format(self._vhost_path, self.config_prefix, self.site_name)
ng_save_path = "{}{}".format(his_path, nginx_config_file)
if os.path.isdir(ng_save_path):
for i in os.listdir(ng_save_path):
if i == history_id:
os.remove(os.path.join(ng_save_path, i))
apache_config_file = "{}/apache/{}{}.conf".format(self._vhost_path, self.config_prefix, self.site_name)
ap_save_path = "{}{}".format(his_path, apache_config_file)
if os.path.isdir(ap_save_path):
for i in os.listdir(ap_save_path):
if i == history_id:
os.remove(os.path.join(ng_save_path, i))
def clear_history_file(self) -> None:
"""
清空所有的历史文件
"""
his_path = '/www/backup/file_history'
nginx_config_file = "{}/nginx/{}{}.conf".format(self._vhost_path, self.config_prefix, self.site_name)
ng_save_path = "{}{}".format(his_path, nginx_config_file)
if os.path.isdir(ng_save_path):
for i in os.listdir(ng_save_path):
os.remove(os.path.join(ng_save_path, i))
apache_config_file = "{}/apache/{}{}.conf".format(self._vhost_path, self.config_prefix, self.site_name)
ap_save_path = "{}{}".format(his_path, apache_config_file)
if os.path.isdir(ap_save_path):
for i in os.listdir(ap_save_path):
os.remove(os.path.join(ng_save_path, i))
@staticmethod
def _file_md5(filename):
if not os.path.isfile(filename):
return False
md5_obj = md5()
with open(filename, mode="rb") as f:
while True:
b = f.read(8096)
if not b:
break
md5_obj.update(b)
return md5_obj.hexdigest()
def _save_history(self, web_server: str):
if os.path.exists('/www/server/panel/data/not_file_history.pl'):
return True
his_path = '/www/backup/file_history'
filename = "{}/{}/{}{}.conf".format(self._vhost_path, web_server, self.config_prefix, self.site_name)
save_path = "{}{}".format(his_path, filename)
if not os.path.isdir(save_path):
os.makedirs(save_path, 384)
his_list = sorted(os.listdir(save_path), reverse=True) # 倒序排列已有的历史文件
try:
num = int(read_file('data/history_num.pl'))
except (ValueError, TypeError):
num = 100
is_write = True
if len(his_list) > 0:
new_file_md5 = self._file_md5(filename)
last_file_md5 = self._file_md5(os.path.join(save_path, his_list[0]))
is_write = new_file_md5 != last_file_md5
if is_write:
new_name = str(int(time.time()))
write_file(os.path.join(save_path, new_name), read_file(filename, 'rb'), "wb")
his_list.insert(0, new_name)
# 删除多余的副本
for i in his_list[num:]:
rm_file = save_path + '/' + i
if os.path.exists(rm_file):
os.remove(rm_file)