1
0
Files
envoy-apisix/plugins/ctx.lua
2023-04-14 17:12:13 +08:00

47 lines
991 B
Lua

local _M = { version = 0.2 }
local function get_client_ip(stream)
local ip = stream:downstreamLocalAddress()
if ip then
return ip
end
ip = stream:downstreamDirectRemoteAddress()
if ip then
return ip
end
end
function _M.set_vars_meta(handle)
local stream = handle:streamInfo()
local meta = stream:dynamicMetadata()
local var = {}
var._cache = meta:get("envoy.filters.http.lua") or {}
var.remote_addr = get_client_ip(stream)
setmetatable(var, {
__index = function(self, key)
local cached = self._cache[key]
if cached ~= nil then
return cached
end
return nil
end,
__newindex = function(self, key, val)
meta:set("envoy.filters.http.lua", key, val)
self._cache[key] = val
end,
__pairs = function (self)
return next, self._cache, nil
end,
})
return var
end
return _M