Skip to content

Commit

Permalink
libruleset: advanced rule actions
Browse files Browse the repository at this point in the history
Signed-off-by: He Xian <hexian000@outlook.com>
  • Loading branch information
hexian000 committed Oct 17, 2024
1 parent cc4ed59 commit 083cffa
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions libruleset.lua
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ function rlist:get(i)
pos = pos + len
end
return self[pos]
elseif -len <= i and i <= -1 then
end
if -len <= i and i <= -1 then
local pos = self.pos - (len + i + 1)
if pos < 1 then
pos = pos + len
Expand Down Expand Up @@ -658,6 +659,31 @@ function rule.proxy(...)
end
end

function rule.rewrite(pattern, repl, ...)
local chain = list:new({ ... }):reverse()
return function(addr)
return addr:gsub(pattern, repl), chain:unpack()
end
end

function rule.loopback(pattern, repl)
return function(addr)
local ruleset = table.get(_G, "ruleset")
if not ruleset then
return nil
end
addr = addr:gsub(pattern, repl)
local host, _ = splithostport(addr)
if parse_ipv4(host) then
return ruleset.route(addr)
end
if parse_ipv6(host) then
return ruleset.route6(addr)
end
return ruleset.resolve(addr)
end
end

_G.rule = rule

-- [[ load balancing actions ]] --
Expand Down Expand Up @@ -815,7 +841,8 @@ local function resolve_(addr)
-- check if the addr is a raw address
if parse_ipv4(host) then
return route_(addr)
elseif parse_ipv6(host) then
end
if parse_ipv6(host) then
return route6_(addr)
end
-- global default
Expand Down

0 comments on commit 083cffa

Please sign in to comment.