]> source.dussan.org Git - rspamd.git/commitdiff
[Feature] Monitored: Support random lookups
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Fri, 23 Aug 2019 17:23:44 +0000 (18:23 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Fri, 23 Aug 2019 17:23:44 +0000 (18:23 +0100)
src/libserver/monitored.c
src/libserver/monitored.h

index d64ec92fe5c5af88120658d9807ee60066b924c4..05e5efcc78d8cbf13c60d93eea4aa22fcb849d06 100644 (file)
@@ -199,6 +199,29 @@ struct rspamd_dns_monitored_conf {
        gdouble check_tm;
 };
 
+static void
+rspamd_monitored_dns_random (struct rspamd_monitored *m,
+                                                        struct rspamd_dns_monitored_conf *conf)
+{
+       gchar random_prefix[64];
+       const gchar dns_chars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_";
+       gint len;
+
+       len = rspamd_random_uint64_fast () % sizeof (random_prefix);
+
+       if (len < 8) {
+               len = 8;
+       }
+
+       for (guint i = 0; i < len; i ++) {
+               guint idx = rspamd_random_uint64_fast () % sizeof (dns_chars);
+               random_prefix[i] = dns_chars[idx];
+       }
+
+       conf->request->len = 0;
+       rspamd_printf_gstring (conf->request, "%*.s.%s", len, random_prefix, m->url);
+}
+
 static void *
 rspamd_monitored_dns_conf (struct rspamd_monitored *m,
                struct rspamd_monitored_ctx *ctx,
@@ -229,10 +252,13 @@ rspamd_monitored_dns_conf (struct rspamd_monitored *m,
                        }
                }
 
-               elt = ucl_object_lookup (opts, "prefix");
+               if (!(m->flags & RSPAMD_MONITORED_RANDOM)) {
+                       /* Prefix is useless for random monitored */
+                       elt = ucl_object_lookup (opts, "prefix");
 
-               if (elt && ucl_object_type (elt) == UCL_STRING) {
-                       rspamd_printf_gstring (req, "%s.", ucl_object_tostring (elt));
+                       if (elt && ucl_object_type (elt) == UCL_STRING) {
+                               rspamd_printf_gstring (req, "%s.", ucl_object_tostring (elt));
+                       }
                }
 
                elt = ucl_object_lookup (opts, "ipnet");
@@ -267,7 +293,10 @@ rspamd_monitored_dns_conf (struct rspamd_monitored *m,
                }
        }
 
-       rspamd_printf_gstring (req, "%s", m->url);
+       if (!(m->flags & RSPAMD_MONITORED_RANDOM)) {
+               rspamd_printf_gstring (req, "%s", m->url);
+       }
+
        conf->request = req;
 
        return conf;
@@ -378,6 +407,10 @@ rspamd_monitored_dns_mon (struct rspamd_monitored *m,
 {
        struct rspamd_dns_monitored_conf *conf = ud;
 
+       if (m->flags & RSPAMD_MONITORED_RANDOM) {
+               rspamd_monitored_dns_random (m, conf);
+       }
+
        if (!rdns_make_request_full (ctx->resolver, rspamd_monitored_dns_cb,
                        conf, ctx->cfg->dns_timeout, ctx->cfg->dns_retransmits,
                        1, conf->request->str, conf->rt)) {
@@ -478,11 +511,11 @@ rspamd_monitored_create_ (struct rspamd_monitored_ctx *ctx,
        gchar *cksum_encoded, cksum[rspamd_cryptobox_HASHBYTES];
 
        g_assert (ctx != NULL);
-       g_assert (line != NULL);
 
        m = g_malloc0 (sizeof (*m));
        m->type = type;
        m->flags = flags;
+
        m->url = g_strdup (line);
        m->ctx = ctx;
        m->monitoring_mult = 1.0;
@@ -500,6 +533,18 @@ rspamd_monitored_create_ (struct rspamd_monitored_ctx *ctx,
                return NULL;
        }
 
+       if (opts) {
+               const ucl_object_t *rnd_obj;
+
+               rnd_obj = ucl_object_lookup (opts, "random");
+
+               if (rnd_obj && ucl_object_type (rnd_obj) == UCL_BOOLEAN) {
+                       if (ucl_object_toboolean (rnd_obj)) {
+                               m->flags |= RSPAMD_MONITORED_RANDOM;
+                       }
+               }
+       }
+
        m->proc.ud = m->proc.monitored_config (m, ctx, opts);
 
        if (m->proc.ud == NULL) {
index 5b2b60d11f850ba1034cf16334cf8d9e742ac00f..2453a8f48c271d0ef03831c62e5c752fb062b35d 100644 (file)
@@ -34,8 +34,9 @@ enum rspamd_monitored_type {
 };
 
 enum rspamd_monitored_flags {
-       RSPAMD_MONITORED_DEFAULT = 0,
-       RSPAMD_MONITORED_RBL = (1 << 0),
+       RSPAMD_MONITORED_DEFAULT = 0u,
+       RSPAMD_MONITORED_RBL = (1u << 0u),
+       RSPAMD_MONITORED_RANDOM = (1u << 1u)
 };
 
 /**