aboutsummaryrefslogtreecommitdiffstats
path: root/rules/controller
diff options
context:
space:
mode:
authorAndrew Lewis <nerf@judo.za.org>2021-11-17 15:57:01 +0200
committerAndrew Lewis <nerf@judo.za.org>2021-11-17 15:57:01 +0200
commitb7e34400244638aa01a3520ba965250a15e86904 (patch)
tree3cb05eaf012e160dce179b06c604ba8c593e18e0 /rules/controller
parentda4034ce506fd5bb93687791c5a4af3c9218bba3 (diff)
downloadrspamd-b7e34400244638aa01a3520ba965250a15e86904.tar.gz
rspamd-b7e34400244638aa01a3520ba965250a15e86904.zip
[Feature] JSON endpoint for querying maps
Diffstat (limited to 'rules/controller')
-rw-r--r--rules/controller/maps.lua66
1 files changed, 65 insertions, 1 deletions
diff --git a/rules/controller/maps.lua b/rules/controller/maps.lua
index 71bf4beef..9079cb39a 100644
--- a/rules/controller/maps.lua
+++ b/rules/controller/maps.lua
@@ -18,6 +18,8 @@ limitations under the License.
local maps_cache
local maps_aliases
local lua_util = require "lua_util"
+local ts = require("tableshape").types
+local ucl = require "ucl"
local function maybe_fill_maps_cache()
if not maps_cache then
@@ -140,11 +142,73 @@ local function handle_list_maps(_, conn, _)
}
end
+local query_json_schema = ts.shape{
+ maps = ts.array_of(ts.string):is_optional(),
+ report_misses = ts.boolean:is_optional(),
+ values = ts.array_of(ts.string),
+}
+
+local function handle_query_json(task, conn)
+ maybe_fill_maps_cache()
+
+ local parser = ucl.parser()
+ local ok, err = parser:parse_text(task:get_rawbody())
+ if not ok then
+ conn:send_error(400, err)
+ return
+ end
+ local obj = parser:get_object()
+
+ ok, err = query_json_schema:transform(obj)
+ if not ok then
+ conn:send_error(400, err)
+ return
+ end
+
+ local maps_to_check = {}
+ local report_misses = obj.report_misses
+ local results = {}
+
+ if obj.maps then
+ for _,mn in ipairs(obj.maps) do
+ if maps_cache[mn] then
+ maps_to_check[mn] = maps_cache[mn]
+ else
+ local alias = maps_aliases[mn]
+
+ if alias then
+ maps_to_check[alias] = maps_cache[alias]
+ else
+ conn:send_error(400, 'no such map: ' .. mn)
+ return
+ end
+ end
+ end
+ else
+ maps_to_check = maps_cache
+ end
+
+ for _,key in ipairs(obj.values) do
+ for uri,m in pairs(maps_to_check) do
+ check_specific_map(key, uri, m, results, report_misses)
+ end
+ end
+ conn:send_ucl{
+ success = (#results > 0),
+ results = results
+ }
+end
+
return {
query = {
handler = handle_query_map,
enable = false,
},
+ query_json = {
+ handler = handle_query_json,
+ enable = false,
+ need_task = true,
+ },
query_specific = {
handler = handle_query_specific_map,
enable = false,
@@ -153,4 +217,4 @@ return {
handler = handle_list_maps,
enable = false,
},
-} \ No newline at end of file
+}
an class="nx">Home(ctx *context.Context) { if ctx.IsSigned { if !ctx.Doer.IsActive && setting.Service.RegisterEmailConfirm { ctx.Data["Title"] = ctx.Tr("auth.active_your_account") ctx.HTML(http.StatusOK, auth.TplActivate) } else if !ctx.Doer.IsActive || ctx.Doer.ProhibitLogin { log.Info("Failed authentication attempt for %s from %s", ctx.Doer.Name, ctx.RemoteAddr()) ctx.Data["Title"] = ctx.Tr("auth.prohibit_login") ctx.HTML(http.StatusOK, "user/auth/prohibit_login") } else if ctx.Doer.MustChangePassword { ctx.Data["Title"] = ctx.Tr("auth.must_change_password") ctx.Data["ChangePasscodeLink"] = setting.AppSubURL + "/user/change_password" middleware.SetRedirectToCookie(ctx.Resp, setting.AppSubURL+ctx.Req.URL.RequestURI()) ctx.Redirect(setting.AppSubURL + "/user/settings/change_password") } else { user.Dashboard(ctx) } return // Check non-logged users landing page. } else if setting.LandingPageURL != setting.LandingPageHome { ctx.Redirect(setting.AppSubURL + string(setting.LandingPageURL)) return } // Check auto-login. uname := ctx.GetSiteCookie(setting.CookieUserName) if len(uname) != 0 { ctx.Redirect(setting.AppSubURL + "/user/login") return } ctx.Data["PageIsHome"] = true ctx.Data["IsRepoIndexerEnabled"] = setting.Indexer.RepoIndexerEnabled ctx.HTML(http.StatusOK, tplHome) } // HomeSitemap renders the main sitemap func HomeSitemap(ctx *context.Context) { m := sitemap.NewSitemapIndex() if !setting.Service.Explore.DisableUsersPage { _, cnt, err := user_model.SearchUsers(ctx, &user_model.SearchUserOptions{ Type: user_model.UserTypeIndividual, ListOptions: db.ListOptions{PageSize: 1}, IsActive: util.OptionalBoolTrue, Visible: []structs.VisibleType{structs.VisibleTypePublic}, }) if err != nil { ctx.ServerError("SearchUsers", err) return } count := int(cnt) idx := 1 for i := 0; i < count; i += setting.UI.SitemapPagingNum { m.Add(sitemap.URL{URL: setting.AppURL + "explore/users/sitemap-" + strconv.Itoa(idx) + ".xml"}) idx++ } } _, cnt, err := repo_model.SearchRepository(ctx, &repo_model.SearchRepoOptions{ ListOptions: db.ListOptions{ PageSize: 1, }, Actor: ctx.Doer, AllPublic: true, }) if err != nil { ctx.ServerError("SearchRepository", err) return } count := int(cnt) idx := 1 for i := 0; i < count; i += setting.UI.SitemapPagingNum { m.Add(sitemap.URL{URL: setting.AppURL + "explore/repos/sitemap-" + strconv.Itoa(idx) + ".xml"}) idx++ } ctx.Resp.Header().Set("Content-Type", "text/xml") if _, err := m.WriteTo(ctx.Resp); err != nil { log.Error("Failed writing sitemap: %v", err) } } // NotFound render 404 page func NotFound(ctx *context.Context) { ctx.Data["Title"] = "Page Not Found" ctx.NotFound("home.NotFound", nil) }