QiuSimons / openwrt-mos

GNU General Public License v3.0
229 stars 89 forks source link

重构 #163

Closed daiaji closed 6 months ago

daiaji commented 7 months ago

使用GPT重构了大量的代码,让代码更规范更现代化。

添加功能:

  1. 现在可以在LUCI设置反代地址了,workers.dev在我这不太好用,基本上报废了.
  2. 现在你可以在使用“自定义配置”时也能切换“启用 DNS 广告过滤”选项了。
daiaji commented 7 months ago
local sys = require "luci.sys"

local ruleFiles = {
  whiteList = "/etc/mosdns/rule/whitelist.txt",
  blockList = "/etc/mosdns/rule/blocklist.txt",
  hostsList = "/etc/mosdns/rule/hosts.txt",
  redirectList = "/etc/mosdns/rule/redirect.txt",
  cusConfig = "/etc/mosdns/cus_config.yaml"
}

local function readFile(filePath)
  local file = io.open(filePath, "r")
  if not file then
    luci.util.perror("Failed to read file: " .. filePath)
    return ""
  end

  local content = file:read("*a")
  file:close()
  return content
end

local function writeFile(filePath, content)
  local file = io.open(filePath, "w")
  if not file then
    luci.util.perror("Failed to write file: " .. filePath)
    return
  end

  file:write(content)
  file:close()
end

local function removeFile(filePath)
  local success, errorMsg = os.remove(filePath)
  if not success then
    luci.util.perror("Failed to remove file: " .. filePath)
  end
end

local function reloadMosDNS()
  local _, _, code = sys.call("/etc/init.d/mosdns reload")
  if code ~= 0 then
    luci.util.perror("Failed to reload MosDNS.")
  end
end

local m = Map("mosdns")
local s = m:section(TypedSection, "mosdns", translate("Rule Settings"))
s.anonymous = true

local function createTextOption(tabName, optionName, filePath, description)
  local o = s:taboption(tabName, TextValue, optionName, "", description .. "<font color='#00bd3e'>" .. translate("<br>The list of rules only applies to 'Default Config' profiles.") .. "</font>")
  o.rows = 15
  o.wrap = "off"
  o.cfgvalue = function(self, section) return readFile(filePath) end
  o.write = function(self, section, value) writeFile(filePath, value:gsub("\r\n", "\n")) end
  o.remove = function(self, section, value) removeFile(filePath) end
  o.validate = function(self, value)
    return value
  end
end

createTextOption("white_list", "whitelist", ruleFiles.whiteList, "<font color='red'>" .. translate("These domain names allow DNS resolution with the highest priority. Please input the domain names of websites. Each line should contain only one website domain. For example: hm.baidu.com.") .. "</font>")

createTextOption("block_list", "blocklist", ruleFiles.blockList, "<font color='red'>" .. translate("These domains are blocked from DNS resolution. Please input the domain names of websites. Each line should contain only one website domain. For example: baidu.com.") .. "</font>")

createTextOption("hosts_list", "hosts", ruleFiles.hostsList, "<font color='red'>" .. translate("Hosts. For example: baidu.com 10.0.0.1") .. "</font>")

createTextOption("redirect_list", "redirect", ruleFiles.redirectList, "<font color='red'>" .. translate("The domain name to redirect the request to. Requests for domain A will return records for domain B. Example: a.com b.com") .. "</font>")

createTextOption("cus_config", "cus_config", ruleFiles.cusConfig, "<font color='red'>" .. translate("View the Custom YAML Configuration file used by this MosDNS. You can edit it according to your own needs.") .. "</font>")

local apply = luci.http.formvalue("cbi.apply")
if apply then
  reloadMosDNS()
end

return m

GPT还有一个关于rule_list.lua代码复用的改进。 但我不会lua,实在跑不起来就没合并了,作者看看怎么改?

daiaji commented 7 months ago

