remove reverse proxy, it should be handled by the plugin

This commit is contained in:
lxsang 2021-01-23 16:20:16 +01:00
parent 1f143327e4
commit 6c4713ac62
3 changed files with 7 additions and 211 deletions

View File

@ -46,15 +46,15 @@ ssl.enable=0
; this is helpful to redirect many sub domains
; to a sub folder of the same server
; ^([a-zA-Z][a-zA-Z0-9]*)\.[a-zA-Z0-9]+\..*$ = /<1><url>?<query>
; example of reverse proxy
; ^\/os\/+(.*)$ = http://localhost:80/os/router.lua?r=<1>&<query>
; example of reverse proxy with the proxy plugin
; ^\/os\/+(.*)$ = /proxy/http://localhost:80/os/router.lua?r=<1>&<query>
; Sytax: [regular expression on the original request]=[new request rule]
[PORT:80]
htdocs=/opt/www/htdocs
; enable or disable SSL
ssl.enable=0
^\/os\/+(.*)$ = http://localhost:443/test.html?<query>
; ^\/os\/+(.*)$ = /proxy/http://localhost:443/test.html?<query>
; other config shoud be rules applied on this port
; For example the following rule will
; convert a request of type:

View File

@ -755,201 +755,6 @@ char *apply_rules(dictionary_t rules, const char *host, char *url)
return strdup(query_string);
}
static void *proxy_monitor(void *data)
{
antd_request_t *rq = (antd_request_t *)data;
antd_client_t *proxy = (antd_client_t *)dvalue(rq->request, "PROXY_HANDLE");
char* method = (char *)dvalue(rq->request, "METHOD");
antd_task_t *task = antd_create_task(NULL, data, NULL, rq->client->last_io);
int ret, sz;
struct pollfd pfd[2];
char *buf = NULL;
pfd[0].fd = rq->client->sock;
pfd[1].fd = proxy->sock;
pfd[1].events = POLLIN;
pfd[0].events = POLLIN;
if(rq->client->ssl)
{
pfd[0].events |= POLLOUT;
}
// select
ret = poll(pfd, 2, POLL_EVENT_TO);
if (ret > 0)
{
if(
pfd[0].revents & POLLERR ||
pfd[0].revents & POLLRDHUP ||
pfd[0].revents & POLLHUP ||
pfd[0].revents & POLLNVAL||
pfd[1].revents & POLLERR ||
pfd[1].revents & POLLHUP ||
pfd[1].revents & POLLRDHUP ||
pfd[1].revents & POLLNVAL
)
{
(void)close(proxy->sock);
return task;
}
buf = (char *)malloc(BUFFLEN);
memset(buf, '\0', BUFFLEN);
if ((pfd[0].revents & POLLIN) || (pfd[0].revents & POLLOUT))
{
sz = antd_recv_upto(rq->client, buf, BUFFLEN);
if ( (sz < 0) || (sz == 0 && !rq->client->ssl) || ( antd_send(proxy, buf, sz) != sz ))
{
free(buf);
(void)close(proxy->sock);
return task;
}
}
if (pfd[1].revents & POLLIN)
{
if ((sz = antd_recv_upto(proxy, buf, BUFFLEN)) <= 0 || antd_send(rq->client, buf, sz) != sz)
{
free(buf);
(void)close(proxy->sock);
return task;
}
}
free(buf);
}
if (ret < 0)
{
(void)close(proxy->sock);
return task;
}
task->handle = proxy_monitor;
task->access_time = rq->client->last_io;
// register event
if(rq->client->ssl && (EQU(method, "POST") || ws_enable(rq->request)))
{
antd_task_bind_event(task, rq->client->sock, 0, TASK_EVT_ON_READABLE | TASK_EVT_ON_WRITABLE);
}
else
{
antd_task_bind_event(task, rq->client->sock, 0, TASK_EVT_ON_READABLE );
}
antd_task_bind_event(task, proxy->sock, 0, TASK_EVT_ON_READABLE);
return task;
}
static void *proxify(void *data)
{
int sock_fd, size, ret;
char *str = NULL;
chain_t it;
antd_request_t *rq = (antd_request_t *)data;
antd_client_t *proxy = NULL;
rq->client->state = ANTD_CLIENT_RESOLVE_REQUEST;
char *host = dvalue(rq->request, "PROXY_HOST");
int port = atoi(dvalue(rq->request, "PROXY_PORT"));
char *path = dvalue(rq->request, "PROXY_PATH");
char *query = dvalue(rq->request, "PROXY_QUERY");
dictionary_t xheader = dvalue(rq->request, "REQUEST_HEADER");
char* method = (char *)dvalue(rq->request, "METHOD");
antd_task_t *task = antd_create_task(NULL, data, NULL, rq->client->last_io);
if (!xheader)
{
antd_error(rq->client, 400, "Badd Request");
return task;
}
sock_fd = request_socket(ip_from_hostname(host), port);
if (sock_fd == -1)
{
antd_error(rq->client, 503, "Service Unavailable");
return task;
}
set_nonblock(sock_fd);
proxy = (antd_client_t *)malloc(sizeof(antd_client_t));
proxy->sock = sock_fd;
proxy->ssl = NULL;
proxy->zstream = NULL;
proxy->z_level = ANTD_CNONE;
// store content length here
dput(rq->request, "PROXY_HANDLE", proxy);
str = __s("%s %s?%s HTTP/1.1\r\n", (char *)dvalue(rq->request, "METHOD"), path, query);
size = strlen(str);
ret = antd_send(proxy, str, size);
free(str);
if (ret != size)
{
antd_error(rq->client, 500, "");
(void)close(sock_fd);
return task;
}
for_each_assoc(it, xheader)
{
str = __s("%s: %s\r\n", it->key, (char *)it->value);
size = strlen(str);
ret = antd_send(proxy, str, size);
free(str);
if (ret != size)
{
antd_error(rq->client, 500, "");
(void)close(sock_fd);
return task;
}
}
(void)antd_send(proxy, "\r\n", 2);
// now monitor the proxy
task->handle = proxy_monitor;
task->access_time = rq->client->last_io;
// register event
if(rq->client->ssl && (EQU(method, "POST") || ws_enable(rq->request)))
{
antd_task_bind_event(task, rq->client->sock, 0, TASK_EVT_ON_READABLE | TASK_EVT_ON_WRITABLE);
}
else
{
antd_task_bind_event(task, rq->client->sock, 0, TASK_EVT_ON_READABLE );
}
antd_task_bind_event(task, proxy->sock, 0, TASK_EVT_ON_READABLE);
return task;
}
/**
* Check if the current request is e reverse proxy
* return a proxy task if this is the case
*/
static void *check_proxy(antd_request_t *rq, const char *path, const char *query)
{
char *pattern = "^(https?)://([^:]+):([0-9]+)(.*)$";
char buff[256];
regmatch_t matches[5];
int ret, size;
ret = regex_match(pattern, path, 5, matches);
if (!ret)
{
return NULL;
}
if (matches[1].rm_eo - matches[1].rm_so == 5)
{
// https is not supported for now
// TODO add https support
antd_error(rq->client, 503, "Service Unavailable");
return antd_create_task(NULL, (void *)rq, NULL, time(NULL));
}
// http proxy request
size = matches[2].rm_eo - matches[2].rm_so < (int)sizeof(buff) ? matches[2].rm_eo - matches[2].rm_so : (int)sizeof(buff);
(void)memcpy(buff, path + matches[2].rm_so, size);
buff[size] = '\0';
dput(rq->request, "PROXY_HOST", strdup(buff));
size = matches[3].rm_eo - matches[3].rm_so < (int)sizeof(buff) ? matches[3].rm_eo - matches[3].rm_so : (int)sizeof(buff);
(void)memcpy(buff, path + matches[3].rm_so, size);
buff[size] = '\0';
dput(rq->request, "PROXY_PORT", strdup(buff));
dput(rq->request, "PROXY_PATH", strdup(path + matches[4].rm_so));
dput(rq->request, "PROXY_QUERY", strdup(query));
return antd_create_task(proxify, (void *)rq, NULL, rq->client->last_io);
}
/**
* Decode the HTTP request header
*/
@ -1060,15 +865,6 @@ void *decode_request_header(void *data)
dput(rq->request, "COOKIE", cookie);
if (host)
free(host);
// check if this is a reverse proxy ?
task = check_proxy(rq, buf, query);
if (task)
{
if (query)
free(query);
return task;
}
// otherwise it a normal query
dput(rq->request, "RESOURCE_PATH", url_decode(buf));
if (query)
{

View File

@ -302,12 +302,12 @@ void antd_scheduler_destroy_data(void *data)
int antd_task_data_id(void *data)
{
/*antd_request_t *rq = (antd_request_t *)data;
antd_request_t *rq = (antd_request_t *)data;
if(!rq)
return 0;
return antd_scheduler_next_id(scheduler,rq->client->sock);*/
UNUSED(data);
return antd_scheduler_next_id(scheduler,0);
return antd_scheduler_next_id(scheduler,rq->client->sock);
//UNUSED(data);
//return antd_scheduler_next_id(scheduler,0);
}
int main(int argc, char *argv[])