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.3KB

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