我还有一点想法,就是把规则更新用lua做,这样应该能在网页端看到进度条,这个更新脚本,其他的地方不调用吧? 就算调用,lua脚本貌似也不是调用不了,init脚本用lua不知道能不能重构。

QiuSimons commented 6 months ago

辛苦了

daiaji commented 6 months ago

辛苦了

~你检查一下我没提交的rule_list.lua重构,看看有什么问题?~ ~修一下应该能用。~


(整完了,等上传)


local datatypes = require "luci.cbi.datatypes"好像就没用上过,删掉也行的样子。 本来想把反代地址填我的,但想想就算了,用得多了怕不是分分钟吃ban。

daiaji commented 6 months ago

辛苦了

我希望更新按钮能够显示下载的进度,controller控制器里的代码能获取CBI models中的变量吗?CBI models似乎无法用entry定义http访问接口啊。

daiaji commented 6 months ago

上游在逐渐使用js代替lua,这意味着似乎应该考虑完全的重构了。

sbwml commented 6 months ago

呃~ 话说你们这样玩不会炸吗,双配置下就注定无法为选项定义 rmempty,所以一些值的读取并不是非0即1,它还存在空值的可能性,这种情况整数判断 cli 真的不会异常中断执行吗

daiaji commented 6 months ago

呃~ 话说你们这样玩不会炸吗,双配置下就注定无法为选项定义 rmempty,所以一些值的读取并不是非0即1,它还存在空值的可能性,这种情况整数判断 cli 真的不会异常中断执行吗

其实鲁棒性一直挺糟糕的,老毛病了🙁 稍微删点东西,或者别的啥的,就会寄。

daiaji commented 6 months ago

呃~ 话说你们这样玩不会炸吗,双配置下就注定无法为选项定义 rmempty,所以一些值的读取并不是非0即1,它还存在空值的可能性,这种情况整数判断 cli 真的不会异常中断执行吗

有空你也可以大修,现在来说init脚本似乎还是有点问题,我姑且用软链接代替了部分复制,在路由器上I/O实在不怎么明智,一些奇怪的问题是更新规则似乎有时候会导致openwrt出现问题,暂时我没搞明白发生了什么,所以也无从下手。

sbwml commented 6 months ago

我不懂得修,我用的 own revision sbwml/luci-app-mosdns

从 Windows 版邮件发送

