From: Matteo Nastasi Date: Sat, 4 Apr 2020 12:23:45 +0000 (+0200) Subject: add pull of unix sockets to interact with brisk X-Git-Tag: v3.0.1~11 X-Git-Url: http://mop.ddnsfree.com/gitweb/?a=commitdiff_plain;h=82c96ac29b5c1faec95b7e25518aa7c7bc745ab9;p=mod-proxy-fdpass.git add pull of unix sockets to interact with brisk --- diff --git a/mod_proxy_fdpass2.c b/mod_proxy_fdpass2.c index 7d4c6ae..139dd5a 100644 --- a/mod_proxy_fdpass2.c +++ b/mod_proxy_fdpass2.c @@ -19,6 +19,8 @@ #include #include #include +#include +#include #ifndef CMSG_DATA #error This module only works on unix platforms with the correct OS support @@ -34,22 +36,25 @@ module AP_MODULE_DECLARE_DATA proxy_fdpass2_module; -#define MOP_DEBUG 1 +#define ALTOUT_USOCK_N 10 +#define ALTOUT_DEBUG 1 +#define ALTOUT_DBG_FILE "/home/log/fdpass2.log" static int proxy_fdpass2_canon(request_rec *r, char *url) { - const char *path; + const char *path, sfx[16]; - if (MOP_DEBUG == 1) { +#if ALTOUT_DEBUG > 1 + { int mop_fd; char mop_bf[512]; - mop_fd = open("/tmp/apache_mop.log", O_WRONLY | O_APPEND | O_CREAT, 0644); + mop_fd = open(ALTOUT_DBG_FILE, O_WRONLY | O_APPEND | O_CREAT, 0644); sprintf(mop_bf, "proxy_http_canon: start [%p]\n", r->headers_in); write(mop_fd, mop_bf, strlen(mop_bf)); close(mop_fd); - } +#endif if (strncasecmp(url, "fd://", 5) == 0) { url += 5; @@ -57,10 +62,11 @@ static int proxy_fdpass2_canon(request_rec *r, char *url) else { return DECLINED; } - + path = ap_server_root_relative(r->pool, url); - r->filename = apr_pstrcat(r->pool, "proxy:fd://", path, NULL); + sprintf(sfx, "%d.sock", rand() % ALTOUT_USOCK_N); + r->filename = apr_pstrcat(r->pool, "proxy:fd://", path, sfx, NULL); /* ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "proxy: FD: set r->filename to %s", r->filename); */ @@ -112,7 +118,7 @@ static apr_status_t socket_connect_un(request_rec *r, apr_socket_t *sock, return rv; } } - + if (rv == -1 && errno != EISCONN) { ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, "proxy: FD: socket_connect_un preexit %d", errno); @@ -183,16 +189,18 @@ static apr_status_t send_socket(apr_pool_t *p, char b = '\0', *buf; ANCIL_FD_BUFFER(2) ancil_buf; - if (MOP_DEBUG == 1) { +#if ALTOUT_DEBUG > 1 + { int mop_fd; char mop_bf[512]; - mop_fd = open("/tmp/apache_mop.log", O_WRONLY | O_APPEND | O_CREAT, 0644); + mop_fd = open(ALTOUT_DBG_FILE, O_WRONLY | O_APPEND | O_CREAT, 0644); sprintf(mop_bf, "send_socket: start\n"); write(mop_fd, mop_bf, strlen(mop_bf)); close(mop_fd); } - +#endif + rv = apr_os_sock_get(&rawsock, outbound); if (rv != APR_SUCCESS) { return rv; @@ -207,17 +215,19 @@ static apr_status_t send_socket(apr_pool_t *p, if (rv != APR_SUCCESS) { return rv; } - - if (MOP_DEBUG == 1) { + +#if ALTOUT_DEBUG > 1 + { int mop_fd; char mop_bf[512]; - mop_fd = open("/tmp/apache_mop.log", O_WRONLY | O_APPEND | O_CREAT, 0644); + mop_fd = open(ALTOUT_DBG_FILE, O_WRONLY | O_APPEND | O_CREAT, 0644); write(mop_fd, "XX", 2); write(mop_fd, &srawsock, sizeof(apr_os_sock_t)); write(mop_fd, "XX", 2); close(mop_fd); } +#endif memset(&msg, 0, sizeof(msg)); @@ -241,21 +251,178 @@ static apr_status_t send_socket(apr_pool_t *p, rv = sendmsg(srawsock, &msg, 0); - if (MOP_DEBUG == 1) { +#if ALTOUT_DEBUG > 1 + { int mop_fd; char mop_bf[512]; - mop_fd = open("/tmp/apache_mop.log", O_WRONLY | O_APPEND | O_CREAT, 0644); + mop_fd = open(ALTOUT_DBG_FILE, O_WRONLY | O_APPEND | O_CREAT, 0644); sprintf(mop_bf, "SENT BYTES: %d\n", rv); write(mop_fd, mop_bf, strlen(mop_bf)); close(mop_fd); } +#endif + + if (rv == -1) { + return errno; + } + + + return APR_SUCCESS; +} + +static int headers_builder(void *rec, const char *key, const char *value) +{ + char *s; + + s = (char *)rec; + +#if ALTOUT_DEBUG > 1 + { + int mop_fd; + char mop_bf[512]; + + mop_fd = open(ALTOUT_DBG_FILE, O_WRONLY | O_APPEND | O_CREAT, 0644); + sprintf(mop_bf, "HEADERS_BUILDER: [%s:%s]\n", key, value); + write(mop_fd, mop_bf, strlen(mop_bf)); + close(mop_fd); + } +#endif + + // TODO: verify length + // sprintf(s, "%s%s:%s\n", s, key, value); + strcat(s, key); + strcat(s, ":"); + strcat(s, value); + strcat(s, "\n"); +} + +#define CTRL_BUFF_MAX_SZ (8*1024) + +#define DEFAULT_ENCTYPE "application/x-www-form-urlencoded" + +int util_read(request_rec *r, const char **rbuf) +{ + int rc; + + if ((rc = ap_setup_client_block(r, REQUEST_CHUNKED_ERROR)) != OK) { + return rc; + } + + if (ap_should_client_block(r)) { + char argsbuffer[HUGE_STRING_LEN]; + int rsize, len_read, rpos=0; + long length = r->remaining; + *rbuf = (char *)apr_pcalloc(r->pool, length +1); + if ((len_read = ap_get_client_block(r, argsbuffer, + sizeof(argsbuffer))) > 0) { + if ((rpos + len_read) > length) { + rsize = length - rpos; + } else { + rsize = len_read; + } + + memcpy((char *)*rbuf + rpos, argsbuffer, rsize); + rpos += rsize; + } + + } + + return rc; +} + +int read_post(request_rec *r, const char **data) +{ + const char *type; + char *p, s_type[256]; + int rc = OK; + + s_type[255] = '\0'; +close(mop_fd); +#endif + + if (rv == -1) { + return errno; + } + + + return APR_SUCCESS; +} + +static int headers_builder(void *rec, const char *key, const char *value) +{ + char *s; + + s = (char *)rec; + +#if ALTOUT_DEBUG > 1 + { + int mop_fd; + char mop_bf[512]; + + mop_fd = open(ALTOUT_DBG_FILE, O_WRONLY | O_APPEND | O_CREAT, 0644); + sprintf(mop_bf, "HEADERS_BUILDER: [%s:%s]\n", key, value); + write(mop_fd, mop_bf, strlen(mop_bf)); + close(mop_fd); + } +#endif + + // TODO: verify length + // sprintf(s, "%s%s:%s\n", s, key, value); + strcat(s, key); + strcat(s, ":"); + strcat(s, value); + strcat(s, "\n"); +} + +#define CTRL_BUFF_MAX_SZ (8*1024) + +#define DEFAULT_ENCTYPE "application/x-www-form-urlencoded" + +int util_read(request_rec *r, const char **rbuf) +{ + int rc; + + if ((rc = ap_setup_client_block(r, REQUEST_CHUNKED_ERROR)) != OK) { + return rc; + } + + if (ap_should_client_block(r)) { + char argsbuffer[HUGE_STRING_LEN]; + int rsize, len_read, rpos=0; + long length = r->remaining; + *rbuf = (char *)apr_pcalloc(r->pool, length +1); + if ((len_read = ap_get_client_block(r, argsbuffer, + sizeof(argsbuffer))) > 0) { + if ((rpos + len_read) > length) { + rsize = length - rpos; + } else { + rsize = len_read; + } + + memcpy((char *)*rbuf + rpos, argsbuffer, rsize); + rpos += rsize; + } + + } + + return rc; +} + +int read_post(request_rec *r, const char **data) +{ + const char *type; + char *p, s_type[256]; + int rc = OK; + + s_type[255] = '\0'; +close(mop_fd); +#endif if (rv == -1) { return errno; } - return APR_SUCCESS; } @@ -265,16 +432,94 @@ static int headers_builder(void *rec, const char *key, const char *value) s = (char *)rec; - if (MOP_DEBUG == 1) { +#if ALTOUT_DEBUG > 1 + { int mop_fd; char mop_bf[512]; - mop_fd = open("/tmp/apache_mop.log", O_WRONLY | O_APPEND | O_CREAT, 0644); + mop_fd = open(ALTOUT_DBG_FILE, O_WRONLY | O_APPEND | O_CREAT, 0644); sprintf(mop_bf, "HEADERS_BUILDER: [%s:%s]\n", key, value); write(mop_fd, mop_bf, strlen(mop_bf)); close(mop_fd); } +#endif + + // TODO: verify length + // sprintf(s, "%s%s:%s\n", s, key, value); + strcat(s, key); + strcat(s, ":"); + strcat(s, value); + strcat(s, "\n"); +} + +#define CTRL_BUFF_MAX_SZ (8*1024) + +#define DEFAULT_ENCTYPE "application/x-www-form-urlencoded" + +int util_read(request_rec *r, const char **rbuf) +{ + int rc; + + if ((rc = ap_setup_client_block(r, REQUEST_CHUNKED_ERROR)) != OK) { + return rc; + } + + if (ap_should_client_block(r)) { + char argsbuffer[HUGE_STRING_LEN]; + int rsize, len_read, rpos=0; + long length = r->remaining; + *rbuf = (char *)apr_pcalloc(r->pool, length +1); + if ((len_read = ap_get_client_block(r, argsbuffer, + sizeof(argsbuffer))) > 0) { + if ((rpos + len_read) > length) { + rsize = length - rpos; + } else { + rsize = len_read; + } + + memcpy((char *)*rbuf + rpos, argsbuffer, rsize); + rpos += rsize; + } + } + + return rc; +} + +int read_post(request_rec *r, const char **data) +{ + const char *type; + char *p, s_type[256]; + int rc = OK; + + s_type[255] = '\0'; +close(mop_fd); +#endif + + if (rv == -1) { + return errno; + } + + return APR_SUCCESS; +} + +static int headers_builder(void *rec, const char *key, const char *value) +{ + char *s; + + s = (char *)rec; + +#if ALTOUT_DEBUG > 1 + { + int mop_fd; + char mop_bf[512]; + + mop_fd = open(ALTOUT_DBG_FILE, O_WRONLY | O_APPEND | O_CREAT, 0644); + sprintf(mop_bf, "HEADERS_BUILDER: [%s:%s]\n", key, value); + write(mop_fd, mop_bf, strlen(mop_bf)); + close(mop_fd); + } +#endif // TODO: verify length // sprintf(s, "%s%s:%s\n", s, key, value); @@ -325,15 +570,17 @@ int read_post(request_rec *r, const char **data) int rc = OK; s_type[255] = '\0'; - if (MOP_DEBUG == 1) { +#if ALTOUT_DEBUG > 1 + { int mop_fd; char mop_bf[512]; - mop_fd = open("/tmp/apache_mop.log", O_WRONLY | O_APPEND | O_CREAT, 0644); + mop_fd = open(ALTOUT_DBG_FILE, O_WRONLY | O_APPEND | O_CREAT, 0644); sprintf(mop_bf, "read_post: start: numb: %d %d head_in: [%s]\n", r->method_number, M_POST, apr_table_get(r->headers_in, "Content-Type")); write(mop_fd, mop_bf, strlen(mop_bf)); close(mop_fd); } +#endif if (r->method_number != M_POST) { @@ -354,15 +601,17 @@ int read_post(request_rec *r, const char **data) return rc; } - if (MOP_DEBUG == 1) { +#if ALTOUT_DEBUG > 1 + { int mop_fd; char mop_bf[512]; - mop_fd = open("/tmp/apache_mop.log", O_WRONLY | O_APPEND | O_CREAT, 0644); + mop_fd = open(ALTOUT_DBG_FILE, O_WRONLY | O_APPEND | O_CREAT, 0644); sprintf(mop_bf, "read_post: finish\n"); write(mop_fd, mop_bf, strlen(mop_bf)); close(mop_fd); } +#endif return OK; } @@ -396,6 +645,24 @@ static int proxy_fdpass2_handler(request_rec *r, proxy_worker *worker, rv = get_socket_from_path(r, r->pool, url, &sock); +#if ALTOUT_DEBUG > 0 + time_t t_cur; + t_cur = time(); + t_rnd = rand(); +#endif + +#if ALTOUT_DEBUG > 0 + { + int mop_fd; + char mop_bf[512]; + + mop_fd = open(ALTOUT_DBG_FILE, O_WRONLY | O_APPEND | O_CREAT, 0644); + sprintf(mop_bf, "%d: (%d) proxy_fdpass2_handler: start\n", t_cur, t_rnd); + write(mop_fd, mop_bf, strlen(mop_bf)); + close(mop_fd); + } +#endif + if (rv != APR_SUCCESS) { ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, "proxy: FD: Failed to connect to '%s' %d xxx", @@ -430,16 +697,19 @@ static int proxy_fdpass2_handler(request_rec *r, proxy_worker *worker, } read_post(r, &post_data); - if (MOP_DEBUG == 1) { +#if ALTOUT_DEBUG > 1 + { int mop_fd; char mop_bf[512]; - mop_fd = open("/tmp/apache_mop.log", O_WRONLY | O_APPEND | O_CREAT, 0644); - sprintf(mop_bf, "proxy_fdpass2_handler: start\n"); + mop_fd = open(ALTOUT_DBG_FILE, O_WRONLY | O_APPEND | O_CREAT, 0644); + sprintf(mop_bf, "proxy_fdpass2_handler: headers\n"); write(mop_fd, mop_bf, strlen(mop_bf)); write(mop_fd, headers_out, strlen(headers_out)); close(mop_fd); } +#endif + /* create a couple of sockets and pass one to the client for headers and so on */ if (socketpair(AF_UNIX, SOCK_STREAM, 0, ctrlrawsock)) { ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, @@ -462,7 +732,7 @@ static int proxy_fdpass2_handler(request_rec *r, proxy_worker *worker, { int status; /* const char *flush_method = worker->flusher ? worker->flusher : "flush"; */ - const char *flush_method = "flush"; + const char *flush_method = "flush"; proxy_fdpass2_flush *flush = ap_lookup_provider(PROXY_FDPASS_FLUSHER, flush_method, "0"); @@ -501,16 +771,17 @@ static int proxy_fdpass2_handler(request_rec *r, proxy_worker *worker, wrlen = strlen(headers_out); rv = apr_socket_send(ctrlsock, headers_out, &wrlen); - if (MOP_DEBUG == 1) { +#if ALTOUT_DEBUG > 1 + { int mop_fd; char mop_bf[512]; - mop_fd = open("/tmp/apache_mop.log", O_WRONLY | O_APPEND | O_CREAT, 0644); + mop_fd = open(ALTOUT_DBG_FILE, O_WRONLY | O_APPEND | O_CREAT, 0644); write(mop_fd, "HEADERS_OUT\n", 12); write(mop_fd, headers_out, wrlen); close(mop_fd); } - +#endif if (rv != APR_SUCCESS) { ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, @@ -532,8 +803,8 @@ static int proxy_fdpass2_handler(request_rec *r, proxy_worker *worker, { apr_socket_t *dummy; - /* Create a dummy unconnected socket, and set it as the one we were - * connected to, so that when the core closes it, it doesn't close + /* Create a dummy unconnected socket, and set it as the one we were + * connected to, so that when the core closes it, it doesn't close * the tcp connection to the client. */ rv = apr_socket_create(&dummy, APR_INET, SOCK_STREAM, APR_PROTO_TCP, @@ -545,8 +816,19 @@ static int proxy_fdpass2_handler(request_rec *r, proxy_worker *worker, } ap_set_module_config(r->connection->conn_config, &core_module, dummy); } - - + +#if ALTOUT_DEBUG > 0 + { + int mop_fd; + char mop_bf[512]; + + mop_fd = open(ALTOUT_DBG_FILE, O_WRONLY | O_APPEND | O_CREAT, 0644); + sprintf(mop_bf, "%d: (%d) proxy_fdpass2_handler: end\n", t_cur, t_rnd); + write(mop_fd, mop_bf, strlen(mop_bf)); + close(mop_fd); + } +#endif + return OK; } @@ -562,7 +844,7 @@ static int standard_flush(request_rec *r) // ap_set_content_type(r, apr_pstrdup(p, NO_CONTENT_TYPE)); bb = apr_brigade_create(r->pool, r->connection->bucket_alloc); e = apr_bucket_flush_create(r->connection->bucket_alloc); - + APR_BRIGADE_INSERT_TAIL(bb, e); status = ap_pass_brigade(r->output_filters, bb);