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

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