发件人: daiaji 发送时间: 2024年3月19日 18:50 收件人: QiuSimons/openwrt-mos 抄送: Bard; Comment 主题: Re: [QiuSimons/openwrt-mos] 重构 (PR #163)

呃~ 话说你们这样玩不会炸吗,双配置下就注定无法为选项定义 rmempty,所以一些值的读取并不是非0即1,它还存在空值的可能性,这种情况整数判断 cli 真的不会异常中断执行吗 有空你也可以大修,现在来说init脚本似乎还是有点问题,我姑且用软链接代替了部分复制,在路由器上I/O实在不怎么明智,一些奇怪的问题是更新规则似乎有时候会导致openwrt出现问题,暂时我没搞明白发生了什么,所以也无从下手。 — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.***>

sbwml commented 6 months ago

但是按你这个pr 来看的话,受 luci 控制的变量脱离init.d 后肯定是要 string comparison 的,不能直接integer comparison,就像前面说的,切配置后它不再是非1即0

从 Windows 版邮件发送

发件人: daiaji 发送时间: 2024年3月19日 18:50 收件人: QiuSimons/openwrt-mos 抄送: Bard; Comment 主题: Re: [QiuSimons/openwrt-mos] 重构 (PR #163)

呃~ 话说你们这样玩不会炸吗,双配置下就注定无法为选项定义 rmempty,所以一些值的读取并不是非0即1,它还存在空值的可能性,这种情况整数判断 cli 真的不会异常中断执行吗 有空你也可以大修,现在来说init脚本似乎还是有点问题,我姑且用软链接代替了部分复制,在路由器上I/O实在不怎么明智,一些奇怪的问题是更新规则似乎有时候会导致openwrt出现问题,暂时我没搞明白发生了什么,所以也无从下手。 — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.***>

daiaji commented 6 months ago

我不懂得修,我用的 own revision sbwml/luci-app-mosdns 从 Windows 版邮件发送 发件人: daiaji 发送时间: 2024年3月19日 18:50 收件人: QiuSimons/openwrt-mos 抄送: Bard; Comment 主题: Re: [QiuSimons/openwrt-mos] 重构 (PR #163) 呃~ 话说你们这样玩不会炸吗,双配置下就注定无法为选项定义 rmempty,所以一些值的读取并不是非0即1,它还存在空值的可能性,这种情况整数判断 cli 真的不会异常中断执行吗 有空你也可以大修,现在来说init脚本似乎还是有点问题,我姑且用软链接代替了部分复制,在路由器上I/O实在不怎么明智,一些奇怪的问题是更新规则似乎有时候会导致openwrt出现问题,暂时我没搞明白发生了什么,所以也无从下手。 — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.***>

说起来你的分支不是有对目录结构和代码都有大量修改吗?干脆PR呗。

sbwml commented 6 months ago

没有意义,mos能跑就够用,至于文件怎么放、lua怎么改都是虚的。我自用的mos 和 404 佬这里的,想要的东西不一样

从 Windows 版邮件发送

发件人: daiaji 发送时间: 2024年3月19日 20:14 收件人: QiuSimons/openwrt-mos 抄送: Bard; Comment 主题: Re: [QiuSimons/openwrt-mos] 重构 (PR #163)

我不懂得修,我用的 own revision sbwml/luci-app-mosdns 从 Windows 版邮件发送 发件人: daiaji 发送时间: 2024年3月19日 18:50 收件人: QiuSimons/openwrt-mos 抄送: Bard; Comment 主题: Re: [QiuSimons/openwrt-mos] 重构 (PR #163) 呃~ 话说你们这样玩不会炸吗,双配置下就注定无法为选项定义 rmempty,所以一些值的读取并不是非0即1,它还存在空值的可能性,这种情况整数判断 cli 真的不会异常中断执行吗 有空你也可以大修,现在来说init脚本似乎还是有点问题,我姑且用软链接代替了部分复制,在路由器上I/O实在不怎么明智,一些奇怪的问题是更新规则似乎有时候会导致openwrt出现问题,暂时我没搞明白发生了什么,所以也无从下手。 — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.> 说起来你的分支不是有对目录结构和代码都有大量修改吗?干脆PR呗。 — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.>

daiaji commented 6 months ago

没有意义,mos能跑就够用,至于文件怎么放、lua怎么改都是虚的。我自用的mos 和 404 佬这里的,想要的东西不一样 从 Windows 版邮件发送 发件人: daiaji 发送时间: 2024年3月19日 20:14 收件人: QiuSimons/openwrt-mos 抄送: Bard; Comment 主题: Re: [QiuSimons/openwrt-mos] 重构 (PR #163) 我不懂得修,我用的 own revision sbwml/luci-app-mosdns 从 Windows 版邮件发送 发件人: daiaji 发送时间: 2024年3月19日 18:50 收件人: QiuSimons/openwrt-mos 抄送: Bard; Comment 主题: Re: [QiuSimons/openwrt-mos] 重构 (PR #163) 呃~ 话说你们这样玩不会炸吗,双配置下就注定无法为选项定义 rmempty,所以一些值的读取并不是非0即1,它还存在空值的可能性,这种情况整数判断 cli 真的不会异常中断执行吗 有空你也可以大修,现在来说init脚本似乎还是有点问题,我姑且用软链接代替了部分复制,在路由器上I/O实在不怎么明智,一些奇怪的问题是更新规则似乎有时候会导致openwrt出现问题,暂时我没搞明白发生了什么,所以也无从下手。 — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.> 说起来你的分支不是有对目录结构和代码都有大量修改吗?干脆PR呗。 — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.>

这不已经是应对屎山的说辞了吗?