You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

http_backend.cxx 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /*-
  2. * Copyright 2022 Vsevolod Stakhov
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "config.h"
  17. #include "stat_internal.h"
  18. #include "libserver/http/http_connection.h"
  19. #include "libserver/mempool_vars_internal.h"
  20. #include "upstream.h"
  21. #include "contrib/robin-hood/robin_hood.h"
  22. #include <vector>
  23. namespace rspamd::stat::http {
  24. #define msg_debug_stat_http(...) rspamd_conditional_debug_fast (NULL, NULL, \
  25. rspamd_stat_http_log_id, "stat_http", task->task_pool->tag.uid, \
  26. RSPAMD_LOG_FUNC, \
  27. __VA_ARGS__)
  28. INIT_LOG_MODULE(stat_http)
  29. /* Represents all http backends defined in some configuration */
  30. class http_backends_collection {
  31. std::vector<struct rspamd_statfile *> backends;
  32. double timeout = 1.0; /* Default timeout */
  33. struct upstream_list *read_servers = nullptr;
  34. struct upstream_list *write_servers = nullptr;
  35. public:
  36. static auto get() -> http_backends_collection& {
  37. static http_backends_collection *singleton = nullptr;
  38. if (singleton == nullptr) {
  39. singleton = new http_backends_collection;
  40. }
  41. return *singleton;
  42. }
  43. /**
  44. * Add a new backend and (optionally initialize the basic backend parameters
  45. * @param ctx
  46. * @param cfg
  47. * @param st
  48. * @return
  49. */
  50. auto add_backend(struct rspamd_stat_ctx *ctx,
  51. struct rspamd_config *cfg,
  52. struct rspamd_statfile *st) -> bool;
  53. /**
  54. * Remove a statfile cleaning things up if the last statfile is removed
  55. * @param st
  56. * @return
  57. */
  58. auto remove_backend(struct rspamd_statfile *st) -> bool;
  59. upstream *get_upstream(bool is_learn);
  60. private:
  61. http_backends_collection() = default;
  62. auto first_init(struct rspamd_stat_ctx *ctx,
  63. struct rspamd_config *cfg,
  64. struct rspamd_statfile *st) -> bool;
  65. };
  66. /*
  67. * Created one per each task
  68. */
  69. class http_backend_runtime final {
  70. public:
  71. static auto create(struct rspamd_task *task, bool is_learn) -> http_backend_runtime *;
  72. /* Add a new statfile with a specific id to the list of statfiles */
  73. auto notice_statfile(int id, const struct rspamd_statfile_config *st) -> void {
  74. seen_statfiles[id] = st;
  75. }
  76. auto process_tokens(struct rspamd_task* task,
  77. GPtrArray* tokens,
  78. gint id,
  79. bool learn) -> bool;
  80. private:
  81. http_backends_collection *all_backends;
  82. robin_hood::unordered_flat_map<int, const struct rspamd_statfile_config *> seen_statfiles;
  83. struct upstream *selected;
  84. private:
  85. http_backend_runtime(struct rspamd_task *task, bool is_learn) :
  86. all_backends(&http_backends_collection::get()) {
  87. selected = all_backends->get_upstream(is_learn);
  88. }
  89. ~http_backend_runtime() = default;
  90. static auto dtor(void *p) -> void {
  91. ((http_backend_runtime *)p)->~http_backend_runtime();
  92. }
  93. };
  94. /*
  95. * Efficient way to make a messagepack payload from stat tokens,
  96. * avoiding any intermediate libraries, as we would send many tokens
  97. * all together
  98. */
  99. static auto
  100. stat_tokens_to_msgpack(GPtrArray *tokens) -> std::vector<std::uint8_t>
  101. {
  102. std::vector<std::uint8_t> ret;
  103. rspamd_token_t *cur;
  104. int i;
  105. /*
  106. * We define array, it's size and N elements each is uint64_t
  107. * Layout:
  108. * 0xdd - array marker
  109. * [4 bytes be] - size of the array
  110. * [ 0xcf + <8 bytes BE integer>] * N - array elements
  111. */
  112. ret.resize(tokens->len * (sizeof(std::uint64_t) + 1) + 5);
  113. ret.push_back('\xdd');
  114. std::uint32_t ulen = GUINT32_TO_BE(tokens->len);
  115. std::copy((const std::uint8_t *)&ulen,
  116. ((const std::uint8_t *)&ulen) + sizeof(ulen), std::back_inserter(ret));
  117. PTR_ARRAY_FOREACH(tokens, i, cur) {
  118. ret.push_back('\xcf');
  119. std::uint64_t val = GUINT64_TO_BE(cur->data);
  120. std::copy((const std::uint8_t *)&val,
  121. ((const std::uint8_t *)&val) + sizeof(val), std::back_inserter(ret));
  122. }
  123. return ret;
  124. }
  125. auto http_backend_runtime::create(struct rspamd_task *task, bool is_learn) -> http_backend_runtime *
  126. {
  127. /* Alloc type provide proper size and alignment */
  128. auto *allocated_runtime = rspamd_mempool_alloc_type(task->task_pool, http_backend_runtime);
  129. rspamd_mempool_add_destructor(task->task_pool, http_backend_runtime::dtor, allocated_runtime);
  130. return new (allocated_runtime) http_backend_runtime{task, is_learn};
  131. }
  132. auto
  133. http_backend_runtime::process_tokens(struct rspamd_task *task, GPtrArray *tokens, gint id, bool learn) -> bool
  134. {
  135. if (!learn) {
  136. if (id == seen_statfiles.size() - 1) {
  137. /* Emit http request on the last statfile */
  138. }
  139. }
  140. else {
  141. /* On learn we need to learn all statfiles that we were requested to learn */
  142. if (seen_statfiles.empty()) {
  143. /* Request has been already set, or nothing to learn */
  144. return true;
  145. }
  146. else {
  147. seen_statfiles.clear();
  148. }
  149. }
  150. return true;
  151. }
  152. auto
  153. http_backends_collection::add_backend(struct rspamd_stat_ctx *ctx,
  154. struct rspamd_config *cfg,
  155. struct rspamd_statfile *st) -> bool
  156. {
  157. /* On empty list of backends we know that we need to load backend data actually */
  158. if (backends.empty()) {
  159. if (!first_init(ctx, cfg, st)) {
  160. return false;
  161. }
  162. }
  163. backends.push_back(st);
  164. return true;
  165. }
  166. auto http_backends_collection::first_init(struct rspamd_stat_ctx *ctx,
  167. struct rspamd_config *cfg,
  168. struct rspamd_statfile *st) -> bool
  169. {
  170. auto try_load_backend_config = [&](const ucl_object_t *obj) -> bool {
  171. if (!obj || ucl_object_type(obj) != UCL_OBJECT) {
  172. return false;
  173. }
  174. /* First try to load read servers */
  175. auto *rs = ucl_object_lookup_any(obj, "read_servers", "servers", nullptr);
  176. if (rs) {
  177. read_servers = rspamd_upstreams_create(cfg->ups_ctx);
  178. if (read_servers == nullptr) {
  179. return false;
  180. }
  181. if (!rspamd_upstreams_from_ucl(read_servers, rs, 80, this)) {
  182. rspamd_upstreams_destroy(read_servers);
  183. return false;
  184. }
  185. }
  186. auto *ws = ucl_object_lookup_any(obj, "write_servers", "servers", nullptr);
  187. if (ws) {
  188. write_servers = rspamd_upstreams_create(cfg->ups_ctx);
  189. if (write_servers == nullptr) {
  190. return false;
  191. }
  192. if (!rspamd_upstreams_from_ucl(write_servers, rs, 80, this)) {
  193. rspamd_upstreams_destroy(write_servers);
  194. return false;
  195. }
  196. }
  197. auto *tim = ucl_object_lookup(obj, "timeout");
  198. if (tim) {
  199. timeout = ucl_object_todouble(tim);
  200. }
  201. return true;
  202. };
  203. auto ret = false;
  204. auto obj = ucl_object_lookup (st->classifier->cfg->opts, "backend");
  205. if (obj != nullptr) {
  206. ret = try_load_backend_config(obj);
  207. }
  208. /* Now try statfiles config */
  209. if (!ret && st->stcf->opts) {
  210. ret = try_load_backend_config(st->stcf->opts);
  211. }
  212. /* Now try classifier config */
  213. if (!ret && st->classifier->cfg->opts) {
  214. ret = try_load_backend_config(st->classifier->cfg->opts);
  215. }
  216. return ret;
  217. }
  218. auto http_backends_collection::remove_backend(struct rspamd_statfile *st) -> bool
  219. {
  220. auto backend_it = std::remove(std::begin(backends), std::end(backends), st);
  221. if (backend_it != std::end(backends)) {
  222. /* Fast erasure with no order preservation */
  223. std::swap(*backend_it, backends.back());
  224. backends.pop_back();
  225. if (backends.empty()) {
  226. /* De-init collection - likely config reload */
  227. if (read_servers) {
  228. rspamd_upstreams_destroy(read_servers);
  229. read_servers = nullptr;
  230. }
  231. if (write_servers) {
  232. rspamd_upstreams_destroy(write_servers);
  233. write_servers = nullptr;
  234. }
  235. }
  236. return true;
  237. }
  238. return false;
  239. }
  240. upstream *http_backends_collection::get_upstream(bool is_learn)
  241. {
  242. auto *ups_list = read_servers;
  243. if (is_learn) {
  244. ups_list = write_servers;
  245. }
  246. return rspamd_upstream_get(ups_list, RSPAMD_UPSTREAM_ROUND_ROBIN, nullptr, 0);
  247. }
  248. }
  249. /* C API */
  250. gpointer
  251. rspamd_http_init(struct rspamd_stat_ctx* ctx,
  252. struct rspamd_config* cfg,
  253. struct rspamd_statfile* st)
  254. {
  255. auto &collections = rspamd::stat::http::http_backends_collection::get();
  256. if (!collections.add_backend(ctx, cfg, st)) {
  257. msg_err_config("cannot load http backend");
  258. return nullptr;
  259. }
  260. return (void *)&collections;
  261. }
  262. gpointer
  263. rspamd_http_runtime(struct rspamd_task* task,
  264. struct rspamd_statfile_config* stcf,
  265. gboolean learn,
  266. gpointer ctx,
  267. gint id)
  268. {
  269. auto maybe_existing = rspamd_mempool_get_variable(task->task_pool, RSPAMD_MEMPOOL_HTTP_STAT_BACKEND_RUNTIME);
  270. if (maybe_existing != nullptr) {
  271. auto real_runtime = (rspamd::stat::http::http_backend_runtime *)maybe_existing;
  272. real_runtime->notice_statfile(id, stcf);
  273. return maybe_existing;
  274. }
  275. auto runtime = rspamd::stat::http::http_backend_runtime::create(task, learn);
  276. if (runtime) {
  277. runtime->notice_statfile(id, stcf);
  278. rspamd_mempool_set_variable(task->task_pool, RSPAMD_MEMPOOL_HTTP_STAT_BACKEND_RUNTIME,
  279. (void *)runtime, nullptr);
  280. }
  281. return (void *)runtime;
  282. }
  283. gboolean
  284. rspamd_http_process_tokens(struct rspamd_task* task,
  285. GPtrArray* tokens,
  286. gint id,
  287. gpointer runtime)
  288. {
  289. auto real_runtime = (rspamd::stat::http::http_backend_runtime *)runtime;
  290. if (real_runtime) {
  291. return real_runtime->process_tokens(task, tokens, id, false);
  292. }
  293. return false;
  294. }
  295. gboolean
  296. rspamd_http_finalize_process(struct rspamd_task* task,
  297. gpointer runtime,
  298. gpointer ctx)
  299. {
  300. /* Not needed */
  301. return true;
  302. }
  303. gboolean
  304. rspamd_http_learn_tokens(struct rspamd_task* task,
  305. GPtrArray* tokens,
  306. gint id,
  307. gpointer runtime)
  308. {
  309. auto real_runtime = (rspamd::stat::http::http_backend_runtime *)runtime;
  310. if (real_runtime) {
  311. return real_runtime->process_tokens(task, tokens, id, true);
  312. }
  313. return false;
  314. }
  315. gboolean
  316. rspamd_http_finalize_learn(struct rspamd_task* task,
  317. gpointer runtime,
  318. gpointer ctx,
  319. GError** err)
  320. {
  321. return false;
  322. }
  323. gulong rspamd_http_total_learns(struct rspamd_task* task,
  324. gpointer runtime,
  325. gpointer ctx)
  326. {
  327. /* TODO */
  328. return 0;
  329. }
  330. gulong
  331. rspamd_http_inc_learns(struct rspamd_task* task,
  332. gpointer runtime,
  333. gpointer ctx)
  334. {
  335. /* TODO */
  336. return 0;
  337. }
  338. gulong
  339. rspamd_http_dec_learns(struct rspamd_task* task,
  340. gpointer runtime,
  341. gpointer ctx)
  342. {
  343. /* TODO */
  344. return (gulong)-1;
  345. }
  346. gulong
  347. rspamd_http_learns(struct rspamd_task* task,
  348. gpointer runtime,
  349. gpointer ctx)
  350. {
  351. /* TODO */
  352. return 0;
  353. }
  354. ucl_object_t*
  355. rspamd_http_get_stat(gpointer runtime, gpointer ctx)
  356. {
  357. /* TODO */
  358. return nullptr;
  359. }
  360. gpointer
  361. rspamd_http_load_tokenizer_config(gpointer runtime, gsize* len)
  362. {
  363. return nullptr;
  364. }
  365. void
  366. rspamd_http_close(gpointer ctx)
  367. {
  368. /* TODO */
  369. }