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_context.c 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. /*-
  2. * Copyright 2019 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 <contrib/http-parser/http_parser.h>
  17. #include "http_context.h"
  18. #include "http_private.h"
  19. #include "keypair.h"
  20. #include "keypairs_cache.h"
  21. #include "cfg_file.h"
  22. #include "contrib/libottery/ottery.h"
  23. #include "contrib/http-parser/http_parser.h"
  24. #include "rspamd.h"
  25. #include "libev_helper.h"
  26. INIT_LOG_MODULE(http_context)
  27. #define msg_debug_http_context(...) rspamd_conditional_debug_fast (NULL, NULL, \
  28. rspamd_http_context_log_id, "http_context", NULL, \
  29. G_STRFUNC, \
  30. __VA_ARGS__)
  31. static struct rspamd_http_context *default_ctx = NULL;
  32. struct rspamd_http_keepalive_cbdata {
  33. struct rspamd_http_connection *conn;
  34. struct rspamd_http_context *ctx;
  35. GQueue *queue;
  36. GList *link;
  37. struct rspamd_io_ev ev;
  38. };
  39. static void
  40. rspamd_http_keepalive_queue_cleanup (GQueue *conns)
  41. {
  42. GList *cur;
  43. cur = conns->head;
  44. while (cur) {
  45. struct rspamd_http_keepalive_cbdata *cbd;
  46. cbd = (struct rspamd_http_keepalive_cbdata *)cur->data;
  47. rspamd_http_connection_unref (cbd->conn);
  48. rspamd_ev_watcher_stop (cbd->ctx->event_loop, &cbd->ev);
  49. g_free (cbd);
  50. cur = cur->next;
  51. }
  52. g_queue_clear (conns);
  53. }
  54. static void
  55. rspamd_http_context_client_rotate_ev (struct ev_loop *loop, ev_timer *w, int revents)
  56. {
  57. struct rspamd_http_context *ctx = (struct rspamd_http_context *)w->data;
  58. gpointer kp;
  59. w->repeat = rspamd_time_jitter (ctx->config.client_key_rotate_time, 0);
  60. msg_debug_http_context ("rotate local keypair, next rotate in %.0f seconds",
  61. w->repeat);
  62. ev_timer_again (loop, w);
  63. kp = ctx->client_kp;
  64. ctx->client_kp = rspamd_keypair_new (RSPAMD_KEYPAIR_KEX,
  65. RSPAMD_CRYPTOBOX_MODE_25519);
  66. rspamd_keypair_unref (kp);
  67. }
  68. static struct rspamd_http_context*
  69. rspamd_http_context_new_default (struct rspamd_config *cfg,
  70. struct ev_loop *ev_base,
  71. struct upstream_ctx *ups_ctx)
  72. {
  73. struct rspamd_http_context *ctx;
  74. static const int default_kp_size = 1024;
  75. static const gdouble default_rotate_time = 120;
  76. static const gdouble default_keepalive_interval = 65;
  77. static const gchar *default_user_agent = "rspamd-" RSPAMD_VERSION_FULL;
  78. static const gchar *default_server_hdr = "rspamd/" RSPAMD_VERSION_FULL;
  79. ctx = g_malloc0 (sizeof (*ctx));
  80. ctx->config.kp_cache_size_client = default_kp_size;
  81. ctx->config.kp_cache_size_server = default_kp_size;
  82. ctx->config.client_key_rotate_time = default_rotate_time;
  83. ctx->config.user_agent = default_user_agent;
  84. ctx->config.keepalive_interval = default_keepalive_interval;
  85. ctx->config.server_hdr = default_server_hdr;
  86. ctx->ups_ctx = ups_ctx;
  87. if (cfg) {
  88. ctx->ssl_ctx = cfg->libs_ctx->ssl_ctx;
  89. ctx->ssl_ctx_noverify = cfg->libs_ctx->ssl_ctx_noverify;
  90. }
  91. else {
  92. ctx->ssl_ctx = rspamd_init_ssl_ctx ();
  93. ctx->ssl_ctx_noverify = rspamd_init_ssl_ctx_noverify ();
  94. }
  95. ctx->event_loop = ev_base;
  96. ctx->keep_alive_hash = kh_init (rspamd_keep_alive_hash);
  97. return ctx;
  98. }
  99. static void
  100. rspamd_http_context_parse_proxy (struct rspamd_http_context *ctx,
  101. const gchar *name,
  102. struct upstream_list **pls)
  103. {
  104. struct http_parser_url u;
  105. struct upstream_list *uls;
  106. if (!ctx->ups_ctx) {
  107. msg_err ("cannot parse http_proxy %s - upstreams context is udefined", name);
  108. return;
  109. }
  110. memset (&u, 0, sizeof (u));
  111. if (http_parser_parse_url (name, strlen (name), 1, &u) == 0) {
  112. if (!(u.field_set & (1u << UF_HOST)) || u.port == 0) {
  113. msg_err ("cannot parse http(s) proxy %s - invalid host or port", name);
  114. return;
  115. }
  116. uls = rspamd_upstreams_create (ctx->ups_ctx);
  117. if (!rspamd_upstreams_parse_line_len (uls,
  118. name + u.field_data[UF_HOST].off,
  119. u.field_data[UF_HOST].len, u.port, NULL)) {
  120. msg_err ("cannot parse http(s) proxy %s - invalid data", name);
  121. rspamd_upstreams_destroy (uls);
  122. }
  123. else {
  124. *pls = uls;
  125. msg_info ("set http(s) proxy to %s", name);
  126. }
  127. }
  128. else {
  129. uls = rspamd_upstreams_create (ctx->ups_ctx);
  130. if (!rspamd_upstreams_parse_line (uls,
  131. name, 3128, NULL)) {
  132. msg_err ("cannot parse http(s) proxy %s - invalid data", name);
  133. rspamd_upstreams_destroy (uls);
  134. }
  135. else {
  136. *pls = uls;
  137. msg_info ("set http(s) proxy to %s", name);
  138. }
  139. }
  140. }
  141. static void
  142. rspamd_http_context_init (struct rspamd_http_context *ctx)
  143. {
  144. if (ctx->config.kp_cache_size_client > 0) {
  145. ctx->client_kp_cache = rspamd_keypair_cache_new (ctx->config.kp_cache_size_client);
  146. }
  147. if (ctx->config.kp_cache_size_server > 0) {
  148. ctx->server_kp_cache = rspamd_keypair_cache_new (ctx->config.kp_cache_size_server);
  149. }
  150. if (ctx->config.client_key_rotate_time > 0 && ctx->event_loop) {
  151. double jittered = rspamd_time_jitter (ctx->config.client_key_rotate_time,
  152. 0);
  153. ev_timer_init (&ctx->client_rotate_ev,
  154. rspamd_http_context_client_rotate_ev, jittered, 0);
  155. ev_timer_start (ctx->event_loop, &ctx->client_rotate_ev);
  156. ctx->client_rotate_ev.data = ctx;
  157. }
  158. if (ctx->config.http_proxy) {
  159. rspamd_http_context_parse_proxy (ctx, ctx->config.http_proxy,
  160. &ctx->http_proxies);
  161. }
  162. default_ctx = ctx;
  163. }
  164. struct rspamd_http_context*
  165. rspamd_http_context_create (struct rspamd_config *cfg,
  166. struct ev_loop *ev_base,
  167. struct upstream_ctx *ups_ctx)
  168. {
  169. struct rspamd_http_context *ctx;
  170. const ucl_object_t *http_obj;
  171. ctx = rspamd_http_context_new_default (cfg, ev_base, ups_ctx);
  172. http_obj = ucl_object_lookup (cfg->rcl_obj, "http");
  173. if (http_obj) {
  174. const ucl_object_t *server_obj, *client_obj;
  175. client_obj = ucl_object_lookup (http_obj, "client");
  176. if (client_obj) {
  177. const ucl_object_t *kp_size;
  178. kp_size = ucl_object_lookup (client_obj, "cache_size");
  179. if (kp_size) {
  180. ctx->config.kp_cache_size_client = ucl_object_toint (kp_size);
  181. }
  182. const ucl_object_t *rotate_time;
  183. rotate_time = ucl_object_lookup (client_obj, "rotate_time");
  184. if (rotate_time) {
  185. ctx->config.client_key_rotate_time = ucl_object_todouble (rotate_time);
  186. }
  187. const ucl_object_t *user_agent;
  188. user_agent = ucl_object_lookup (client_obj, "user_agent");
  189. if (user_agent) {
  190. ctx->config.user_agent = ucl_object_tostring (user_agent);
  191. if (ctx->config.user_agent && strlen (ctx->config.user_agent) == 0) {
  192. ctx->config.user_agent = NULL;
  193. }
  194. }
  195. const ucl_object_t *server_hdr;
  196. server_hdr = ucl_object_lookup (client_obj, "server_hdr");
  197. if (server_hdr) {
  198. ctx->config.server_hdr = ucl_object_tostring (server_hdr);
  199. if (ctx->config.server_hdr && strlen (ctx->config.server_hdr) == 0) {
  200. ctx->config.server_hdr = "";
  201. }
  202. }
  203. const ucl_object_t *keepalive_interval;
  204. keepalive_interval = ucl_object_lookup (client_obj, "keepalive_interval");
  205. if (keepalive_interval) {
  206. ctx->config.keepalive_interval = ucl_object_todouble (keepalive_interval);
  207. }
  208. const ucl_object_t *http_proxy;
  209. http_proxy = ucl_object_lookup (client_obj, "http_proxy");
  210. if (http_proxy) {
  211. ctx->config.http_proxy = ucl_object_tostring (http_proxy);
  212. }
  213. }
  214. server_obj = ucl_object_lookup (http_obj, "server");
  215. if (server_obj) {
  216. const ucl_object_t *kp_size;
  217. kp_size = ucl_object_lookup (server_obj, "cache_size");
  218. if (kp_size) {
  219. ctx->config.kp_cache_size_server = ucl_object_toint (kp_size);
  220. }
  221. }
  222. }
  223. rspamd_http_context_init (ctx);
  224. return ctx;
  225. }
  226. void
  227. rspamd_http_context_free (struct rspamd_http_context *ctx)
  228. {
  229. if (ctx == default_ctx) {
  230. default_ctx = NULL;
  231. }
  232. if (ctx->client_kp_cache) {
  233. rspamd_keypair_cache_destroy (ctx->client_kp_cache);
  234. }
  235. if (ctx->server_kp_cache) {
  236. rspamd_keypair_cache_destroy (ctx->server_kp_cache);
  237. }
  238. if (ctx->config.client_key_rotate_time > 0) {
  239. ev_timer_stop (ctx->event_loop, &ctx->client_rotate_ev);
  240. if (ctx->client_kp) {
  241. rspamd_keypair_unref (ctx->client_kp);
  242. }
  243. }
  244. struct rspamd_keepalive_hash_key *hk;
  245. kh_foreach_key (ctx->keep_alive_hash, hk, {
  246. msg_debug_http_context ("cleanup keepalive elt %s (%s)",
  247. rspamd_inet_address_to_string_pretty (hk->addr),
  248. hk->host);
  249. if (hk->host) {
  250. g_free (hk->host);
  251. }
  252. rspamd_inet_address_free (hk->addr);
  253. rspamd_http_keepalive_queue_cleanup (&hk->conns);
  254. g_free (hk);
  255. });
  256. kh_destroy (rspamd_keep_alive_hash, ctx->keep_alive_hash);
  257. if (ctx->http_proxies) {
  258. rspamd_upstreams_destroy (ctx->http_proxies);
  259. }
  260. g_free (ctx);
  261. }
  262. struct rspamd_http_context*
  263. rspamd_http_context_create_config (struct rspamd_http_context_cfg *cfg,
  264. struct ev_loop *ev_base,
  265. struct upstream_ctx *ups_ctx)
  266. {
  267. struct rspamd_http_context *ctx;
  268. ctx = rspamd_http_context_new_default (NULL, ev_base, ups_ctx);
  269. memcpy (&ctx->config, cfg, sizeof (*cfg));
  270. rspamd_http_context_init (ctx);
  271. return ctx;
  272. }
  273. struct rspamd_http_context*
  274. rspamd_http_context_default (void)
  275. {
  276. g_assert (default_ctx != NULL);
  277. return default_ctx;
  278. }
  279. gint32
  280. rspamd_keep_alive_key_hash (struct rspamd_keepalive_hash_key *k)
  281. {
  282. gint32 h;
  283. h = rspamd_inet_address_port_hash (k->addr);
  284. if (k->host) {
  285. h = rspamd_cryptobox_fast_hash (k->host, strlen (k->host), h);
  286. }
  287. return h;
  288. }
  289. bool
  290. rspamd_keep_alive_key_equal (struct rspamd_keepalive_hash_key *k1,
  291. struct rspamd_keepalive_hash_key *k2)
  292. {
  293. if (k1->host && k2->host) {
  294. if (rspamd_inet_address_port_equal (k1->addr, k2->addr)) {
  295. return strcmp (k1->host, k2->host) == 0;
  296. }
  297. }
  298. else if (!k1->host && !k2->host) {
  299. return rspamd_inet_address_port_equal (k1->addr, k2->addr);
  300. }
  301. /* One has host and another has no host */
  302. return false;
  303. }
  304. struct rspamd_http_connection*
  305. rspamd_http_context_check_keepalive (struct rspamd_http_context *ctx,
  306. const rspamd_inet_addr_t *addr,
  307. const gchar *host)
  308. {
  309. struct rspamd_keepalive_hash_key hk, *phk;
  310. khiter_t k;
  311. hk.addr = (rspamd_inet_addr_t *)addr;
  312. hk.host = (gchar *)host;
  313. k = kh_get (rspamd_keep_alive_hash, ctx->keep_alive_hash, &hk);
  314. if (k != kh_end (ctx->keep_alive_hash)) {
  315. phk = kh_key (ctx->keep_alive_hash, k);
  316. GQueue *conns = &phk->conns;
  317. /* Use stack based approach */
  318. if (g_queue_get_length (conns) > 0) {
  319. struct rspamd_http_keepalive_cbdata *cbd;
  320. struct rspamd_http_connection *conn;
  321. cbd = g_queue_pop_head (conns);
  322. rspamd_ev_watcher_stop (ctx->event_loop, &cbd->ev);
  323. conn = cbd->conn;
  324. g_free (cbd);
  325. msg_debug_http_context ("reused keepalive element %s (%s), %d connections queued",
  326. rspamd_inet_address_to_string_pretty (phk->addr),
  327. phk->host, conns->length);
  328. /* We transfer refcount here! */
  329. return conn;
  330. }
  331. else {
  332. msg_debug_http_context ("found empty keepalive element %s (%s), cannot reuse",
  333. rspamd_inet_address_to_string_pretty (phk->addr),
  334. phk->host);
  335. }
  336. }
  337. return NULL;
  338. }
  339. void
  340. rspamd_http_context_prepare_keepalive (struct rspamd_http_context *ctx,
  341. struct rspamd_http_connection *conn,
  342. const rspamd_inet_addr_t *addr,
  343. const gchar *host)
  344. {
  345. struct rspamd_keepalive_hash_key hk, *phk;
  346. khiter_t k;
  347. hk.addr = (rspamd_inet_addr_t *)addr;
  348. hk.host = (gchar *)host;
  349. k = kh_get (rspamd_keep_alive_hash, ctx->keep_alive_hash, &hk);
  350. if (k != kh_end (ctx->keep_alive_hash)) {
  351. /* Reuse existing */
  352. conn->keepalive_hash_key = kh_key (ctx->keep_alive_hash, k);
  353. msg_debug_http_context ("use existing keepalive element %s (%s)",
  354. rspamd_inet_address_to_string_pretty (conn->keepalive_hash_key->addr),
  355. conn->keepalive_hash_key->host);
  356. }
  357. else {
  358. /* Create new one */
  359. GQueue empty_init = G_QUEUE_INIT;
  360. gint r;
  361. phk = g_malloc (sizeof (*phk));
  362. phk->conns = empty_init;
  363. phk->host = g_strdup (host);
  364. phk->addr = rspamd_inet_address_copy (addr);
  365. kh_put (rspamd_keep_alive_hash, ctx->keep_alive_hash, phk, &r);
  366. conn->keepalive_hash_key = phk;
  367. msg_debug_http_context ("create new keepalive element %s (%s)",
  368. rspamd_inet_address_to_string_pretty (conn->keepalive_hash_key->addr),
  369. conn->keepalive_hash_key->host);
  370. }
  371. }
  372. static void
  373. rspamd_http_keepalive_handler (gint fd, short what, gpointer ud)
  374. {
  375. struct rspamd_http_keepalive_cbdata *cbdata =
  376. (struct rspamd_http_keepalive_cbdata *)ud;/*
  377. * We can get here if a remote side reported something or it has
  378. * timed out. In both cases we just terminate keepalive connection.
  379. */
  380. g_queue_delete_link (cbdata->queue, cbdata->link);
  381. msg_debug_http_context ("remove keepalive element %s (%s), %d connections left",
  382. rspamd_inet_address_to_string_pretty (cbdata->conn->keepalive_hash_key->addr),
  383. cbdata->conn->keepalive_hash_key->host,
  384. cbdata->queue->length);
  385. rspamd_http_connection_unref (cbdata->conn);
  386. rspamd_ev_watcher_stop (cbdata->ctx->event_loop, &cbdata->ev);
  387. g_free (cbdata);
  388. }
  389. void
  390. rspamd_http_context_push_keepalive (struct rspamd_http_context *ctx,
  391. struct rspamd_http_connection *conn,
  392. struct rspamd_http_message *msg,
  393. struct ev_loop *event_loop)
  394. {
  395. struct rspamd_http_keepalive_cbdata *cbdata;
  396. gdouble timeout = ctx->config.keepalive_interval;
  397. g_assert (conn->keepalive_hash_key != NULL);
  398. if (msg) {
  399. const rspamd_ftok_t *tok;
  400. rspamd_ftok_t cmp;
  401. tok = rspamd_http_message_find_header (msg, "Connection");
  402. if (!tok) {
  403. /* Server has not stated that it can do keep alive */
  404. conn->finished = TRUE;
  405. msg_debug_http_context ("no Connection header");
  406. return;
  407. }
  408. RSPAMD_FTOK_ASSIGN (&cmp, "keep-alive");
  409. if (rspamd_ftok_casecmp (&cmp, tok) != 0) {
  410. conn->finished = TRUE;
  411. msg_debug_http_context ("connection header is not `keep-alive`");
  412. return;
  413. }
  414. /* We can proceed, check timeout */
  415. tok = rspamd_http_message_find_header (msg, "Keep-Alive");
  416. if (tok) {
  417. goffset pos = rspamd_substring_search_caseless (tok->begin,
  418. tok->len, "timeout=", sizeof ("timeout=") - 1);
  419. if (pos != -1) {
  420. pos += sizeof ("timeout=");
  421. gchar *end_pos = memchr (tok->begin + pos, ',', tok->len - pos);
  422. glong real_timeout;
  423. if (end_pos) {
  424. if (rspamd_strtol (tok->begin + pos + 1,
  425. (end_pos - tok->begin) - pos - 1, &real_timeout) &&
  426. real_timeout > 0) {
  427. timeout = real_timeout;
  428. msg_debug_http_context ("got timeout attr %.2f", timeout);
  429. }
  430. }
  431. else {
  432. if (rspamd_strtol (tok->begin + pos + 1,
  433. tok->len - pos - 1, &real_timeout) &&
  434. real_timeout > 0) {
  435. timeout = real_timeout;
  436. msg_debug_http_context ("got timeout attr %.2f", timeout);
  437. }
  438. }
  439. }
  440. }
  441. }
  442. /* Move connection to the keepalive pool */
  443. cbdata = g_malloc0 (sizeof (*cbdata));
  444. cbdata->conn = rspamd_http_connection_ref (conn);
  445. g_queue_push_tail (&conn->keepalive_hash_key->conns, cbdata);
  446. cbdata->link = conn->keepalive_hash_key->conns.tail;
  447. cbdata->queue = &conn->keepalive_hash_key->conns;
  448. cbdata->ctx = ctx;
  449. conn->finished = FALSE;
  450. rspamd_ev_watcher_init (&cbdata->ev, conn->fd, EV_READ,
  451. rspamd_http_keepalive_handler,
  452. cbdata);
  453. rspamd_ev_watcher_start (event_loop, &cbdata->ev, timeout);
  454. msg_debug_http_context ("push keepalive element %s (%s), %d connections queued, %.1f timeout",
  455. rspamd_inet_address_to_string_pretty (cbdata->conn->keepalive_hash_key->addr),
  456. cbdata->conn->keepalive_hash_key->host,
  457. cbdata->queue->length,
  458. timeout);
  459. }