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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*-
  2. * Copyright 2016 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 "rspamd.h"
  18. #include "util.h"
  19. #include "http.h"
  20. #include "ottery.h"
  21. #include "cryptobox.h"
  22. #include "unix-std.h"
  23. #include <math.h>
  24. #ifdef HAVE_SYS_WAIT_H
  25. #include <sys/wait.h>
  26. #endif
  27. static guint port = 43000;
  28. static guint cache_size = 10;
  29. static guint nworkers = 1;
  30. static gboolean openssl_mode = FALSE;
  31. static GHashTable *maps = NULL;
  32. static gchar *key = NULL;
  33. static struct rspamd_keypair_cache *c;
  34. static gpointer server_key;
  35. static struct timeval io_tv = {
  36. .tv_sec = 20,
  37. .tv_usec = 0
  38. };
  39. static GOptionEntry entries[] = {
  40. {"port", 'p', 0, G_OPTION_ARG_INT, &port,
  41. "Port number (default: 43000)", NULL},
  42. {"cache", 'c', 0, G_OPTION_ARG_INT, &cache_size,
  43. "Keys cache size (default: 10)", NULL},
  44. {"workers", 'n', 0, G_OPTION_ARG_INT, &nworkers,
  45. "Number of workers to start (default: 1)", NULL},
  46. {"openssl", 'o', 0, G_OPTION_ARG_NONE, &openssl_mode,
  47. "Use openssl crypto", NULL},
  48. {"key", 'k', 0, G_OPTION_ARG_STRING, &key,
  49. "Use static keypair instead of new one (base32 encoded sk || pk)", NULL},
  50. {NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL}
  51. };
  52. struct rspamd_http_server_session {
  53. struct rspamd_http_connection *conn;
  54. struct event_base *ev_base;
  55. guint req_size;
  56. gboolean reply;
  57. gint fd;
  58. };
  59. static void
  60. rspamd_server_error (struct rspamd_http_connection *conn,
  61. GError *err)
  62. {
  63. struct rspamd_http_server_session *session = conn->ud;
  64. rspamd_fprintf (stderr, "http error occurred: %s\n", err->message);
  65. rspamd_http_connection_unref (conn);
  66. close (session->fd);
  67. g_slice_free1 (sizeof (*session), session);
  68. }
  69. static int
  70. rspamd_server_finish (struct rspamd_http_connection *conn,
  71. struct rspamd_http_message *msg)
  72. {
  73. struct rspamd_http_server_session *session = conn->ud;
  74. struct rspamd_http_message *reply;
  75. gulong size;
  76. const gchar *url_str;
  77. guint url_len;
  78. if (!session->reply) {
  79. session->reply = TRUE;
  80. reply = rspamd_http_new_message (HTTP_RESPONSE);
  81. url_str = msg->url->str;
  82. url_len = msg->url->len;
  83. if (url_str[0] == '/') {
  84. url_str ++;
  85. url_len --;
  86. }
  87. if (rspamd_strtoul (url_str, url_len, &size)) {
  88. session->req_size = size;
  89. reply->code = 200;
  90. reply->status = rspamd_fstring_new_init ("OK", 2);
  91. reply->body = rspamd_fstring_sized_new (size);
  92. reply->body->len = size;
  93. memset (reply->body->str, 0, size);
  94. }
  95. else {
  96. reply->code = 404;
  97. reply->status = rspamd_fstring_new_init ("Not found", 9);
  98. }
  99. rspamd_http_connection_reset (conn);
  100. rspamd_http_connection_write_message (conn, reply, NULL,
  101. "application/octet-stream", session, session->fd,
  102. &io_tv, session->ev_base);
  103. }
  104. else {
  105. /* Destroy session */
  106. rspamd_http_connection_unref (conn);
  107. close (session->fd);
  108. g_slice_free1 (sizeof (*session), session);
  109. }
  110. return 0;
  111. }
  112. static void
  113. rspamd_server_accept (gint fd, short what, void *arg)
  114. {
  115. struct event_base *ev_base = arg;
  116. struct rspamd_http_server_session *session;
  117. rspamd_inet_addr_t *addr;
  118. gint nfd;
  119. do {
  120. if ((nfd =
  121. rspamd_accept_from_socket (fd, &addr)) == -1) {
  122. rspamd_fprintf (stderr, "accept failed: %s", strerror (errno));
  123. return;
  124. }
  125. /* Check for EAGAIN */
  126. if (nfd == 0) {
  127. return;
  128. }
  129. rspamd_inet_address_destroy (addr);
  130. session = g_slice_alloc (sizeof (*session));
  131. session->conn = rspamd_http_connection_new (NULL, rspamd_server_error,
  132. rspamd_server_finish, 0, RSPAMD_HTTP_SERVER, c);
  133. rspamd_http_connection_set_key (session->conn, server_key);
  134. rspamd_http_connection_read_message (session->conn,
  135. session,
  136. nfd,
  137. &io_tv,
  138. ev_base);
  139. session->reply = FALSE;
  140. session->fd = nfd;
  141. session->ev_base = ev_base;
  142. } while (nfd > 0);
  143. }
  144. static void
  145. rspamd_http_term_handler (gint fd, short what, void *arg)
  146. {
  147. struct event_base *ev_base = arg;
  148. struct timeval tv = {0, 0};
  149. event_base_loopexit (ev_base, &tv);
  150. }
  151. static void
  152. rspamd_http_server_func (gint fd, rspamd_inet_addr_t *addr)
  153. {
  154. struct event_base *ev_base = event_init ();
  155. struct event accept_ev, term_ev;
  156. event_set (&accept_ev, fd, EV_READ | EV_PERSIST, rspamd_server_accept, ev_base);
  157. event_base_set (ev_base, &accept_ev);
  158. event_add (&accept_ev, NULL);
  159. evsignal_set (&term_ev, SIGTERM, rspamd_http_term_handler, ev_base);
  160. event_base_set (ev_base, &term_ev);
  161. event_add (&term_ev, NULL);
  162. event_base_loop (ev_base, 0);
  163. }
  164. static void
  165. rspamd_http_start_servers (pid_t *sfd, rspamd_inet_addr_t *addr)
  166. {
  167. guint i;
  168. gint fd;
  169. g_assert (
  170. (fd = rspamd_inet_address_listen (addr, SOCK_STREAM, TRUE)) != -1);
  171. for (i = 0; i < nworkers; i++) {
  172. sfd[i] = fork ();
  173. g_assert (sfd[i] != -1);
  174. if (sfd[i] == 0) {
  175. gperf_profiler_init (NULL, "http-server");
  176. rspamd_http_server_func (fd, addr);
  177. gperf_profiler_stop ();
  178. exit (EXIT_SUCCESS);
  179. }
  180. }
  181. close (fd);
  182. }
  183. static void
  184. rspamd_http_stop_servers (pid_t *sfd)
  185. {
  186. guint i;
  187. gint res;
  188. for (i = 0; i < nworkers; i++) {
  189. kill (sfd[i], SIGTERM);
  190. wait (&res);
  191. }
  192. }
  193. static void
  194. rspamd_http_server_term (int fd, short what, void *arg)
  195. {
  196. pid_t *sfd = arg;
  197. rspamd_http_stop_servers (sfd);
  198. event_loopexit (NULL);
  199. }
  200. int
  201. main (int argc, gchar **argv)
  202. {
  203. GOptionContext *context;
  204. GError *error = NULL;
  205. struct event_base *ev_base;
  206. GString *b32_key;
  207. pid_t *sfd;
  208. rspamd_inet_addr_t *addr;
  209. struct event term_ev, int_ev;
  210. struct in_addr ina = {INADDR_ANY};
  211. rspamd_init_libs ();
  212. context = g_option_context_new (
  213. "rspamd-http-server - test server for benchmarks");
  214. g_option_context_set_summary (context,
  215. "Summary:\n Rspamd test HTTP server "
  216. RVERSION
  217. "\n Release id: "
  218. RID);
  219. g_option_context_add_main_entries (context, entries, NULL);
  220. if (!g_option_context_parse (context, &argc, &argv, &error)) {
  221. rspamd_fprintf (stderr, "option parsing failed: %s\n", error->message);
  222. g_error_free (error);
  223. exit (1);
  224. }
  225. maps = g_hash_table_new (g_int_hash, g_int_equal);
  226. if (openssl_mode) {
  227. g_assert (rspamd_cryptobox_openssl_mode (TRUE));
  228. }
  229. if (key == NULL) {
  230. server_key = rspamd_http_connection_gen_key ();
  231. b32_key = rspamd_http_connection_print_key (server_key,
  232. RSPAMD_KEYPAIR_PUBKEY | RSPAMD_KEYPAIR_BASE32);
  233. rspamd_printf ("key: %v\n", b32_key);
  234. }
  235. else {
  236. server_key = rspamd_http_connection_make_key (key, strlen (key));
  237. if (server_key == NULL) {
  238. rspamd_fprintf (stderr, "cannot load key %s\n", key);
  239. exit (EXIT_FAILURE);
  240. }
  241. }
  242. if (cache_size > 0) {
  243. c = rspamd_keypair_cache_new (cache_size);
  244. }
  245. sfd = g_alloca (sizeof (*sfd) * nworkers);
  246. addr = rspamd_inet_address_new (AF_INET, &ina);
  247. rspamd_inet_address_set_port (addr, port);
  248. rspamd_http_start_servers (sfd, addr);
  249. /* Just wait for workers */
  250. ev_base = event_init ();
  251. event_set (&term_ev, SIGTERM, EV_SIGNAL, rspamd_http_server_term, sfd);
  252. event_base_set (ev_base, &term_ev);
  253. event_add (&term_ev, NULL);
  254. event_set (&int_ev, SIGINT, EV_SIGNAL, rspamd_http_server_term, sfd);
  255. event_base_set (ev_base, &int_ev);
  256. event_add (&int_ev, NULL);
  257. event_base_loop (ev_base, 0);
  258. return 0;
  259. }