fix 自定义Hosts v1.1 关闭插件时自动恢复系统hosts

This commit is contained in:
thsrite
2024-06-15 19:44:40 +08:00
parent 98faa952c4
commit 5f93e2b0a0
2 changed files with 128 additions and 101 deletions

View File

@@ -151,10 +151,13 @@
"name": "自定义Hosts",
"description": "修改系统hosts文件加速网络访问。",
"labels": "网络",
"version": "1.0",
"version": "1.1",
"icon": "hosts.png",
"author": "thsrite",
"level": 1
"level": 1,
"history": {
"v1.1": "关闭插件时自动恢复系统hosts"
}
},
"SpeedLimiter": {
"name": "播放限速",

View File

@@ -18,7 +18,7 @@ class CustomHosts(_PluginBase):
# 插件图标
plugin_icon = "hosts.png"
# 插件版本
plugin_version = "1.0"
plugin_version = "1.1"
# 插件作者
plugin_author = "thsrite"
# 作者主页
@@ -59,6 +59,9 @@ class CustomHosts(_PluginBase):
"err_hosts": error_hosts,
"enabled": self._enabled
})
else:
# hosts为空或未启用清除系统hosts
self.__clear_system_hosts()
def get_state(self) -> bool:
return self._enabled
@@ -75,104 +78,104 @@ class CustomHosts(_PluginBase):
拼装插件配置页面需要返回两块数据1、页面配置2、数据结构
"""
return [
{
'component': 'VForm',
'content': [
{
'component': 'VRow',
'content': [
{
'component': 'VCol',
'props': {
'cols': 12,
'md': 6
},
'content': [
{
'component': 'VSwitch',
'props': {
'model': 'enabled',
'label': '启用插件',
}
}
]
}
]
},
{
'component': 'VRow',
'content': [
{
'component': 'VCol',
'props': {
'cols': 12
},
'content': [
{
'component': 'VTextarea',
'props': {
'model': 'hosts',
'label': '自定义hosts',
'rows': 10,
'placeholder': '每行一个配置格式为ip host1 host2 ...'
}
}
]
}
]
},
{
'component': 'VRow',
'content': [
{
'component': 'VCol',
'props': {
'cols': 12
},
'content': [
{
'component': 'VTextarea',
'props': {
'model': 'err_hosts',
'readonly': True,
'label': '错误hosts',
'rows': 2,
'placeholder': '错误的hosts配置会展示在此处请修改上方hosts重新提交错误的hosts不会写入系统hosts文件'
}
}
]
}
]
},
{
'component': 'VRow',
'content': [
{
'component': 'VCol',
'props': {
'cols': 12,
},
'content': [
{
'component': 'VAlert',
'props': {
'type': 'info',
'variant': 'tonal',
'text': 'host格式ip host中间有空格'
'容器运行则更新容器hosts非宿主机'
}
}
]
}
]
}
]
}
], {
"enabled": False,
"hosts": "",
"err_hosts": ""
}
{
'component': 'VForm',
'content': [
{
'component': 'VRow',
'content': [
{
'component': 'VCol',
'props': {
'cols': 12,
'md': 6
},
'content': [
{
'component': 'VSwitch',
'props': {
'model': 'enabled',
'label': '启用插件',
}
}
]
}
]
},
{
'component': 'VRow',
'content': [
{
'component': 'VCol',
'props': {
'cols': 12
},
'content': [
{
'component': 'VTextarea',
'props': {
'model': 'hosts',
'label': '自定义hosts',
'rows': 10,
'placeholder': '每行一个配置格式为ip host1 host2 ...'
}
}
]
}
]
},
{
'component': 'VRow',
'content': [
{
'component': 'VCol',
'props': {
'cols': 12
},
'content': [
{
'component': 'VTextarea',
'props': {
'model': 'err_hosts',
'readonly': True,
'label': '错误hosts',
'rows': 2,
'placeholder': '错误的hosts配置会展示在此处请修改上方hosts重新提交错误的hosts不会写入系统hosts文件'
}
}
]
}
]
},
{
'component': 'VRow',
'content': [
{
'component': 'VCol',
'props': {
'cols': 12,
},
'content': [
{
'component': 'VAlert',
'props': {
'type': 'info',
'variant': 'tonal',
'text': 'host格式ip host中间有空格'
'容器运行则更新容器hosts非宿主机'
}
}
]
}
]
}
]
}
], {
"enabled": False,
"hosts": "",
"err_hosts": ""
}
def get_page(self) -> List[dict]:
pass
@@ -190,6 +193,27 @@ class CustomHosts(_PluginBase):
# 读取系统hosts
return Hosts(path=hosts_path)
def __clear_system_hosts(self):
"""
清除系统hosts
"""
# 系统hosts对象
system_hosts = self.__read_system_hosts()
# 过滤掉插件添加的hosts
orgin_entries = []
for entry in system_hosts.entries:
if entry.entry_type == "comment" and entry.comment == "# CustomHostsPlugin":
break
orgin_entries.append(entry)
system_hosts.entries = orgin_entries
try:
system_hosts.write()
logger.info("系统hosts文件已恢复")
except Exception as err:
logger.error(f"恢复系统hosts文件失败{str(err) or '请检查权限'}")
# 推送实时消息
self.systemmessage.put(f"恢复系统hosts文件失败{str(err) or '请检查权限'}", title="自定义Hosts")
def __add_hosts_to_system(self, hosts):
"""
添加hosts到系统