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.

rspamd_http_server.c 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. * Copyright (c) 2015, Vsevolod Stakhov
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY AUTHOR ''AS IS'' AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  15. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. * DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY
  17. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  18. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  19. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  20. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  21. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  22. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. */
  24. #include "config.h"
  25. #include "rspamd.h"
  26. #include "util.h"
  27. #include "http.h"
  28. #include "ottery.h"
  29. #include "cryptobox.h"
  30. #include "unix-std.h"
  31. #include <math.h>
  32. #ifdef HAVE_SYS_WAIT_H
  33. #include <sys/wait.h>
  34. #endif
  35. static guint port = 43000;
  36. static guint cache_size = 10;
  37. static guint nworkers = 1;
  38. static gboolean openssl_mode = FALSE;
  39. static GHashTable *maps = NULL;
  40. static gchar *key = NULL;
  41. static struct rspamd_keypair_cache *c;
  42. static gpointer server_key;
  43. static struct timeval io_tv = {
  44. .tv_sec = 20,
  45. .tv_usec = 0
  46. };
  47. static GOptionEntry entries[] = {
  48. {"port", 'p', 0, G_OPTION_ARG_INT, &port,
  49. "Port number (default: 43000)", NULL},
  50. {"cache", 'c', 0, G_OPTION_ARG_INT, &cache_size,
  51. "Keys cache size (default: 10)", NULL},
  52. {"workers", 'n', 0, G_OPTION_ARG_INT, &nworkers,
  53. "Number of workers to start (default: 1)", NULL},
  54. {"openssl", 'o', 0, G_OPTION_ARG_NONE, &openssl_mode,
  55. "Use openssl crypto", NULL},
  56. {"key", 'k', 0, G_OPTION_ARG_STRING, &key,
  57. "Use static keypair instead of new one (base32 encoded sk || pk)", NULL},
  58. {NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL}
  59. };
  60. struct rspamd_http_server_session {
  61. struct rspamd_http_connection *conn;
  62. struct event_base *ev_base;
  63. guint req_size;
  64. gboolean reply;
  65. gint fd;
  66. };
  67. static void
  68. rspamd_server_error (struct rspamd_http_connection *conn,
  69. GError *err)
  70. {
  71. struct rspamd_http_server_session *session = conn->ud;
  72. rspamd_fprintf (stderr, "http error occurred: %s\n", err->message);
  73. rspamd_http_connection_unref (conn);
  74. close (session->fd);
  75. g_slice_free1 (sizeof (*session), session);
  76. }
  77. static int
  78. rspamd_server_finish (struct rspamd_http_connection *conn,
  79. struct rspamd_http_message *msg)
  80. {
  81. struct rspamd_http_server_session *session = conn->ud;
  82. struct rspamd_http_message *reply;
  83. gulong size;
  84. const gchar *url_str;
  85. guint url_len;
  86. if (!session->reply) {
  87. session->reply = TRUE;
  88. reply = rspamd_http_new_message (HTTP_RESPONSE);
  89. url_str = msg->url->str;
  90. url_len = msg->url->len;
  91. if (url_str[0] == '/') {
  92. url_str ++;
  93. url_len --;
  94. }
  95. if (rspamd_strtoul (url_str, url_len, &size)) {
  96. session->req_size = size;
  97. reply->code = 200;
  98. reply->status = rspamd_fstring_new_init ("OK", 2);
  99. reply->body = rspamd_fstring_sized_new (size);
  100. reply->body->len = size;
  101. memset (reply->body->str, 0, size);
  102. }
  103. else {
  104. reply->code = 404;
  105. reply->status = rspamd_fstring_new_init ("Not found", 9);
  106. }
  107. rspamd_http_connection_reset (conn);
  108. rspamd_http_connection_write_message (conn, reply, NULL,
  109. "application/octet-stream", session, session->fd,
  110. &io_tv, session->ev_base);
  111. }
  112. else {
  113. /* Destroy session */
  114. rspamd_http_connection_unref (conn);
  115. close (session->fd);
  116. g_slice_free1 (sizeof (*session), session);
  117. }
  118. return 0;
  119. }
  120. static void
  121. rspamd_server_accept (gint fd, short what, void *arg)
  122. {
  123. struct event_base *ev_base = arg;
  124. struct rspamd_http_server_session *session;
  125. rspamd_inet_addr_t *addr;
  126. gint nfd;
  127. do {
  128. if ((nfd =
  129. rspamd_accept_from_socket (fd, &addr)) == -1) {
  130. rspamd_fprintf (stderr, "accept failed: %s", strerror (errno));
  131. return;
  132. }
  133. /* Check for EAGAIN */
  134. if (nfd == 0) {
  135. return;
  136. }
  137. rspamd_inet_address_destroy (addr);
  138. session = g_slice_alloc (sizeof (*session));
  139. session->conn = rspamd_http_connection_new (NULL, rspamd_server_error,
  140. rspamd_server_finish, 0, RSPAMD_HTTP_SERVER, c);
  141. rspamd_http_connection_set_key (session->conn, server_key);
  142. rspamd_http_connection_read_message (session->conn,
  143. session,
  144. nfd,
  145. &io_tv,
  146. ev_base);
  147. session->reply = FALSE;
  148. session->fd = nfd;
  149. session->ev_base = ev_base;
  150. } while (nfd > 0);
  151. }
  152. static void
  153. rspamd_http_term_handler (gint fd, short what, void *arg)
  154. {
  155. struct event_base *ev_base = arg;
  156. struct timeval tv = {0, 0};
  157. event_base_loopexit (ev_base, &tv);
  158. }
  159. static void
  160. rspamd_http_server_func (gint fd, rspamd_inet_addr_t *addr)
  161. {
  162. struct event_base *ev_base = event_init ();
  163. struct event accept_ev, term_ev;
  164. event_set (&accept_ev, fd, EV_READ | EV_PERSIST, rspamd_server_accept, ev_base);
  165. event_base_set (ev_base, &accept_ev);
  166. event_add (&accept_ev, NULL);
  167. evsignal_set (&term_ev, SIGTERM, rspamd_http_term_handler, ev_base);
  168. event_base_set (ev_base, &term_ev);
  169. event_add (&term_ev, NULL);
  170. event_base_loop (ev_base, 0);
  171. }
  172. static void
  173. rspamd_http_start_servers (pid_t *sfd, rspamd_inet_addr_t *addr)
  174. {
  175. guint i;
  176. gint fd;
  177. g_assert (
  178. (fd = rspamd_inet_address_listen (addr, SOCK_STREAM, TRUE)) != -1);
  179. for (i = 0; i < nworkers; i++) {
  180. sfd[i] = fork ();
  181. g_assert (sfd[i] != -1);
  182. if (sfd[i] == 0) {
  183. gperf_profiler_init (NULL, "http-server");
  184. rspamd_http_server_func (fd, addr);
  185. gperf_profiler_stop ();
  186. exit (EXIT_SUCCESS);
  187. }
  188. }
  189. close (fd);
  190. }
  191. static void
  192. rspamd_http_stop_servers (pid_t *sfd)
  193. {
  194. guint i;
  195. gint res;
  196. for (i = 0; i < nworkers; i++) {
  197. kill (sfd[i], SIGTERM);
  198. wait (&res);
  199. }
  200. }
  201. static void
  202. rspamd_http_server_term (int fd, short what, void *arg)
  203. {
  204. pid_t *sfd = arg;
  205. rspamd_http_stop_servers (sfd);
  206. event_loopexit (NULL);
  207. }
  208. int
  209. main (int argc, gchar **argv)
  210. {
  211. GOptionContext *context;
  212. GError *error = NULL;
  213. struct event_base *ev_base;
  214. GString *b32_key;
  215. pid_t *sfd;
  216. rspamd_inet_addr_t *addr;
  217. struct event term_ev, int_ev;
  218. struct in_addr ina = {INADDR_ANY};
  219. rspamd_init_libs ();
  220. context = g_option_context_new (
  221. "rspamd-http-server - test server for benchmarks");
  222. g_option_context_set_summary (context,
  223. "Summary:\n Rspamd test HTTP server "
  224. RVERSION
  225. "\n Release id: "
  226. RID);
  227. g_option_context_add_main_entries (context, entries, NULL);
  228. if (!g_option_context_parse (context, &argc, &argv, &error)) {
  229. rspamd_fprintf (stderr, "option parsing failed: %s\n", error->message);
  230. g_error_free (error);
  231. exit (1);
  232. }
  233. maps = g_hash_table_new (g_int_hash, g_int_equal);
  234. if (openssl_mode) {
  235. g_assert (rspamd_cryptobox_openssl_mode (TRUE));
  236. }
  237. if (key == NULL) {
  238. server_key = rspamd_http_connection_gen_key ();
  239. b32_key = rspamd_http_connection_print_key (server_key,
  240. RSPAMD_KEYPAIR_PUBKEY | RSPAMD_KEYPAIR_BASE32);
  241. rspamd_printf ("key: %v\n", b32_key);
  242. }
  243. else {
  244. server_key = rspamd_http_connection_make_key (key, strlen (key));
  245. if (server_key == NULL) {
  246. rspamd_fprintf (stderr, "cannot load key %s\n", key);
  247. exit (EXIT_FAILURE);
  248. }
  249. }
  250. if (cache_size > 0) {
  251. c = rspamd_keypair_cache_new (cache_size);
  252. }
  253. sfd = g_alloca (sizeof (*sfd) * nworkers);
  254. addr = rspamd_inet_address_new (AF_INET, &ina);
  255. rspamd_inet_address_set_port (addr, port);
  256. rspamd_http_start_servers (sfd, addr);
  257. /* Just wait for workers */
  258. ev_base = event_init ();
  259. event_set (&term_ev, SIGTERM, EV_SIGNAL, rspamd_http_server_term, sfd);
  260. event_base_set (ev_base, &term_ev);
  261. event_add (&term_ev, NULL);
  262. event_set (&int_ev, SIGINT, EV_SIGNAL, rspamd_http_server_term, sfd);
  263. event_base_set (ev_base, &int_ev);
  264. event_add (&int_ev, NULL);
  265. event_base_loop (ev_base, 0);
  266. return 0;
  267. }