luci: lua code convert indentation to TAB
This commit is contained in:
parent
2d5a76a95d
commit
47f44fcfdd
@ -18,12 +18,12 @@ LOG_FILE = "/tmp/log/" .. appname .. ".log"
|
|||||||
CACHE_PATH = "/tmp/etc/" .. appname .. "_tmp"
|
CACHE_PATH = "/tmp/etc/" .. appname .. "_tmp"
|
||||||
|
|
||||||
function log(...)
|
function log(...)
|
||||||
local result = os.date("%Y-%m-%d %H:%M:%S: ") .. table.concat({...}, " ")
|
local result = os.date("%Y-%m-%d %H:%M:%S: ") .. table.concat({...}, " ")
|
||||||
local f, err = io.open(LOG_FILE, "a")
|
local f, err = io.open(LOG_FILE, "a")
|
||||||
if f and err == nil then
|
if f and err == nil then
|
||||||
f:write(result .. "\n")
|
f:write(result .. "\n")
|
||||||
f:close()
|
f:close()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function exec_call(cmd)
|
function exec_call(cmd)
|
||||||
|
@ -8,13 +8,13 @@ local uci = api.uci
|
|||||||
local sys = api.sys
|
local sys = api.sys
|
||||||
|
|
||||||
local log = function(...)
|
local log = function(...)
|
||||||
api.log(...)
|
api.log(...)
|
||||||
end
|
end
|
||||||
|
|
||||||
function get_ip_port_from(str)
|
function get_ip_port_from(str)
|
||||||
local result_port = sys.exec("echo -n " .. str .. " | sed -n 's/^.*[:#]\\([0-9]*\\)$/\\1/p'")
|
local result_port = sys.exec("echo -n " .. str .. " | sed -n 's/^.*[:#]\\([0-9]*\\)$/\\1/p'")
|
||||||
local result_ip = sys.exec(string.format("__host=%s;__varport=%s;", str, result_port) .. "echo -n ${__host%%${__varport:+[:#]${__varport}*}}")
|
local result_ip = sys.exec(string.format("__host=%s;__varport=%s;", str, result_port) .. "echo -n ${__host%%${__varport:+[:#]${__varport}*}}")
|
||||||
return result_ip, result_port
|
return result_ip, result_port
|
||||||
end
|
end
|
||||||
|
|
||||||
local new_port
|
local new_port
|
||||||
@ -44,148 +44,148 @@ local f_out = io.open(haproxy_file, "a")
|
|||||||
|
|
||||||
local haproxy_config = [[
|
local haproxy_config = [[
|
||||||
global
|
global
|
||||||
daemon
|
daemon
|
||||||
log 127.0.0.1 local2
|
log 127.0.0.1 local2
|
||||||
maxconn 60000
|
maxconn 60000
|
||||||
stats socket %s/haproxy.sock
|
stats socket %s/haproxy.sock
|
||||||
%s
|
%s
|
||||||
|
|
||||||
defaults
|
defaults
|
||||||
mode tcp
|
mode tcp
|
||||||
log global
|
log global
|
||||||
option tcplog
|
option tcplog
|
||||||
option dontlognull
|
option dontlognull
|
||||||
option http-server-close
|
option http-server-close
|
||||||
#option forwardfor except 127.0.0.0/8
|
#option forwardfor except 127.0.0.0/8
|
||||||
option redispatch
|
option redispatch
|
||||||
retries 2
|
retries 2
|
||||||
timeout http-request 10s
|
timeout http-request 10s
|
||||||
timeout queue 1m
|
timeout queue 1m
|
||||||
timeout connect 10s
|
timeout connect 10s
|
||||||
timeout client 1m
|
timeout client 1m
|
||||||
timeout server 1m
|
timeout server 1m
|
||||||
timeout http-keep-alive 10s
|
timeout http-keep-alive 10s
|
||||||
timeout check 10s
|
timeout check 10s
|
||||||
maxconn 3000
|
maxconn 3000
|
||||||
|
|
||||||
resolvers mydns
|
resolvers mydns
|
||||||
resolve_retries 3
|
resolve_retries 3
|
||||||
timeout retry 3s
|
timeout retry 3s
|
||||||
hold valid 600s
|
hold valid 600s
|
||||||
]]
|
]]
|
||||||
|
|
||||||
f_out:write(string.format(haproxy_config, haproxy_path, health_check_type == "passwall_logic" and string.format([[
|
f_out:write(string.format(haproxy_config, haproxy_path, health_check_type == "passwall_logic" and string.format([[
|
||||||
external-check
|
external-check
|
||||||
insecure-fork-wanted
|
insecure-fork-wanted
|
||||||
]]) or ""
|
]]) or ""
|
||||||
))
|
))
|
||||||
|
|
||||||
local index = 0
|
local index = 0
|
||||||
string.gsub(haproxy_dns, '[^' .. "," .. ']+', function(w)
|
string.gsub(haproxy_dns, '[^' .. "," .. ']+', function(w)
|
||||||
index = index + 1
|
index = index + 1
|
||||||
local s = w:gsub("#", ":")
|
local s = w:gsub("#", ":")
|
||||||
if not s:find(":") then
|
if not s:find(":") then
|
||||||
s = s .. ":53"
|
s = s .. ":53"
|
||||||
end
|
end
|
||||||
f_out:write(string.format([[
|
f_out:write(string.format([[
|
||||||
nameserver dns%s %s
|
nameserver dns%s %s
|
||||||
]], index, s))
|
]], index, s))
|
||||||
end)
|
end)
|
||||||
|
|
||||||
local listens = {}
|
local listens = {}
|
||||||
|
|
||||||
uci:foreach(appname, "haproxy_config", function(t)
|
uci:foreach(appname, "haproxy_config", function(t)
|
||||||
if t.enabled == "1" then
|
if t.enabled == "1" then
|
||||||
local server_remark
|
local server_remark
|
||||||
local server_address
|
local server_address
|
||||||
local server_port
|
local server_port
|
||||||
local lbss = t.lbss
|
local lbss = t.lbss
|
||||||
local listen_port = tonumber(t.haproxy_port) or 0
|
local listen_port = tonumber(t.haproxy_port) or 0
|
||||||
local server_node = uci:get_all(appname, lbss)
|
local server_node = uci:get_all(appname, lbss)
|
||||||
if server_node and server_node.address and server_node.port then
|
if server_node and server_node.address and server_node.port then
|
||||||
server_remark = server_node.address .. ":" .. server_node.port
|
server_remark = server_node.address .. ":" .. server_node.port
|
||||||
server_address = server_node.address
|
server_address = server_node.address
|
||||||
server_port = server_node.port
|
server_port = server_node.port
|
||||||
t.origin_address = server_address
|
t.origin_address = server_address
|
||||||
t.origin_port = server_port
|
t.origin_port = server_port
|
||||||
if health_check_type == "passwall_logic" then
|
if health_check_type == "passwall_logic" then
|
||||||
if server_node.type ~= "Socks" then
|
if server_node.type ~= "Socks" then
|
||||||
local relay_port = server_node.port
|
local relay_port = server_node.port
|
||||||
new_port = get_new_port()
|
new_port = get_new_port()
|
||||||
local config_file = string.format("haproxy_%s_%s.json", t[".name"], new_port)
|
local config_file = string.format("haproxy_%s_%s.json", t[".name"], new_port)
|
||||||
sys.call(string.format('/usr/share/%s/app.sh run_socks "%s"> /dev/null',
|
sys.call(string.format('/usr/share/%s/app.sh run_socks "%s"> /dev/null',
|
||||||
appname,
|
appname,
|
||||||
string.format("flag=%s node=%s bind=%s socks_port=%s config_file=%s",
|
string.format("flag=%s node=%s bind=%s socks_port=%s config_file=%s",
|
||||||
new_port, --flag
|
new_port, --flag
|
||||||
server_node[".name"], --node
|
server_node[".name"], --node
|
||||||
"127.0.0.1", --bind
|
"127.0.0.1", --bind
|
||||||
new_port, --socks port
|
new_port, --socks port
|
||||||
config_file --config file
|
config_file --config file
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
server_address = "127.0.0.1"
|
server_address = "127.0.0.1"
|
||||||
server_port = new_port
|
server_port = new_port
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
server_address, server_port = get_ip_port_from(lbss)
|
server_address, server_port = get_ip_port_from(lbss)
|
||||||
server_remark = server_address .. ":" .. server_port
|
server_remark = server_address .. ":" .. server_port
|
||||||
t.origin_address = server_address
|
t.origin_address = server_address
|
||||||
t.origin_port = server_port
|
t.origin_port = server_port
|
||||||
end
|
end
|
||||||
if server_address and server_port and listen_port > 0 then
|
if server_address and server_port and listen_port > 0 then
|
||||||
if not listens[listen_port] then
|
if not listens[listen_port] then
|
||||||
listens[listen_port] = {}
|
listens[listen_port] = {}
|
||||||
end
|
end
|
||||||
t.server_remark = server_remark
|
t.server_remark = server_remark
|
||||||
t.server_address = server_address
|
t.server_address = server_address
|
||||||
t.server_port = server_port
|
t.server_port = server_port
|
||||||
table.insert(listens[listen_port], t)
|
table.insert(listens[listen_port], t)
|
||||||
else
|
else
|
||||||
log(" - 丢弃1个明显无效的节点")
|
log(" - 丢弃1个明显无效的节点")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
local sortTable = {}
|
local sortTable = {}
|
||||||
for i in pairs(listens) do
|
for i in pairs(listens) do
|
||||||
if i ~= nil then
|
if i ~= nil then
|
||||||
table.insert(sortTable, i)
|
table.insert(sortTable, i)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
table.sort(sortTable, function(a,b) return (a < b) end)
|
table.sort(sortTable, function(a,b) return (a < b) end)
|
||||||
|
|
||||||
for i, port in pairs(sortTable) do
|
for i, port in pairs(sortTable) do
|
||||||
log(" + 入口 0.0.0.0:%s..." % port)
|
log(" + 入口 0.0.0.0:%s..." % port)
|
||||||
|
|
||||||
f_out:write(string.format([[
|
f_out:write(string.format([[
|
||||||
listen %s
|
listen %s
|
||||||
bind 0.0.0.0:%s
|
bind 0.0.0.0:%s
|
||||||
mode tcp
|
mode tcp
|
||||||
balance roundrobin
|
balance roundrobin
|
||||||
]], port, port))
|
]], port, port))
|
||||||
|
|
||||||
if health_check_type == "passwall_logic" then
|
if health_check_type == "passwall_logic" then
|
||||||
f_out:write(string.format([[
|
f_out:write(string.format([[
|
||||||
option external-check
|
option external-check
|
||||||
external-check command "/usr/share/passwall/haproxy_check.sh"
|
external-check command "/usr/share/passwall/haproxy_check.sh"
|
||||||
]], port, port))
|
]], port, port))
|
||||||
end
|
end
|
||||||
|
|
||||||
for i, o in ipairs(listens[port]) do
|
for i, o in ipairs(listens[port]) do
|
||||||
local remark = o.server_remark
|
local remark = o.server_remark
|
||||||
local server = o.server_address .. ":" .. o.server_port
|
local server = o.server_address .. ":" .. o.server_port
|
||||||
f_out:write(string.format([[
|
f_out:write(string.format([[
|
||||||
server %s %s weight %s check resolvers mydns inter %s rise 1 fall 3 %s
|
server %s %s weight %s check resolvers mydns inter %s rise 1 fall 3 %s
|
||||||
]], remark, server, o.lbweight, health_check_inter, o.backup == "1" and "backup" or ""))
|
]], remark, server, o.lbweight, health_check_inter, o.backup == "1" and "backup" or ""))
|
||||||
|
|
||||||
if o.export ~= "0" then
|
if o.export ~= "0" then
|
||||||
sys.call(string.format("/usr/share/passwall/app.sh add_ip2route %s %s", o.origin_address, o.export))
|
sys.call(string.format("/usr/share/passwall/app.sh add_ip2route %s %s", o.origin_address, o.export))
|
||||||
end
|
end
|
||||||
|
|
||||||
log(string.format(" | - 出口节点:%s:%s,权重:%s", o.origin_address, o.origin_port, o.lbweight))
|
log(string.format(" | - 出口节点:%s:%s,权重:%s", o.origin_address, o.origin_port, o.lbweight))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
--控制台配置
|
--控制台配置
|
||||||
@ -194,12 +194,12 @@ local console_user = uci:get(appname, "@global_haproxy[0]", "console_user")
|
|||||||
local console_password = uci:get(appname, "@global_haproxy[0]", "console_password")
|
local console_password = uci:get(appname, "@global_haproxy[0]", "console_password")
|
||||||
local str = [[
|
local str = [[
|
||||||
listen console
|
listen console
|
||||||
bind 0.0.0.0:%s
|
bind 0.0.0.0:%s
|
||||||
mode http
|
mode http
|
||||||
stats refresh 30s
|
stats refresh 30s
|
||||||
stats uri /
|
stats uri /
|
||||||
stats admin if TRUE
|
stats admin if TRUE
|
||||||
%s
|
%s
|
||||||
]]
|
]]
|
||||||
f_out:write(string.format(str, console_port, (console_user and console_user ~= "" and console_password and console_password ~= "") and "stats auth " .. console_user .. ":" .. console_password or ""))
|
f_out:write(string.format(str, console_port, (console_user and console_user ~= "" and console_password and console_password ~= "") and "stats auth " .. console_user .. ":" .. console_password or ""))
|
||||||
log(string.format(" * 控制台端口:%s", console_port))
|
log(string.format(" * 控制台端口:%s", console_port))
|
||||||
|
Loading…
Reference in New Issue
Block a user