Compare commits
12 Commits
7653c3117e
...
45514f90ca
Author | SHA1 | Date | |
---|---|---|---|
45514f90ca
|
|||
44913c1e0e
|
|||
b244f86c72
|
|||
a3c1ecc0bf
|
|||
3227e615fe
|
|||
3ce72975b8
|
|||
1f20c70772
|
|||
fabb55d94b
|
|||
6d473bfa49
|
|||
8e83d6aa5f
|
|||
b422b37806
|
|||
74c97a512f
|
8
Makefile
8
Makefile
@@ -42,16 +42,16 @@ bin/lib/%.o: src/lib/%.c
|
|||||||
|
|
||||||
bin/sesimos: bin/server.o bin/client.o bin/logger.o \
|
bin/sesimos: bin/server.o bin/client.o bin/logger.o \
|
||||||
bin/lib/cache.o bin/lib/compress.o bin/lib/config.o bin/lib/fastcgi.o bin/lib/geoip.o \
|
bin/lib/cache.o bin/lib/compress.o bin/lib/config.o bin/lib/fastcgi.o bin/lib/geoip.o \
|
||||||
bin/lib/http.o bin/lib/http_static.o bin/lib/rev_proxy.o bin/lib/sock.o bin/lib/uri.o \
|
bin/lib/http.o bin/lib/http_static.o bin/lib/proxy.o bin/lib/sock.o bin/lib/uri.o \
|
||||||
bin/lib/utils.o bin/lib/websocket.o
|
bin/lib/utils.o bin/lib/websocket.o
|
||||||
$(CC) -o $@ $^ $(CFLAGS) $(LDFLAGS)
|
$(CC) -o $@ $^ $(CFLAGS) $(LDFLAGS)
|
||||||
|
|
||||||
|
|
||||||
bin/server.o: src/server.h src/defs.h src/client.h src/lib/cache.h src/lib/config.h src/lib/sock.h \
|
bin/server.o: src/server.h src/defs.h src/client.h src/lib/cache.h src/lib/config.h src/lib/sock.h \
|
||||||
src/lib/rev_proxy.h src/lib/geoip.h src/lib/utils.h src/logger.h
|
src/lib/proxy.h src/lib/geoip.h src/lib/utils.h src/logger.h
|
||||||
|
|
||||||
bin/client.o: src/client.h src/defs.h src/server.h src/lib/utils.h src/lib/config.h src/lib/sock.h \
|
bin/client.o: src/client.h src/defs.h src/server.h src/lib/utils.h src/lib/config.h src/lib/sock.h \
|
||||||
src/lib/http.h src/lib/rev_proxy.h src/lib/fastcgi.h src/lib/cache.h src/lib/geoip.h src/lib/compress.h \
|
src/lib/http.h src/lib/proxy.h src/lib/fastcgi.h src/lib/cache.h src/lib/geoip.h src/lib/compress.h \
|
||||||
src/lib/websocket.h src/logger.h
|
src/lib/websocket.h src/logger.h
|
||||||
|
|
||||||
bin/logger.o: src/logger.h
|
bin/logger.o: src/logger.h
|
||||||
@@ -69,7 +69,7 @@ bin/lib/geoip.o: src/lib/geoip.h
|
|||||||
|
|
||||||
bin/lib/http.o: src/lib/http.h src/lib/utils.h src/lib/compress.h src/lib/sock.h src/logger.h
|
bin/lib/http.o: src/lib/http.h src/lib/utils.h src/lib/compress.h src/lib/sock.h src/logger.h
|
||||||
|
|
||||||
bin/lib/rev_proxy.o: src/lib/rev_proxy.h src/defs.h src/server.h src/lib/compress.h src/logger.h
|
bin/lib/proxy.o: src/lib/proxy.h src/defs.h src/server.h src/lib/compress.h src/logger.h
|
||||||
|
|
||||||
bin/lib/sock.o: src/lib/sock.h
|
bin/lib/sock.o: src/lib/sock.h
|
||||||
|
|
||||||
|
@@ -7,7 +7,7 @@
|
|||||||
* connection_initializer
|
* connection_initializer
|
||||||
* request_handler
|
* request_handler
|
||||||
* local_handler
|
* local_handler
|
||||||
* rev_proxy_handler
|
* proxy_handler
|
||||||
* ws_handler
|
* ws_handler
|
||||||
* fastcgi_handler
|
* fastcgi_handler
|
||||||
|
|
||||||
@@ -19,4 +19,4 @@
|
|||||||
* request_handler -> local_handler -> request_handler
|
* request_handler -> local_handler -> request_handler
|
||||||
* local_handler -> fastcgi_handler -> request_handler
|
* local_handler -> fastcgi_handler -> request_handler
|
||||||
* request_handler -> rp_handler -> request_handler
|
* request_handler -> rp_handler -> request_handler
|
||||||
* rev_proxy_handler -> ws_handler -> request_handler
|
* proxy_handler -> ws_handler -> request_handler
|
||||||
|
70
src/async.c
Normal file
70
src/async.c
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
|
||||||
|
|
||||||
|
#include "logger.h"
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <poll.h>
|
||||||
|
#include <signal.h>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
int fd;
|
||||||
|
short events;
|
||||||
|
int flags;
|
||||||
|
void (*cb)(void *);
|
||||||
|
void *arg;
|
||||||
|
void (*err_cb)(void *);
|
||||||
|
void *err_arg;
|
||||||
|
} evt_listen_t;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
int n;
|
||||||
|
evt_listen_t q[256];
|
||||||
|
} listen_queue_t;
|
||||||
|
|
||||||
|
|
||||||
|
static listen_queue_t listen1;
|
||||||
|
static listen_queue_t listen2;
|
||||||
|
listen_queue_t *listen = &listen1;
|
||||||
|
|
||||||
|
volatile sig_atomic_t alive = 1;
|
||||||
|
|
||||||
|
int async(int fd, int events, int flags, void (*cb)(void *), void *arg, void (*err_cb)(void *), void *err_arg) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void async_thread(void) {
|
||||||
|
|
||||||
|
int num_fds = 0;
|
||||||
|
struct pollfd fds[256];
|
||||||
|
|
||||||
|
// main event loop
|
||||||
|
while (alive) {
|
||||||
|
// swap listen queue
|
||||||
|
listen_queue_t *l = listen;
|
||||||
|
listen = (listen == &listen1) ? &listen2 : &listen1;
|
||||||
|
|
||||||
|
// fill fds with newly added
|
||||||
|
for (int i = 0; i < l->n; i++, num_fds++) {
|
||||||
|
fds[num_fds].fd = l->q[i].fd;
|
||||||
|
fds[num_fds].events = l->q[i].events;
|
||||||
|
}
|
||||||
|
|
||||||
|
int ready_fds = poll(fds, num_fds, -1);
|
||||||
|
if (ready_fds < 0) {
|
||||||
|
if (errno == EINTR) {
|
||||||
|
// interrupt
|
||||||
|
continue;
|
||||||
|
} else {
|
||||||
|
// other error
|
||||||
|
critical("Unable to poll for events");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < num_fds; i++) {
|
||||||
|
// TODO
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
13
src/async.h
Normal file
13
src/async.h
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
|
||||||
|
|
||||||
|
#ifndef SESIMOS_ASYNC_H
|
||||||
|
#define SESIMOS_ASYNC_H
|
||||||
|
|
||||||
|
#define async_read(fd, cb, arg, err_cb, err_arg) async(fd, 0, 0, cb, arg, err, err_arg)
|
||||||
|
#define async_read_keep(fd, cb, arg, err_cb, err_arg) async(fd, 0, 0, cb, arg, err, err_arg)
|
||||||
|
|
||||||
|
int async(int fd, int events, int flags, void (*cb)(void *), void *arg, void (*err_cb)(void *), void *err_arg);
|
||||||
|
|
||||||
|
void async_thread(void);
|
||||||
|
|
||||||
|
#endif //SESIMOS_ASYNC_H
|
184
src/client.c
184
src/client.c
@@ -15,7 +15,7 @@
|
|||||||
#include "lib/config.h"
|
#include "lib/config.h"
|
||||||
#include "lib/sock.h"
|
#include "lib/sock.h"
|
||||||
#include "lib/http.h"
|
#include "lib/http.h"
|
||||||
#include "lib/rev_proxy.h"
|
#include "lib/proxy.h"
|
||||||
#include "lib/fastcgi.h"
|
#include "lib/fastcgi.h"
|
||||||
#include "lib/cache.h"
|
#include "lib/cache.h"
|
||||||
#include "lib/geoip.h"
|
#include "lib/geoip.h"
|
||||||
@@ -31,12 +31,11 @@
|
|||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
|
|
||||||
|
|
||||||
volatile sig_atomic_t server_keep_alive = 1;
|
static const char *color_table[] = {"\x1B[31m", "\x1B[32m", "\x1B[33m", "\x1B[34m", "\x1B[35m", "\x1B[36m"};
|
||||||
struct timeval client_timeout = {.tv_sec = CLIENT_TIMEOUT, .tv_usec = 0};
|
|
||||||
|
|
||||||
host_config *get_host_config(const char *host) {
|
host_config_t *get_host_config(const char *host) {
|
||||||
for (int i = 0; i < CONFIG_MAX_HOST_CONFIG; i++) {
|
for (int i = 0; i < CONFIG_MAX_HOST_CONFIG; i++) {
|
||||||
host_config *hc = &config->hosts[i];
|
host_config_t *hc = &config.hosts[i];
|
||||||
if (hc->type == CONFIG_TYPE_UNSET) break;
|
if (hc->type == CONFIG_TYPE_UNSET) break;
|
||||||
if (strcmp(hc->name, host) == 0) return hc;
|
if (strcmp(hc->name, host) == 0) return hc;
|
||||||
if (hc->name[0] == '*' && hc->name[1] == '.') {
|
if (hc->name[0] == '*' && hc->name[1] == '.') {
|
||||||
@@ -47,14 +46,15 @@ host_config *get_host_config(const char *host) {
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
void client_terminate(int _) {
|
void client_terminate(int _) {
|
||||||
server_keep_alive = 0;
|
server_keep_alive = 0;
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
int client_request_handler(client_ctx_t *cctx, sock *client, unsigned long client_num, unsigned int req_num, const char *restrict log_client_prefix) {
|
int client_request_handler(client_ctx_t *cctx, sock *client, unsigned long client_num, unsigned int req_num, const char *restrict log_client_prefix) {
|
||||||
struct timespec begin, end;
|
struct timespec begin, end;
|
||||||
long ret;
|
long ret;
|
||||||
int client_keep_alive;
|
|
||||||
|
|
||||||
char buf0[1024], buf1[1024];
|
char buf0[1024], buf1[1024];
|
||||||
char msg_buf[8192], msg_pre_buf_1[4096], msg_pre_buf_2[4096], err_msg[256];
|
char msg_buf[8192], msg_pre_buf_1[4096], msg_pre_buf_2[4096], err_msg[256];
|
||||||
@@ -68,13 +68,13 @@ int client_request_handler(client_ctx_t *cctx, sock *client, unsigned long clien
|
|||||||
err_msg[0] = 0;
|
err_msg[0] = 0;
|
||||||
msg_content[0] = 0;
|
msg_content[0] = 0;
|
||||||
|
|
||||||
host_config *conf = NULL;
|
host_config_t *conf = NULL;
|
||||||
FILE *file = NULL;
|
FILE *file = NULL;
|
||||||
|
|
||||||
long content_length = 0;
|
long content_length = 0;
|
||||||
int accept_if_modified_since = 0;
|
int accept_if_modified_since = 0;
|
||||||
int use_fastcgi = 0;
|
int use_fastcgi = 0;
|
||||||
int use_rev_proxy = 0;
|
int use_proxy = 0;
|
||||||
int p_len;
|
int p_len;
|
||||||
|
|
||||||
fastcgi_conn fcgi_conn = {.socket = 0, .req_id = 0, .ctx = cctx};
|
fastcgi_conn fcgi_conn = {.socket = 0, .req_id = 0, .ctx = cctx};
|
||||||
@@ -92,7 +92,7 @@ int client_request_handler(client_ctx_t *cctx, sock *client, unsigned long clien
|
|||||||
if (ret <= 0) {
|
if (ret <= 0) {
|
||||||
if (errno != 0) return 1;
|
if (errno != 0) return 1;
|
||||||
|
|
||||||
client_keep_alive = 0;
|
cctx->c_keep_alive = 0;
|
||||||
res.status = http_get_status(408);
|
res.status = http_get_status(408);
|
||||||
goto respond;
|
goto respond;
|
||||||
}
|
}
|
||||||
@@ -101,7 +101,7 @@ int client_request_handler(client_ctx_t *cctx, sock *client, unsigned long clien
|
|||||||
http_req req;
|
http_req req;
|
||||||
ret = http_receive_request(client, &req);
|
ret = http_receive_request(client, &req);
|
||||||
if (ret != 0) {
|
if (ret != 0) {
|
||||||
client_keep_alive = 0;
|
cctx->c_keep_alive = 0;
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
goto abort;
|
goto abort;
|
||||||
} else if (ret == 1) {
|
} else if (ret == 1) {
|
||||||
@@ -120,7 +120,7 @@ int client_request_handler(client_ctx_t *cctx, sock *client, unsigned long clien
|
|||||||
}
|
}
|
||||||
|
|
||||||
hdr_connection = http_get_header_field(&req.hdr, "Connection");
|
hdr_connection = http_get_header_field(&req.hdr, "Connection");
|
||||||
client_keep_alive = (hdr_connection != NULL && (strstr(hdr_connection, "keep-alive") != NULL || strstr(hdr_connection, "Keep-Alive") != NULL));
|
cctx->c_keep_alive = (hdr_connection != NULL && (strstr(hdr_connection, "keep-alive") != NULL || strstr(hdr_connection, "Keep-Alive") != NULL));
|
||||||
host_ptr = http_get_header_field(&req.hdr, "Host");
|
host_ptr = http_get_header_field(&req.hdr, "Host");
|
||||||
if (host_ptr != NULL && strlen(host_ptr) > 255) {
|
if (host_ptr != NULL && strlen(host_ptr) > 255) {
|
||||||
host[0] = 0;
|
host[0] = 0;
|
||||||
@@ -140,7 +140,7 @@ int client_request_handler(client_ctx_t *cctx, sock *client, unsigned long clien
|
|||||||
strcpy(host, host_ptr);
|
strcpy(host, host_ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
sprintf(log_req_prefix, "[%6i][%s%*s%s]%s", getpid(), BLD_STR, INET6_ADDRSTRLEN, host, CLR_STR, log_client_prefix);
|
sprintf(log_req_prefix, "[%s%*s%s]%s", BLD_STR, INET6_ADDRSTRLEN, host, CLR_STR, log_client_prefix);
|
||||||
logger_set_prefix(log_req_prefix);
|
logger_set_prefix(log_req_prefix);
|
||||||
info(BLD_STR "%s %s", req.method, req.uri);
|
info(BLD_STR "%s %s", req.method, req.uri);
|
||||||
|
|
||||||
@@ -252,8 +252,7 @@ int client_request_handler(client_ctx_t *cctx, sock *client, unsigned long clien
|
|||||||
goto respond;
|
goto respond;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = uri_cache_init(&uri);
|
if ((ret = cache_init_uri(conf->cache, &uri)) != 0) {
|
||||||
if (ret != 0) {
|
|
||||||
res.status = http_get_status(500);
|
res.status = http_get_status(500);
|
||||||
sprintf(err_msg, "Unable to communicate with internal file cache.");
|
sprintf(err_msg, "Unable to communicate with internal file cache.");
|
||||||
goto respond;
|
goto respond;
|
||||||
@@ -270,7 +269,7 @@ int client_request_handler(client_ctx_t *cctx, sock *client, unsigned long clien
|
|||||||
if (uri.meta->filename_comp_br[0] != 0 && strstr(accept_encoding, "br") != NULL) {
|
if (uri.meta->filename_comp_br[0] != 0 && strstr(accept_encoding, "br") != NULL) {
|
||||||
file = fopen(uri.meta->filename_comp_br, "rb");
|
file = fopen(uri.meta->filename_comp_br, "rb");
|
||||||
if (file == NULL) {
|
if (file == NULL) {
|
||||||
cache_filename_comp_invalid(uri.filename);
|
cache_mark_dirty(conf->cache, uri.filename);
|
||||||
} else {
|
} else {
|
||||||
http_add_header_field(&res.hdr, "Content-Encoding", "br");
|
http_add_header_field(&res.hdr, "Content-Encoding", "br");
|
||||||
enc = COMPRESS_BR;
|
enc = COMPRESS_BR;
|
||||||
@@ -278,7 +277,7 @@ int client_request_handler(client_ctx_t *cctx, sock *client, unsigned long clien
|
|||||||
} else if (uri.meta->filename_comp_gz[0] != 0 && strstr(accept_encoding, "gzip") != NULL) {
|
} else if (uri.meta->filename_comp_gz[0] != 0 && strstr(accept_encoding, "gzip") != NULL) {
|
||||||
file = fopen(uri.meta->filename_comp_gz, "rb");
|
file = fopen(uri.meta->filename_comp_gz, "rb");
|
||||||
if (file == NULL) {
|
if (file == NULL) {
|
||||||
cache_filename_comp_invalid(uri.filename);
|
cache_mark_dirty(conf->cache, uri.filename);
|
||||||
} else {
|
} else {
|
||||||
http_add_header_field(&res.hdr, "Content-Encoding", "gzip");
|
http_add_header_field(&res.hdr, "Content-Encoding", "gzip");
|
||||||
enc = COMPRESS_GZ;
|
enc = COMPRESS_GZ;
|
||||||
@@ -474,12 +473,12 @@ int client_request_handler(client_ctx_t *cctx, sock *client, unsigned long clien
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (conf->type == CONFIG_TYPE_REVERSE_PROXY) {
|
} else if (conf->type == CONFIG_TYPE_REVERSE_PROXY) {
|
||||||
info("Reverse proxy for " BLD_STR "%s:%i" CLR_STR, conf->rev_proxy.hostname, conf->rev_proxy.port);
|
info("Reverse proxy for " BLD_STR "%s:%i" CLR_STR, conf->proxy.hostname, conf->proxy.port);
|
||||||
http_remove_header_field(&res.hdr, "Date", HTTP_REMOVE_ALL);
|
http_remove_header_field(&res.hdr, "Date", HTTP_REMOVE_ALL);
|
||||||
http_remove_header_field(&res.hdr, "Server", HTTP_REMOVE_ALL);
|
http_remove_header_field(&res.hdr, "Server", HTTP_REMOVE_ALL);
|
||||||
|
|
||||||
ret = rev_proxy_init(&req, &res, &ctx, conf, client, cctx, &custom_status, err_msg);
|
ret = proxy_init(&req, &res, &ctx, conf, client, cctx, &custom_status, err_msg);
|
||||||
use_rev_proxy = (ret == 0);
|
use_proxy = (ret == 0);
|
||||||
|
|
||||||
if (res.status->code == 101) {
|
if (res.status->code == 101) {
|
||||||
const char *connection = http_get_header_field(&res.hdr, "Connection");
|
const char *connection = http_get_header_field(&res.hdr, "Connection");
|
||||||
@@ -490,7 +489,7 @@ int client_request_handler(client_ctx_t *cctx, sock *client, unsigned long clien
|
|||||||
{
|
{
|
||||||
const char *ws_accept = http_get_header_field(&res.hdr, "Sec-WebSocket-Accept");
|
const char *ws_accept = http_get_header_field(&res.hdr, "Sec-WebSocket-Accept");
|
||||||
if (ws_calc_accept_key(ctx.ws_key, buf0) == 0) {
|
if (ws_calc_accept_key(ctx.ws_key, buf0) == 0) {
|
||||||
use_rev_proxy = (strcmp(buf0, ws_accept) == 0) ? 2 : 1;
|
use_proxy = (strcmp(buf0, ws_accept) == 0) ? 2 : 1;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
ctx.status = 101;
|
ctx.status = 101;
|
||||||
@@ -500,7 +499,7 @@ int client_request_handler(client_ctx_t *cctx, sock *client, unsigned long clien
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Let 300 be formatted by origin server
|
// Let 300 be formatted by origin server
|
||||||
if (use_rev_proxy && res.status->code >= 301 && res.status->code < 600) {
|
if (use_proxy && res.status->code >= 301 && res.status->code < 600) {
|
||||||
const char *content_type = http_get_header_field(&res.hdr, "Content-Type");
|
const char *content_type = http_get_header_field(&res.hdr, "Content-Type");
|
||||||
const char *content_length_f = http_get_header_field(&res.hdr, "Content-Length");
|
const char *content_length_f = http_get_header_field(&res.hdr, "Content-Length");
|
||||||
const char *content_encoding = http_get_header_field(&res.hdr, "Content-Encoding");
|
const char *content_encoding = http_get_header_field(&res.hdr, "Content-Encoding");
|
||||||
@@ -511,20 +510,20 @@ int client_request_handler(client_ctx_t *cctx, sock *client, unsigned long clien
|
|||||||
ctx.status = res.status->code;
|
ctx.status = res.status->code;
|
||||||
ctx.origin = res.status->code >= 400 ? SERVER : NONE;
|
ctx.origin = res.status->code >= 400 ? SERVER : NONE;
|
||||||
}
|
}
|
||||||
use_rev_proxy = 0;
|
use_proxy = 0;
|
||||||
rev_proxy_dump(msg_content, content_len);
|
proxy_dump(msg_content, content_len);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
char *content_encoding = http_get_header_field(&res.hdr, "Content-Encoding");
|
char *content_encoding = http_get_header_field(&res.hdr, "Content-Encoding");
|
||||||
if (use_rev_proxy && content_encoding == NULL) {
|
if (use_proxy && content_encoding == NULL) {
|
||||||
int http_comp = http_get_compression(&req, &res);
|
int http_comp = http_get_compression(&req, &res);
|
||||||
if (http_comp & COMPRESS_BR) {
|
if (http_comp & COMPRESS_BR) {
|
||||||
use_rev_proxy |= REV_PROXY_COMPRESS_BR;
|
use_proxy |= PROXY_COMPRESS_BR;
|
||||||
} else if (http_comp & COMPRESS_GZ) {
|
} else if (http_comp & COMPRESS_GZ) {
|
||||||
use_rev_proxy |= REV_PROXY_COMPRESS_GZ;
|
use_proxy |= PROXY_COMPRESS_GZ;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -532,9 +531,9 @@ int client_request_handler(client_ctx_t *cctx, sock *client, unsigned long clien
|
|||||||
int chunked = transfer_encoding != NULL && strcmp(transfer_encoding, "chunked") == 0;
|
int chunked = transfer_encoding != NULL && strcmp(transfer_encoding, "chunked") == 0;
|
||||||
http_remove_header_field(&res.hdr, "Transfer-Encoding", HTTP_REMOVE_ALL);
|
http_remove_header_field(&res.hdr, "Transfer-Encoding", HTTP_REMOVE_ALL);
|
||||||
ret = sprintf(buf0, "%s%s%s",
|
ret = sprintf(buf0, "%s%s%s",
|
||||||
(use_rev_proxy & REV_PROXY_COMPRESS_BR) ? "br" :
|
(use_proxy & PROXY_COMPRESS_BR) ? "br" :
|
||||||
((use_rev_proxy & REV_PROXY_COMPRESS_GZ) ? "gzip" : ""),
|
((use_proxy & PROXY_COMPRESS_GZ) ? "gzip" : ""),
|
||||||
((use_rev_proxy & REV_PROXY_COMPRESS) && chunked) ? ", " : "",
|
((use_proxy & PROXY_COMPRESS) && chunked) ? ", " : "",
|
||||||
chunked ? "chunked" : "");
|
chunked ? "chunked" : "");
|
||||||
if (ret > 0) {
|
if (ret > 0) {
|
||||||
http_add_header_field(&res.hdr, "Transfer-Encoding", buf0);
|
http_add_header_field(&res.hdr, "Transfer-Encoding", buf0);
|
||||||
@@ -546,7 +545,7 @@ int client_request_handler(client_ctx_t *cctx, sock *client, unsigned long clien
|
|||||||
}
|
}
|
||||||
|
|
||||||
respond:
|
respond:
|
||||||
if (!use_rev_proxy) {
|
if (!use_proxy) {
|
||||||
if (conf != NULL && conf->type == CONFIG_TYPE_LOCAL && uri.is_static && res.status->code == 405) {
|
if (conf != NULL && conf->type == CONFIG_TYPE_LOCAL && uri.is_static && res.status->code == 405) {
|
||||||
http_add_header_field(&res.hdr, "Allow", "GET, HEAD, TRACE");
|
http_add_header_field(&res.hdr, "Allow", "GET, HEAD, TRACE");
|
||||||
}
|
}
|
||||||
@@ -580,12 +579,12 @@ int client_request_handler(client_ctx_t *cctx, sock *client, unsigned long clien
|
|||||||
// TODO let relevant information pass?
|
// TODO let relevant information pass?
|
||||||
}
|
}
|
||||||
|
|
||||||
char *rev_proxy_doc = "";
|
char *proxy_doc = "";
|
||||||
if (conf != NULL && conf->type == CONFIG_TYPE_REVERSE_PROXY) {
|
if (conf != NULL && conf->type == CONFIG_TYPE_REVERSE_PROXY) {
|
||||||
const http_status *status = http_get_status(ctx.status);
|
const http_status *status = http_get_status(ctx.status);
|
||||||
char stat_str[8];
|
char stat_str[8];
|
||||||
sprintf(stat_str, "%03i", ctx.status);
|
sprintf(stat_str, "%03i", ctx.status);
|
||||||
sprintf(msg_pre_buf_2, http_rev_proxy_document,
|
sprintf(msg_pre_buf_2, http_proxy_document,
|
||||||
" success",
|
" success",
|
||||||
(ctx.origin == CLIENT_REQ) ? " error" : " success",
|
(ctx.origin == CLIENT_REQ) ? " error" : " success",
|
||||||
(ctx.origin == INTERNAL) ? " error" : " success",
|
(ctx.origin == INTERNAL) ? " error" : " success",
|
||||||
@@ -600,30 +599,30 @@ int client_request_handler(client_ctx_t *cctx, sock *client, unsigned long clien
|
|||||||
(ctx.status == 0) ? "???" : stat_str,
|
(ctx.status == 0) ? "???" : stat_str,
|
||||||
(status != NULL) ? status->msg : "",
|
(status != NULL) ? status->msg : "",
|
||||||
host);
|
host);
|
||||||
rev_proxy_doc = msg_pre_buf_2;
|
proxy_doc = msg_pre_buf_2;
|
||||||
}
|
}
|
||||||
|
|
||||||
sprintf(msg_pre_buf_1, info->doc, res.status->code, res.status->msg, http_msg != NULL ? http_msg->msg : "", err_msg[0] != 0 ? err_msg : "");
|
sprintf(msg_pre_buf_1, info->doc, res.status->code, res.status->msg, http_msg != NULL ? http_msg->msg : "", err_msg[0] != 0 ? err_msg : "");
|
||||||
content_length = snprintf(msg_buf, sizeof(msg_buf), http_default_document, res.status->code,
|
content_length = snprintf(msg_buf, sizeof(msg_buf), http_default_document, res.status->code,
|
||||||
res.status->msg, msg_pre_buf_1, info->mode, info->icon, info->color, host,
|
res.status->msg, msg_pre_buf_1, info->mode, info->icon, info->color, host,
|
||||||
rev_proxy_doc, msg_content[0] != 0 ? msg_content : "");
|
proxy_doc, msg_content[0] != 0 ? msg_content : "");
|
||||||
}
|
}
|
||||||
if (content_length >= 0) {
|
if (content_length >= 0) {
|
||||||
sprintf(buf0, "%li", content_length);
|
sprintf(buf0, "%li", content_length);
|
||||||
http_remove_header_field(&res.hdr, "Content-Length", HTTP_REMOVE_ALL);
|
http_remove_header_field(&res.hdr, "Content-Length", HTTP_REMOVE_ALL);
|
||||||
http_add_header_field(&res.hdr, "Content-Length", buf0);
|
http_add_header_field(&res.hdr, "Content-Length", buf0);
|
||||||
} else if (http_get_header_field(&res.hdr, "Transfer-Encoding") == NULL) {
|
} else if (http_get_header_field(&res.hdr, "Transfer-Encoding") == NULL) {
|
||||||
server_keep_alive = 0;
|
cctx->s_keep_alive = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int close_proxy = 0;
|
int close_proxy = 0;
|
||||||
if (use_rev_proxy != 2) {
|
if (use_proxy != 2) {
|
||||||
const char *conn = http_get_header_field(&res.hdr, "Connection");
|
const char *conn = http_get_header_field(&res.hdr, "Connection");
|
||||||
close_proxy = (conn == NULL || (strstr(conn, "keep-alive") == NULL && strstr(conn, "Keep-Alive") == NULL));
|
close_proxy = (conn == NULL || (strstr(conn, "keep-alive") == NULL && strstr(conn, "Keep-Alive") == NULL));
|
||||||
http_remove_header_field(&res.hdr, "Connection", HTTP_REMOVE_ALL);
|
http_remove_header_field(&res.hdr, "Connection", HTTP_REMOVE_ALL);
|
||||||
http_remove_header_field(&res.hdr, "Keep-Alive", HTTP_REMOVE_ALL);
|
http_remove_header_field(&res.hdr, "Keep-Alive", HTTP_REMOVE_ALL);
|
||||||
if (server_keep_alive && client_keep_alive) {
|
if (cctx->s_keep_alive && cctx->c_keep_alive) {
|
||||||
http_add_header_field(&res.hdr, "Connection", "keep-alive");
|
http_add_header_field(&res.hdr, "Connection", "keep-alive");
|
||||||
sprintf(buf0, "timeout=%i, max=%i", CLIENT_TIMEOUT, REQ_PER_CONNECTION);
|
sprintf(buf0, "timeout=%i, max=%i", CLIENT_TIMEOUT, REQ_PER_CONNECTION);
|
||||||
http_add_header_field(&res.hdr, "Keep-Alive", buf0);
|
http_add_header_field(&res.hdr, "Keep-Alive", buf0);
|
||||||
@@ -636,18 +635,18 @@ int client_request_handler(client_ctx_t *cctx, sock *client, unsigned long clien
|
|||||||
clock_gettime(CLOCK_MONOTONIC, &end);
|
clock_gettime(CLOCK_MONOTONIC, &end);
|
||||||
const char *location = http_get_header_field(&res.hdr, "Location");
|
const char *location = http_get_header_field(&res.hdr, "Location");
|
||||||
unsigned long micros = (end.tv_nsec - begin.tv_nsec) / 1000 + (end.tv_sec - begin.tv_sec) * 1000000;
|
unsigned long micros = (end.tv_nsec - begin.tv_nsec) / 1000 + (end.tv_sec - begin.tv_sec) * 1000000;
|
||||||
info("%s%s%03i %s%s%s (%s)%s", http_get_status_color(res.status), use_rev_proxy ? "-> " : "", res.status->code,
|
info("%s%s%03i %s%s%s (%s)%s", http_get_status_color(res.status), use_proxy ? "-> " : "", res.status->code,
|
||||||
res.status->msg, location != NULL ? " -> " : "", location != NULL ? location : "",
|
res.status->msg, location != NULL ? " -> " : "", location != NULL ? location : "",
|
||||||
format_duration(micros, buf0), CLR_STR);
|
format_duration(micros, buf0), CLR_STR);
|
||||||
|
|
||||||
// TODO access/error log file
|
// TODO access/error log file
|
||||||
|
|
||||||
if (use_rev_proxy == 2) {
|
if (use_proxy == 2) {
|
||||||
// WebSocket
|
// WebSocket
|
||||||
info("Upgrading connection to WebSocket connection");
|
info("Upgrading connection to WebSocket connection");
|
||||||
ret = ws_handle_connection(client, &rev_proxy);
|
ret = ws_handle_connection(client, &proxy);
|
||||||
if (ret != 0) {
|
if (ret != 0) {
|
||||||
client_keep_alive = 0;
|
cctx->c_keep_alive = 0;
|
||||||
close_proxy = 1;
|
close_proxy = 1;
|
||||||
}
|
}
|
||||||
info("WebSocket connection closed");
|
info("WebSocket connection closed");
|
||||||
@@ -680,7 +679,7 @@ int client_request_handler(client_ctx_t *cctx, sock *client, unsigned long clien
|
|||||||
|
|
||||||
int flags = (chunked ? FASTCGI_CHUNKED : 0) | (use_fastcgi & (FASTCGI_COMPRESS | FASTCGI_COMPRESS_HOLD));
|
int flags = (chunked ? FASTCGI_CHUNKED : 0) | (use_fastcgi & (FASTCGI_COMPRESS | FASTCGI_COMPRESS_HOLD));
|
||||||
ret = fastcgi_send(&fcgi_conn, client, flags);
|
ret = fastcgi_send(&fcgi_conn, client, flags);
|
||||||
} else if (use_rev_proxy) {
|
} else if (use_proxy) {
|
||||||
const char *transfer_encoding = http_get_header_field(&res.hdr, "Transfer-Encoding");
|
const char *transfer_encoding = http_get_header_field(&res.hdr, "Transfer-Encoding");
|
||||||
int chunked = transfer_encoding != NULL && strstr(transfer_encoding, "chunked") != NULL;
|
int chunked = transfer_encoding != NULL && strstr(transfer_encoding, "chunked") != NULL;
|
||||||
|
|
||||||
@@ -690,18 +689,18 @@ int client_request_handler(client_ctx_t *cctx, sock *client, unsigned long clien
|
|||||||
len_to_send = strtol(content_len, NULL, 10);
|
len_to_send = strtol(content_len, NULL, 10);
|
||||||
}
|
}
|
||||||
|
|
||||||
int flags = (chunked ? REV_PROXY_CHUNKED : 0) | (use_rev_proxy & REV_PROXY_COMPRESS);
|
int flags = (chunked ? PROXY_CHUNKED : 0) | (use_proxy & PROXY_COMPRESS);
|
||||||
ret = rev_proxy_send(client, len_to_send, flags);
|
ret = proxy_send(client, len_to_send, flags);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
client_keep_alive = 0;
|
cctx->c_keep_alive = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (close_proxy && rev_proxy.socket != 0) {
|
if (close_proxy && proxy.socket != 0) {
|
||||||
info(BLUE_STR "Closing proxy connection");
|
info(BLUE_STR "Closing proxy connection");
|
||||||
sock_close(&rev_proxy);
|
sock_close(&proxy);
|
||||||
}
|
}
|
||||||
|
|
||||||
clock_gettime(CLOCK_MONOTONIC, &end);
|
clock_gettime(CLOCK_MONOTONIC, &end);
|
||||||
@@ -717,12 +716,12 @@ int client_request_handler(client_ctx_t *cctx, sock *client, unsigned long clien
|
|||||||
}
|
}
|
||||||
http_free_req(&req);
|
http_free_req(&req);
|
||||||
http_free_res(&res);
|
http_free_res(&res);
|
||||||
return !client_keep_alive;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int client_connection_handler(client_ctx_t *ctx, sock *client, unsigned long client_num, const char *restrict log_conn_prefix, const char *restrict log_client_prefix) {
|
int client_connection_handler(client_ctx_t *ctx, sock *client, unsigned long client_num, const char *restrict log_conn_prefix, const char *restrict log_client_prefix) {
|
||||||
struct timespec begin, end;
|
struct timespec begin, end;
|
||||||
int ret, req_num;
|
int ret;
|
||||||
char buf[1024];
|
char buf[1024];
|
||||||
|
|
||||||
clock_gettime(CLOCK_MONOTONIC, &begin);
|
clock_gettime(CLOCK_MONOTONIC, &begin);
|
||||||
@@ -751,62 +750,18 @@ int client_connection_handler(client_ctx_t *ctx, sock *client, unsigned long cli
|
|||||||
ctx->host[0] = 0;
|
ctx->host[0] = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
long str_off = 0;
|
|
||||||
for (int i = 0; i < MAX_MMDB && mmdbs[i].filename != NULL; i++) {
|
|
||||||
int gai_error, mmdb_res;
|
|
||||||
MMDB_lookup_result_s result = MMDB_lookup_string(&mmdbs[i], ctx->addr, &gai_error, &mmdb_res);
|
|
||||||
if (mmdb_res != MMDB_SUCCESS) {
|
|
||||||
error("Unable to lookup geoip info: %s", MMDB_strerror(mmdb_res));
|
|
||||||
continue;
|
|
||||||
} else if (gai_error != 0) {
|
|
||||||
error("Unable to lookup geoip info");
|
|
||||||
continue;
|
|
||||||
} else if (!result.found_entry) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
MMDB_entry_data_list_s *list;
|
|
||||||
mmdb_res = MMDB_get_entry_data_list(&result.entry, &list);
|
|
||||||
if (mmdb_res != MMDB_SUCCESS) {
|
|
||||||
error("Unable to lookup geoip info: %s", MMDB_strerror(mmdb_res));
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
long prev = str_off;
|
|
||||||
if (str_off != 0) {
|
|
||||||
str_off--;
|
|
||||||
}
|
|
||||||
mmdb_json(list, ctx->geoip, &str_off, GEOIP_MAX_SIZE);
|
|
||||||
if (prev != 0) {
|
|
||||||
ctx->geoip[prev - 1] = ',';
|
|
||||||
}
|
|
||||||
|
|
||||||
MMDB_free_entry_data_list(list);
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx->cc[0] = 0;
|
ctx->cc[0] = 0;
|
||||||
if (str_off == 0) {
|
|
||||||
ctx->geoip[0] = 0;
|
ctx->geoip[0] = 0;
|
||||||
} else {
|
geoip_lookup_country(&client->addr.sock, ctx->cc);
|
||||||
const char *pos = ctx->geoip;
|
|
||||||
pos = strstr(pos, "\"country\":");
|
|
||||||
if (pos != NULL) {
|
|
||||||
pos = strstr(pos, "\"iso_code\":");
|
|
||||||
pos += 12;
|
|
||||||
snprintf(ctx->cc, sizeof(ctx->cc), "%s", pos);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
info("Connection accepted from %s %s%s%s[%s]", ctx->addr, ctx->host[0] != 0 ? "(" : "",
|
info("Connection accepted from %s %s%s%s[%s]", ctx->addr, ctx->host[0] != 0 ? "(" : "",
|
||||||
ctx->host[0] != 0 ? ctx->host : "", ctx->host[0] != 0 ? ") " : "",
|
ctx->host[0] != 0 ? ctx->host : "", ctx->host[0] != 0 ? ") " : "",
|
||||||
ctx->cc[0] != 0 ? ctx->cc : "N/A");
|
ctx->cc[0] != 0 ? ctx->cc : "N/A");
|
||||||
|
|
||||||
client_timeout.tv_sec = CLIENT_TIMEOUT;
|
struct timeval client_timeout = {.tv_sec = CLIENT_TIMEOUT, .tv_usec = 0};
|
||||||
client_timeout.tv_usec = 0;
|
if (setsockopt(client->socket, SOL_SOCKET, SO_RCVTIMEO, &client_timeout, sizeof(client_timeout)) == -1 ||
|
||||||
if (setsockopt(client->socket, SOL_SOCKET, SO_RCVTIMEO, &client_timeout, sizeof(client_timeout)) < 0)
|
setsockopt(client->socket, SOL_SOCKET, SO_SNDTIMEO, &client_timeout, sizeof(client_timeout)) == -1)
|
||||||
goto set_timeout_err;
|
{
|
||||||
if (setsockopt(client->socket, SOL_SOCKET, SO_SNDTIMEO, &client_timeout, sizeof(client_timeout)) < 0) {
|
|
||||||
set_timeout_err:
|
|
||||||
error("Unable to set timeout for socket");
|
error("Unable to set timeout for socket");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@@ -827,9 +782,10 @@ int client_connection_handler(client_ctx_t *ctx, sock *client, unsigned long cli
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
req_num = 0;
|
int req_num = 0;
|
||||||
ret = 0;
|
ctx->s_keep_alive = 1;
|
||||||
while (ret == 0 && server_keep_alive && req_num < REQ_PER_CONNECTION) {
|
ctx->c_keep_alive = 1;
|
||||||
|
while (ctx->c_keep_alive && ctx->s_keep_alive && req_num < REQ_PER_CONNECTION) {
|
||||||
ret = client_request_handler(ctx, client, client_num, req_num++, log_client_prefix);
|
ret = client_request_handler(ctx, client, client_num, req_num++, log_client_prefix);
|
||||||
logger_set_prefix(log_conn_prefix);
|
logger_set_prefix(log_conn_prefix);
|
||||||
}
|
}
|
||||||
@@ -837,9 +793,9 @@ int client_connection_handler(client_ctx_t *ctx, sock *client, unsigned long cli
|
|||||||
close:
|
close:
|
||||||
sock_close(client);
|
sock_close(client);
|
||||||
|
|
||||||
if (rev_proxy.socket != 0) {
|
if (proxy.socket != 0) {
|
||||||
info(BLUE_STR "Closing proxy connection");
|
info(BLUE_STR "Closing proxy connection");
|
||||||
sock_close(&rev_proxy);
|
sock_close(&proxy);
|
||||||
}
|
}
|
||||||
|
|
||||||
clock_gettime(CLOCK_MONOTONIC, &end);
|
clock_gettime(CLOCK_MONOTONIC, &end);
|
||||||
@@ -849,19 +805,19 @@ int client_connection_handler(client_ctx_t *ctx, sock *client, unsigned long cli
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int client_handler(sock *client, unsigned long client_num, struct sockaddr_in6 *client_addr) {
|
void *client_handler(sock *client) {
|
||||||
struct sockaddr_in6 *server_addr;
|
struct sockaddr_in6 *server_addr;
|
||||||
struct sockaddr_storage server_addr_storage;
|
struct sockaddr_storage server_addr_storage;
|
||||||
|
|
||||||
client_ctx_t ctx;
|
client_ctx_t ctx;
|
||||||
char log_client_prefix[256], log_conn_prefix[512];
|
char log_client_prefix[256], log_conn_prefix[512];
|
||||||
|
|
||||||
char *color_table[] = {"\x1B[31m", "\x1B[32m", "\x1B[33m", "\x1B[34m", "\x1B[35m", "\x1B[36m"};
|
logger_set_name("client");
|
||||||
|
|
||||||
signal(SIGINT, client_terminate);
|
//signal(SIGINT, client_terminate);
|
||||||
signal(SIGTERM, client_terminate);
|
//signal(SIGTERM, client_terminate);
|
||||||
|
|
||||||
inet_ntop(client_addr->sin6_family, (void *) &client_addr->sin6_addr, ctx._c_addr, sizeof(ctx._c_addr));
|
inet_ntop(client->addr.ipv6.sin6_family, &client->addr.ipv6.sin6_addr, ctx._c_addr, sizeof(ctx._c_addr));
|
||||||
if (strncmp(ctx._c_addr, "::ffff:", 7) == 0) {
|
if (strncmp(ctx._c_addr, "::ffff:", 7) == 0) {
|
||||||
ctx.addr = ctx._c_addr + 7;
|
ctx.addr = ctx._c_addr + 7;
|
||||||
} else {
|
} else {
|
||||||
@@ -879,13 +835,15 @@ int client_handler(sock *client, unsigned long client_num, struct sockaddr_in6 *
|
|||||||
}
|
}
|
||||||
|
|
||||||
sprintf(log_client_prefix, "[%s%4i%s]%s[%*s][%5i]%s", (int) client->enc ? HTTPS_STR : HTTP_STR,
|
sprintf(log_client_prefix, "[%s%4i%s]%s[%*s][%5i]%s", (int) client->enc ? HTTPS_STR : HTTP_STR,
|
||||||
ntohs(server_addr->sin6_port), CLR_STR, color_table[client_num % 6], INET6_ADDRSTRLEN, ctx.addr,
|
ntohs(server_addr->sin6_port), CLR_STR, color_table[0], INET6_ADDRSTRLEN, ctx.addr,
|
||||||
ntohs(client_addr->sin6_port), CLR_STR);
|
ntohs(client->addr.ipv6.sin6_port), CLR_STR);
|
||||||
|
|
||||||
sprintf(log_conn_prefix, "[%6i][%*s]%s", getpid(), INET6_ADDRSTRLEN, ctx.s_addr, log_client_prefix);
|
sprintf(log_conn_prefix, "[%*s]%s", INET6_ADDRSTRLEN, ctx.s_addr, log_client_prefix);
|
||||||
logger_set_prefix(log_conn_prefix);
|
logger_set_prefix(log_conn_prefix);
|
||||||
|
|
||||||
info("Started child process with PID %i", getpid());
|
info("Started thread");
|
||||||
|
|
||||||
return client_connection_handler(&ctx, client, client_num, log_conn_prefix, log_client_prefix);
|
client_connection_handler(&ctx, client, 0, log_conn_prefix, log_client_prefix);
|
||||||
|
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
@@ -18,15 +18,17 @@
|
|||||||
typedef struct {
|
typedef struct {
|
||||||
char *addr;
|
char *addr;
|
||||||
char *s_addr;
|
char *s_addr;
|
||||||
|
unsigned char s_keep_alive:1;
|
||||||
|
unsigned char c_keep_alive:1;
|
||||||
char cc[3];
|
char cc[3];
|
||||||
char host[256];
|
char host[256];
|
||||||
char geoip[GEOIP_MAX_SIZE + 1];
|
char geoip[GEOIP_MAX_JSON_SIZE + 1];
|
||||||
char _c_addr[INET6_ADDRSTRLEN + 1];
|
char _c_addr[INET6_ADDRSTRLEN + 1];
|
||||||
char _s_addr[INET6_ADDRSTRLEN + 1];
|
char _s_addr[INET6_ADDRSTRLEN + 1];
|
||||||
} client_ctx_t;
|
} client_ctx_t;
|
||||||
|
|
||||||
host_config *get_host_config(const char *host);
|
host_config_t *get_host_config(const char *host);
|
||||||
|
|
||||||
int client_handler(sock *client, unsigned long client_num, struct sockaddr_in6 *client_addr);
|
void *client_handler(sock *client);
|
||||||
|
|
||||||
#endif //SESIMOS_CLIENT_H
|
#endif //SESIMOS_CLIENT_H
|
||||||
|
475
src/lib/cache.c
475
src/lib/cache.c
@@ -6,125 +6,121 @@
|
|||||||
* @date 2020-12-19
|
* @date 2020-12-19
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include "../server.h"
|
||||||
#include "../logger.h"
|
#include "../logger.h"
|
||||||
#include "cache.h"
|
#include "cache.h"
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
#include "compress.h"
|
#include "compress.h"
|
||||||
|
#include "config.h"
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <magic.h>
|
#include <magic.h>
|
||||||
#include <sys/ipc.h>
|
|
||||||
#include <sys/shm.h>
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <openssl/evp.h>
|
#include <openssl/evp.h>
|
||||||
|
#include <sys/mman.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <semaphore.h>
|
||||||
|
|
||||||
|
#define CACHE_BUF_SIZE 16
|
||||||
|
|
||||||
|
|
||||||
int cache_continue = 1;
|
|
||||||
magic_t magic;
|
magic_t magic;
|
||||||
cache_entry *cache;
|
|
||||||
|
|
||||||
int magic_init(void) {
|
static pthread_t thread;
|
||||||
magic = magic_open(MAGIC_MIME);
|
static sem_t sem_free, sem_used, sem_lock;
|
||||||
if (magic == NULL) {
|
|
||||||
|
typedef struct {
|
||||||
|
int rd;
|
||||||
|
int wr;
|
||||||
|
cache_entry_t *msgs[CACHE_BUF_SIZE];
|
||||||
|
} buf_t;
|
||||||
|
|
||||||
|
static buf_t buffer;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
static int magic_init(void) {
|
||||||
|
if ((magic = magic_open(MAGIC_MIME)) == NULL) {
|
||||||
critical("Unable to open magic cookie");
|
critical("Unable to open magic cookie");
|
||||||
return -1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (magic_load(magic, CACHE_MAGIC_FILE) != 0) {
|
if (magic_load(magic, CACHE_MAGIC_FILE) != 0) {
|
||||||
critical("Unable to load magic cookie: %s", magic_error(magic));
|
critical("Unable to load magic cookie: %s", magic_error(magic));
|
||||||
return -2;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void cache_process_term(int _) {
|
static void cache_free(void) {
|
||||||
cache_continue = 0;
|
for (int i = 0; i < CONFIG_MAX_HOST_CONFIG; i++) {
|
||||||
|
host_config_t *hc = &config.hosts[i];
|
||||||
|
if (hc->type == CONFIG_TYPE_UNSET) break;
|
||||||
|
if (hc->type != CONFIG_TYPE_LOCAL) continue;
|
||||||
|
|
||||||
|
munmap(hc->cache, sizeof(cache_t));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int cache_process(void) {
|
static cache_entry_t *cache_get_entry(cache_t *cache, const char *filename) {
|
||||||
errno = 0;
|
// search entry
|
||||||
signal(SIGINT, cache_process_term);
|
cache_entry_t *entry;
|
||||||
signal(SIGTERM, cache_process_term);
|
|
||||||
|
|
||||||
logger_set_name("cache");
|
|
||||||
|
|
||||||
int shm_id = shmget(CACHE_SHM_KEY, CACHE_ENTRIES * sizeof(cache_entry), 0);
|
|
||||||
if (shm_id < 0) {
|
|
||||||
critical("Unable to create cache shared memory");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
shmdt(cache);
|
|
||||||
errno = 0;
|
|
||||||
void *shm_rw = shmat(shm_id, NULL, 0);
|
|
||||||
if (shm_rw == (void *) -1) {
|
|
||||||
critical("Unable to attach cache shared memory (rw)");
|
|
||||||
return -2;
|
|
||||||
}
|
|
||||||
cache = shm_rw;
|
|
||||||
|
|
||||||
if (mkdir("/var/sesimos/", 0755) < 0 && errno != EEXIST) {
|
|
||||||
critical("Unable to create directory '/var/sesimos/'");
|
|
||||||
return -3;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (mkdir("/var/sesimos/server/", 0755) < 0 && errno != EEXIST) {
|
|
||||||
critical("Unable to create directory '/var/sesimos/server/'");
|
|
||||||
return -3;
|
|
||||||
}
|
|
||||||
|
|
||||||
FILE *cache_file = fopen("/var/sesimos/server/cache", "rb");
|
|
||||||
if (cache_file != NULL) {
|
|
||||||
fread(cache, sizeof(cache_entry), CACHE_ENTRIES, cache_file);
|
|
||||||
fclose(cache_file);
|
|
||||||
}
|
|
||||||
|
|
||||||
errno = 0;
|
|
||||||
|
|
||||||
for (int i = 0; i < CACHE_ENTRIES; i++) {
|
for (int i = 0; i < CACHE_ENTRIES; i++) {
|
||||||
cache[i].is_updating = 0;
|
entry = &cache->entries[i];
|
||||||
|
if (entry->filename[0] == 0) break;
|
||||||
|
if (strcmp(entry->filename, filename) == 0) {
|
||||||
|
// found
|
||||||
|
return entry;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
FILE *file;
|
// not found
|
||||||
char buf[CACHE_BUF_SIZE], comp_buf[CACHE_BUF_SIZE], filename_comp_gz[256], filename_comp_br[256];
|
return NULL;
|
||||||
unsigned long read;
|
}
|
||||||
int compress;
|
|
||||||
EVP_MD_CTX *ctx;
|
|
||||||
unsigned char hash[EVP_MAX_MD_SIZE];
|
|
||||||
unsigned int md_len;
|
|
||||||
int cache_changed = 0;
|
|
||||||
int p_len_gz, p_len_br;
|
|
||||||
int ret;
|
|
||||||
while (cache_continue) {
|
|
||||||
for (int i = 0; i < CACHE_ENTRIES; i++) {
|
|
||||||
if (cache[i].filename[0] != 0 && cache[i].meta.etag[0] == 0 && !cache[i].is_updating) {
|
|
||||||
cache[i].is_updating = 1;
|
|
||||||
info("Hashing file %s", cache[i].filename);
|
|
||||||
|
|
||||||
ctx = EVP_MD_CTX_new();
|
static cache_entry_t *cache_get_new_entry(cache_t *cache) {
|
||||||
|
// search empty slot
|
||||||
|
cache_entry_t *entry;
|
||||||
|
for (int i = 0; i < CACHE_ENTRIES; i++) {
|
||||||
|
entry = &cache->entries[i];
|
||||||
|
if (entry->filename[0] == 0)
|
||||||
|
return entry;
|
||||||
|
}
|
||||||
|
|
||||||
|
// not found
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void cache_process_entry(cache_entry_t *entry) {
|
||||||
|
char buf[16384], comp_buf[16384], filename_comp_gz[256], filename_comp_br[256];
|
||||||
|
|
||||||
|
info("Hashing file %s", entry->filename);
|
||||||
|
|
||||||
|
EVP_MD_CTX *ctx = EVP_MD_CTX_new();
|
||||||
EVP_DigestInit(ctx, EVP_sha1());
|
EVP_DigestInit(ctx, EVP_sha1());
|
||||||
file = fopen(cache[i].filename, "rb");
|
FILE *file = fopen(entry->filename, "rb");
|
||||||
compress = mime_is_compressible(cache[i].meta.type);
|
int compress = mime_is_compressible(entry->meta.type);
|
||||||
|
|
||||||
compress_ctx comp_ctx;
|
compress_ctx comp_ctx;
|
||||||
FILE *comp_file_gz = NULL;
|
FILE *comp_file_gz = NULL, *comp_file_br = NULL;
|
||||||
FILE *comp_file_br = NULL;
|
|
||||||
if (compress) {
|
if (compress) {
|
||||||
sprintf(buf, "%.*s/.sesimos", cache[i].webroot_len, cache[i].filename);
|
sprintf(buf, "%.*s/.sesimos", entry->webroot_len, entry->filename);
|
||||||
if (mkdir(buf, 0755) != 0 && errno != EEXIST) {
|
if (mkdir(buf, 0755) != 0 && errno != EEXIST) {
|
||||||
error("Unable to create directory %s", buf);
|
error("Unable to create directory %s", buf);
|
||||||
goto comp_err;
|
goto comp_err;
|
||||||
}
|
}
|
||||||
|
|
||||||
sprintf(buf, "%.*s/.sesimos/cache", cache[i].webroot_len, cache[i].filename);
|
sprintf(buf, "%.*s/.sesimos/cache", entry->webroot_len, entry->filename);
|
||||||
if (mkdir(buf, 0700) != 0 && errno != EEXIST) {
|
if (mkdir(buf, 0700) != 0 && errno != EEXIST) {
|
||||||
error("Unable to create directory %s", buf);
|
error("Unable to create directory %s", buf);
|
||||||
goto comp_err;
|
goto comp_err;
|
||||||
}
|
}
|
||||||
errno = 0;
|
errno = 0;
|
||||||
|
|
||||||
char *rel_path = cache[i].filename + cache[i].webroot_len + 1;
|
char *rel_path = entry->filename + entry->webroot_len + 1;
|
||||||
for (int j = 0; j < strlen(rel_path); j++) {
|
for (int j = 0; j < strlen(rel_path); j++) {
|
||||||
char ch = rel_path[j];
|
char ch = rel_path[j];
|
||||||
if (ch == '/') ch = '_';
|
if (ch == '/') ch = '_';
|
||||||
@@ -132,18 +128,18 @@ int cache_process(void) {
|
|||||||
}
|
}
|
||||||
buf[strlen(rel_path)] = 0;
|
buf[strlen(rel_path)] = 0;
|
||||||
|
|
||||||
p_len_gz = snprintf(filename_comp_gz, sizeof(filename_comp_gz),
|
int p_len_gz = snprintf(filename_comp_gz, sizeof(filename_comp_gz),
|
||||||
"%.*s/.sesimos/cache/%s.gz",
|
"%.*s/.sesimos/cache/%s.gz",
|
||||||
cache[i].webroot_len, cache[i].filename, buf);
|
entry->webroot_len, entry->filename, buf);
|
||||||
p_len_br = snprintf(filename_comp_br, sizeof(filename_comp_br),
|
int p_len_br = snprintf(filename_comp_br, sizeof(filename_comp_br),
|
||||||
"%.*s/.sesimos/cache/%s.br",
|
"%.*s/.sesimos/cache/%s.br",
|
||||||
cache[i].webroot_len, cache[i].filename, buf);
|
entry->webroot_len, entry->filename, buf);
|
||||||
if (p_len_gz < 0 || p_len_gz >= sizeof(filename_comp_gz) || p_len_br < 0 || p_len_br >= sizeof(filename_comp_br)) {
|
if (p_len_gz < 0 || p_len_gz >= sizeof(filename_comp_gz) || p_len_br < 0 || p_len_br >= sizeof(filename_comp_br)) {
|
||||||
error("Unable to open cached file: File name for compressed file too long");
|
error("Unable to open cached file: File name for compressed file too long");
|
||||||
goto comp_err;
|
goto comp_err;
|
||||||
}
|
}
|
||||||
|
|
||||||
info("Compressing file %s", cache[i].filename);
|
info("Compressing file %s", entry->filename);
|
||||||
|
|
||||||
comp_file_gz = fopen(filename_comp_gz, "wb");
|
comp_file_gz = fopen(filename_comp_gz, "wb");
|
||||||
comp_file_br = fopen(filename_comp_br, "wb");
|
comp_file_br = fopen(filename_comp_br, "wb");
|
||||||
@@ -152,8 +148,7 @@ int cache_process(void) {
|
|||||||
comp_err:
|
comp_err:
|
||||||
compress = 0;
|
compress = 0;
|
||||||
} else {
|
} else {
|
||||||
ret = compress_init(&comp_ctx, COMPRESS_GZ | COMPRESS_BR);
|
if ((compress_init(&comp_ctx, COMPRESS_GZ | COMPRESS_BR)) != 0) {
|
||||||
if (ret != 0) {
|
|
||||||
error("Unable to init compression");
|
error("Unable to init compression");
|
||||||
compress = 0;
|
compress = 0;
|
||||||
fclose(comp_file_gz);
|
fclose(comp_file_gz);
|
||||||
@@ -162,22 +157,23 @@ int cache_process(void) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
while ((read = fread(buf, 1, CACHE_BUF_SIZE, file)) > 0) {
|
unsigned long read;
|
||||||
|
while ((read = fread(buf, 1, sizeof(buf), file)) > 0) {
|
||||||
EVP_DigestUpdate(ctx, buf, read);
|
EVP_DigestUpdate(ctx, buf, read);
|
||||||
if (compress) {
|
if (compress) {
|
||||||
unsigned long avail_in, avail_out;
|
unsigned long avail_in, avail_out;
|
||||||
avail_in = read;
|
avail_in = read;
|
||||||
do {
|
do {
|
||||||
avail_out = CACHE_BUF_SIZE;
|
avail_out = sizeof(comp_buf);
|
||||||
compress_compress_mode(&comp_ctx, COMPRESS_GZ,buf + read - avail_in, &avail_in, comp_buf, &avail_out, feof(file));
|
compress_compress_mode(&comp_ctx, COMPRESS_GZ, buf + read - avail_in, &avail_in, comp_buf, &avail_out, feof(file));
|
||||||
fwrite(comp_buf, 1, CACHE_BUF_SIZE - avail_out, comp_file_gz);
|
fwrite(comp_buf, 1, sizeof(comp_buf) - avail_out, comp_file_gz);
|
||||||
} while (avail_in != 0 || avail_out != CACHE_BUF_SIZE);
|
} while (avail_in != 0 || avail_out != sizeof(comp_buf));
|
||||||
avail_in = read;
|
avail_in = read;
|
||||||
do {
|
do {
|
||||||
avail_out = CACHE_BUF_SIZE;
|
avail_out = sizeof(comp_buf);
|
||||||
compress_compress_mode(&comp_ctx, COMPRESS_BR, buf + read - avail_in, &avail_in, comp_buf, &avail_out, feof(file));
|
compress_compress_mode(&comp_ctx, COMPRESS_BR, buf + read - avail_in, &avail_in, comp_buf, &avail_out, feof(file));
|
||||||
fwrite(comp_buf, 1, CACHE_BUF_SIZE - avail_out, comp_file_br);
|
fwrite(comp_buf, 1, sizeof(comp_buf) - avail_out, comp_file_br);
|
||||||
} while (avail_in != 0 || avail_out != CACHE_BUF_SIZE);
|
} while (avail_in != 0 || avail_out != sizeof(comp_buf));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -185,129 +181,159 @@ int cache_process(void) {
|
|||||||
compress_free(&comp_ctx);
|
compress_free(&comp_ctx);
|
||||||
fclose(comp_file_gz);
|
fclose(comp_file_gz);
|
||||||
fclose(comp_file_br);
|
fclose(comp_file_br);
|
||||||
info("Finished compressing file %s", cache[i].filename);
|
info("Finished compressing file %s", entry->filename);
|
||||||
strcpy(cache[i].meta.filename_comp_gz, filename_comp_gz);
|
strcpy(entry->meta.filename_comp_gz, filename_comp_gz);
|
||||||
strcpy(cache[i].meta.filename_comp_br, filename_comp_br);
|
strcpy(entry->meta.filename_comp_br, filename_comp_br);
|
||||||
} else {
|
} else {
|
||||||
memset(cache[i].meta.filename_comp_gz, 0, sizeof(cache[i].meta.filename_comp_gz));
|
memset(entry->meta.filename_comp_gz, 0, sizeof(entry->meta.filename_comp_gz));
|
||||||
memset(cache[i].meta.filename_comp_br, 0, sizeof(cache[i].meta.filename_comp_br));
|
memset(entry->meta.filename_comp_br, 0, sizeof(entry->meta.filename_comp_br));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unsigned int md_len;
|
||||||
|
unsigned char hash[EVP_MAX_MD_SIZE];
|
||||||
EVP_DigestFinal(ctx, hash, &md_len);
|
EVP_DigestFinal(ctx, hash, &md_len);
|
||||||
EVP_MD_CTX_free(ctx);
|
EVP_MD_CTX_free(ctx);
|
||||||
|
|
||||||
memset(cache[i].meta.etag, 0, sizeof(cache[i].meta.etag));
|
memset(entry->meta.etag, 0, sizeof(entry->meta.etag));
|
||||||
for (int j = 0; j < md_len; j++) {
|
for (int j = 0; j < md_len; j++) {
|
||||||
sprintf(cache[i].meta.etag + j * 2, "%02x", hash[j]);
|
sprintf(entry->meta.etag + j * 2, "%02x", hash[j]);
|
||||||
}
|
}
|
||||||
fclose(file);
|
fclose(file);
|
||||||
info("Finished hashing file %s", cache[i].filename);
|
entry->flags &= !CACHE_DIRTY;
|
||||||
cache[i].is_updating = 0;
|
|
||||||
cache_changed = 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (cache_changed) {
|
info("Finished hashing file %s", entry->filename);
|
||||||
cache_changed = 0;
|
}
|
||||||
cache_file = fopen("/var/sesimos/server/cache", "wb");
|
|
||||||
if (cache_file == NULL) {
|
static void *cache_thread(void *arg) {
|
||||||
critical("Unable to open cache file");
|
logger_set_name("cache");
|
||||||
return -1;
|
|
||||||
}
|
while (alive) {
|
||||||
fwrite(cache, sizeof(cache_entry), CACHE_ENTRIES, cache_file);
|
pthread_testcancel();
|
||||||
fclose(cache_file);
|
if (sem_wait(&sem_used) != 0) {
|
||||||
|
if (errno == EINTR) {
|
||||||
|
continue;
|
||||||
} else {
|
} else {
|
||||||
sleep(1);
|
error("Unable to lock semaphore");
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
cache_entry_t *entry = buffer.msgs[buffer.wr];
|
||||||
|
buffer.wr = (buffer.wr + 1) % CACHE_BUF_SIZE;
|
||||||
|
|
||||||
|
cache_process_entry(entry);
|
||||||
|
|
||||||
|
// unlock slot in buffer
|
||||||
|
sem_post(&sem_free);
|
||||||
|
}
|
||||||
|
|
||||||
|
cache_free();
|
||||||
|
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
int cache_init(void) {
|
int cache_init(void) {
|
||||||
errno = 0;
|
char buf[512];
|
||||||
if (magic_init() != 0) {
|
int ret, fd;
|
||||||
|
if ((ret = magic_init()) != 0)
|
||||||
|
return ret;
|
||||||
|
|
||||||
|
for (int i = 0; i < CONFIG_MAX_HOST_CONFIG; i++) {
|
||||||
|
host_config_t *hc = &config.hosts[i];
|
||||||
|
if (hc->type == CONFIG_TYPE_UNSET) break;
|
||||||
|
if (hc->type != CONFIG_TYPE_LOCAL) continue;
|
||||||
|
|
||||||
|
sprintf(buf, "%s/.sesimos/metadata", hc->local.webroot);
|
||||||
|
if ((fd = open(buf, O_CREAT | O_RDWR, 0600)) == -1) {
|
||||||
|
critical("Unable to open file %s", buf);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ftruncate(fd, sizeof(cache_t)) == -1) {
|
||||||
|
critical("Unable to truncate file %s", buf);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((hc->cache = mmap(NULL, sizeof(cache_t), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) == NULL) {
|
||||||
|
critical("Unable to map file %s", buf);
|
||||||
|
close(fd);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
close(fd);
|
||||||
|
}
|
||||||
|
|
||||||
|
// try to initialize all three semaphores
|
||||||
|
if (sem_init(&sem_lock, 0, 1) != 0|| sem_init(&sem_free, 0, 1) != 0 || sem_init(&sem_used, 0, 0) != 0) {
|
||||||
|
critical("Unable to initialize semaphore");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int shm_id = shmget(CACHE_SHM_KEY, CACHE_ENTRIES * sizeof(cache_entry), IPC_CREAT | IPC_EXCL | 0600);
|
// initialize read/write heads
|
||||||
if (shm_id < 0) {
|
buffer.rd = 0;
|
||||||
critical("Unable to create cache shared memory");
|
buffer.wr = 0;
|
||||||
return -2;
|
|
||||||
}
|
|
||||||
|
|
||||||
void *shm = shmat(shm_id, NULL, SHM_RDONLY);
|
pthread_create(&thread, NULL, cache_thread, NULL);
|
||||||
if (shm == (void *) -1) {
|
|
||||||
critical("Unable to attach cache shared memory (ro)");
|
|
||||||
return -3;
|
|
||||||
}
|
|
||||||
cache = shm;
|
|
||||||
|
|
||||||
void *shm_rw = shmat(shm_id, NULL, 0);
|
|
||||||
if (shm_rw == (void *) -1) {
|
|
||||||
critical("Unable to attach cache shared memory (rw)");
|
|
||||||
return -4;
|
|
||||||
}
|
|
||||||
cache = shm_rw;
|
|
||||||
memset(cache, 0, CACHE_ENTRIES * sizeof(cache_entry));
|
|
||||||
shmdt(shm_rw);
|
|
||||||
cache = shm;
|
|
||||||
errno = 0;
|
|
||||||
|
|
||||||
pid_t pid = fork();
|
|
||||||
if (pid == 0) {
|
|
||||||
// child
|
|
||||||
if (cache_process() == 0) {
|
|
||||||
return 0;
|
|
||||||
} else {
|
|
||||||
return -6;
|
|
||||||
}
|
|
||||||
} else if (pid > 0) {
|
|
||||||
// parent
|
|
||||||
info("Started child process with PID %i as cache-updater", pid);
|
|
||||||
return pid;
|
|
||||||
} else {
|
|
||||||
critical("Unable to create child process");
|
|
||||||
return -5;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int cache_unload(void) {
|
|
||||||
int shm_id = shmget(CACHE_SHM_KEY, 0, 0);
|
|
||||||
if (shm_id < 0) {
|
|
||||||
critical("Unable to get cache shared memory id");
|
|
||||||
shmdt(cache);
|
|
||||||
return -1;
|
|
||||||
} else if (shmctl(shm_id, IPC_RMID, NULL) < 0) {
|
|
||||||
critical("Unable to configure cache shared memory");
|
|
||||||
shmdt(cache);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
shmdt(cache);
|
|
||||||
errno = 0;
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int cache_update_entry(int entry_num, const char *filename, const char *webroot) {
|
int cache_join(void) {
|
||||||
void *cache_ro = cache;
|
return pthread_join(thread, NULL);
|
||||||
int shm_id = shmget(CACHE_SHM_KEY, 0, 0);
|
}
|
||||||
void *shm_rw = shmat(shm_id, NULL, 0);
|
|
||||||
if (shm_rw == (void *) -1) {
|
|
||||||
error("Unable to attach cache shared memory (rw)");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
cache = shm_rw;
|
|
||||||
|
|
||||||
|
static void cache_mark_entry_dirty(cache_entry_t *entry) {
|
||||||
|
if (entry->flags & CACHE_DIRTY)
|
||||||
|
return;
|
||||||
|
|
||||||
|
memset(entry->meta.etag, 0, sizeof(entry->meta.etag));
|
||||||
|
memset(entry->meta.filename_comp_gz, 0, sizeof(entry->meta.filename_comp_gz));
|
||||||
|
memset(entry->meta.filename_comp_br, 0, sizeof(entry->meta.filename_comp_br));
|
||||||
|
entry->flags |= CACHE_DIRTY;
|
||||||
|
|
||||||
|
try_again_free:
|
||||||
|
if (sem_wait(&sem_free) != 0) {
|
||||||
|
if (errno == EINTR) {
|
||||||
|
goto try_again_free;
|
||||||
|
} else {
|
||||||
|
error("Unable to lock semaphore");
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// try to lock buffer
|
||||||
|
try_again_lock:
|
||||||
|
if (sem_wait(&sem_lock) != 0) {
|
||||||
|
if (errno == EINTR) {
|
||||||
|
goto try_again_lock;
|
||||||
|
} else {
|
||||||
|
error("Unable to lock semaphore");
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// write to buffer
|
||||||
|
buffer.msgs[buffer.rd] = entry;
|
||||||
|
buffer.rd = (buffer.rd + 1) % CACHE_BUF_SIZE;
|
||||||
|
|
||||||
|
// unlock buffer
|
||||||
|
sem_post(&sem_lock);
|
||||||
|
|
||||||
|
// unlock slot in buffer for logger
|
||||||
|
sem_post(&sem_used);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int cache_update_entry(cache_entry_t *entry, const char *filename, const char *webroot) {
|
||||||
struct stat statbuf;
|
struct stat statbuf;
|
||||||
stat(filename, &statbuf);
|
stat(filename, &statbuf);
|
||||||
memcpy(&cache[entry_num].meta.stat, &statbuf, sizeof(statbuf));
|
memcpy(&entry->meta.stat, &statbuf, sizeof(statbuf));
|
||||||
|
|
||||||
cache[entry_num].webroot_len = (unsigned char) strlen(webroot);
|
entry->webroot_len = (unsigned char) strlen(webroot);
|
||||||
strcpy(cache[entry_num].filename, filename);
|
strcpy(entry->filename, filename);
|
||||||
|
|
||||||
magic_setflags(magic, MAGIC_MIME_TYPE);
|
magic_setflags(magic, MAGIC_MIME_TYPE);
|
||||||
const char *type = magic_file(magic, filename);
|
const char *type = magic_file(magic, filename);
|
||||||
char type_new[24];
|
char type_new[URI_TYPE_SIZE];
|
||||||
sprintf(type_new, "%s", type);
|
sprintf(type_new, "%s", type);
|
||||||
if (strncmp(type, "text/", 5) == 0) {
|
if (strncmp(type, "text/", 5) == 0) {
|
||||||
if (strcmp(filename + strlen(filename) - 4, ".css") == 0) {
|
if (strcmp(filename + strlen(filename) - 4, ".css") == 0) {
|
||||||
@@ -316,87 +342,48 @@ int cache_update_entry(int entry_num, const char *filename, const char *webroot)
|
|||||||
sprintf(type_new, "application/javascript");
|
sprintf(type_new, "application/javascript");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
strcpy(cache[entry_num].meta.type, type_new);
|
strcpy(entry->meta.type, type_new);
|
||||||
|
|
||||||
magic_setflags(magic, MAGIC_MIME_ENCODING);
|
magic_setflags(magic, MAGIC_MIME_ENCODING);
|
||||||
strcpy(cache[entry_num].meta.charset, magic_file(magic, filename));
|
strcpy(entry->meta.charset, magic_file(magic, filename));
|
||||||
|
|
||||||
memset(cache[entry_num].meta.etag, 0, sizeof(cache[entry_num].meta.etag));
|
cache_mark_entry_dirty(entry);
|
||||||
memset(cache[entry_num].meta.filename_comp_gz, 0, sizeof(cache[entry_num].meta.filename_comp_gz));
|
|
||||||
memset(cache[entry_num].meta.filename_comp_br, 0, sizeof(cache[entry_num].meta.filename_comp_br));
|
|
||||||
cache[entry_num].is_updating = 0;
|
|
||||||
|
|
||||||
shmdt(shm_rw);
|
|
||||||
cache = cache_ro;
|
|
||||||
errno = 0;
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int cache_filename_comp_invalid(const char *filename) {
|
void cache_mark_dirty(cache_t *cache, const char *filename) {
|
||||||
void *cache_ro = cache;
|
cache_entry_t *entry = cache_get_entry(cache, filename);
|
||||||
int shm_id = shmget(CACHE_SHM_KEY, 0, 0);
|
if (entry) cache_mark_entry_dirty(entry);
|
||||||
void *shm_rw = shmat(shm_id, NULL, 0);
|
|
||||||
if (shm_rw == (void *) -1) {
|
|
||||||
error("Unable to attach cache shared memory (rw)");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
cache = shm_rw;
|
|
||||||
|
|
||||||
int i;
|
|
||||||
for (i = 0; i < CACHE_ENTRIES; i++) {
|
|
||||||
if (cache[i].filename[0] != 0 && strlen(cache[i].filename) == strlen(filename) &&
|
|
||||||
strcmp(cache[i].filename, filename) == 0) {
|
|
||||||
if (cache[i].is_updating) {
|
|
||||||
return 0;
|
|
||||||
} else {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
memset(cache[i].meta.etag, 0, sizeof(cache[i].meta.etag));
|
|
||||||
memset(cache[i].meta.filename_comp_gz, 0, sizeof(cache[i].meta.filename_comp_gz));
|
|
||||||
memset(cache[i].meta.filename_comp_br, 0, sizeof(cache[i].meta.filename_comp_br));
|
|
||||||
cache[i].is_updating = 0;
|
|
||||||
|
|
||||||
shmdt(shm_rw);
|
|
||||||
cache = cache_ro;
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int uri_cache_init(http_uri *uri) {
|
int cache_init_uri(cache_t *cache, http_uri *uri) {
|
||||||
if (uri->filename == NULL) {
|
if (uri->filename == NULL)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
|
||||||
|
|
||||||
int i;
|
cache_entry_t *entry = cache_get_entry(cache, uri->filename);
|
||||||
for (i = 0; i < CACHE_ENTRIES; i++) {
|
if (entry == NULL) {
|
||||||
if (cache[i].filename[0] != 0 && strlen(cache[i].filename) == strlen(uri->filename) &&
|
// no entry found -> create new entry
|
||||||
strcmp(cache[i].filename, uri->filename) == 0) {
|
entry = cache_get_new_entry(cache);
|
||||||
uri->meta = &cache[i].meta;
|
if (entry) {
|
||||||
if (cache[i].is_updating) {
|
if (cache_update_entry(entry, uri->filename, uri->webroot) != 0) {
|
||||||
return 0;
|
|
||||||
} else {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (uri->meta == NULL) {
|
|
||||||
for (i = 0; i < CACHE_ENTRIES; i++) {
|
|
||||||
if (cache[i].filename[0] == 0) {
|
|
||||||
if (cache_update_entry(i, uri->filename, uri->webroot) != 0) {
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
uri->meta = &cache[i].meta;
|
uri->meta = &entry->meta;
|
||||||
break;
|
} else {
|
||||||
}
|
warning("No empty cache entry slot found");
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
uri->meta = &entry->meta;
|
||||||
|
if (entry->flags & CACHE_DIRTY)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
// check, if file has changed
|
||||||
struct stat statbuf;
|
struct stat statbuf;
|
||||||
stat(uri->filename, &statbuf);
|
stat(uri->filename, &statbuf);
|
||||||
if (memcmp(&uri->meta->stat.st_mtime, &statbuf.st_mtime, sizeof(statbuf.st_mtime)) != 0) {
|
if (memcmp(&uri->meta->stat.st_mtime, &statbuf.st_mtime, sizeof(statbuf.st_mtime)) != 0) {
|
||||||
if (cache_update_entry(i, uri->filename, uri->webroot) != 0) {
|
// modify time has changed
|
||||||
|
if (cache_update_entry(entry, uri->filename, uri->webroot) != 0) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -11,9 +11,9 @@
|
|||||||
|
|
||||||
#include "uri.h"
|
#include "uri.h"
|
||||||
|
|
||||||
#define CACHE_SHM_KEY 255641
|
|
||||||
#define CACHE_ENTRIES 1024
|
#define CACHE_ENTRIES 1024
|
||||||
#define CACHE_BUF_SIZE 16384
|
|
||||||
|
#define CACHE_DIRTY 1
|
||||||
|
|
||||||
#ifndef CACHE_MAGIC_FILE
|
#ifndef CACHE_MAGIC_FILE
|
||||||
# define CACHE_MAGIC_FILE "/usr/share/file/misc/magic.mgc"
|
# define CACHE_MAGIC_FILE "/usr/share/file/misc/magic.mgc"
|
||||||
@@ -23,28 +23,22 @@
|
|||||||
typedef struct {
|
typedef struct {
|
||||||
char filename[256];
|
char filename[256];
|
||||||
unsigned char webroot_len;
|
unsigned char webroot_len;
|
||||||
unsigned char is_updating:1;
|
unsigned char flags;
|
||||||
meta_data meta;
|
metadata_t meta;
|
||||||
} cache_entry;
|
} cache_entry_t;
|
||||||
|
|
||||||
extern cache_entry *cache;
|
typedef struct {
|
||||||
|
char sig[6];
|
||||||
extern int cache_continue;
|
unsigned char ver;
|
||||||
|
cache_entry_t entries[CACHE_ENTRIES];
|
||||||
int magic_init(void);
|
} cache_t;
|
||||||
|
|
||||||
void cache_process_term(int _);
|
|
||||||
|
|
||||||
int cache_process(void);
|
|
||||||
|
|
||||||
int cache_init(void);
|
int cache_init(void);
|
||||||
|
|
||||||
int cache_unload(void);
|
int cache_join(void);
|
||||||
|
|
||||||
int cache_update_entry(int entry_num, const char *filename, const char *webroot);
|
void cache_mark_dirty(cache_t *cache, const char *filename);
|
||||||
|
|
||||||
int cache_filename_comp_invalid(const char *filename);
|
int cache_init_uri(cache_t *cache, http_uri *uri);
|
||||||
|
|
||||||
int uri_cache_init(http_uri *uri);
|
|
||||||
|
|
||||||
#endif //SESIMOS_CACHE_H
|
#endif //SESIMOS_CACHE_H
|
||||||
|
@@ -8,22 +8,19 @@
|
|||||||
|
|
||||||
#include "compress.h"
|
#include "compress.h"
|
||||||
|
|
||||||
#include <malloc.h>
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
|
||||||
|
|
||||||
int compress_init(compress_ctx *ctx, int mode) {
|
int compress_init(compress_ctx *ctx, int mode) {
|
||||||
ctx->gzip = NULL;
|
|
||||||
ctx->brotli = NULL;
|
ctx->brotli = NULL;
|
||||||
ctx->mode = 0;
|
ctx->mode = 0;
|
||||||
int ret;
|
int ret;
|
||||||
if (mode & COMPRESS_GZ) {
|
if (mode & COMPRESS_GZ) {
|
||||||
ctx->mode |= COMPRESS_GZ;
|
ctx->mode |= COMPRESS_GZ;
|
||||||
ctx->gzip = malloc(sizeof(z_stream));
|
ctx->gzip.zalloc = Z_NULL;
|
||||||
ctx->gzip->zalloc = Z_NULL;
|
ctx->gzip.zfree = Z_NULL;
|
||||||
ctx->gzip->zfree = Z_NULL;
|
ctx->gzip.opaque = Z_NULL;
|
||||||
ctx->gzip->opaque = Z_NULL;
|
ret = deflateInit2(&ctx->gzip, COMPRESS_LEVEL_GZIP, Z_DEFLATED, 15 + 16, 9, Z_DEFAULT_STRATEGY);
|
||||||
ret = deflateInit2(ctx->gzip, COMPRESS_LEVEL_GZIP, Z_DEFLATED, 15 + 16, 9, Z_DEFAULT_STRATEGY);
|
|
||||||
if (ret != Z_OK) return -1;
|
if (ret != Z_OK) return -1;
|
||||||
}
|
}
|
||||||
if (mode & COMPRESS_BR) {
|
if (mode & COMPRESS_BR) {
|
||||||
@@ -49,13 +46,13 @@ int compress_compress_mode(compress_ctx *ctx, int mode, const char *in, unsigned
|
|||||||
errno = EINVAL;
|
errno = EINVAL;
|
||||||
return -1;
|
return -1;
|
||||||
} else if (mode & COMPRESS_GZ) {
|
} else if (mode & COMPRESS_GZ) {
|
||||||
ctx->gzip->next_in = (unsigned char*) in;
|
ctx->gzip.next_in = (unsigned char*) in;
|
||||||
ctx->gzip->avail_in = *in_len;
|
ctx->gzip.avail_in = *in_len;
|
||||||
ctx->gzip->next_out = (unsigned char*) out;
|
ctx->gzip.next_out = (unsigned char*) out;
|
||||||
ctx->gzip->avail_out = *out_len;
|
ctx->gzip.avail_out = *out_len;
|
||||||
int ret = deflate(ctx->gzip, finish ? Z_FINISH : Z_NO_FLUSH);
|
int ret = deflate(&ctx->gzip, finish ? Z_FINISH : Z_NO_FLUSH);
|
||||||
*in_len = ctx->gzip->avail_in;
|
*in_len = ctx->gzip.avail_in;
|
||||||
*out_len = ctx->gzip->avail_out;
|
*out_len = ctx->gzip.avail_out;
|
||||||
return ret;
|
return ret;
|
||||||
} else if (mode & COMPRESS_BR) {
|
} else if (mode & COMPRESS_BR) {
|
||||||
int ret = BrotliEncoderCompressStream(ctx->brotli, finish ? BROTLI_OPERATION_FINISH : BROTLI_OPERATION_PROCESS,
|
int ret = BrotliEncoderCompressStream(ctx->brotli, finish ? BROTLI_OPERATION_FINISH : BROTLI_OPERATION_PROCESS,
|
||||||
@@ -68,11 +65,6 @@ int compress_compress_mode(compress_ctx *ctx, int mode, const char *in, unsigned
|
|||||||
}
|
}
|
||||||
|
|
||||||
int compress_free(compress_ctx *ctx) {
|
int compress_free(compress_ctx *ctx) {
|
||||||
if (ctx->gzip != NULL) {
|
|
||||||
deflateEnd(ctx->gzip);
|
|
||||||
free(ctx->gzip);
|
|
||||||
ctx->gzip = NULL;
|
|
||||||
}
|
|
||||||
if (ctx->brotli != NULL) {
|
if (ctx->brotli != NULL) {
|
||||||
BrotliEncoderDestroyInstance(ctx->brotli);
|
BrotliEncoderDestroyInstance(ctx->brotli);
|
||||||
ctx->brotli = NULL;
|
ctx->brotli = NULL;
|
||||||
|
@@ -21,17 +21,15 @@
|
|||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
int mode;
|
int mode;
|
||||||
z_stream *gzip;
|
z_stream gzip;
|
||||||
BrotliEncoderState *brotli;
|
BrotliEncoderState *brotli;
|
||||||
} compress_ctx;
|
} compress_ctx;
|
||||||
|
|
||||||
int compress_init(compress_ctx *ctx, int mode);
|
int compress_init(compress_ctx *ctx, int mode);
|
||||||
|
|
||||||
int compress_compress(compress_ctx *ctx, const char *in, unsigned long *in_len, char *out, unsigned long *out_len,
|
int compress_compress(compress_ctx *ctx, const char *in, unsigned long *in_len, char *out, unsigned long *out_len, int finish);
|
||||||
int finish);
|
|
||||||
|
|
||||||
int compress_compress_mode(compress_ctx *ctx, int mode, const char *in, unsigned long *in_len, char *out,
|
int compress_compress_mode(compress_ctx *ctx, int mode, const char *in, unsigned long *in_len, char *out, unsigned long *out_len, int finish);
|
||||||
unsigned long *out_len, int finish);
|
|
||||||
|
|
||||||
int compress_free(compress_ctx *ctx);
|
int compress_free(compress_ctx *ctx);
|
||||||
|
|
||||||
|
123
src/lib/config.c
123
src/lib/config.c
@@ -8,60 +8,14 @@
|
|||||||
|
|
||||||
#include "../logger.h"
|
#include "../logger.h"
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "utils.h"
|
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <sys/ipc.h>
|
|
||||||
#include <sys/shm.h>
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <errno.h>
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
config_t config;
|
||||||
t_config *config;
|
|
||||||
char geoip_dir[256], dns_server[256];
|
char geoip_dir[256], dns_server[256];
|
||||||
|
|
||||||
int config_init(void) {
|
|
||||||
int shm_id = shmget(CONFIG_SHM_KEY, sizeof(t_config), IPC_CREAT | IPC_EXCL | 0640);
|
|
||||||
if (shm_id < 0) {
|
|
||||||
critical("Unable to create config shared memory");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
void *shm = shmat(shm_id, NULL, SHM_RDONLY);
|
|
||||||
if (shm == (void *) -1) {
|
|
||||||
critical("Unable to attach config shared memory (ro)");
|
|
||||||
return -2;
|
|
||||||
}
|
|
||||||
config = shm;
|
|
||||||
|
|
||||||
void *shm_rw = shmat(shm_id, NULL, 0);
|
|
||||||
if (shm_rw == (void *) -1) {
|
|
||||||
critical("Unable to attach config shared memory (rw)");
|
|
||||||
return -3;
|
|
||||||
}
|
|
||||||
config = shm_rw;
|
|
||||||
memset(config, 0, sizeof(t_config));
|
|
||||||
shmdt(shm_rw);
|
|
||||||
config = shm;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int config_unload(void) {
|
|
||||||
int shm_id = shmget(CONFIG_SHM_KEY, 0, 0);
|
|
||||||
if (shm_id < 0) {
|
|
||||||
critical("Unable to get config shared memory id");
|
|
||||||
shmdt(config);
|
|
||||||
return -1;
|
|
||||||
} else if (shmctl(shm_id, IPC_RMID, NULL) < 0) {
|
|
||||||
critical("Unable to configure config shared memory");
|
|
||||||
shmdt(config);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
shmdt(config);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int config_load(const char *filename) {
|
int config_load(const char *filename) {
|
||||||
FILE *file = fopen(filename, "r");
|
FILE *file = fopen(filename, "r");
|
||||||
if (file == NULL) {
|
if (file == NULL) {
|
||||||
@@ -69,30 +23,25 @@ int config_load(const char *filename) {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
fseek(file, 0, SEEK_END);
|
memset(&config, 0, sizeof(config));
|
||||||
unsigned long len = ftell(file);
|
|
||||||
fseek(file, 0, SEEK_SET);
|
|
||||||
char *conf = alloca(len + 1);
|
|
||||||
fread(conf, 1, len, file);
|
|
||||||
conf[len] = 0;
|
|
||||||
fclose(file);
|
|
||||||
|
|
||||||
t_config *tmp_config = malloc(sizeof(t_config));
|
|
||||||
memset(tmp_config, 0, sizeof(t_config));
|
|
||||||
|
|
||||||
int i = 0;
|
int i = 0;
|
||||||
int j = 0;
|
int j = 0;
|
||||||
int line = 0;
|
int line_num = 0;
|
||||||
int mode = 0;
|
int mode = 0;
|
||||||
char section = 0;
|
char section = 0;
|
||||||
char *ptr = NULL;
|
|
||||||
char *source, *target;
|
char *source, *target;
|
||||||
while ((ptr = strsep(&conf, "\r\n")) != NULL) {
|
|
||||||
line++;
|
char *line = NULL;
|
||||||
char *comment = strchr(ptr, '#');
|
ssize_t read;
|
||||||
|
size_t line_len = 0;
|
||||||
|
while ((read = getline(&line, &line_len, file)) != -1) {
|
||||||
|
line_num++;
|
||||||
|
char *ptr = line;
|
||||||
|
char *comment = strpbrk(ptr, "#\r\n");
|
||||||
if (comment != NULL) comment[0] = 0;
|
if (comment != NULL) comment[0] = 0;
|
||||||
|
|
||||||
len = strlen(ptr);
|
unsigned long len = strlen(ptr);
|
||||||
char *end_ptr = ptr + len - 1;
|
char *end_ptr = ptr + len - 1;
|
||||||
while (end_ptr[0] == ' ' || end_ptr[0] == '\t') {
|
while (end_ptr[0] == ' ' || end_ptr[0] == '\t') {
|
||||||
end_ptr[0] = 0;
|
end_ptr[0] = 0;
|
||||||
@@ -110,7 +59,7 @@ int config_load(const char *filename) {
|
|||||||
while (ptr[0] == ' ' || ptr[0] == '\t' || ptr[0] == ']') ptr++;
|
while (ptr[0] == ' ' || ptr[0] == '\t' || ptr[0] == ']') ptr++;
|
||||||
while (ptr[l] != ' ' && ptr[l] != '\t' && ptr[l] != ']') l++;
|
while (ptr[l] != ' ' && ptr[l] != '\t' && ptr[l] != ']') l++;
|
||||||
if (l == 0) goto err;
|
if (l == 0) goto err;
|
||||||
snprintf(tmp_config->hosts[i].name, sizeof(tmp_config->hosts[i].name), "%.*s", l, ptr);
|
snprintf(config.hosts[i].name, sizeof(config.hosts[i].name), "%.*s", l, ptr);
|
||||||
i++;
|
i++;
|
||||||
section = 'h';
|
section = 'h';
|
||||||
} else if (strncmp(ptr, "cert", 4) == 0 && (ptr[4] == ' ' || ptr[4] == '\t')) {
|
} else if (strncmp(ptr, "cert", 4) == 0 && (ptr[4] == ' ' || ptr[4] == '\t')) {
|
||||||
@@ -118,7 +67,7 @@ int config_load(const char *filename) {
|
|||||||
while (ptr[0] == ' ' || ptr[0] == '\t' || ptr[0] == ']') ptr++;
|
while (ptr[0] == ' ' || ptr[0] == '\t' || ptr[0] == ']') ptr++;
|
||||||
while (ptr[l] != ' ' && ptr[l] != '\t' && ptr[l] != ']') l++;
|
while (ptr[l] != ' ' && ptr[l] != '\t' && ptr[l] != ']') l++;
|
||||||
if (l == 0) goto err;
|
if (l == 0) goto err;
|
||||||
snprintf(tmp_config->certs[j].name, sizeof(tmp_config->certs[j].name), "%.*s", l, ptr);
|
snprintf(config.certs[j].name, sizeof(config.certs[j].name), "%.*s", l, ptr);
|
||||||
j++;
|
j++;
|
||||||
section = 'c';
|
section = 'c';
|
||||||
} else {
|
} else {
|
||||||
@@ -136,7 +85,7 @@ int config_load(const char *filename) {
|
|||||||
goto err;
|
goto err;
|
||||||
}
|
}
|
||||||
} else if (section == 'c') {
|
} else if (section == 'c') {
|
||||||
cert_config *cc = &tmp_config->certs[j - 1];
|
cert_config_t *cc = &config.certs[j - 1];
|
||||||
if (len > 12 && strncmp(ptr, "certificate", 11) == 0 && (ptr[11] == ' ' || ptr[11] == '\t')) {
|
if (len > 12 && strncmp(ptr, "certificate", 11) == 0 && (ptr[11] == ' ' || ptr[11] == '\t')) {
|
||||||
source = ptr + 11;
|
source = ptr + 11;
|
||||||
target = cc->full_chain;
|
target = cc->full_chain;
|
||||||
@@ -147,7 +96,7 @@ int config_load(const char *filename) {
|
|||||||
goto err;
|
goto err;
|
||||||
}
|
}
|
||||||
} else if (section == 'h') {
|
} else if (section == 'h') {
|
||||||
host_config *hc = &tmp_config->hosts[i - 1];
|
host_config_t *hc = &config.hosts[i - 1];
|
||||||
if (len > 8 && strncmp(ptr, "webroot", 7) == 0 && (ptr[7] == ' ' || ptr[7] == '\t')) {
|
if (len > 8 && strncmp(ptr, "webroot", 7) == 0 && (ptr[7] == ' ' || ptr[7] == '\t')) {
|
||||||
source = ptr + 7;
|
source = ptr + 7;
|
||||||
target = hc->local.webroot;
|
target = hc->local.webroot;
|
||||||
@@ -170,7 +119,7 @@ int config_load(const char *filename) {
|
|||||||
}
|
}
|
||||||
} else if (len > 9 && strncmp(ptr, "hostname", 8) == 0 && (ptr[8] == ' ' || ptr[8] == '\t')) {
|
} else if (len > 9 && strncmp(ptr, "hostname", 8) == 0 && (ptr[8] == ' ' || ptr[8] == '\t')) {
|
||||||
source = ptr + 8;
|
source = ptr + 8;
|
||||||
target = hc->rev_proxy.hostname;
|
target = hc->proxy.hostname;
|
||||||
if (hc->type != 0 && hc->type != CONFIG_TYPE_REVERSE_PROXY) {
|
if (hc->type != 0 && hc->type != CONFIG_TYPE_REVERSE_PROXY) {
|
||||||
goto err;
|
goto err;
|
||||||
} else {
|
} else {
|
||||||
@@ -190,7 +139,7 @@ int config_load(const char *filename) {
|
|||||||
goto err;
|
goto err;
|
||||||
} else {
|
} else {
|
||||||
hc->type = CONFIG_TYPE_REVERSE_PROXY;
|
hc->type = CONFIG_TYPE_REVERSE_PROXY;
|
||||||
hc->rev_proxy.enc = 0;
|
hc->proxy.enc = 0;
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
} else if (strcmp(ptr, "https") == 0) {
|
} else if (strcmp(ptr, "https") == 0) {
|
||||||
@@ -198,7 +147,7 @@ int config_load(const char *filename) {
|
|||||||
goto err;
|
goto err;
|
||||||
} else {
|
} else {
|
||||||
hc->type = CONFIG_TYPE_REVERSE_PROXY;
|
hc->type = CONFIG_TYPE_REVERSE_PROXY;
|
||||||
hc->rev_proxy.enc = 1;
|
hc->proxy.enc = 1;
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
} else {
|
} else {
|
||||||
@@ -211,7 +160,6 @@ int config_load(const char *filename) {
|
|||||||
while (source[0] == ' ' || source[0] == '\t') source++;
|
while (source[0] == ' ' || source[0] == '\t') source++;
|
||||||
if (strlen(source) == 0) {
|
if (strlen(source) == 0) {
|
||||||
err:
|
err:
|
||||||
free(tmp_config);
|
|
||||||
critical("Unable to parse config file (line %i)", line);
|
critical("Unable to parse config file (line %i)", line);
|
||||||
return -2;
|
return -2;
|
||||||
}
|
}
|
||||||
@@ -220,23 +168,25 @@ int config_load(const char *filename) {
|
|||||||
strcpy(target, source);
|
strcpy(target, source);
|
||||||
} else if (mode == 1) {
|
} else if (mode == 1) {
|
||||||
if (strcmp(source, "forbidden") == 0) {
|
if (strcmp(source, "forbidden") == 0) {
|
||||||
tmp_config->hosts[i - 1].local.dir_mode = URI_DIR_MODE_FORBIDDEN;
|
config.hosts[i - 1].local.dir_mode = URI_DIR_MODE_FORBIDDEN;
|
||||||
} else if (strcmp(source, "info") == 0) {
|
} else if (strcmp(source, "info") == 0) {
|
||||||
tmp_config->hosts[i - 1].local.dir_mode = URI_DIR_MODE_INFO;
|
config.hosts[i - 1].local.dir_mode = URI_DIR_MODE_INFO;
|
||||||
} else if (strcmp(source, "list") == 0) {
|
} else if (strcmp(source, "list") == 0) {
|
||||||
tmp_config->hosts[i - 1].local.dir_mode = URI_DIR_MODE_LIST;
|
config.hosts[i - 1].local.dir_mode = URI_DIR_MODE_LIST;
|
||||||
} else {
|
} else {
|
||||||
goto err;
|
goto err;
|
||||||
}
|
}
|
||||||
} else if (mode == 2) {
|
} else if (mode == 2) {
|
||||||
tmp_config->hosts[i - 1].rev_proxy.port = (unsigned short) strtoul(source, NULL, 10);
|
config.hosts[i - 1].proxy.port = (unsigned short) strtoul(source, NULL, 10);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
free(line);
|
||||||
|
|
||||||
for (int k = 0; k < i; k++) {
|
for (int k = 0; k < i; k++) {
|
||||||
host_config *hc = &tmp_config->hosts[k];
|
host_config_t *hc = &config.hosts[k];
|
||||||
if (hc->type == CONFIG_TYPE_LOCAL) {
|
if (hc->type == CONFIG_TYPE_LOCAL) {
|
||||||
char *webroot = tmp_config->hosts[k].local.webroot;
|
char *webroot = config.hosts[k].local.webroot;
|
||||||
if (webroot[strlen(webroot) - 1] == '/') {
|
if (webroot[strlen(webroot) - 1] == '/') {
|
||||||
webroot[strlen(webroot) - 1] = 0;
|
webroot[strlen(webroot) - 1] = 0;
|
||||||
}
|
}
|
||||||
@@ -244,7 +194,7 @@ int config_load(const char *filename) {
|
|||||||
if (hc->cert_name[0] == 0) goto err2;
|
if (hc->cert_name[0] == 0) goto err2;
|
||||||
int found = 0;
|
int found = 0;
|
||||||
for (int m = 0; m < j; m++) {
|
for (int m = 0; m < j; m++) {
|
||||||
if (strcmp(tmp_config->certs[m].name, hc->cert_name) == 0) {
|
if (strcmp(config.certs[m].name, hc->cert_name) == 0) {
|
||||||
hc->cert = m;
|
hc->cert = m;
|
||||||
found = 1;
|
found = 1;
|
||||||
break;
|
break;
|
||||||
@@ -252,27 +202,10 @@ int config_load(const char *filename) {
|
|||||||
}
|
}
|
||||||
if (!found) {
|
if (!found) {
|
||||||
err2:
|
err2:
|
||||||
free(tmp_config);
|
|
||||||
critical("Unable to parse config file");
|
critical("Unable to parse config file");
|
||||||
return -2;
|
return -2;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int shm_id = shmget(CONFIG_SHM_KEY, 0, 0);
|
|
||||||
if (shm_id < 0) {
|
|
||||||
critical("Unable to get config shared memory id");
|
|
||||||
shmdt(config);
|
|
||||||
return -3;
|
|
||||||
}
|
|
||||||
|
|
||||||
void *shm_rw = shmat(shm_id, NULL, 0);
|
|
||||||
if (shm_rw == (void *) -1) {
|
|
||||||
free(tmp_config);
|
|
||||||
critical("Unable to attach config shared memory (rw)");
|
|
||||||
return -4;
|
|
||||||
}
|
|
||||||
memcpy(shm_rw, tmp_config, sizeof(t_config));
|
|
||||||
free(tmp_config);
|
|
||||||
shmdt(shm_rw);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@@ -10,8 +10,8 @@
|
|||||||
#define SESIMOS_CONFIG_H
|
#define SESIMOS_CONFIG_H
|
||||||
|
|
||||||
#include "uri.h"
|
#include "uri.h"
|
||||||
|
#include "cache.h"
|
||||||
|
|
||||||
#define CONFIG_SHM_KEY 255642
|
|
||||||
#define CONFIG_MAX_HOST_CONFIG 64
|
#define CONFIG_MAX_HOST_CONFIG 64
|
||||||
#define CONFIG_MAX_CERT_CONFIG 64
|
#define CONFIG_MAX_CERT_CONFIG 64
|
||||||
|
|
||||||
@@ -29,37 +29,34 @@ typedef struct {
|
|||||||
char name[256];
|
char name[256];
|
||||||
char cert_name[256];
|
char cert_name[256];
|
||||||
int cert;
|
int cert;
|
||||||
|
cache_t *cache;
|
||||||
union {
|
union {
|
||||||
struct {
|
struct {
|
||||||
char hostname[256];
|
char hostname[256];
|
||||||
unsigned short port;
|
unsigned short port;
|
||||||
unsigned char enc:1;
|
unsigned char enc:1;
|
||||||
} rev_proxy;
|
} proxy;
|
||||||
struct {
|
struct {
|
||||||
char webroot[256];
|
char webroot[256];
|
||||||
unsigned char dir_mode:2;
|
unsigned char dir_mode:2;
|
||||||
} local;
|
} local;
|
||||||
};
|
};
|
||||||
} host_config;
|
} host_config_t;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
char name[256];
|
char name[256];
|
||||||
char full_chain[256];
|
char full_chain[256];
|
||||||
char priv_key[256];
|
char priv_key[256];
|
||||||
} cert_config;
|
} cert_config_t;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
host_config hosts[CONFIG_MAX_HOST_CONFIG];
|
host_config_t hosts[CONFIG_MAX_HOST_CONFIG];
|
||||||
cert_config certs[CONFIG_MAX_CERT_CONFIG];
|
cert_config_t certs[CONFIG_MAX_CERT_CONFIG];
|
||||||
} t_config;
|
} config_t;
|
||||||
|
|
||||||
extern t_config *config;
|
extern config_t config;
|
||||||
extern char geoip_dir[256], dns_server[256];
|
extern char geoip_dir[256], dns_server[256];
|
||||||
|
|
||||||
int config_init(void);
|
|
||||||
|
|
||||||
int config_load(const char *filename);
|
int config_load(const char *filename);
|
||||||
|
|
||||||
int config_unload(void);
|
|
||||||
|
|
||||||
#endif //SESIMOS_CONFIG_H
|
#endif //SESIMOS_CONFIG_H
|
||||||
|
143
src/lib/geoip.c
143
src/lib/geoip.c
@@ -7,9 +7,14 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "geoip.h"
|
#include "geoip.h"
|
||||||
|
#include "../logger.h"
|
||||||
|
#include <memory.h>
|
||||||
|
#include <dirent.h>
|
||||||
|
|
||||||
|
|
||||||
MMDB_entry_data_list_s *mmdb_json(MMDB_entry_data_list_s *list, char *str, long *str_off, long str_len) {
|
static MMDB_s mmdbs[GEOIP_MAX_MMDB];
|
||||||
|
|
||||||
|
static MMDB_entry_data_list_s *geoip_json(MMDB_entry_data_list_s *list, char *str, long *str_off, long str_len) {
|
||||||
switch (list->entry_data.type) {
|
switch (list->entry_data.type) {
|
||||||
case MMDB_DATA_TYPE_MAP:
|
case MMDB_DATA_TYPE_MAP:
|
||||||
*str_off += sprintf(str + *str_off, "{");
|
*str_off += sprintf(str + *str_off, "{");
|
||||||
@@ -33,7 +38,7 @@ MMDB_entry_data_list_s *mmdb_json(MMDB_entry_data_list_s *list, char *str, long
|
|||||||
*str_off += sprintf(str + *str_off, "%llu", (unsigned long long) list->entry_data.uint128);
|
*str_off += sprintf(str + *str_off, "%llu", (unsigned long long) list->entry_data.uint128);
|
||||||
break;
|
break;
|
||||||
case MMDB_DATA_TYPE_INT32:
|
case MMDB_DATA_TYPE_INT32:
|
||||||
*str_off += sprintf(str + *str_off, "%i", list->entry_data.uint32);
|
*str_off += sprintf(str + *str_off, "%i", list->entry_data.int32);
|
||||||
break;
|
break;
|
||||||
case MMDB_DATA_TYPE_BOOLEAN:
|
case MMDB_DATA_TYPE_BOOLEAN:
|
||||||
*str_off += sprintf(str + *str_off, "%s", list->entry_data.boolean ? "true" : "false");
|
*str_off += sprintf(str + *str_off, "%s", list->entry_data.boolean ? "true" : "false");
|
||||||
@@ -45,13 +50,14 @@ MMDB_entry_data_list_s *mmdb_json(MMDB_entry_data_list_s *list, char *str, long
|
|||||||
*str_off += sprintf(str + *str_off, "%f", list->entry_data.double_value);
|
*str_off += sprintf(str + *str_off, "%f", list->entry_data.double_value);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (list->entry_data.type != MMDB_DATA_TYPE_MAP && list->entry_data.type != MMDB_DATA_TYPE_ARRAY) {
|
|
||||||
|
if (list->entry_data.type != MMDB_DATA_TYPE_MAP && list->entry_data.type != MMDB_DATA_TYPE_ARRAY)
|
||||||
return list->next;
|
return list->next;
|
||||||
}
|
|
||||||
MMDB_entry_data_list_s *next = list->next;
|
MMDB_entry_data_list_s *next = list->next;
|
||||||
int stat = 0;
|
int stat = 0;
|
||||||
for (int i = 0; i < list->entry_data.data_size; i++) {
|
for (int i = 0; i < list->entry_data.data_size; i++) {
|
||||||
next = mmdb_json(next, str, str_off, str_len);
|
next = geoip_json(next, str, str_off, str_len);
|
||||||
if (list->entry_data.type == MMDB_DATA_TYPE_MAP) {
|
if (list->entry_data.type == MMDB_DATA_TYPE_MAP) {
|
||||||
stat = !stat;
|
stat = !stat;
|
||||||
if (stat) {
|
if (stat) {
|
||||||
@@ -60,12 +66,127 @@ MMDB_entry_data_list_s *mmdb_json(MMDB_entry_data_list_s *list, char *str, long
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (i != list->entry_data.data_size - 1) *str_off += sprintf(str + *str_off, ",");
|
if (i != list->entry_data.data_size - 1)
|
||||||
}
|
*str_off += sprintf(str + *str_off, ",");
|
||||||
if (list->entry_data.type == MMDB_DATA_TYPE_MAP) {
|
|
||||||
*str_off += sprintf(str + *str_off, "}");
|
|
||||||
} else {
|
|
||||||
*str_off += sprintf(str + *str_off, "]");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
*str_off += sprintf(str + *str_off, (list->entry_data.type == MMDB_DATA_TYPE_MAP) ? "}" : "]");
|
||||||
|
|
||||||
return next;
|
return next;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int geoip_init(const char *directory) {
|
||||||
|
char buf[512];
|
||||||
|
|
||||||
|
memset(mmdbs, 0, sizeof(mmdbs));
|
||||||
|
|
||||||
|
if (directory[0] == 0)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
DIR *geoip_dir;
|
||||||
|
if ((geoip_dir = opendir(directory)) == NULL)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
struct dirent *entry;
|
||||||
|
int i = 0, status;
|
||||||
|
while ((entry = readdir(geoip_dir)) != NULL) {
|
||||||
|
if (strcmp(entry->d_name + strlen(entry->d_name) - 5, ".mmdb") != 0)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (i >= GEOIP_MAX_MMDB) {
|
||||||
|
critical("Unable to initialize geoip: Too many .mmdb files");
|
||||||
|
closedir(geoip_dir);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
sprintf(buf, "%s/%s", directory, entry->d_name);
|
||||||
|
if ((status = MMDB_open(buf, 0, &mmdbs[i])) != MMDB_SUCCESS) {
|
||||||
|
critical("Unable to initialize geoip: Unable to open .mmdb file: %s", MMDB_strerror(status));
|
||||||
|
closedir(geoip_dir);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
closedir(geoip_dir);
|
||||||
|
|
||||||
|
if (i == 0) {
|
||||||
|
critical("Unable to initialize geoip: No .mmdb files found in %s", directory);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void geoip_free() {
|
||||||
|
for (int i = 0; i < GEOIP_MAX_MMDB && mmdbs[i].file_content != NULL; i++) {
|
||||||
|
MMDB_close(&mmdbs[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int geoip_lookup_country(struct sockaddr *addr, char *str) {
|
||||||
|
for (int i = 0; i < GEOIP_MAX_MMDB && mmdbs[i].file_content != NULL; i++) {
|
||||||
|
// lookup
|
||||||
|
int mmdb_res;
|
||||||
|
MMDB_lookup_result_s result = MMDB_lookup_sockaddr(&mmdbs[i], addr, &mmdb_res);
|
||||||
|
if (mmdb_res != MMDB_SUCCESS) {
|
||||||
|
return -1;
|
||||||
|
} else if (!result.found_entry) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// get country iso code
|
||||||
|
MMDB_entry_data_s data;
|
||||||
|
int status;
|
||||||
|
if ((status = MMDB_get_value(&result.entry, &data, "country", "iso_code", NULL)) != MMDB_SUCCESS) {
|
||||||
|
if (status == MMDB_IO_ERROR) {
|
||||||
|
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// no, or invalid data
|
||||||
|
if (!data.has_data || data.type != MMDB_DATA_TYPE_UTF8_STRING)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
// return country code
|
||||||
|
sprintf(str, "%.2s", data.utf8_string);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// not found
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int geoip_lookup_json(struct sockaddr *addr, char *json, long len) {
|
||||||
|
long str_off = 0;
|
||||||
|
for (int i = 0; i < GEOIP_MAX_MMDB && mmdbs[i].filename != NULL; i++) {
|
||||||
|
int mmdb_res;
|
||||||
|
MMDB_lookup_result_s result = MMDB_lookup_sockaddr(&mmdbs[i], addr, &mmdb_res);
|
||||||
|
if (mmdb_res != MMDB_SUCCESS) {
|
||||||
|
error("Unable to lookup geoip info: %s", MMDB_strerror(mmdb_res));
|
||||||
|
continue;
|
||||||
|
} else if (!result.found_entry) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
MMDB_entry_data_list_s *list;
|
||||||
|
if ((mmdb_res = MMDB_get_entry_data_list(&result.entry, &list)) != MMDB_SUCCESS) {
|
||||||
|
error("Unable to lookup geoip info: %s", MMDB_strerror(mmdb_res));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
long prev = str_off;
|
||||||
|
if (str_off != 0) {
|
||||||
|
str_off--;
|
||||||
|
}
|
||||||
|
geoip_json(list, json, &str_off, len);
|
||||||
|
if (prev != 0) {
|
||||||
|
json[prev - 1] = ',';
|
||||||
|
}
|
||||||
|
|
||||||
|
MMDB_free_entry_data_list(list);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
@@ -12,8 +12,13 @@
|
|||||||
|
|
||||||
#include <maxminddb.h>
|
#include <maxminddb.h>
|
||||||
|
|
||||||
#define GEOIP_MAX_SIZE 8192
|
#define GEOIP_MAX_JSON_SIZE 8192
|
||||||
|
#define GEOIP_MAX_MMDB 3
|
||||||
|
|
||||||
MMDB_entry_data_list_s *mmdb_json(MMDB_entry_data_list_s *list, char *str, long *str_off, long str_len);
|
int geoip_init(const char *directory);
|
||||||
|
|
||||||
|
void geoip_free();
|
||||||
|
|
||||||
|
int geoip_lookup_country(struct sockaddr *addr, char *str);
|
||||||
|
|
||||||
#endif //SESIMOS_GEOIP_H
|
#endif //SESIMOS_GEOIP_H
|
||||||
|
@@ -405,8 +405,8 @@ const char *http_get_status_color(const http_status *status) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
char *http_format_date(time_t time, char *buf, size_t size) {
|
char *http_format_date(time_t time, char *buf, size_t size) {
|
||||||
struct tm *timeinfo = gmtime(&time);
|
struct tm timeinfo;
|
||||||
strftime(buf, size, "%a, %d %b %Y %H:%M:%S GMT", timeinfo);
|
strftime(buf, size, "%a, %d %b %Y %H:%M:%S GMT", gmtime_r(&time, &timeinfo));
|
||||||
return buf;
|
return buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -118,7 +118,7 @@ extern const int http_statuses_size;
|
|||||||
extern const int http_status_messages_size;
|
extern const int http_status_messages_size;
|
||||||
|
|
||||||
extern const char http_default_document[];
|
extern const char http_default_document[];
|
||||||
extern const char http_rev_proxy_document[];
|
extern const char http_proxy_document[];
|
||||||
extern const char http_error_document[];
|
extern const char http_error_document[];
|
||||||
extern const char http_error_icon[];
|
extern const char http_error_icon[];
|
||||||
extern const char http_warning_document[];
|
extern const char http_warning_document[];
|
||||||
|
@@ -212,7 +212,7 @@ const char http_default_document[] =
|
|||||||
"</body>\n"
|
"</body>\n"
|
||||||
"</html>\n";
|
"</html>\n";
|
||||||
|
|
||||||
const char http_rev_proxy_document[] =
|
const char http_proxy_document[] =
|
||||||
"\t\t<section class=\"error-ctx\">\n"
|
"\t\t<section class=\"error-ctx\">\n"
|
||||||
"\t\t\t<div class=\"box%1$s\">\n"
|
"\t\t\t<div class=\"box%1$s\">\n"
|
||||||
"\t\t\t\t<div class=\"content\">\n"
|
"\t\t\t\t<div class=\"content\">\n"
|
||||||
|
@@ -1,7 +1,7 @@
|
|||||||
/**
|
/**
|
||||||
* sesimos - secure, simple, modern web server
|
* sesimos - secure, simple, modern web server
|
||||||
* @brief Reverse proxy
|
* @brief Reverse proxy
|
||||||
* @file src/lib/rev_proxy.c
|
* @file src/lib/proxy.c
|
||||||
* @author Lorenz Stechauner
|
* @author Lorenz Stechauner
|
||||||
* @date 2021-01-07
|
* @date 2021-01-07
|
||||||
*/
|
*/
|
||||||
@@ -9,7 +9,7 @@
|
|||||||
#include "../defs.h"
|
#include "../defs.h"
|
||||||
#include "../server.h"
|
#include "../server.h"
|
||||||
#include "../logger.h"
|
#include "../logger.h"
|
||||||
#include "rev_proxy.h"
|
#include "proxy.h"
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
#include "compress.h"
|
#include "compress.h"
|
||||||
|
|
||||||
@@ -21,16 +21,16 @@
|
|||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
|
|
||||||
|
|
||||||
sock rev_proxy;
|
sock proxy;
|
||||||
char *rev_proxy_host = NULL;
|
char *proxy_host = NULL;
|
||||||
struct timeval server_timeout = {.tv_sec = SERVER_TIMEOUT, .tv_usec = 0};
|
struct timeval server_timeout = {.tv_sec = SERVER_TIMEOUT, .tv_usec = 0};
|
||||||
|
|
||||||
int rev_proxy_preload(void) {
|
int proxy_preload(void) {
|
||||||
rev_proxy.ctx = SSL_CTX_new(TLS_client_method());
|
proxy.ctx = SSL_CTX_new(TLS_client_method());
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int rev_proxy_request_header(http_req *req, int enc, client_ctx_t *ctx) {
|
int proxy_request_header(http_req *req, int enc, client_ctx_t *ctx) {
|
||||||
char buf1[256], buf2[256];
|
char buf1[256], buf2[256];
|
||||||
int p_len;
|
int p_len;
|
||||||
|
|
||||||
@@ -128,7 +128,7 @@ int rev_proxy_request_header(http_req *req, int enc, client_ctx_t *ctx) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int rev_proxy_response_header(http_req *req, http_res *res, host_config *conf) {
|
int proxy_response_header(http_req *req, http_res *res, host_config_t *conf) {
|
||||||
char buf1[256], buf2[256];
|
char buf1[256], buf2[256];
|
||||||
int p_len;
|
int p_len;
|
||||||
|
|
||||||
@@ -152,7 +152,7 @@ int rev_proxy_response_header(http_req *req, http_res *res, host_config *conf) {
|
|||||||
|
|
||||||
const char *location = http_get_header_field(&res->hdr, "Location");
|
const char *location = http_get_header_field(&res->hdr, "Location");
|
||||||
if (location != NULL) {
|
if (location != NULL) {
|
||||||
char *hostnames[] = {conf->name, conf->rev_proxy.hostname};
|
char *hostnames[] = {conf->name, conf->proxy.hostname};
|
||||||
for (int i = 0; i < sizeof(hostnames) / sizeof(hostnames[0]); i++) {
|
for (int i = 0; i < sizeof(hostnames) / sizeof(hostnames[0]); i++) {
|
||||||
char *hostname = hostnames[i];
|
char *hostname = hostnames[i];
|
||||||
|
|
||||||
@@ -162,10 +162,10 @@ int rev_proxy_response_header(http_req *req, http_res *res, host_config *conf) {
|
|||||||
p_len = snprintf(buf1, sizeof(buf1), "https://%s/", hostname);
|
p_len = snprintf(buf1, sizeof(buf1), "https://%s/", hostname);
|
||||||
if (strncmp(location, buf1, p_len) == 0) goto match;
|
if (strncmp(location, buf1, p_len) == 0) goto match;
|
||||||
|
|
||||||
p_len = snprintf(buf1, sizeof(buf1), "http://%s:%i/", hostname, conf->rev_proxy.port);
|
p_len = snprintf(buf1, sizeof(buf1), "http://%s:%i/", hostname, conf->proxy.port);
|
||||||
if (strncmp(location, buf1, p_len) == 0) goto match;
|
if (strncmp(location, buf1, p_len) == 0) goto match;
|
||||||
|
|
||||||
p_len = snprintf(buf1, sizeof(buf1), "https://%s:%i/", hostname, conf->rev_proxy.port);
|
p_len = snprintf(buf1, sizeof(buf1), "https://%s:%i/", hostname, conf->proxy.port);
|
||||||
if (strncmp(location, buf1, p_len) == 0) goto match;
|
if (strncmp(location, buf1, p_len) == 0) goto match;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -180,25 +180,25 @@ int rev_proxy_response_header(http_req *req, http_res *res, host_config *conf) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int rev_proxy_init(http_req *req, http_res *res, http_status_ctx *ctx, host_config *conf, sock *client, client_ctx_t *cctx, http_status *custom_status, char *err_msg) {
|
int proxy_init(http_req *req, http_res *res, http_status_ctx *ctx, host_config_t *conf, sock *client, client_ctx_t *cctx, http_status *custom_status, char *err_msg) {
|
||||||
char buffer[CHUNK_SIZE];
|
char buffer[CHUNK_SIZE];
|
||||||
const char *connection, *upgrade, *ws_version;
|
const char *connection, *upgrade, *ws_version;
|
||||||
long ret;
|
long ret;
|
||||||
int tries = 0, retry = 0;
|
int tries = 0, retry = 0;
|
||||||
|
|
||||||
if (rev_proxy.socket != 0 && strcmp(rev_proxy_host, conf->name) == 0 && sock_check(&rev_proxy) == 0)
|
if (proxy.socket != 0 && strcmp(proxy_host, conf->name) == 0 && sock_check(&proxy) == 0)
|
||||||
goto rev_proxy;
|
goto proxy;
|
||||||
|
|
||||||
retry:
|
retry:
|
||||||
if (rev_proxy.socket != 0) {
|
if (proxy.socket != 0) {
|
||||||
info(BLUE_STR "Closing proxy connection");
|
info(BLUE_STR "Closing proxy connection");
|
||||||
sock_close(&rev_proxy);
|
sock_close(&proxy);
|
||||||
}
|
}
|
||||||
retry = 0;
|
retry = 0;
|
||||||
tries++;
|
tries++;
|
||||||
|
|
||||||
rev_proxy.socket = socket(AF_INET6, SOCK_STREAM, 0);
|
proxy.socket = socket(AF_INET6, SOCK_STREAM, 0);
|
||||||
if (rev_proxy.socket < 0) {
|
if (proxy.socket < 0) {
|
||||||
error("Unable to create socket");
|
error("Unable to create socket");
|
||||||
res->status = http_get_status(500);
|
res->status = http_get_status(500);
|
||||||
ctx->origin = INTERNAL;
|
ctx->origin = INTERNAL;
|
||||||
@@ -207,14 +207,14 @@ int rev_proxy_init(http_req *req, http_res *res, http_status_ctx *ctx, host_conf
|
|||||||
|
|
||||||
server_timeout.tv_sec = SERVER_TIMEOUT_INIT;
|
server_timeout.tv_sec = SERVER_TIMEOUT_INIT;
|
||||||
server_timeout.tv_usec = 0;
|
server_timeout.tv_usec = 0;
|
||||||
if (setsockopt(rev_proxy.socket, SOL_SOCKET, SO_RCVTIMEO, &server_timeout, sizeof(server_timeout)) < 0)
|
if (setsockopt(proxy.socket, SOL_SOCKET, SO_RCVTIMEO, &server_timeout, sizeof(server_timeout)) < 0)
|
||||||
goto rev_proxy_timeout_err;
|
goto proxy_timeout_err;
|
||||||
if (setsockopt(rev_proxy.socket, SOL_SOCKET, SO_SNDTIMEO, &server_timeout, sizeof(server_timeout)) < 0)
|
if (setsockopt(proxy.socket, SOL_SOCKET, SO_SNDTIMEO, &server_timeout, sizeof(server_timeout)) < 0)
|
||||||
goto rev_proxy_timeout_err;
|
goto proxy_timeout_err;
|
||||||
|
|
||||||
struct hostent *host_ent = gethostbyname2(conf->rev_proxy.hostname, AF_INET6);
|
struct hostent *host_ent = gethostbyname2(conf->proxy.hostname, AF_INET6);
|
||||||
if (host_ent == NULL) {
|
if (host_ent == NULL) {
|
||||||
host_ent = gethostbyname2(conf->rev_proxy.hostname, AF_INET);
|
host_ent = gethostbyname2(conf->proxy.hostname, AF_INET);
|
||||||
if (host_ent == NULL) {
|
if (host_ent == NULL) {
|
||||||
res->status = http_get_status(503);
|
res->status = http_get_status(503);
|
||||||
ctx->origin = SERVER_REQ;
|
ctx->origin = SERVER_REQ;
|
||||||
@@ -224,7 +224,7 @@ int rev_proxy_init(http_req *req, http_res *res, http_status_ctx *ctx, host_conf
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct sockaddr_in6 address = {.sin6_family = AF_INET6, .sin6_port = htons(conf->rev_proxy.port)};
|
struct sockaddr_in6 address = {.sin6_family = AF_INET6, .sin6_port = htons(conf->proxy.port)};
|
||||||
if (host_ent->h_addrtype == AF_INET6) {
|
if (host_ent->h_addrtype == AF_INET6) {
|
||||||
memcpy(&address.sin6_addr, host_ent->h_addr_list[0], host_ent->h_length);
|
memcpy(&address.sin6_addr, host_ent->h_addr_list[0], host_ent->h_length);
|
||||||
} else if (host_ent->h_addrtype == AF_INET) {
|
} else if (host_ent->h_addrtype == AF_INET) {
|
||||||
@@ -235,8 +235,8 @@ int rev_proxy_init(http_req *req, http_res *res, http_status_ctx *ctx, host_conf
|
|||||||
|
|
||||||
inet_ntop(address.sin6_family, (void *) &address.sin6_addr, buffer, sizeof(buffer));
|
inet_ntop(address.sin6_family, (void *) &address.sin6_addr, buffer, sizeof(buffer));
|
||||||
|
|
||||||
info(BLUE_STR "Connecting to " BLD_STR "[%s]:%i" CLR_STR BLUE_STR "...", buffer, conf->rev_proxy.port);
|
info(BLUE_STR "Connecting to " BLD_STR "[%s]:%i" CLR_STR BLUE_STR "...", buffer, conf->proxy.port);
|
||||||
if (connect(rev_proxy.socket, (struct sockaddr *) &address, sizeof(address)) < 0) {
|
if (connect(proxy.socket, (struct sockaddr *) &address, sizeof(address)) < 0) {
|
||||||
if (errno == ETIMEDOUT || errno == EINPROGRESS) {
|
if (errno == ETIMEDOUT || errno == EINPROGRESS) {
|
||||||
res->status = http_get_status(504);
|
res->status = http_get_status(504);
|
||||||
ctx->origin = SERVER_REQ;
|
ctx->origin = SERVER_REQ;
|
||||||
@@ -247,17 +247,17 @@ int rev_proxy_init(http_req *req, http_res *res, http_status_ctx *ctx, host_conf
|
|||||||
res->status = http_get_status(500);
|
res->status = http_get_status(500);
|
||||||
ctx->origin = INTERNAL;
|
ctx->origin = INTERNAL;
|
||||||
}
|
}
|
||||||
error("Unable to connect to [%s]:%i: %s", buffer, conf->rev_proxy.port, strerror(errno));
|
error("Unable to connect to [%s]:%i: %s", buffer, conf->proxy.port, strerror(errno));
|
||||||
sprintf(err_msg, "Unable to connect to server: %s.", strerror(errno));
|
sprintf(err_msg, "Unable to connect to server: %s.", strerror(errno));
|
||||||
goto proxy_err;
|
goto proxy_err;
|
||||||
}
|
}
|
||||||
|
|
||||||
server_timeout.tv_sec = SERVER_TIMEOUT;
|
server_timeout.tv_sec = SERVER_TIMEOUT;
|
||||||
server_timeout.tv_usec = 0;
|
server_timeout.tv_usec = 0;
|
||||||
if (setsockopt(rev_proxy.socket, SOL_SOCKET, SO_RCVTIMEO, &server_timeout, sizeof(server_timeout)) < 0)
|
if (setsockopt(proxy.socket, SOL_SOCKET, SO_RCVTIMEO, &server_timeout, sizeof(server_timeout)) < 0)
|
||||||
goto rev_proxy_timeout_err;
|
goto proxy_timeout_err;
|
||||||
if (setsockopt(rev_proxy.socket, SOL_SOCKET, SO_SNDTIMEO, &server_timeout, sizeof(server_timeout)) < 0) {
|
if (setsockopt(proxy.socket, SOL_SOCKET, SO_SNDTIMEO, &server_timeout, sizeof(server_timeout)) < 0) {
|
||||||
rev_proxy_timeout_err:
|
proxy_timeout_err:
|
||||||
res->status = http_get_status(500);
|
res->status = http_get_status(500);
|
||||||
ctx->origin = INTERNAL;
|
ctx->origin = INTERNAL;
|
||||||
error("Unable to set timeout for reverse proxy socket");
|
error("Unable to set timeout for reverse proxy socket");
|
||||||
@@ -265,29 +265,29 @@ int rev_proxy_init(http_req *req, http_res *res, http_status_ctx *ctx, host_conf
|
|||||||
goto proxy_err;
|
goto proxy_err;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (conf->rev_proxy.enc) {
|
if (conf->proxy.enc) {
|
||||||
rev_proxy.ssl = SSL_new(rev_proxy.ctx);
|
proxy.ssl = SSL_new(proxy.ctx);
|
||||||
SSL_set_fd(rev_proxy.ssl, rev_proxy.socket);
|
SSL_set_fd(proxy.ssl, proxy.socket);
|
||||||
SSL_set_connect_state(rev_proxy.ssl);
|
SSL_set_connect_state(proxy.ssl);
|
||||||
|
|
||||||
ret = SSL_do_handshake(rev_proxy.ssl);
|
ret = SSL_do_handshake(proxy.ssl);
|
||||||
rev_proxy._last_ret = ret;
|
proxy._last_ret = ret;
|
||||||
rev_proxy._errno = errno;
|
proxy._errno = errno;
|
||||||
rev_proxy._ssl_error = ERR_get_error();
|
proxy._ssl_error = ERR_get_error();
|
||||||
rev_proxy.enc = 1;
|
proxy.enc = 1;
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
res->status = http_get_status(502);
|
res->status = http_get_status(502);
|
||||||
ctx->origin = SERVER_REQ;
|
ctx->origin = SERVER_REQ;
|
||||||
error("Unable to perform handshake: %s", sock_strerror(&rev_proxy));
|
error("Unable to perform handshake: %s", sock_strerror(&proxy));
|
||||||
sprintf(err_msg, "Unable to perform handshake: %s.", sock_strerror(&rev_proxy));
|
sprintf(err_msg, "Unable to perform handshake: %s.", sock_strerror(&proxy));
|
||||||
goto proxy_err;
|
goto proxy_err;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
rev_proxy_host = conf->name;
|
proxy_host = conf->name;
|
||||||
info(BLUE_STR "Established new connection with " BLD_STR "[%s]:%i", buffer, conf->rev_proxy.port);
|
info(BLUE_STR "Established new connection with " BLD_STR "[%s]:%i", buffer, conf->proxy.port);
|
||||||
|
|
||||||
rev_proxy:
|
proxy:
|
||||||
connection = http_get_header_field(&req->hdr, "Connection");
|
connection = http_get_header_field(&req->hdr, "Connection");
|
||||||
if (connection != NULL && (strstr(connection, "upgrade") != NULL || strstr(connection, "Upgrade") != NULL)) {
|
if (connection != NULL && (strstr(connection, "upgrade") != NULL || strstr(connection, "Upgrade") != NULL)) {
|
||||||
upgrade = http_get_header_field(&req->hdr, "Upgrade");
|
upgrade = http_get_header_field(&req->hdr, "Upgrade");
|
||||||
@@ -304,19 +304,19 @@ int rev_proxy_init(http_req *req, http_res *res, http_status_ctx *ctx, host_conf
|
|||||||
http_add_header_field(&req->hdr, "Connection", "keep-alive");
|
http_add_header_field(&req->hdr, "Connection", "keep-alive");
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = rev_proxy_request_header(req, (int) client->enc, cctx);
|
ret = proxy_request_header(req, (int) client->enc, cctx);
|
||||||
if (ret != 0) {
|
if (ret != 0) {
|
||||||
res->status = http_get_status(500);
|
res->status = http_get_status(500);
|
||||||
ctx->origin = INTERNAL;
|
ctx->origin = INTERNAL;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = http_send_request(&rev_proxy, req);
|
ret = http_send_request(&proxy, req);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
res->status = http_get_status(502);
|
res->status = http_get_status(502);
|
||||||
ctx->origin = SERVER_REQ;
|
ctx->origin = SERVER_REQ;
|
||||||
error("Unable to send request to server (1): %s", sock_strerror(&rev_proxy));
|
error("Unable to send request to server (1): %s", sock_strerror(&proxy));
|
||||||
sprintf(err_msg, "Unable to send request to server: %s.", sock_strerror(&rev_proxy));
|
sprintf(err_msg, "Unable to send request to server: %s.", sock_strerror(&proxy));
|
||||||
retry = tries < 4;
|
retry = tries < 4;
|
||||||
goto proxy_err;
|
goto proxy_err;
|
||||||
}
|
}
|
||||||
@@ -327,17 +327,17 @@ int rev_proxy_init(http_req *req, http_res *res, http_status_ctx *ctx, host_conf
|
|||||||
|
|
||||||
ret = 0;
|
ret = 0;
|
||||||
if (content_len > 0) {
|
if (content_len > 0) {
|
||||||
ret = sock_splice(&rev_proxy, client, buffer, sizeof(buffer), content_len);
|
ret = sock_splice(&proxy, client, buffer, sizeof(buffer), content_len);
|
||||||
} else if (transfer_encoding != NULL && strstr(transfer_encoding, "chunked") != NULL) {
|
} else if (transfer_encoding != NULL && strstr(transfer_encoding, "chunked") != NULL) {
|
||||||
ret = sock_splice_chunked(&rev_proxy, client, buffer, sizeof(buffer));
|
ret = sock_splice_chunked(&proxy, client, buffer, sizeof(buffer));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ret < 0 || (content_len != 0 && ret != content_len)) {
|
if (ret < 0 || (content_len != 0 && ret != content_len)) {
|
||||||
if (ret == -1) {
|
if (ret == -1) {
|
||||||
res->status = http_get_status(502);
|
res->status = http_get_status(502);
|
||||||
ctx->origin = SERVER_REQ;
|
ctx->origin = SERVER_REQ;
|
||||||
error("Unable to send request to server (2): %s", sock_strerror(&rev_proxy));
|
error("Unable to send request to server (2): %s", sock_strerror(&proxy));
|
||||||
sprintf(err_msg, "Unable to send request to server: %s.", sock_strerror(&rev_proxy));
|
sprintf(err_msg, "Unable to send request to server: %s.", sock_strerror(&proxy));
|
||||||
retry = tries < 4;
|
retry = tries < 4;
|
||||||
goto proxy_err;
|
goto proxy_err;
|
||||||
} else if (ret == -2) {
|
} else if (ret == -2) {
|
||||||
@@ -353,9 +353,9 @@ int rev_proxy_init(http_req *req, http_res *res, http_status_ctx *ctx, host_conf
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = sock_recv(&rev_proxy, buffer, sizeof(buffer), MSG_PEEK);
|
ret = sock_recv(&proxy, buffer, sizeof(buffer), MSG_PEEK);
|
||||||
if (ret <= 0) {
|
if (ret <= 0) {
|
||||||
int enc_err = sock_enc_error(&rev_proxy);
|
int enc_err = sock_enc_error(&proxy);
|
||||||
if (errno == EAGAIN || errno == EINPROGRESS || enc_err == SSL_ERROR_WANT_READ ||
|
if (errno == EAGAIN || errno == EINPROGRESS || enc_err == SSL_ERROR_WANT_READ ||
|
||||||
enc_err == SSL_ERROR_WANT_WRITE)
|
enc_err == SSL_ERROR_WANT_WRITE)
|
||||||
{
|
{
|
||||||
@@ -365,8 +365,8 @@ int rev_proxy_init(http_req *req, http_res *res, http_status_ctx *ctx, host_conf
|
|||||||
res->status = http_get_status(502);
|
res->status = http_get_status(502);
|
||||||
ctx->origin = SERVER_RES;
|
ctx->origin = SERVER_RES;
|
||||||
}
|
}
|
||||||
error("Unable to receive response from server: %s", sock_strerror(&rev_proxy));
|
error("Unable to receive response from server: %s", sock_strerror(&proxy));
|
||||||
sprintf(err_msg, "Unable to receive response from server: %s.", sock_strerror(&rev_proxy));
|
sprintf(err_msg, "Unable to receive response from server: %s.", sock_strerror(&proxy));
|
||||||
retry = tries < 4;
|
retry = tries < 4;
|
||||||
goto proxy_err;
|
goto proxy_err;
|
||||||
}
|
}
|
||||||
@@ -440,9 +440,9 @@ int rev_proxy_init(http_req *req, http_res *res, http_status_ctx *ctx, host_conf
|
|||||||
}
|
}
|
||||||
ptr = pos0 + 2;
|
ptr = pos0 + 2;
|
||||||
}
|
}
|
||||||
sock_recv(&rev_proxy, buffer, header_len, 0);
|
sock_recv(&proxy, buffer, header_len, 0);
|
||||||
|
|
||||||
ret = rev_proxy_response_header(req, res, conf);
|
ret = proxy_response_header(req, res, conf);
|
||||||
if (ret != 0) {
|
if (ret != 0) {
|
||||||
res->status = http_get_status(500);
|
res->status = http_get_status(500);
|
||||||
ctx->origin = INTERNAL;
|
ctx->origin = INTERNAL;
|
||||||
@@ -456,42 +456,42 @@ int rev_proxy_init(http_req *req, http_res *res, http_status_ctx *ctx, host_conf
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int rev_proxy_send(sock *client, unsigned long len_to_send, int flags) {
|
int proxy_send(sock *client, unsigned long len_to_send, int flags) {
|
||||||
char buffer[CHUNK_SIZE], comp_out[CHUNK_SIZE], buf[256], *ptr;
|
char buffer[CHUNK_SIZE], comp_out[CHUNK_SIZE], buf[256], *ptr;
|
||||||
long ret = 0, len, snd_len;
|
long ret = 0, len, snd_len;
|
||||||
int finish_comp = 0;
|
int finish_comp = 0;
|
||||||
|
|
||||||
compress_ctx comp_ctx;
|
compress_ctx comp_ctx;
|
||||||
if (flags & REV_PROXY_COMPRESS_BR) {
|
if (flags & PROXY_COMPRESS_BR) {
|
||||||
flags &= ~REV_PROXY_COMPRESS_GZ;
|
flags &= ~PROXY_COMPRESS_GZ;
|
||||||
if (compress_init(&comp_ctx, COMPRESS_BR) != 0) {
|
if (compress_init(&comp_ctx, COMPRESS_BR) != 0) {
|
||||||
error("Unable to init brotli");
|
error("Unable to init brotli");
|
||||||
flags &= ~REV_PROXY_COMPRESS_BR;
|
flags &= ~PROXY_COMPRESS_BR;
|
||||||
}
|
}
|
||||||
} else if (flags & REV_PROXY_COMPRESS_GZ) {
|
} else if (flags & PROXY_COMPRESS_GZ) {
|
||||||
flags &= ~REV_PROXY_COMPRESS_BR;
|
flags &= ~PROXY_COMPRESS_BR;
|
||||||
if (compress_init(&comp_ctx, COMPRESS_GZ) != 0) {
|
if (compress_init(&comp_ctx, COMPRESS_GZ) != 0) {
|
||||||
error("Unable to init gzip");
|
error("Unable to init gzip");
|
||||||
flags &= ~REV_PROXY_COMPRESS_GZ;
|
flags &= ~PROXY_COMPRESS_GZ;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
do {
|
do {
|
||||||
snd_len = 0;
|
snd_len = 0;
|
||||||
if (flags & REV_PROXY_CHUNKED) {
|
if (flags & PROXY_CHUNKED) {
|
||||||
ret = sock_get_chunk_header(&rev_proxy);
|
ret = sock_get_chunk_header(&proxy);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
if (ret == -1) {
|
if (ret == -1) {
|
||||||
error("Unable to receive from server: Malformed chunk header");
|
error("Unable to receive from server: Malformed chunk header");
|
||||||
} else {
|
} else {
|
||||||
error("Unable to receive from server: %s", sock_strerror(&rev_proxy));
|
error("Unable to receive from server: %s", sock_strerror(&proxy));
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
len_to_send = ret;
|
len_to_send = ret;
|
||||||
ret = 1;
|
ret = 1;
|
||||||
if (len_to_send == 0 && (flags & REV_PROXY_COMPRESS)) {
|
if (len_to_send == 0 && (flags & PROXY_COMPRESS)) {
|
||||||
finish_comp = 1;
|
finish_comp = 1;
|
||||||
len = 0;
|
len = 0;
|
||||||
ptr = NULL;
|
ptr = NULL;
|
||||||
@@ -502,9 +502,9 @@ int rev_proxy_send(sock *client, unsigned long len_to_send, int flags) {
|
|||||||
}
|
}
|
||||||
while (snd_len < len_to_send) {
|
while (snd_len < len_to_send) {
|
||||||
unsigned long avail_in, avail_out;
|
unsigned long avail_in, avail_out;
|
||||||
ret = sock_recv(&rev_proxy, buffer, CHUNK_SIZE < (len_to_send - snd_len) ? CHUNK_SIZE : len_to_send - snd_len, 0);
|
ret = sock_recv(&proxy, buffer, CHUNK_SIZE < (len_to_send - snd_len) ? CHUNK_SIZE : len_to_send - snd_len, 0);
|
||||||
if (ret <= 0) {
|
if (ret <= 0) {
|
||||||
error("Unable to receive from server: %s", sock_strerror(&rev_proxy));
|
error("Unable to receive from server: %s", sock_strerror(&proxy));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
len = ret;
|
len = ret;
|
||||||
@@ -514,7 +514,7 @@ int rev_proxy_send(sock *client, unsigned long len_to_send, int flags) {
|
|||||||
char *next_in = ptr;
|
char *next_in = ptr;
|
||||||
do {
|
do {
|
||||||
long buf_len = len;
|
long buf_len = len;
|
||||||
if (flags & REV_PROXY_COMPRESS) {
|
if (flags & PROXY_COMPRESS) {
|
||||||
avail_out = sizeof(comp_out);
|
avail_out = sizeof(comp_out);
|
||||||
compress_compress(&comp_ctx, next_in + len - avail_in, &avail_in, comp_out, &avail_out, finish_comp);
|
compress_compress(&comp_ctx, next_in + len - avail_in, &avail_in, comp_out, &avail_out, finish_comp);
|
||||||
ptr = comp_out;
|
ptr = comp_out;
|
||||||
@@ -525,31 +525,31 @@ int rev_proxy_send(sock *client, unsigned long len_to_send, int flags) {
|
|||||||
len = sprintf(buf, "%lX\r\n", buf_len);
|
len = sprintf(buf, "%lX\r\n", buf_len);
|
||||||
ret = 1;
|
ret = 1;
|
||||||
|
|
||||||
if (flags & REV_PROXY_CHUNKED) ret = sock_send(client, buf, len, 0);
|
if (flags & PROXY_CHUNKED) ret = sock_send(client, buf, len, 0);
|
||||||
if (ret <= 0) goto err;
|
if (ret <= 0) goto err;
|
||||||
|
|
||||||
ret = sock_send(client, ptr, buf_len, 0);
|
ret = sock_send(client, ptr, buf_len, 0);
|
||||||
if (ret <= 0) goto err;
|
if (ret <= 0) goto err;
|
||||||
if (!(flags & REV_PROXY_COMPRESS)) snd_len += ret;
|
if (!(flags & PROXY_COMPRESS)) snd_len += ret;
|
||||||
|
|
||||||
if (flags & REV_PROXY_CHUNKED) ret = sock_send(client, "\r\n", 2, 0);
|
if (flags & PROXY_CHUNKED) ret = sock_send(client, "\r\n", 2, 0);
|
||||||
if (ret <= 0) {
|
if (ret <= 0) {
|
||||||
err:
|
err:
|
||||||
error("Unable to send: %s", sock_strerror(client));
|
error("Unable to send: %s", sock_strerror(client));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} while ((flags & REV_PROXY_COMPRESS) && (avail_in != 0 || avail_out != sizeof(comp_out)));
|
} while ((flags & PROXY_COMPRESS) && (avail_in != 0 || avail_out != sizeof(comp_out)));
|
||||||
if (ret <= 0) break;
|
if (ret <= 0) break;
|
||||||
if (finish_comp) goto finish;
|
if (finish_comp) goto finish;
|
||||||
}
|
}
|
||||||
if (ret <= 0) break;
|
if (ret <= 0) break;
|
||||||
if (flags & REV_PROXY_CHUNKED) sock_recv(&rev_proxy, buffer, 2, 0);
|
if (flags & PROXY_CHUNKED) sock_recv(&proxy, buffer, 2, 0);
|
||||||
} while ((flags & REV_PROXY_CHUNKED) && len_to_send > 0);
|
} while ((flags & PROXY_CHUNKED) && len_to_send > 0);
|
||||||
|
|
||||||
if (ret <= 0) return -1;
|
if (ret <= 0) return -1;
|
||||||
|
|
||||||
if (flags & REV_PROXY_CHUNKED) {
|
if (flags & PROXY_CHUNKED) {
|
||||||
ret = sock_send(client, "0\r\n\r\n", 5, 0);
|
ret = sock_send(client, "0\r\n\r\n", 5, 0);
|
||||||
if (ret <= 0) {
|
if (ret <= 0) {
|
||||||
error("Unable to send: %s", sock_strerror(client));
|
error("Unable to send: %s", sock_strerror(client));
|
||||||
@@ -560,8 +560,8 @@ int rev_proxy_send(sock *client, unsigned long len_to_send, int flags) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int rev_proxy_dump(char *buf, long len) {
|
int proxy_dump(char *buf, long len) {
|
||||||
sock_recv(&rev_proxy, buf, len, 0);
|
sock_recv(&proxy, buf, len, 0);
|
||||||
sock_close(&rev_proxy);
|
sock_close(&proxy);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
39
src/lib/proxy.h
Normal file
39
src/lib/proxy.h
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
/**
|
||||||
|
* sesimos - secure, simple, modern web server
|
||||||
|
* @brief Reverse proxy (header file)
|
||||||
|
* @file src/lib/proxy.h
|
||||||
|
* @author Lorenz Stechauner
|
||||||
|
* @date 2021-01-07
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef SESIMOS_PROXY_H
|
||||||
|
#define SESIMOS_PROXY_H
|
||||||
|
|
||||||
|
#define PROXY_CHUNKED 1
|
||||||
|
#define PROXY_COMPRESS_GZ 2
|
||||||
|
#define PROXY_COMPRESS_BR 4
|
||||||
|
#define PROXY_COMPRESS 6
|
||||||
|
|
||||||
|
#ifndef SERVER_NAME
|
||||||
|
# define SERVER_NAME "revproxy"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include "http.h"
|
||||||
|
#include "config.h"
|
||||||
|
#include "../client.h"
|
||||||
|
|
||||||
|
extern sock proxy;
|
||||||
|
|
||||||
|
int proxy_preload(void);
|
||||||
|
|
||||||
|
int proxy_request_header(http_req *req, int enc, client_ctx_t *ctx);
|
||||||
|
|
||||||
|
int proxy_response_header(http_req *req, http_res *res, host_config_t *conf);
|
||||||
|
|
||||||
|
int proxy_init(http_req *req, http_res *res, http_status_ctx *ctx, host_config_t *conf, sock *client, client_ctx_t *cctx, http_status *custom_status, char *err_msg);
|
||||||
|
|
||||||
|
int proxy_send(sock *client, unsigned long len_to_send, int flags);
|
||||||
|
|
||||||
|
int proxy_dump(char *buf, long len);
|
||||||
|
|
||||||
|
#endif //SESIMOS_PROXY_H
|
@@ -1,39 +0,0 @@
|
|||||||
/**
|
|
||||||
* sesimos - secure, simple, modern web server
|
|
||||||
* @brief Reverse proxy (header file)
|
|
||||||
* @file src/lib/rev_proxy.h
|
|
||||||
* @author Lorenz Stechauner
|
|
||||||
* @date 2021-01-07
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef SESIMOS_REV_PROXY_H
|
|
||||||
#define SESIMOS_REV_PROXY_H
|
|
||||||
|
|
||||||
#define REV_PROXY_CHUNKED 1
|
|
||||||
#define REV_PROXY_COMPRESS_GZ 2
|
|
||||||
#define REV_PROXY_COMPRESS_BR 4
|
|
||||||
#define REV_PROXY_COMPRESS 6
|
|
||||||
|
|
||||||
#ifndef SERVER_NAME
|
|
||||||
# define SERVER_NAME "revproxy"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include "http.h"
|
|
||||||
#include "config.h"
|
|
||||||
#include "../client.h"
|
|
||||||
|
|
||||||
extern sock rev_proxy;
|
|
||||||
|
|
||||||
int rev_proxy_preload(void);
|
|
||||||
|
|
||||||
int rev_proxy_request_header(http_req *req, int enc, client_ctx_t *ctx);
|
|
||||||
|
|
||||||
int rev_proxy_response_header(http_req *req, http_res *res, host_config *conf);
|
|
||||||
|
|
||||||
int rev_proxy_init(http_req *req, http_res *res, http_status_ctx *ctx, host_config *conf, sock *client, client_ctx_t *cctx, http_status *custom_status, char *err_msg);
|
|
||||||
|
|
||||||
int rev_proxy_send(sock *client, unsigned long len_to_send, int flags);
|
|
||||||
|
|
||||||
int rev_proxy_dump(char *buf, long len);
|
|
||||||
|
|
||||||
#endif //SESIMOS_REV_PROXY_H
|
|
@@ -11,10 +11,15 @@
|
|||||||
|
|
||||||
#include <openssl/crypto.h>
|
#include <openssl/crypto.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
unsigned int enc:1;
|
unsigned int enc:1;
|
||||||
int socket;
|
int socket;
|
||||||
|
union {
|
||||||
|
struct sockaddr sock;
|
||||||
|
struct sockaddr_in6 ipv6;
|
||||||
|
} addr;
|
||||||
SSL_CTX *ctx;
|
SSL_CTX *ctx;
|
||||||
SSL *ssl;
|
SSL *ssl;
|
||||||
long _last_ret;
|
long _last_ret;
|
||||||
|
@@ -9,6 +9,7 @@
|
|||||||
#include "uri.h"
|
#include "uri.h"
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
|
|
||||||
|
#include <errno.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
@@ -20,12 +21,16 @@ int path_is_directory(const char *path) {
|
|||||||
|
|
||||||
int path_is_file(const char *path) {
|
int path_is_file(const char *path) {
|
||||||
struct stat statbuf;
|
struct stat statbuf;
|
||||||
return stat(path, &statbuf) == 0 && S_ISDIR(statbuf.st_mode) == 0;
|
int ret = stat(path, &statbuf);
|
||||||
|
errno = 0;
|
||||||
|
return ret == 0 && S_ISDIR(statbuf.st_mode) == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int path_exists(const char *path) {
|
int path_exists(const char *path) {
|
||||||
struct stat statbuf;
|
struct stat statbuf;
|
||||||
return stat(path, &statbuf) == 0;
|
int ret = stat(path, &statbuf);
|
||||||
|
errno = 0;
|
||||||
|
return ret == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int uri_init(http_uri *uri, const char *webroot, const char *uri_str, int dir_mode) {
|
int uri_init(http_uri *uri, const char *webroot, const char *uri_str, int dir_mode) {
|
||||||
|
@@ -16,14 +16,18 @@
|
|||||||
#define URI_DIR_MODE_LIST 2
|
#define URI_DIR_MODE_LIST 2
|
||||||
#define URI_DIR_MODE_INFO 3
|
#define URI_DIR_MODE_INFO 3
|
||||||
|
|
||||||
|
#define URI_ETAG_SIZE 65 // SHA256 size (hex)
|
||||||
|
#define URI_TYPE_SIZE 64
|
||||||
|
#define URI_CHARSET_SIZE 16
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
char etag[64];
|
char etag[URI_ETAG_SIZE];
|
||||||
char type[24];
|
char type[URI_TYPE_SIZE];
|
||||||
char charset[16];
|
char charset[URI_CHARSET_SIZE];
|
||||||
char filename_comp_gz[256];
|
char filename_comp_gz[256];
|
||||||
char filename_comp_br[256];
|
char filename_comp_br[256];
|
||||||
struct stat stat;
|
struct stat stat;
|
||||||
} meta_data;
|
} metadata_t;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
char *webroot; // "/srv/www/www.test.org"
|
char *webroot; // "/srv/www/www.test.org"
|
||||||
@@ -33,9 +37,9 @@ typedef struct {
|
|||||||
char *query; // "username=test"
|
char *query; // "username=test"
|
||||||
char *filename; // "/account/index.php"
|
char *filename; // "/account/index.php"
|
||||||
char *uri; // "/account/login?username=test"
|
char *uri; // "/account/login?username=test"
|
||||||
meta_data *meta;
|
metadata_t *meta;
|
||||||
unsigned char is_static:1;
|
unsigned int is_static:1;
|
||||||
unsigned char is_dir:1;
|
unsigned int is_dir:1;
|
||||||
} http_uri;
|
} http_uri;
|
||||||
|
|
||||||
|
|
||||||
|
83
src/logger.c
83
src/logger.c
@@ -36,6 +36,7 @@ typedef struct {
|
|||||||
log_msg_t msgs[LOG_BUF_SIZE];
|
log_msg_t msgs[LOG_BUF_SIZE];
|
||||||
} buf_t;
|
} buf_t;
|
||||||
|
|
||||||
|
static pthread_t thread;
|
||||||
static volatile sig_atomic_t logger_alive = 0;
|
static volatile sig_atomic_t logger_alive = 0;
|
||||||
static sem_t sem_buf, sem_buf_free, sem_buf_used;
|
static sem_t sem_buf, sem_buf_free, sem_buf_used;
|
||||||
static buf_t buffer;
|
static buf_t buffer;
|
||||||
@@ -57,7 +58,7 @@ static const char *level_keywords[] = {
|
|||||||
static void err(const char *restrict msg) {
|
static void err(const char *restrict msg) {
|
||||||
char err_buf[64];
|
char err_buf[64];
|
||||||
strerror_r(errno, err_buf, sizeof(err_buf));
|
strerror_r(errno, err_buf, sizeof(err_buf));
|
||||||
fprintf(stderr, ERR_STR "[%6s][logger] %s: %s" CLR_STR "\n", level_keywords[LOG_CRITICAL], msg, err_buf);
|
fprintf(stderr, ERR_STR "[logger][%6s] %s: %s" CLR_STR "\n", level_keywords[LOG_CRITICAL], msg, err_buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
void logmsgf(log_lvl_t level, const char *restrict format, ...) {
|
void logmsgf(log_lvl_t level, const char *restrict format, ...) {
|
||||||
@@ -82,7 +83,7 @@ void logmsgf(log_lvl_t level, const char *restrict format, ...) {
|
|||||||
if (!logger_alive) {
|
if (!logger_alive) {
|
||||||
// no logger thread running
|
// no logger thread running
|
||||||
// simply write to stdout without synchronization
|
// simply write to stdout without synchronization
|
||||||
printf("%s[%-6s][%-6s]%s%s ", color, level_keywords[level], (name != NULL) ? (char *) name : "", CLR_STR, (prefix != NULL) ? (char *) prefix : "");
|
printf("%s[%-6s][%-6s]%s%s ", color, (name != NULL) ? (char *) name : "", level_keywords[level], CLR_STR, (prefix != NULL) ? (char *) prefix : "");
|
||||||
vprintf(buf, args);
|
vprintf(buf, args);
|
||||||
printf("\n");
|
printf("\n");
|
||||||
} else {
|
} else {
|
||||||
@@ -149,31 +150,6 @@ static void logger_destroy(void) {
|
|||||||
sem_destroy(&sem_buf_used);
|
sem_destroy(&sem_buf_used);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int logger_init(void) {
|
|
||||||
int ret;
|
|
||||||
|
|
||||||
// try to initialize all three semaphores
|
|
||||||
if (sem_init(&sem_buf, 0, 1) != 0 || sem_init(&sem_buf_free, 0, 1) != 0 || sem_init(&sem_buf_used, 0, 0) != 0) {
|
|
||||||
err("Unable to initialize semaphore");
|
|
||||||
logger_destroy();
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// initialize read/write heads
|
|
||||||
buffer.rd = 0;
|
|
||||||
buffer.wr = 0;
|
|
||||||
|
|
||||||
// initialize thread specific values (keys)
|
|
||||||
if ((ret = pthread_key_create(&key_name, free)) != 0 || (ret = pthread_key_create(&key_prefix, free)) != 0) {
|
|
||||||
errno = ret;
|
|
||||||
err("Unable to initialize thread specific values");
|
|
||||||
logger_destroy();
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int logger_remaining(void) {
|
static int logger_remaining(void) {
|
||||||
int val = 0;
|
int val = 0;
|
||||||
sem_getvalue(&sem_buf_used, &val);
|
sem_getvalue(&sem_buf_used, &val);
|
||||||
@@ -200,14 +176,14 @@ void logger_set_name(const char *restrict name) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void logger_set_prefix(const char *restrict prefix) {
|
void logger_set_prefix(const char *restrict prefix) {
|
||||||
if (key_name == -1) {
|
if (key_prefix == -1) {
|
||||||
|
// not initialized
|
||||||
strncpy(global_prefix, prefix, sizeof(global_prefix));
|
strncpy(global_prefix, prefix, sizeof(global_prefix));
|
||||||
} else {
|
} else {
|
||||||
int ret;
|
int ret;
|
||||||
void *ptr = pthread_getspecific(key_name);
|
void *ptr = pthread_getspecific(key_prefix);
|
||||||
if (!ptr) {
|
if (!ptr) {
|
||||||
ptr = malloc(LOG_PREFIX_LEN);
|
ptr = malloc(LOG_PREFIX_LEN);
|
||||||
pthread_setspecific(key_prefix, ptr);
|
|
||||||
if ((ret = pthread_setspecific(key_prefix, ptr)) != 0) {
|
if ((ret = pthread_setspecific(key_prefix, ptr)) != 0) {
|
||||||
errno = ret;
|
errno = ret;
|
||||||
err("Unable to set thread specific values");
|
err("Unable to set thread specific values");
|
||||||
@@ -218,14 +194,7 @@ void logger_set_prefix(const char *restrict prefix) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void logger_stop(void) {
|
static void *logger_thread(void *arg) {
|
||||||
logger_alive = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void logger(void) {
|
|
||||||
if (logger_init() != 0)
|
|
||||||
return;
|
|
||||||
|
|
||||||
logger_set_name("logger");
|
logger_set_name("logger");
|
||||||
logger_alive = 1;
|
logger_alive = 1;
|
||||||
|
|
||||||
@@ -243,11 +212,47 @@ void logger(void) {
|
|||||||
log_msg_t *msg = &buffer.msgs[buffer.wr];
|
log_msg_t *msg = &buffer.msgs[buffer.wr];
|
||||||
buffer.wr = (buffer.wr + 1) % LOG_BUF_SIZE;
|
buffer.wr = (buffer.wr + 1) % LOG_BUF_SIZE;
|
||||||
|
|
||||||
printf("[%s]%s %s\n", msg->name, (msg->prefix[0] != 0) ? msg->prefix : "", msg->txt);
|
printf("%s[%-6s][%-6s]%s%s %s\n",
|
||||||
|
(msg->lvl <= LOG_ERROR) ? ERR_STR : ((msg->lvl <= LOG_WARNING) ? WRN_STR : ""),
|
||||||
|
(msg->name[0] != 0) ? (char *) msg->name : "", level_keywords[msg->lvl], CLR_STR,
|
||||||
|
(msg->prefix[0] != 0) ? (char *) msg->prefix : "", msg->txt);
|
||||||
|
|
||||||
// unlock slot in buffer
|
// unlock slot in buffer
|
||||||
sem_post(&sem_buf_free);
|
sem_post(&sem_buf_free);
|
||||||
}
|
}
|
||||||
|
|
||||||
logger_destroy();
|
logger_destroy();
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
int logger_init(void) {
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
// try to initialize all three semaphores
|
||||||
|
if (sem_init(&sem_buf, 0, 1) != 0 || sem_init(&sem_buf_free, 0, LOG_BUF_SIZE) != 0 || sem_init(&sem_buf_used, 0, 0) != 0) {
|
||||||
|
err("Unable to initialize semaphore");
|
||||||
|
logger_destroy();
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// initialize read/write heads
|
||||||
|
buffer.rd = 0;
|
||||||
|
buffer.wr = 0;
|
||||||
|
|
||||||
|
// initialize thread specific values (keys)
|
||||||
|
if ((ret = pthread_key_create(&key_name, free)) != 0 || (ret = pthread_key_create(&key_prefix, free)) != 0) {
|
||||||
|
errno = ret;
|
||||||
|
err("Unable to initialize thread specific values");
|
||||||
|
logger_destroy();
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
pthread_create(&thread, NULL, logger_thread, NULL);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void logger_stop(void) {
|
||||||
|
logger_alive = 0;
|
||||||
}
|
}
|
||||||
|
@@ -33,7 +33,7 @@ void logger_set_name(const char *restrict name);
|
|||||||
|
|
||||||
void logger_set_prefix(const char *restrict prefix);
|
void logger_set_prefix(const char *restrict prefix);
|
||||||
|
|
||||||
void logger(void);
|
int logger_init(void);
|
||||||
|
|
||||||
void logger_stop(void);
|
void logger_stop(void);
|
||||||
|
|
||||||
|
299
src/server.c
299
src/server.c
@@ -14,11 +14,12 @@
|
|||||||
#include "lib/cache.h"
|
#include "lib/cache.h"
|
||||||
#include "lib/config.h"
|
#include "lib/config.h"
|
||||||
#include "lib/sock.h"
|
#include "lib/sock.h"
|
||||||
#include "lib/rev_proxy.h"
|
#include "lib/proxy.h"
|
||||||
#include "lib/geoip.h"
|
#include "lib/geoip.h"
|
||||||
#include "lib/utils.h"
|
#include "lib/utils.h"
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <getopt.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
@@ -26,147 +27,111 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
#include <wait.h>
|
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <openssl/err.h>
|
#include <openssl/err.h>
|
||||||
#include <openssl/pem.h>
|
#include <openssl/pem.h>
|
||||||
#include <openssl/ssl.h>
|
#include <openssl/ssl.h>
|
||||||
#include <openssl/conf.h>
|
#include <openssl/conf.h>
|
||||||
#include <dirent.h>
|
|
||||||
|
|
||||||
|
|
||||||
volatile sig_atomic_t active = 1;
|
volatile sig_atomic_t alive = 1;
|
||||||
const char *config_file;
|
const char *config_file;
|
||||||
int sockets[NUM_SOCKETS];
|
|
||||||
pid_t children[MAX_CHILDREN];
|
static int sockets[NUM_SOCKETS];
|
||||||
MMDB_s mmdbs[MAX_MMDB];
|
static sock clients[MAX_CHILDREN];
|
||||||
SSL_CTX *contexts[CONFIG_MAX_CERT_CONFIG];
|
static pthread_t children[MAX_CHILDREN];
|
||||||
|
static SSL_CTX *contexts[CONFIG_MAX_CERT_CONFIG];
|
||||||
|
|
||||||
|
static int clean() {
|
||||||
|
remove("/var/sesimos/server/cache");
|
||||||
|
rmdir("/var/sesimos/server/");
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
static int ssl_servername_cb(SSL *ssl, int *ad, void *arg) {
|
static int ssl_servername_cb(SSL *ssl, int *ad, void *arg) {
|
||||||
const char *servername = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
|
const char *servername = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
|
||||||
if (servername != NULL) {
|
if (servername != NULL) {
|
||||||
const host_config *conf = get_host_config(servername);
|
const host_config_t *conf = get_host_config(servername);
|
||||||
if (conf != NULL) SSL_set_SSL_CTX(ssl, contexts[conf->cert]);
|
if (conf != NULL) SSL_set_SSL_CTX(ssl, contexts[conf->cert]);
|
||||||
}
|
}
|
||||||
return SSL_TLSEXT_ERR_OK;
|
return SSL_TLSEXT_ERR_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
void destroy(int sig) {
|
static void accept_cb() {
|
||||||
critical("Terminating forcefully!");
|
|
||||||
int status = 0;
|
}
|
||||||
|
|
||||||
|
static void accept_err_cb() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
static void terminate_forcefully(int sig) {
|
||||||
|
fprintf(stderr, "\n");
|
||||||
|
notice("Terminating forcefully!");
|
||||||
|
|
||||||
int ret;
|
int ret;
|
||||||
int kills = 0;
|
|
||||||
for (int i = 0; i < MAX_CHILDREN; i++) {
|
for (int i = 0; i < MAX_CHILDREN; i++) {
|
||||||
if (children[i] != 0) {
|
if (children[i] != 0) {
|
||||||
ret = waitpid(children[i], &status, WNOHANG);
|
if ((ret = pthread_kill(children[i], SIGKILL)) < 0) {
|
||||||
if (ret < 0) {
|
errno = ret;
|
||||||
error("Unable to wait for child process (PID %i)", children[i]);
|
error("Unable to wait for child process (PID %i)", children[i]);
|
||||||
} else if (ret == children[i]) {
|
errno = 0;
|
||||||
children[i] = 0;
|
|
||||||
if (status != 0) {
|
|
||||||
error("Child process with PID %i terminated with exit code %i", ret, status);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
kill(children[i], SIGKILL);
|
|
||||||
kills++;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (kills > 0) {
|
|
||||||
critical("Killed %i child process(es)", kills);
|
geoip_free();
|
||||||
}
|
|
||||||
cache_unload();
|
|
||||||
config_unload();
|
|
||||||
exit(2);
|
exit(2);
|
||||||
}
|
}
|
||||||
|
|
||||||
void terminate(int sig) {
|
static void terminate_gracefully(int sig) {
|
||||||
fprintf(stderr, "\n");
|
fprintf(stderr, "\n");
|
||||||
notice("Terminating gracefully...");
|
notice("Terminating gracefully...");
|
||||||
active = 0;
|
|
||||||
|
|
||||||
signal(SIGINT, destroy);
|
alive = 0;
|
||||||
signal(SIGTERM, destroy);
|
signal(SIGINT, terminate_forcefully);
|
||||||
|
signal(SIGTERM, terminate_forcefully);
|
||||||
|
|
||||||
for (int i = 0; i < NUM_SOCKETS; i++) {
|
for (int i = 0; i < NUM_SOCKETS; i++) {
|
||||||
shutdown(sockets[i], SHUT_RDWR);
|
shutdown(sockets[i], SHUT_RDWR);
|
||||||
close(sockets[i]);
|
close(sockets[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
int status = 0;
|
|
||||||
int wait_num = 0;
|
|
||||||
int ret;
|
int ret;
|
||||||
for (int i = 0; i < MAX_CHILDREN; i++) {
|
for (int i = 0; i < MAX_CHILDREN; i++) {
|
||||||
if (children[i] != 0) {
|
if (children[i] != 0) {
|
||||||
ret = waitpid(children[i], &status, WNOHANG);
|
ret = pthread_kill(children[i], SIGKILL);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
critical("Unable to wait for child process (PID %i)", children[i]);
|
critical("Unable to wait for child process (PID %i)", children[i]);
|
||||||
} else if (ret == children[i]) {
|
} else if (ret == children[i]) {
|
||||||
children[i] = 0;
|
children[i] = 0;
|
||||||
if (status != 0) {
|
|
||||||
critical("Child process with PID %i terminated with exit code %i", ret, status);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
kill(children[i], SIGTERM);
|
|
||||||
wait_num++;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (wait_num > 0) {
|
|
||||||
notice("Waiting for %i child process(es)...", wait_num);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int i = 0; i < MAX_CHILDREN; i++) {
|
|
||||||
if (children[i] != 0) {
|
|
||||||
ret = waitpid(children[i], &status, 0);
|
|
||||||
if (ret < 0) {
|
|
||||||
critical("Unable to wait for child process (PID %i)", children[i]);
|
|
||||||
} else if (ret == children[i]) {
|
|
||||||
children[i] = 0;
|
|
||||||
if (status != 0) {
|
|
||||||
critical("Child process with PID %i terminated with exit code %i", ret, status);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (wait_num > 0) {
|
|
||||||
// Wait another 50 ms to let child processes write to stdout/stderr
|
|
||||||
signal(SIGINT, SIG_IGN);
|
|
||||||
signal(SIGTERM, SIG_IGN);
|
|
||||||
struct timespec ts = {.tv_sec = 0, .tv_nsec = 50000000};
|
|
||||||
nanosleep(&ts, &ts);
|
|
||||||
}
|
|
||||||
|
|
||||||
info("Goodbye");
|
info("Goodbye");
|
||||||
cache_unload();
|
geoip_free();
|
||||||
config_unload();
|
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, const char *argv[]) {
|
int main(int argc, char *const argv[]) {
|
||||||
const int YES = 1;
|
const int YES = 1;
|
||||||
struct pollfd poll_fds[NUM_SOCKETS];
|
struct pollfd poll_fds[NUM_SOCKETS];
|
||||||
int ready_sockets_num;
|
int ready_sockets_num;
|
||||||
long client_num = 0;
|
long client_num = 0;
|
||||||
char buf[1024];
|
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
int client_fd;
|
|
||||||
sock client;
|
|
||||||
struct sockaddr_in6 client_addr;
|
|
||||||
unsigned int client_addr_len = sizeof(client_addr);
|
|
||||||
|
|
||||||
memset(sockets, 0, sizeof(sockets));
|
memset(sockets, 0, sizeof(sockets));
|
||||||
memset(children, 0, sizeof(children));
|
memset(children, 0, sizeof(children));
|
||||||
memset(mmdbs, 0, sizeof(mmdbs));
|
|
||||||
|
|
||||||
const struct sockaddr_in6 addresses[2] = {
|
const struct sockaddr_in6 addresses[2] = {
|
||||||
{.sin6_family = AF_INET6, .sin6_addr = IN6ADDR_ANY_INIT, .sin6_port = htons(80)},
|
{.sin6_family = AF_INET6, .sin6_addr = IN6ADDR_ANY_INIT, .sin6_port = htons(80)},
|
||||||
{.sin6_family = AF_INET6, .sin6_addr = IN6ADDR_ANY_INIT, .sin6_port = htons(443)}
|
{.sin6_family = AF_INET6, .sin6_addr = IN6ADDR_ANY_INIT, .sin6_port = htons(443)}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
logger_init();
|
||||||
|
|
||||||
logger_set_name("server");
|
logger_set_name("server");
|
||||||
|
|
||||||
if (setvbuf(stdout, NULL, _IOLBF, 0) != 0 || setvbuf(stderr, NULL, _IOLBF, 0) != 0) {
|
if (setvbuf(stdout, NULL, _IOLBF, 0) != 0 || setvbuf(stderr, NULL, _IOLBF, 0) != 0) {
|
||||||
@@ -175,116 +140,81 @@ int main(int argc, const char *argv[]) {
|
|||||||
}
|
}
|
||||||
printf("Sesimos web server " SERVER_VERSION "\n");
|
printf("Sesimos web server " SERVER_VERSION "\n");
|
||||||
|
|
||||||
ret = config_init();
|
static const struct option long_opts[] = {
|
||||||
if (ret != 0) {
|
{"help", no_argument, 0, 'h'},
|
||||||
return 1;
|
{"config", required_argument, 0, 'c'},
|
||||||
}
|
{ 0, 0, 0, 0 }
|
||||||
|
};
|
||||||
|
|
||||||
config_file = NULL;
|
config_file = NULL;
|
||||||
for (int i = 1; i < argc; i++) {
|
int c, opt_idx;
|
||||||
const char *arg = argv[i];
|
while ((c = getopt_long(argc, argv, "hc:", long_opts, &opt_idx)) != -1) {
|
||||||
if (strcmp(arg, "-h") == 0 || strcmp(arg, "--help") == 0) {
|
switch (c) {
|
||||||
printf("Usage: sesimos [-h] [-c <CONFIG-FILE>]\n"
|
case 'h':
|
||||||
|
fprintf(stderr,
|
||||||
|
"Usage: sesimos [-h] [-c <CONFIG FILE>]\n"
|
||||||
"\n"
|
"\n"
|
||||||
"Options:\n"
|
"Options:\n"
|
||||||
" -c, --config <CONFIG-FILE> path to the config file. If not provided, default will be used\n"
|
" -c, --config <CONFIG-FILE> path to the config file. If not provided, default will be used\n"
|
||||||
" -h, --help print this dialogue\n");
|
" -h, --help print this dialogue\n");
|
||||||
config_unload();
|
|
||||||
return 0;
|
return 0;
|
||||||
} else if (strcmp(arg, "-c") == 0 || strcmp(arg, "--config") == 0) {
|
case 'c':
|
||||||
if (i == argc - 1) {
|
config_file = optarg;
|
||||||
critical("Unable to parse argument %s, usage: --config <CONFIG-FILE>", arg);
|
break;
|
||||||
config_unload();
|
case '?':
|
||||||
return 1;
|
default:
|
||||||
}
|
critical("Unable to parse arguments");
|
||||||
config_file = argv[++i];
|
|
||||||
} else {
|
|
||||||
critical("Unable to parse argument '%s'", arg);
|
|
||||||
config_unload();
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = config_load(config_file == NULL ? DEFAULT_CONFIG_FILE : config_file);
|
if (optind != argc) {
|
||||||
if (ret != 0) {
|
critical("No positional arguments expected");
|
||||||
config_unload();
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
sockets[0] = socket(AF_INET6, SOCK_STREAM, 0);
|
if (config_load(config_file == NULL ? DEFAULT_CONFIG_FILE : config_file) != 0)
|
||||||
if (sockets[0] < 0) goto socket_err;
|
return 1;
|
||||||
sockets[1] = socket(AF_INET6, SOCK_STREAM, 0);
|
|
||||||
if (sockets[1] < 0) {
|
if ((sockets[0] = socket(AF_INET6, SOCK_STREAM, 0)) == -1 ||
|
||||||
socket_err:
|
(sockets[1] = socket(AF_INET6, SOCK_STREAM, 0)) == -1)
|
||||||
|
{
|
||||||
critical("Unable to create socket");
|
critical("Unable to create socket");
|
||||||
config_unload();
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < NUM_SOCKETS; i++) {
|
for (int i = 0; i < NUM_SOCKETS; i++) {
|
||||||
if (setsockopt(sockets[i], SOL_SOCKET, SO_REUSEADDR, &YES, sizeof(YES)) < 0) {
|
if (setsockopt(sockets[i], SOL_SOCKET, SO_REUSEADDR, &YES, sizeof(YES)) < 0) {
|
||||||
critical("Unable to set options for socket %i", i);
|
critical("Unable to set options for socket %i", i);
|
||||||
config_unload();
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (bind(sockets[0], (struct sockaddr *) &addresses[0], sizeof(addresses[0])) < 0) goto bind_err;
|
if (bind(sockets[0], (struct sockaddr *) &addresses[0], sizeof(addresses[0])) == -1 ||
|
||||||
if (bind(sockets[1], (struct sockaddr *) &addresses[1], sizeof(addresses[1])) < 0) {
|
bind(sockets[1], (struct sockaddr *) &addresses[1], sizeof(addresses[1])) == -1)
|
||||||
bind_err:
|
{
|
||||||
critical("Unable to bind socket to address");
|
critical("Unable to bind socket to address");
|
||||||
config_unload();
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
signal(SIGINT, terminate);
|
signal(SIGINT, terminate_gracefully);
|
||||||
signal(SIGTERM, terminate);
|
signal(SIGTERM, terminate_gracefully);
|
||||||
|
|
||||||
if (geoip_dir[0] != 0) {
|
if ((ret = geoip_init(geoip_dir)) != 0) {
|
||||||
DIR *geoip = opendir(geoip_dir);
|
if (ret == -1) {
|
||||||
if (geoip == NULL) {
|
critical("Unable to initialize geoip");
|
||||||
critical("Unable to open GeoIP dir");
|
}
|
||||||
config_unload();
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
struct dirent *dir;
|
|
||||||
int i = 0;
|
|
||||||
while ((dir = readdir(geoip)) != NULL) {
|
|
||||||
if (strcmp(dir->d_name + strlen(dir->d_name) - 5, ".mmdb") != 0) continue;
|
|
||||||
if (i >= MAX_MMDB) {
|
|
||||||
critical("Too many .mmdb files");
|
|
||||||
config_unload();
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
sprintf(buf, "%s/%s", geoip_dir, dir->d_name);
|
|
||||||
ret = MMDB_open(buf, 0, &mmdbs[i]);
|
|
||||||
if (ret != MMDB_SUCCESS) {
|
|
||||||
critical("Unable to open .mmdb file: %s", MMDB_strerror(ret));
|
|
||||||
config_unload();
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
if (i == 0) {
|
|
||||||
critical("No .mmdb files found in %s", geoip_dir);
|
|
||||||
config_unload();
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
closedir(geoip);
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = cache_init();
|
if ((ret = cache_init()) != 0) {
|
||||||
if (ret < 0) {
|
geoip_free();
|
||||||
config_unload();
|
if (ret == -1) critical("Unable to initialize cache");
|
||||||
return 1;
|
return 1;
|
||||||
} else if (ret != 0) {
|
|
||||||
children[0] = ret; // pid
|
|
||||||
} else {
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < CONFIG_MAX_CERT_CONFIG; i++) {
|
for (int i = 0; i < CONFIG_MAX_CERT_CONFIG; i++) {
|
||||||
const cert_config *conf = &config->certs[i];
|
const cert_config_t *conf = &config.certs[i];
|
||||||
if (conf->name[0] == 0) break;
|
if (conf->name[0] == 0) break;
|
||||||
|
|
||||||
contexts[i] = SSL_CTX_new(TLS_server_method());
|
contexts[i] = SSL_CTX_new(TLS_server_method());
|
||||||
@@ -299,28 +229,22 @@ int main(int argc, const char *argv[]) {
|
|||||||
|
|
||||||
if (SSL_CTX_use_certificate_chain_file(ctx, conf->full_chain) != 1) {
|
if (SSL_CTX_use_certificate_chain_file(ctx, conf->full_chain) != 1) {
|
||||||
critical("Unable to load certificate chain file: %s: %s", ERR_reason_error_string(ERR_get_error()), conf->full_chain);
|
critical("Unable to load certificate chain file: %s: %s", ERR_reason_error_string(ERR_get_error()), conf->full_chain);
|
||||||
config_unload();
|
geoip_free();
|
||||||
cache_unload();
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
if (SSL_CTX_use_PrivateKey_file(ctx, conf->priv_key, SSL_FILETYPE_PEM) != 1) {
|
if (SSL_CTX_use_PrivateKey_file(ctx, conf->priv_key, SSL_FILETYPE_PEM) != 1) {
|
||||||
critical("Unable to load private key file: %s: %s", ERR_reason_error_string(ERR_get_error()), conf->priv_key);
|
critical("Unable to load private key file: %s: %s", ERR_reason_error_string(ERR_get_error()), conf->priv_key);
|
||||||
config_unload();
|
geoip_free();
|
||||||
cache_unload();
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
client.ctx = contexts[0];
|
proxy_preload();
|
||||||
|
|
||||||
|
|
||||||
rev_proxy_preload();
|
|
||||||
|
|
||||||
for (int i = 0; i < NUM_SOCKETS; i++) {
|
for (int i = 0; i < NUM_SOCKETS; i++) {
|
||||||
if (listen(sockets[i], LISTEN_BACKLOG) < 0) {
|
if (listen(sockets[i], LISTEN_BACKLOG) < 0) {
|
||||||
critical("Unable to listen on socket %i", i);
|
critical("Unable to listen on socket %i", i);
|
||||||
config_unload();
|
geoip_free();
|
||||||
cache_unload();
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -333,65 +257,58 @@ int main(int argc, const char *argv[]) {
|
|||||||
errno = 0;
|
errno = 0;
|
||||||
notice("Ready to accept connections");
|
notice("Ready to accept connections");
|
||||||
|
|
||||||
while (active) {
|
while (alive) {
|
||||||
ready_sockets_num = poll(poll_fds, NUM_SOCKETS, 1000);
|
ready_sockets_num = poll(poll_fds, NUM_SOCKETS, 1000);
|
||||||
if (ready_sockets_num < 0) {
|
if (ready_sockets_num < 0) {
|
||||||
critical("Unable to poll sockets");
|
critical("Unable to poll sockets");
|
||||||
terminate(0);
|
terminate_gracefully(0);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < NUM_SOCKETS; i++) {
|
for (int i = 0; i < NUM_SOCKETS; i++) {
|
||||||
if (poll_fds[i].revents & POLLIN) {
|
if (poll_fds[i].revents & POLLIN) {
|
||||||
client_fd = accept(sockets[i], (struct sockaddr *) &client_addr, &client_addr_len);
|
int j;
|
||||||
|
for (j = 0; j < MAX_CHILDREN; j++) {
|
||||||
|
if (children[j] == 0) break;
|
||||||
|
}
|
||||||
|
sock *client = &clients[j];
|
||||||
|
|
||||||
|
client->ctx = contexts[0];
|
||||||
|
socklen_t addr_len = sizeof(client->addr);
|
||||||
|
int client_fd = accept(sockets[i], &client->addr.sock, &addr_len);
|
||||||
if (client_fd < 0) {
|
if (client_fd < 0) {
|
||||||
critical("Unable to accept connection");
|
critical("Unable to accept connection");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
pid_t pid = fork();
|
client->socket = client_fd;
|
||||||
if (pid == 0) {
|
client->enc = (i == 1);
|
||||||
// child
|
ret = pthread_create(&children[j], NULL, (void *(*)(void *)) &client_handler, client);
|
||||||
signal(SIGINT, SIG_IGN);
|
if (ret != 0) {
|
||||||
signal(SIGTERM, SIG_IGN);
|
errno = ret;
|
||||||
|
|
||||||
client.socket = client_fd;
|
|
||||||
client.enc = (i == 1);
|
|
||||||
return client_handler(&client, client_num, &client_addr);
|
|
||||||
} else if (pid > 0) {
|
|
||||||
// parent
|
|
||||||
client_num++;
|
|
||||||
close(client_fd);
|
|
||||||
for (int j = 0; j < MAX_CHILDREN; j++) {
|
|
||||||
if (children[j] == 0) {
|
|
||||||
children[j] = pid;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
critical("Unable to create child process");
|
critical("Unable to create child process");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
client_num++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO outsource in thread
|
// TODO outsource in thread
|
||||||
int status = 0;
|
/*
|
||||||
|
void *ret_val = NULL;
|
||||||
for (int i = 0; i < MAX_CHILDREN; i++) {
|
for (int i = 0; i < MAX_CHILDREN; i++) {
|
||||||
if (children[i] != 0) {
|
if (children[i] != 0) {
|
||||||
ret = waitpid(children[i], &status, WNOHANG);
|
ret = pthread_timed(children[i], &ret_val);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
critical("Unable to wait for child process (PID %i)", children[i]);
|
critical("Unable to wait for thread (PID %i)", children[i]);
|
||||||
} else if (ret == children[i]) {
|
} else if (ret == children[i]) {
|
||||||
children[i] = 0;
|
children[i] = 0;
|
||||||
if (status != 0) {
|
|
||||||
critical("Child process with PID %i terminated with exit code %i", ret, status);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
config_unload();
|
geoip_free();
|
||||||
cache_unload();
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
11
src/server.h
11
src/server.h
@@ -14,19 +14,16 @@
|
|||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
|
|
||||||
#define NUM_SOCKETS 2
|
#define NUM_SOCKETS 2
|
||||||
#define MAX_CHILDREN 1024
|
#define MAX_CHILDREN 64
|
||||||
#define MAX_MMDB 3
|
|
||||||
#define LISTEN_BACKLOG 16
|
#define LISTEN_BACKLOG 16
|
||||||
#define REQ_PER_CONNECTION 200
|
#define REQ_PER_CONNECTION 200
|
||||||
#define CLIENT_TIMEOUT 3600
|
#define CLIENT_TIMEOUT 3600
|
||||||
#define SERVER_TIMEOUT_INIT 4
|
#define SERVER_TIMEOUT_INIT 4
|
||||||
#define SERVER_TIMEOUT 3600
|
#define SERVER_TIMEOUT 3600
|
||||||
|
|
||||||
extern int sockets[NUM_SOCKETS];
|
#define CNX_HANDLER_WORKERS 8
|
||||||
extern pid_t children[MAX_CHILDREN];
|
#define REQ_HANDLER_WORKERS 16
|
||||||
extern MMDB_s mmdbs[MAX_MMDB];
|
|
||||||
|
|
||||||
extern volatile sig_atomic_t server_keep_alive;
|
extern volatile sig_atomic_t alive;
|
||||||
extern struct timeval client_timeout;
|
|
||||||
|
|
||||||
#endif //SESIMOS_SERVER_H
|
#endif //SESIMOS_SERVER_H
|
||||||
|
Reference in New Issue
Block a user