--[[ @object-name: nginx @object-desc: nginx 配置 --]] local utils = require "oui.utils" local uci = require "uci" local M = {} local CONF = "/etc/nginx/conf.d/gl.conf" local function match_port(l) local port = l:match("listen%s+(%d+);$") if port then return tonumber(port), false end port = l:match("listen%s+(%d+)%s+ssl;$") if port then return tonumber(port), true end end --[[ @method-type: call @method-name: get_config @method-desc: 获取配置 @out bool redirect_https 是否重定向 http 到 https @out number http_port HTTP 端口 @out number https_port HTTPS 端口 @in-example: {"jsonrpc":"2.0","id":1,"method":"call","params":["","nginx","get_config",{}]} @out-example: {"jsonrpc": "2.0", "id": 1, "result": { "redirect_https": false, "http_port": 80, "https_port": 443 }} --]] function M.get_config() local c = uci.cursor() local redirect_https = c:get("oui-httpd", "main", "redirect_https") == "1" local http_port local https_port for l in io.lines(CONF) do local port, ssl = match_port(l) if port then if ssl then https_port = port else http_port = port end end if http_port and https_port then break end end return { redirect_https = redirect_https, http_port = tonumber(http_port), https_port = tonumber(https_port) } end --[[ @method-type: call @method-name: set_config @method-desc: 配置 @in bool ?redirect_https=false 是否重定向 http 到 https @in number ?http_port=80 HTTP 端口 @in number ?https_port=443 HTTPS 端口 @in-example: {"jsonrpc":"2.0","id":1,"method":"call","params":["","nginx","set_config",{"redirect_https": false, "http_port": 80, "https_port": 443}]} @out-example: {"jsonrpc": "2.0", "id": 1, "result": null} --]] function M.set_config(params) local c = uci.cursor() local redirect_https = params.redirect_https and 1 or 0 local http_port = params.http_port or 80 local https_port = params.https_port or 443 local content = {} for l in io.lines(CONF) do if l:match("listen %d+;$") or l:match("listen %[::%]:%d+;$") then content[#content + 1] = l:gsub("%d+", http_port) elseif l:match("listen %d+%s+ssl;$") or l:match("listen %[::%]:%d+%s+ssl;$") then content[#content + 1] = l:gsub("%d+", https_port) else content[#content + 1] = l end end content[#content + 1] = "" utils.writefile(CONF, table.concat(content, "\n")) c:set("oui-httpd", "main", "redirect_https", redirect_https) c:set("firewall", "glservice_rule", "dest_port", http_port) c:set("firewall", "glservice_https", "dest_port", https_port) c:set("gl_ddns", "ddns", "http_port", http_port) c:set("gl_ddns", "ddns", "https_port", https_port) c:commit("firewall") c:commit("gl_ddns") local ddns_enabled = c:get("ddns", "glddns", "enabled") if ddns_enabled == "1" then ngx.timer.at(1,function() os.execute("/etc/init.d/firewall reload 2>/dev/null") end) end c:commit("oui-httpd") ngx.timer.at(1,function() os.execute("nginx -s reload 2>/dev/null") end) end return M