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 16KB

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