diff options
author | Omar Polo <op@omarpolo.com> | 2022-01-01 20:16:14 +0000 |
---|---|---|
committer | Omar Polo <op@omarpolo.com> | 2022-01-01 20:16:14 +0000 |
commit | 593e412b4988ca8b72bb7ef9b1cc663cb1184215 (patch) | |
tree | 55599fb76ec918b214af8b5fd7e4e1af1d51aaca /proxy.c | |
parent | 294a57275af3dafa948119e60a8db979be25e1f7 (diff) |
allow to disable TLS when proxying requests
Diffstat (limited to 'proxy.c')
-rw-r--r-- | proxy.c | 81 |
1 files changed, 53 insertions, 28 deletions
@@ -231,12 +231,42 @@ proxy_error(struct bufferevent *bev, short error, void *d) } static void -proxy_handshake(int fd, short event, void *d) +proxy_enqueue_req(struct client *c) { - struct client *c = d; + struct proxy *p = &c->host->proxy; struct evbuffer *evb; char iribuf[GEMINI_URL_LEN]; + c->proxybev = bufferevent_new(c->pfd, proxy_read, proxy_write, + proxy_error, c); + if (c->proxybev == NULL) + fatal("can't allocate bufferevent: %s", strerror(errno)); + + if (!p->notls) { + event_set(&c->proxybev->ev_read, c->pfd, EV_READ, + proxy_tls_readcb, c->proxybev); + event_set(&c->proxybev->ev_write, c->pfd, EV_WRITE, + proxy_tls_writecb, c->proxybev); + +#if HAVE_LIBEVENT2 + evbuffer_unfreeze(c->proxybev->input, 0); + evbuffer_unfreeze(c->proxybev->output, 1); +#endif + } + + serialize_iri(&c->iri, iribuf, sizeof(iribuf)); + + evb = EVBUFFER_OUTPUT(c->proxybev); + evbuffer_add_printf(evb, "%s\r\n", iribuf); + + bufferevent_enable(c->proxybev, EV_READ|EV_WRITE); +} + +static void +proxy_handshake(int fd, short event, void *d) +{ + struct client *c = d; + if (event == EV_TIMEOUT) { start_reply(c, PROXY_ERROR, "timeout"); return; @@ -258,37 +288,15 @@ proxy_handshake(int fd, short event, void *d) return; } - c->proxybev = bufferevent_new(c->pfd, proxy_read, proxy_write, - proxy_error, c); - if (c->proxybev == NULL) - fatal("can't allocate bufferevent: %s", strerror(errno)); - - event_set(&c->proxybev->ev_read, c->pfd, EV_READ, - proxy_tls_readcb, c->proxybev); - event_set(&c->proxybev->ev_write, c->pfd, EV_WRITE, - proxy_tls_writecb, c->proxybev); - -#if HAVE_LIBEVENT2 - evbuffer_unfreeze(c->proxybev->input, 0); - evbuffer_unfreeze(c->proxybev->output, 1); -#endif - - serialize_iri(&c->iri, iribuf, sizeof(iribuf)); - - evb = EVBUFFER_OUTPUT(c->proxybev); - evbuffer_add_printf(evb, "%s\r\n", iribuf); - - bufferevent_enable(c->proxybev, EV_READ|EV_WRITE); + proxy_enqueue_req(c); } -int -proxy_init(struct client *c) +static int +proxy_setup_tls(struct client *c) { struct proxy *p = &c->host->proxy; struct tls_config *conf = NULL; - c->type = REQUEST_PROXY; - if ((conf = tls_config_new()) == NULL) return -1; @@ -327,7 +335,24 @@ proxy_init(struct client *c) err: tls_config_free(conf); - if (c->proxyctx != NULL) + if (c->proxyctx != NULL) { tls_free(c->proxyctx); + c->proxyctx = NULL; + } return -1; } + +int +proxy_init(struct client *c) +{ + struct proxy *p = &c->host->proxy; + + c->type = REQUEST_PROXY; + + if (p->notls) { + proxy_enqueue_req(c); + return 0; + } + + return proxy_setup_tls(c); +} |