为 OpenWrt/LEDE/PandoraBox 的 LuCI 增加了若干个页面,用于操作后台配置文件、执行后台脚本,备忘。

Controller

三个页面的统一入口。新建文件:

/usr/lib/lua/luci/controller/chinternet.lua

module("luci.controller.chinternet", package.seeall)

function index()
	if not nixio.fs.access("/etc/ss.json") then
		return
	end

	entry({"admin", "services", "chinternet"},
		alias("admin", "services", "chinternet", "general"),
		_("Chinternet"), 10)

	entry({"admin", "services", "chinternet", "general"},
		cbi("chinternet/general"),
		_("General Settings"), 10).leaf = true

	entry({"admin", "services", "chinternet", "auto"},
		cbi("chinternet/auto"),
		_("Auto Rule"), 30).leaf = true

	entry({"admin", "services", "chinternet", "custom"},
		cbi("chinternet/custom"),
		_("Custom Rule"), 30).leaf = true
end

CBI

三个简易的配置页面。

General Settings

用于修改配置文件,保存提交后重启进程。新建文件:

/usr/lib/lua/luci/model/cbi/chinternet/general.lua

local fs = require "nixio.fs"
local conffile = "/etc/ss.json" 

f = SimpleForm("general", translate("Chinternet - General Settings"), translate("This is the config file for SS. Do NOT modify local_port, unless you know what you are doing."))

t = f:field(TextValue, "conf")
t.rmempty = true
t.rows = 10
function t.cfgvalue()
	return fs.readfile(conffile) or ""
end

function f.handle(self, state, data)
	if state == FORM_VALID then
		if data.conf then
			fs.writefile(conffile, data.conf:gsub("\r\n", "\n"))
			luci.sys.call("/etc/init.d/ss restart >/dev/null")
		end
	end
	return true
end

return f

Auto Rule

用于查看配置文件,无提交按钮,提供一个按钮用于重新生成配置文件。新建文件:

/usr/lib/lua/luci/model/cbi/chinternet/auto.lua

local fs = require "nixio.fs"
local conffile = "/etc/dnsmasq.d/dnsmasq_glist_ipset.conf" 

f = SimpleForm("custom", translate("Chinternet - Auto Rule"), translate("This is the auto rule file for dnsmasq. Click to regenerate."))
f.reset = false
f.submit = false

m = f:field(Button, "regenerate", "Regenerate Auto Rule")
function m.write(self,section)
	luci.sys.call("/etc/glist2dnsmasq.sh -i -s glist -o /etc/dnsmasq_glist_ipset.conf >/dev/null")
end

t = f:field(TextValue, "conf")
t.rows = 20
function t.cfgvalue()
	return fs.readfile(conffile) or ""
end

return f

Custom Rule

用于修改配置文件,保存提交后重启进程。新建文件:

/usr/lib/lua/luci/model/cbi/chinternet/custom.lua

local fs = require "nixio.fs"
local conffile = "/etc/dnsmasq.d/dnsmasq_custom_ipset.conf" 

f = SimpleForm("custom", translate("Chinternet - Custom Rule"), translate("This is the custom rule file for dnsmasq."))

t = f:field(TextValue, "conf")
t.rmempty = true
t.rows = 20
function t.cfgvalue()
	return fs.readfile(conffile) or ""
end

function f.handle(self, state, data)
	if state == FORM_VALID then
		if data.conf then
			fs.writefile(conffile, data.conf:gsub("\r\n", "\n"))
			luci.sys.call("/etc/init.d/dnsmasq restart")
		end
	end
	return true
end

return f

<推广> 本站使用 BWHCN2 套餐搭建个人网络服务。