1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
|
/*-
* Copyright 2016 Vsevolod Stakhov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "config.h"
#include "rspamd.h"
#include "util.h"
#include "libutil/fstring.h"
#include "libutil/http.h"
#include "libutil/http_private.h"
#include "ottery.h"
#include "cryptobox.h"
#include "keypair.h"
#include "unix-std.h"
#include <math.h>
#ifdef HAVE_SYS_WAIT_H
#include <sys/wait.h>
#endif
static unsigned int port = 43000;
static unsigned int cache_size = 10;
static unsigned int nworkers = 1;
static gboolean openssl_mode = FALSE;
static GHashTable *maps = NULL;
static char *key = NULL;
static struct rspamd_keypair_cache *c;
static struct rspamd_cryptobox_keypair *server_key;
static struct timeval io_tv = {
.tv_sec = 20,
.tv_usec = 0};
static GOptionEntry entries[] = {
{"port", 'p', 0, G_OPTION_ARG_INT, &port,
"Port number (default: 43000)", NULL},
{"cache", 'c', 0, G_OPTION_ARG_INT, &cache_size,
"Keys cache size (default: 10)", NULL},
{"workers", 'n', 0, G_OPTION_ARG_INT, &nworkers,
"Number of workers to start (default: 1)", NULL},
{"openssl", 'o', 0, G_OPTION_ARG_NONE, &openssl_mode,
"Use openssl crypto", NULL},
{"key", 'k', 0, G_OPTION_ARG_STRING, &key,
"Use static keypair instead of new one (base32 encoded sk || pk)", NULL},
{NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL}};
struct rspamd_http_server_session {
struct rspamd_http_connection *conn;
struct ev_loop *ev_base;
unsigned int req_size;
gboolean reply;
int fd;
};
static void
rspamd_server_error(struct rspamd_http_connection *conn,
GError *err)
{
struct rspamd_http_server_session *session = conn->ud;
rspamd_fprintf(stderr, "http error occurred: %s\n", err->message);
rspamd_http_connection_unref(conn);
close(session->fd);
g_slice_free1(sizeof(*session), session);
}
static int
rspamd_server_finish(struct rspamd_http_connection *conn,
struct rspamd_http_message *msg)
{
struct rspamd_http_server_session *session = conn->ud;
struct rspamd_http_message *reply;
gulong size;
const char *url_str;
unsigned int url_len;
rspamd_fstring_t *body;
if (!session->reply) {
session->reply = TRUE;
reply = rspamd_http_new_message(HTTP_RESPONSE);
url_str = msg->url->str;
url_len = msg->url->len;
if (url_str[0] == '/') {
url_str++;
url_len--;
}
if (rspamd_strtoul(url_str, url_len, &size)) {
session->req_size = size;
reply->code = 200;
reply->status = rspamd_fstring_new_init("OK", 2);
body = rspamd_fstring_sized_new(size);
body->len = size;
memset(body->str, 0, size);
rspamd_http_message_set_body_from_fstring_steal(msg, body);
}
else {
reply->code = 404;
reply->status = rspamd_fstring_new_init("Not found", 9);
}
rspamd_http_connection_reset(conn);
rspamd_http_connection_write_message(conn, reply, NULL,
"application/octet-stream", session, session->fd,
&io_tv, session->ev_base);
}
else {
/* Destroy session */
rspamd_http_connection_unref(conn);
close(session->fd);
g_slice_free1(sizeof(*session), session);
}
return 0;
}
static void
rspamd_server_accept(int fd, short what, void *arg)
{
struct ev_loop *ev_base = arg;
struct rspamd_http_server_session *session;
rspamd_inet_addr_t *addr;
int nfd;
do {
if ((nfd =
rspamd_accept_from_socket(fd, &addr, NULL)) == -1) {
rspamd_fprintf(stderr, "accept failed: %s", strerror(errno));
return;
}
/* Check for EAGAIN */
if (nfd == 0) {
rspamd_inet_address_free(addr);
return;
}
rspamd_inet_address_free(addr);
session = g_slice_alloc(sizeof(*session));
session->conn = rspamd_http_connection_new(NULL,
rspamd_server_error,
rspamd_server_finish,
0,
RSPAMD_HTTP_SERVER,
c,
NULL);
rspamd_http_connection_set_key(session->conn, server_key);
rspamd_http_connection_read_message(session->conn,
session,
nfd,
&io_tv,
ev_base);
session->reply = FALSE;
session->fd = nfd;
session->ev_base = ev_base;
} while (nfd > 0);
}
static void
rspamd_http_term_handler(int fd, short what, void *arg)
{
struct ev_loop *ev_base = arg;
struct timeval tv = {0, 0};
event_base_loopexit(ev_base, &tv);
}
static void
rspamd_http_server_func(int fd, rspamd_inet_addr_t *addr)
{
struct ev_loop *ev_base = event_init();
struct event accept_ev, term_ev;
event_set(&accept_ev, fd, EV_READ | EV_PERSIST, rspamd_server_accept, ev_base);
event_base_set(ev_base, &accept_ev);
event_add(&accept_ev, NULL);
evsignal_set(&term_ev, SIGTERM, rspamd_http_term_handler, ev_base);
event_base_set(ev_base, &term_ev);
event_add(&term_ev, NULL);
event_base_loop(ev_base, 0);
}
static void
rspamd_http_start_servers(pid_t *sfd, rspamd_inet_addr_t *addr)
{
unsigned int i;
int fd;
fd = rspamd_inet_address_listen(addr, SOCK_STREAM, TRUE);
g_assert(fd != -1);
for (i = 0; i < nworkers; i++) {
sfd[i] = fork();
g_assert(sfd[i] != -1);
if (sfd[i] == 0) {
rspamd_http_server_func(fd, addr);
exit(EXIT_SUCCESS);
}
}
close(fd);
}
static void
rspamd_http_stop_servers(pid_t *sfd)
{
unsigned int i;
int res;
for (i = 0; i < nworkers; i++) {
kill(sfd[i], SIGTERM);
wait(&res);
}
}
static void
rspamd_http_server_term(int fd, short what, void *arg)
{
pid_t *sfd = arg;
rspamd_http_stop_servers(sfd);
event_loopexit(NULL);
}
int main(int argc, char **argv)
{
GOptionContext *context;
GError *error = NULL;
struct ev_loop *ev_base;
GString *b32_key;
pid_t *sfd;
rspamd_inet_addr_t *addr;
struct event term_ev, int_ev;
struct in_addr ina = {INADDR_ANY};
rspamd_init_libs();
context = g_option_context_new(
"rspamd-http-server - test server for benchmarks");
g_option_context_set_summary(context,
"Summary:\n Rspamd test HTTP server " RVERSION
"\n Release id: " RID);
g_option_context_add_main_entries(context, entries, NULL);
if (!g_option_context_parse(context, &argc, &argv, &error)) {
rspamd_fprintf(stderr, "option parsing failed: %s\n", error->message);
g_error_free(error);
exit(EXIT_FAILURE);
}
maps = g_hash_table_new(g_int_hash, g_int_equal);
if (key == NULL) {
server_key = rspamd_keypair_new(RSPAMD_KEYPAIR_KEX,
openssl_mode ? RSPAMD_CRYPTOBOX_MODE_NIST : RSPAMD_CRYPTOBOX_MODE_25519);
b32_key = rspamd_keypair_print(server_key,
RSPAMD_KEYPAIR_PUBKEY | RSPAMD_KEYPAIR_BASE32);
rspamd_printf("key: %v\n", b32_key);
}
else {
/* TODO: add key loading */
}
if (cache_size > 0) {
c = rspamd_keypair_cache_new(cache_size);
}
sfd = g_alloca(sizeof(*sfd) * nworkers);
addr = rspamd_inet_address_new(AF_INET, &ina);
rspamd_inet_address_set_port(addr, port);
rspamd_http_start_servers(sfd, addr);
/* Just wait for workers */
ev_base = event_init();
event_set(&term_ev, SIGTERM, EV_SIGNAL, rspamd_http_server_term, sfd);
event_base_set(ev_base, &term_ev);
event_add(&term_ev, NULL);
event_set(&int_ev, SIGINT, EV_SIGNAL, rspamd_http_server_term, sfd);
event_base_set(ev_base, &int_ev);
event_add(&int_ev, NULL);
event_base_loop(ev_base, 0);
return 0;
}
|