commit 9e0a015: [Feature] Monitored: Support random lookups
Vsevolod Stakhov
vsevolod at highsecure.ru
Fri Aug 23 17:35:03 UTC 2019
Author: Vsevolod Stakhov
Date: 2019-08-23 18:23:44 +0100
URL: https://github.com/rspamd/rspamd/commit/9e0a0156e3c1a1ed80b2348be8705b37daa86134
[Feature] Monitored: Support random lookups
---
src/libserver/monitored.c | 55 ++++++++++++++++++++++++++++++++++++++++++-----
src/libserver/monitored.h | 5 +++--
2 files changed, 53 insertions(+), 7 deletions(-)
diff --git a/src/libserver/monitored.c b/src/libserver/monitored.c
index d64ec92fe..05e5efcc7 100644
--- a/src/libserver/monitored.c
+++ b/src/libserver/monitored.c
@@ -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) {
diff --git a/src/libserver/monitored.h b/src/libserver/monitored.h
index 5b2b60d11..2453a8f48 100644
--- a/src/libserver/monitored.h
+++ b/src/libserver/monitored.h
@@ -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)
};
/**
More information about the Commits
mailing list