Browse Source

[Feature] Preliminary maps addon for controller

tags/2.6
Vsevolod Stakhov 3 years ago
parent
commit
f860d55ac4
3 changed files with 76 additions and 7 deletions
  1. 2
    1
      rules/controller/init.lua
  2. 72
    0
      rules/controller/maps.lua
  3. 2
    6
      src/lua/lua_map.c

+ 2
- 1
rules/controller/init.lua View File

@@ -25,7 +25,8 @@ local rspamd_logger = require "rspamd_logger"
-- Define default controller paths, could be overridden in local.d/controller.lua

local controller_plugin_paths = {
selectors = dofile(local_rules .. "/controller/selectors.lua")
selectors = dofile(local_rules .. "/controller/selectors.lua"),
maps = dofile(local_rules .. "/controller/maps.lua")
}

if rspamd_util.file_exists(local_conf .. '/controller.lua') then

+ 72
- 0
rules/controller/maps.lua View File

@@ -0,0 +1,72 @@
--[[
Copyright (c) 2020, Vsevolod Stakhov <vsevolod@highsecure.ru>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
]]--

-- Controller maps plugin
local maps_cache
local maps_aliases
local lua_util = require "lua_util"

local function maybe_fill_maps_cache()
if not maps_cache then
maps_cache = {}
maps_aliases = {}
local maps = rspamd_config:get_maps()
for _,m in ipairs(maps) do
-- We get the first url here and that's it
local url = m:get_uri()
if url ~= 'static' then
if not maps_cache[url] then
local alias = url:match('/([^/]+)$')
maps_cache[url] = m
if not maps_aliases[alias] then
maps_aliases[alias] = url
end
else
-- Do not override, as we don't care about duplicate maps that come from different
-- sources.
-- In theory, that should be cached but there are some exceptions even so far...
end
end
end
end
end

local function handle_query_map(_, conn, req_params)
maybe_fill_maps_cache()
if req_params.value and req_params.value ~= '' then

conn:send_ucl({success = false and true})
else
conn:send_error(404, 'missing value')
end
end

local function handle_list_maps(_, conn, _)
maybe_fill_maps_cache()
conn:send_ucl({maps = lua_util.keys(maps_cache),
aliases = maps_aliases})
end

return {
query = {
handler = handle_query_map,
enable = false,
},
list = {
handler = handle_list_maps,
enable = false,
},
}

+ 2
- 6
src/lua/lua_map.c View File

@@ -843,8 +843,6 @@ lua_map_get_key (lua_State * L)

if (!rspamd_parse_inet_address_ip (addr_str, len, addr->addr)) {
addr = NULL;
msg_warn ("invalid ip address: %*s, when checking map: %s",
(gint)len, addr_str, map->map->name);
}
}
else if (lua_type (L, 2) == LUA_TUSERDATA) {
@@ -1222,15 +1220,13 @@ lua_map_get_uri (lua_State *L)
{
LUA_TRACE_POINT;
struct rspamd_lua_map *map = lua_check_map (L, 1);
const gchar *ret = "undefined";
struct rspamd_map_backend *bk;
guint i;
guint i;

if (map != NULL) {
for (i = 0; i < map->map->backends->len; i ++) {
bk = g_ptr_array_index (map->map->backends, i);
ret = bk->uri;
lua_pushstring (L, ret);
lua_pushstring (L, bk->uri);
}
}
else {

Loading…
Cancel
Save