Rev proxy working again

This commit is contained in:
2022-12-30 18:56:44 +01:00
parent 28d7cf68df
commit 9ad1ecf1da
15 changed files with 208 additions and 146 deletions

View File

@ -11,6 +11,7 @@
#include "../lib/utils.h"
#include "../lib/compress.h"
#include "../workers.h"
#include "../lib/fastcgi.h"
#include <string.h>
@ -39,7 +40,7 @@ static int fastcgi_handler_1(client_ctx_t *ctx, fastcgi_cnx_t *fcgi_cnx) {
fcgi_cnx->socket = 0;
fcgi_cnx->req_id = 0;
fcgi_cnx->r_addr = ctx->addr;
fcgi_cnx->r_addr = ctx->socket.addr;
fcgi_cnx->r_host = (ctx->host[0] != 0) ? ctx->host : NULL;
char buf[1024];

View File

@ -9,7 +9,38 @@
#ifndef SESIMOS_FUNC_H
#define SESIMOS_FUNC_H
#include "../server.h"
#include "../lib/sock.h"
#include "../lib/http.h"
#include "../lib/uri.h"
#include "../lib/config.h"
#include "../lib/proxy.h"
typedef struct {
sock socket;
int req_num;
unsigned char in_use: 1, s_keep_alive:1, c_keep_alive:1;
char cc[3], host[256];
char req_host[256], err_msg[256];
char log_prefix[512];
char _c_addr[INET6_ADDRSTRLEN + 1], _s_addr[INET6_ADDRSTRLEN + 1];
struct timespec begin, end;
http_req req;
http_res res;
http_uri uri;
http_status_ctx status;
http_status custom_status;
int use_fastcgi, use_proxy;
host_config_t *conf;
FILE *file;
long content_length;
char msg_buf[8192], msg_content[1024];
proxy_ctx_t *proxy;
} client_ctx_t;
typedef struct {
client_ctx_t *client;
sock *s1, *s2, *s, *r;
} ws_ctx_t;
void tcp_acceptor_func(client_ctx_t *ctx);
@ -23,7 +54,7 @@ void fastcgi_handler_func(client_ctx_t *ctx);
void proxy_handler_func(client_ctx_t *ctx);
void ws_frame_handler_func(client_ctx_t *ctx);
void ws_frame_handler_func(ws_ctx_t *ctx);
int respond(client_ctx_t *ctx);

View File

@ -44,6 +44,7 @@ static int local_handler(client_ctx_t *ctx) {
int accept_if_modified_since = 0;
if (strcmp(req->method, "TRACE") == 0) {
// FIXME not working?
res->status = http_get_status(200);
http_add_header_field(&res->hdr, "Content-Type", "message/http");

View File

@ -31,11 +31,7 @@ void proxy_handler_func(client_ctx_t *ctx) {
static int proxy_handler_1(client_ctx_t *ctx) {
http_res *res = &ctx->res;
http_req *req = &ctx->req;
http_uri *uri = &ctx->uri;
http_status_ctx *status = &ctx->status;
sock *client = &ctx->socket;
char *err_msg = ctx->err_msg;
char buf[1024];
@ -43,8 +39,8 @@ static int proxy_handler_1(client_ctx_t *ctx) {
http_remove_header_field(&res->hdr, "Date", HTTP_REMOVE_ALL);
http_remove_header_field(&res->hdr, "Server", HTTP_REMOVE_ALL);
int ret = proxy_init(req, res, status, ctx->conf, client, ctx, &ctx->custom_status, err_msg);
ctx->use_proxy = (ret == 0);
ctx->proxy = proxy_init(&ctx->req, res, status, ctx->conf, &ctx->socket, &ctx->custom_status, ctx->err_msg);
ctx->use_proxy = (ctx->proxy != NULL);
if (res->status->code == 101) {
const char *connection = http_get_header_field(&res->hdr, "Connection");
@ -77,7 +73,7 @@ static int proxy_handler_1(client_ctx_t *ctx) {
status->origin = res->status->code >= 400 ? SERVER : NONE;
}
ctx->use_proxy = 0;
proxy_dump(ctx->msg_content, content_len);
proxy_dump(ctx->proxy, ctx->msg_content, content_len);
}
}
}
@ -120,7 +116,9 @@ static int proxy_handler_2(client_ctx_t *ctx) {
}
int flags = (chunked ? PROXY_CHUNKED : 0) | (ctx->use_proxy & PROXY_COMPRESS);
int ret = proxy_send(&ctx->socket, len_to_send, flags);
int ret = proxy_send(ctx->proxy, &ctx->socket, len_to_send, flags);
ctx->proxy->in_use = 0;
ctx->proxy = NULL;
if (ret < 0) {
ctx->c_keep_alive = 0;

View File

@ -12,18 +12,17 @@
#include "../lib/mpmc.h"
#include "../logger.h"
#include "../lib/utils.h"
#include "../lib/proxy.h"
#include "../lib/websocket.h"
#include "../server.h"
#include <string.h>
#include <openssl/err.h>
#include <arpa/inet.h>
#include <unistd.h>
static int request_handler(client_ctx_t *ctx);
void request_handler_func(client_ctx_t *ctx) {
logger_set_prefix("[%*s]%s", INET6_ADDRSTRLEN, ctx->s_addr, ctx->log_prefix);
logger_set_prefix("[%*s]%s", INET6_ADDRSTRLEN, ctx->socket.s_addr, ctx->log_prefix);
switch (request_handler(ctx)) {
case 0:
@ -51,8 +50,15 @@ static int request_handler(client_ctx_t *ctx) {
err_msg[0] = 0;
ctx->file = NULL;
ctx->proxy = NULL;
ctx->use_fastcgi = 0;
ctx->use_proxy = 0;
ctx->proxy = NULL;
ctx->msg_content[0] = 0;
ctx->msg_buf[0] = 0;
ctx->req_host[0] = 0;
ctx->err_msg[0] = 0;
http_res *res = &ctx->res;
res->status = http_get_status(501);
@ -67,18 +73,19 @@ static int request_handler(client_ctx_t *ctx) {
clock_gettime(CLOCK_MONOTONIC, &ctx->begin);
//ret = sock_poll_read(&client, NULL, NULL, 1, NULL, NULL, CLIENT_TIMEOUT * 1000);
// FIXME async poll
ret = sock_poll_read(&client, NULL, NULL, 1, NULL, NULL, CLIENT_TIMEOUT * 1000);
http_add_header_field(&res->hdr, "Date", http_get_date(buf0, sizeof(buf0)));
http_add_header_field(&res->hdr, "Server", SERVER_STR);
/*if (ret <= 0) {
if (ret <= 0) {
if (errno != 0) return 0;
ctx->c_keep_alive = 0;
res->status = http_get_status(408);
return 0;
}*/
//clock_gettime(CLOCK_MONOTONIC, &begin);
}
clock_gettime(CLOCK_MONOTONIC, &ctx->begin);
http_req *req = &ctx->req;
ret = http_receive_request(client, req);
@ -110,10 +117,10 @@ static int request_handler(client_ctx_t *ctx) {
sprintf(err_msg, "Host header field is too long.");
return 0;
} else if (host_ptr == NULL || strchr(host_ptr, '/') != NULL) {
if (strchr(ctx->addr, ':') == NULL) {
strcpy(ctx->req_host, ctx->addr);
if (strchr(ctx->socket.addr, ':') == NULL) {
strcpy(ctx->req_host, ctx->socket.addr);
} else {
sprintf(ctx->req_host, "[%s]", ctx->addr);
sprintf(ctx->req_host, "[%s]", ctx->socket.addr);
}
res->status = http_get_status(400);
sprintf(err_msg, "The client provided no or an invalid Host header field.");
@ -303,7 +310,7 @@ int respond(client_ctx_t *ctx) {
if (ctx->use_proxy == 2) {
// WebSocket
info("Upgrading connection to WebSocket connection");
ret = ws_handle_connection(client, &proxy);
ret = ws_handle_connection(client, &ctx->proxy->proxy);
if (ret != 0) {
ctx->c_keep_alive = 0;
close_proxy = 1;
@ -335,7 +342,7 @@ int respond(client_ctx_t *ctx) {
} else if (ctx->use_fastcgi) {
return 2;
} else if (ctx->use_proxy) {
return 3;
return 3;
}
if (ret < 0) {

View File

@ -13,6 +13,7 @@
#include "../lib/utils.h"
#include "../lib/geoip.h"
#include "../workers.h"
#include "../server.h"
#include <string.h>
#include <errno.h>
@ -32,27 +33,27 @@ void tcp_acceptor_func(client_ctx_t *ctx) {
static int tcp_acceptor(client_ctx_t *ctx) {
struct sockaddr_in6 server_addr;
inet_ntop(ctx->socket.addr.ipv6.sin6_family, &ctx->socket.addr.ipv6.sin6_addr, ctx->_c_addr, sizeof(ctx->_c_addr));
inet_ntop(ctx->socket._addr.ipv6.sin6_family, &ctx->socket._addr.ipv6.sin6_addr, ctx->_c_addr, sizeof(ctx->_c_addr));
if (strncmp(ctx->_c_addr, "::ffff:", 7) == 0) {
ctx->addr = ctx->_c_addr + 7;
ctx->socket.addr = ctx->_c_addr + 7;
} else {
ctx->addr = ctx->_c_addr;
ctx->socket.addr = ctx->_c_addr;
}
socklen_t len = sizeof(server_addr);
getsockname(ctx->socket.socket, (struct sockaddr *) &server_addr, &len);
inet_ntop(server_addr.sin6_family, (void *) &server_addr.sin6_addr, ctx->_s_addr, sizeof(ctx->_s_addr));
if (strncmp(ctx->_s_addr, "::ffff:", 7) == 0) {
ctx->s_addr = ctx->_s_addr + 7;
ctx->socket.s_addr = ctx->_s_addr + 7;
} else {
ctx->s_addr = ctx->_s_addr;
ctx->socket.s_addr = ctx->_s_addr;
}
sprintf(ctx->log_prefix, "[%s%4i%s]%s[%*s][%5i]%s", (int) ctx->socket.enc ? HTTPS_STR : HTTP_STR,
ntohs(server_addr.sin6_port), CLR_STR, /*color_table[0]*/ "", INET6_ADDRSTRLEN, ctx->addr,
ntohs(ctx->socket.addr.ipv6.sin6_port), CLR_STR);
ntohs(server_addr.sin6_port), CLR_STR, /*color_table[0]*/ "", INET6_ADDRSTRLEN, ctx->socket.addr,
ntohs(ctx->socket._addr.ipv6.sin6_port), CLR_STR);
logger_set_prefix("[%*s]%s", INET6_ADDRSTRLEN, ctx->s_addr, ctx->log_prefix);
logger_set_prefix("[%*s]%s", INET6_ADDRSTRLEN, ctx->socket.s_addr, ctx->log_prefix);
int ret;
char buf[1024];
@ -61,7 +62,7 @@ static int tcp_acceptor(client_ctx_t *ctx) {
clock_gettime(CLOCK_MONOTONIC, &ctx->begin);
if (config.dns_server[0] != 0) {
sprintf(buf, "dig @%s +short +time=1 -x %s", config.dns_server, ctx->addr);
sprintf(buf, "dig @%s +short +time=1 -x %s", config.dns_server, ctx->socket.addr);
FILE *dig = popen(buf, "r");
if (dig == NULL) {
error("Unable to start dig: %s", strerror(errno));
@ -85,9 +86,9 @@ static int tcp_acceptor(client_ctx_t *ctx) {
}
ctx->cc[0] = 0;
geoip_lookup_country(&client->addr.sock, ctx->cc);
geoip_lookup_country(&client->_addr.sock, ctx->cc);
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->socket.addr, ctx->host[0] != 0 ? "(" : "",
ctx->host[0] != 0 ? ctx->host : "", ctx->host[0] != 0 ? ") " : "",
ctx->cc[0] != 0 ? ctx->cc : "N/A");

View File

@ -13,7 +13,7 @@
#include <memory.h>
void tcp_closer_func(client_ctx_t *ctx) {
logger_set_prefix("[%*s]%s", INET6_ADDRSTRLEN, ctx->s_addr, ctx->log_prefix);
logger_set_prefix("[%*s]%s", INET6_ADDRSTRLEN, ctx->socket.s_addr, ctx->log_prefix);
sock_close(&ctx->socket);

View File

@ -8,12 +8,12 @@
#include "func.h"
static int ws_frame_handler(client_ctx_t *ctx);
static int ws_frame_handler(ws_ctx_t *ctx);
void ws_frame_handler_func(client_ctx_t *ctx) {
void ws_frame_handler_func(ws_ctx_t *ctx) {
}
static int ws_frame_handler(client_ctx_t *ctx) {
static int ws_frame_handler(ws_ctx_t *ctx) {
return 0;
}