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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 "libutil/fstring.h"
  20. #include "libutil/http.h"
  21. #include "libutil/http_private.h"
  22. #include "ottery.h"
  23. #include "cryptobox.h"
  24. #include "keypair.h"
  25. #include "unix-std.h"
  26. #include <math.h>
  27. #ifdef HAVE_SYS_WAIT_H
  28. #include <sys/wait.h>
  29. #endif
  30. static guint port = 43000;
  31. static guint cache_size = 10;
  32. static guint nworkers = 1;
  33. static gboolean openssl_mode = FALSE;
  34. static GHashTable *maps = NULL;
  35. static gchar *key = NULL;
  36. static struct rspamd_keypair_cache *c;
  37. static struct rspamd_cryptobox_keypair *server_key;
  38. static struct timeval io_tv = {
  39. .tv_sec = 20,
  40. .tv_usec = 0
  41. };
  42. static GOptionEntry entries[] = {
  43. {"port", 'p', 0, G_OPTION_ARG_INT, &port,
  44. "Port number (default: 43000)", NULL},
  45. {"cache", 'c', 0, G_OPTION_ARG_INT, &cache_size,
  46. "Keys cache size (default: 10)", NULL},
  47. {"workers", 'n', 0, G_OPTION_ARG_INT, &nworkers,
  48. "Number of workers to start (default: 1)", NULL},
  49. {"openssl", 'o', 0, G_OPTION_ARG_NONE, &openssl_mode,
  50. "Use openssl crypto", NULL},
  51. {"key", 'k', 0, G_OPTION_ARG_STRING, &key,
  52. "Use static keypair instead of new one (base32 encoded sk || pk)", NULL},
  53. {NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL}
  54. };
  55. struct rspamd_http_server_session {
  56. struct rspamd_http_connection *conn;
  57. struct ev_loop *ev_base;
  58. guint req_size;
  59. gboolean reply;
  60. gint fd;
  61. };
  62. static void
  63. rspamd_server_error (struct rspamd_http_connection *conn,
  64. GError *err)
  65. {
  66. struct rspamd_http_server_session *session = conn->ud;
  67. rspamd_fprintf (stderr, "http error occurred: %s\n", err->message);
  68. rspamd_http_connection_unref (conn);
  69. close (session->fd);
  70. g_slice_free1 (sizeof (*session), session);
  71. }
  72. static int
  73. rspamd_server_finish (struct rspamd_http_connection *conn,
  74. struct rspamd_http_message *msg)
  75. {
  76. struct rspamd_http_server_session *session = conn->ud;
  77. struct rspamd_http_message *reply;
  78. gulong size;
  79. const gchar *url_str;
  80. guint url_len;
  81. rspamd_fstring_t *body;
  82. if (!session->reply) {
  83. session->reply = TRUE;
  84. reply = rspamd_http_new_message (HTTP_RESPONSE);
  85. url_str = msg->url->str;
  86. url_len = msg->url->len;
  87. if (url_str[0] == '/') {
  88. url_str ++;
  89. url_len --;
  90. }
  91. if (rspamd_strtoul (url_str, url_len, &size)) {
  92. session->req_size = size;
  93. reply->code = 200;
  94. reply->status = rspamd_fstring_new_init ("OK", 2);
  95. body = rspamd_fstring_sized_new (size);
  96. body->len = size;
  97. memset (body->str, 0, size);
  98. rspamd_http_message_set_body_from_fstring_steal (msg, body);
  99. }
  100. else {
  101. reply->code = 404;
  102. reply->status = rspamd_fstring_new_init ("Not found", 9);
  103. }
  104. rspamd_http_connection_reset (conn);
  105. rspamd_http_connection_write_message (conn, reply, NULL,
  106. "application/octet-stream", session, session->fd,
  107. &io_tv, session->ev_base);
  108. }
  109. else {
  110. /* Destroy session */
  111. rspamd_http_connection_unref (conn);
  112. close (session->fd);
  113. g_slice_free1 (sizeof (*session), session);
  114. }
  115. return 0;
  116. }
  117. static void
  118. rspamd_server_accept (gint fd, short what, void *arg)
  119. {
  120. struct ev_loop *ev_base = arg;
  121. struct rspamd_http_server_session *session;
  122. rspamd_inet_addr_t *addr;
  123. gint nfd;
  124. do {
  125. if ((nfd =
  126. rspamd_accept_from_socket (fd, &addr, NULL)) == -1) {
  127. rspamd_fprintf (stderr, "accept failed: %s", strerror (errno));
  128. return;
  129. }
  130. /* Check for EAGAIN */
  131. if (nfd == 0) {
  132. return;
  133. }
  134. rspamd_inet_address_free (addr);
  135. session = g_slice_alloc (sizeof (*session));
  136. session->conn = rspamd_http_connection_new (NULL,
  137. rspamd_server_error,
  138. rspamd_server_finish,
  139. 0,
  140. RSPAMD_HTTP_SERVER,
  141. c,
  142. NULL);
  143. rspamd_http_connection_set_key (session->conn, server_key);
  144. rspamd_http_connection_read_message (session->conn,
  145. session,
  146. nfd,
  147. &io_tv,
  148. ev_base);
  149. session->reply = FALSE;
  150. session->fd = nfd;
  151. session->ev_base = ev_base;
  152. } while (nfd > 0);
  153. }
  154. static void
  155. rspamd_http_term_handler (gint fd, short what, void *arg)
  156. {
  157. struct ev_loop *ev_base = arg;
  158. struct timeval tv = {0, 0};
  159. event_base_loopexit (ev_base, &tv);
  160. }
  161. static void
  162. rspamd_http_server_func (gint fd, rspamd_inet_addr_t *addr)
  163. {
  164. struct ev_loop *ev_base = event_init ();
  165. struct event accept_ev, term_ev;
  166. event_set (&accept_ev, fd, EV_READ | EV_PERSIST, rspamd_server_accept, ev_base);
  167. event_base_set (ev_base, &accept_ev);
  168. event_add (&accept_ev, NULL);
  169. evsignal_set (&term_ev, SIGTERM, rspamd_http_term_handler, ev_base);
  170. event_base_set (ev_base, &term_ev);
  171. event_add (&term_ev, NULL);
  172. event_base_loop (ev_base, 0);
  173. }
  174. static void
  175. rspamd_http_start_servers (pid_t *sfd, rspamd_inet_addr_t *addr)
  176. {
  177. guint i;
  178. gint fd;
  179. fd = rspamd_inet_address_listen (addr, SOCK_STREAM, TRUE);
  180. g_assert (fd != -1);
  181. for (i = 0; i < nworkers; i++) {
  182. sfd[i] = fork ();
  183. g_assert (sfd[i] != -1);
  184. if (sfd[i] == 0) {
  185. rspamd_http_server_func (fd, addr);
  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 ev_loop *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 (key == NULL) {
  235. server_key = rspamd_keypair_new (RSPAMD_KEYPAIR_KEX,
  236. openssl_mode ? RSPAMD_CRYPTOBOX_MODE_NIST : RSPAMD_CRYPTOBOX_MODE_25519);
  237. b32_key = rspamd_keypair_print (server_key,
  238. RSPAMD_KEYPAIR_PUBKEY | RSPAMD_KEYPAIR_BASE32);
  239. rspamd_printf ("key: %v\n", b32_key);
  240. }
  241. else {
  242. /* TODO: add key loading */
  243. }
  244. if (cache_size > 0) {
  245. c = rspamd_keypair_cache_new (cache_size);
  246. }
  247. sfd = g_alloca (sizeof (*sfd) * nworkers);
  248. addr = rspamd_inet_address_new (AF_INET, &ina);
  249. rspamd_inet_address_set_port (addr, port);
  250. rspamd_http_start_servers (sfd, addr);
  251. /* Just wait for workers */
  252. ev_base = event_init ();
  253. event_set (&term_ev, SIGTERM, EV_SIGNAL, rspamd_http_server_term, sfd);
  254. event_base_set (ev_base, &term_ev);
  255. event_add (&term_ev, NULL);
  256. event_set (&int_ev, SIGINT, EV_SIGNAL, rspamd_http_server_term, sfd);
  257. event_base_set (ev_base, &int_ev);
  258. event_add (&int_ev, NULL);
  259. event_base_loop (ev_base, 0);
  260. return 0;
  261. }