init commit
This commit is contained in:
46
plugins/ctx.lua
Normal file
46
plugins/ctx.lua
Normal file
@@ -0,0 +1,46 @@
|
||||
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
|
||||
Reference in New Issue
Block a user