Exemple #1
0
void
rspamd_http_test_func (void)
{
	struct event_base *ev_base = event_init ();
	rspamd_mempool_t *pool = rspamd_mempool_new (rspamd_mempool_suggest_size ());
	gpointer serv_key, client_key, peer_key;
	struct rspamd_keypair_cache *c;
	rspamd_mempool_mutex_t *mtx;
	rspamd_inet_addr_t addr;
	struct timespec ts1, ts2;
	gchar filepath[PATH_MAX], buf[512];
	gint fd, i, j;
	pid_t sfd;
	GString *b32_key;
	double diff, total_diff = 0.0, latency[pconns * ntests], mean, std;

	rspamd_cryptobox_init ();
	rspamd_snprintf (filepath, sizeof (filepath), "/tmp/http-test-XXXXXX");
	g_assert ((fd = mkstemp (filepath)) != -1);

	for (i = 0; i < file_blocks; i ++) {
		memset (buf, 0, sizeof (buf));
		g_assert (write (fd, buf, sizeof (buf)) == sizeof (buf));
	}

	mtx = rspamd_mempool_get_mutex (pool);

	rspamd_parse_inet_address (&addr, "127.0.0.1");
	rspamd_inet_address_set_port (&addr, ottery_rand_range (30000) + 32768);
	serv_key = rspamd_http_connection_gen_key ();
	client_key = rspamd_http_connection_gen_key ();
	c = rspamd_keypair_cache_new (16);

	rspamd_mempool_lock_mutex (mtx);
	sfd = fork ();
	g_assert (sfd != -1);

	if (sfd == 0) {
		rspamd_http_server_func ("/tmp/", &addr, mtx, serv_key, c);
		exit (EXIT_SUCCESS);
	}

	rspamd_mempool_lock_mutex (mtx);

	/* Do client stuff */
	for (i = 0; i < ntests; i ++) {
		for (j = 0; j < pconns; j ++) {
			rspamd_http_client_func (filepath + sizeof ("/tmp") - 1, &addr,
					NULL, NULL, c, ev_base, &latency[i * pconns + j]);
		}
		clock_gettime (CLOCK_MONOTONIC, &ts1);
		event_base_loop (ev_base, 0);
		clock_gettime (CLOCK_MONOTONIC, &ts2);
		diff = (ts2.tv_sec - ts1.tv_sec) * 1000. +   /* Seconds */
				(ts2.tv_nsec - ts1.tv_nsec) / 1000000.;  /* Nanoseconds */
		total_diff += diff;
	}

	msg_info ("Made %d connections of size %d in %.6f ms, %.6f cps",
			ntests * pconns,
			sizeof (buf) * file_blocks,
			total_diff, ntests * pconns / total_diff * 1000.);
	mean = rspamd_http_calculate_mean (latency, &std);
	msg_info ("Latency: %.6f ms mean, %.6f dev",
			mean, std);

	/* Now test encrypted */
	b32_key = rspamd_http_connection_print_key (serv_key,
			RSPAMD_KEYPAIR_PUBKEY|RSPAMD_KEYPAIR_BASE32);
	g_assert (b32_key != NULL);
	peer_key = rspamd_http_connection_make_peer_key (b32_key->str);
	g_assert (peer_key != NULL);
	total_diff = 0.0;

	for (i = 0; i < ntests; i ++) {
		for (j = 0; j < pconns; j ++) {
			rspamd_http_client_func (filepath + sizeof ("/tmp") - 1, &addr,
					client_key, peer_key, c, ev_base, &latency[i * pconns + j]);
		}
		clock_gettime (CLOCK_MONOTONIC, &ts1);
		event_base_loop (ev_base, 0);
		clock_gettime (CLOCK_MONOTONIC, &ts2);
		diff = (ts2.tv_sec - ts1.tv_sec) * 1000. +   /* Seconds */
				(ts2.tv_nsec - ts1.tv_nsec) / 1000000.;  /* Nanoseconds */
		total_diff += diff;
	}

	msg_info ("Made %d encrypted connections of size %d in %.6f ms, %.6f cps",
			ntests * pconns,
			sizeof (buf) * file_blocks,
			total_diff, ntests * pconns / total_diff * 1000.);
	mean = rspamd_http_calculate_mean (latency, &std);
	msg_info ("Latency: %.6f ms mean, %.6f dev",
			mean, std);

	/* Restart server */
	kill (sfd, SIGTERM);
	wait (&i);
	sfd = fork ();
	g_assert (sfd != -1);

	if (sfd == 0) {
		rspamd_http_server_func ("/tmp/", &addr, mtx, serv_key, NULL);
		exit (EXIT_SUCCESS);
	}

	rspamd_mempool_lock_mutex (mtx);
	total_diff = 0.0;

	for (i = 0; i < ntests; i ++) {
		for (j = 0; j < pconns; j ++) {
			rspamd_http_client_func (filepath + sizeof ("/tmp") - 1, &addr,
					client_key, peer_key, c, ev_base, &latency[i * pconns + j]);
		}
		clock_gettime (CLOCK_MONOTONIC, &ts1);
		event_base_loop (ev_base, 0);
		clock_gettime (CLOCK_MONOTONIC, &ts2);
		diff = (ts2.tv_sec - ts1.tv_sec) * 1000. +   /* Seconds */
				(ts2.tv_nsec - ts1.tv_nsec) / 1000000.;  /* Nanoseconds */
		total_diff += diff;
	}

	msg_info ("Made %d uncached encrypted connections of size %d in %.6f ms, %.6f cps",
			ntests * pconns,
			sizeof (buf) * file_blocks,
			total_diff, ntests * pconns / total_diff * 1000.);
	mean = rspamd_http_calculate_mean (latency, &std);
	msg_info ("Latency: %.6f ms mean, %.6f dev",
			mean, std);

	close (fd);
	unlink (filepath);
	kill (sfd, SIGTERM);
}
Exemple #2
0
static gint
fuzzy_parse_rule (struct rspamd_config *cfg, const ucl_object_t *obj, gint cb_id)
{
	const ucl_object_t *value, *cur;
	struct fuzzy_rule *rule;
	ucl_object_iter_t it = NULL;
	const char *k = NULL;

	if (obj->type != UCL_OBJECT) {
		msg_err_config ("invalid rule definition");
		return -1;
	}

	rule = fuzzy_rule_new (fuzzy_module_ctx->default_symbol,
			fuzzy_module_ctx->fuzzy_pool);

	if ((value = ucl_object_find_key (obj, "mime_types")) != NULL) {
		it = NULL;
		while ((cur = ucl_iterate_object (value, &it, value->type == UCL_ARRAY))
				!= NULL) {
			rule->mime_types = g_list_concat (rule->mime_types,
					parse_mime_types (ucl_obj_tostring (cur)));
		}
	}

	if (rule->mime_types != NULL) {
		rspamd_mempool_add_destructor (fuzzy_module_ctx->fuzzy_pool,
			(rspamd_mempool_destruct_t)g_list_free, rule->mime_types);
	}

	if ((value = ucl_object_find_key (obj, "max_score")) != NULL) {
		rule->max_score = ucl_obj_todouble (value);
	}
	if ((value = ucl_object_find_key (obj,  "symbol")) != NULL) {
		rule->symbol = ucl_obj_tostring (value);
	}
	if ((value = ucl_object_find_key (obj, "read_only")) != NULL) {
		rule->read_only = ucl_obj_toboolean (value);
	}
	if ((value = ucl_object_find_key (obj, "skip_unknown")) != NULL) {
		rule->skip_unknown = ucl_obj_toboolean (value);
	}

	if ((value = ucl_object_find_key (obj, "servers")) != NULL) {
		rule->servers = rspamd_upstreams_create ();
		rspamd_mempool_add_destructor (fuzzy_module_ctx->fuzzy_pool,
				(rspamd_mempool_destruct_t)rspamd_upstreams_destroy,
				rule->servers);
		rspamd_upstreams_from_ucl (rule->servers, value, DEFAULT_PORT, NULL);
	}
	if ((value = ucl_object_find_key (obj, "fuzzy_map")) != NULL) {
		it = NULL;
		while ((cur = ucl_iterate_object (value, &it, true)) != NULL) {
			parse_flags (rule, cfg, cur, cb_id);
		}
	}

	if ((value = ucl_object_find_key (obj, "encryption_key")) != NULL) {
		/* Create key from user's input */
		k = ucl_object_tostring (value);
		if (k == NULL || (rule->peer_key =
					rspamd_http_connection_make_peer_key (k)) == NULL) {
			msg_err_config ("bad encryption key value: %s",
					k);
			return -1;
		}

		rule->local_key = rspamd_http_connection_gen_key ();
	}

	if ((value = ucl_object_find_key (obj, "fuzzy_key")) != NULL) {
		/* Create key from user's input */
		k = ucl_object_tostring (value);
	}

	/* Setup keys */
	if (k == NULL) {
		/* Use some default key for all ops */
		k = "rspamd";
	}
	rule->hash_key = g_string_sized_new (BLAKE2B_KEYBYTES);
	blake2 (rule->hash_key->str, k, NULL, BLAKE2B_KEYBYTES, strlen (k), 0);
	rule->hash_key->len = BLAKE2B_KEYBYTES;

	if ((value = ucl_object_find_key (obj, "fuzzy_shingles_key")) != NULL) {
		k = ucl_object_tostring (value);
	}
	if (k == NULL) {
		k = "rspamd";
	}
	rule->shingles_key = g_string_sized_new (16);
	blake2 (rule->shingles_key->str, k, NULL, 16, strlen (k), 0);
	rule->shingles_key->len = 16;

	if (rspamd_upstreams_count (rule->servers) == 0) {
		msg_err_config ("no servers defined for fuzzy rule with symbol: %s",
			rule->symbol);
		return -1;
	}
	else {
		fuzzy_module_ctx->fuzzy_rules = g_list_prepend (
			fuzzy_module_ctx->fuzzy_rules,
			rule);
		if (rule->symbol != fuzzy_module_ctx->default_symbol) {
			rspamd_symbols_cache_add_symbol (cfg->cache, rule->symbol,
					0,
					NULL, NULL,
					SYMBOL_TYPE_VIRTUAL|SYMBOL_TYPE_FINE,
					cb_id);
		}
	}

	rspamd_mempool_add_destructor (fuzzy_module_ctx->fuzzy_pool, fuzzy_free_rule,
			rule);

	return 0;
}