2 * Copyright (c) 2021, 2022 Omar Polo <op@omarpolo.com>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23#define MIN(a, b) ((a) < (b) ? (a) : (b))
25static const struct timeval handshake_timeout = { 5, 0 };
27static void proxy_tls_readcb(int, short, void *);
28static void proxy_tls_writecb(int, short, void *);
29static void proxy_read(struct bufferevent *, void *);
30static void proxy_write(struct bufferevent *, void *);
31static void proxy_error(struct bufferevent *, short, void *);
34proxy_tls_readcb(int fd, short event, void *d)
36 struct bufferevent *bufev = d;
37 struct client *c = bufev->cbarg;
38 char buf[IBUF_READ_SIZE];
39 int what = EVBUFFER_READ;
40 int howmuch = IBUF_READ_SIZE;
45 if (event == EV_TIMEOUT) {
46 what |= EVBUFFER_TIMEOUT;
50 if (bufev->wm_read.high != 0)
51 howmuch = MIN(sizeof(buf), bufev->wm_read.high);
53 switch (ret = tls_read(c->proxyctx, buf, howmuch)) {
55 case TLS_WANT_POLLOUT:
58 what |= EVBUFFER_ERROR;
68 res = evbuffer_add(bufev->input, buf, len);
70 what |= EVBUFFER_ERROR;
74 event_add(&bufev->ev_read, NULL);
76 len = EVBUFFER_LENGTH(bufev->input);
77 if (bufev->wm_read.low != 0 && len < bufev->wm_read.low)
80 if (bufev->readcb != NULL)
81 (*bufev->readcb)(bufev, bufev->cbarg);
85 event_add(&bufev->ev_read, NULL);
89 (*bufev->errorcb)(bufev, what, bufev->cbarg);
93proxy_tls_writecb(int fd, short event, void *d)
95 struct bufferevent *bufev = d;
96 struct client *c = bufev->cbarg;
99 short what = EVBUFFER_WRITE;
101 if (event & EV_TIMEOUT) {
102 what |= EVBUFFER_TIMEOUT;
106 if (EVBUFFER_LENGTH(bufev->output) != 0) {
107 ret = tls_write(c->proxyctx, EVBUFFER_DATA(bufev->output),
108 EVBUFFER_LENGTH(bufev->output));
110 case TLS_WANT_POLLIN:
111 case TLS_WANT_POLLOUT:
114 what |= EVBUFFER_ERROR;
119 evbuffer_drain(bufev->output, len);
122 if (EVBUFFER_LENGTH(bufev->output) != 0)
123 event_add(&bufev->ev_write, NULL);
125 if (bufev->writecb != NULL &&
126 EVBUFFER_LENGTH(bufev->output) <= bufev->wm_write.low)
127 (*bufev->writecb)(bufev, bufev->cbarg);
131 event_add(&bufev->ev_write, NULL);
135 (*bufev->errorcb)(bufev, what, bufev->cbarg);
139proxy_read(struct bufferevent *bev, void *d)
141 struct client *c = d;
142 struct evbuffer *src = EVBUFFER_INPUT(bev);
147 /* intercept the header */
149 hdr = evbuffer_readln(src, &len, EVBUFFER_EOL_CRLF_STRICT);
151 /* max reply + \r\n */
152 if (EVBUFFER_LENGTH(src) > 1029) {
153 log_warn(c, "upstream server is trying to "
154 "send a header that's too long.");
155 proxy_error(bev, EVBUFFER_READ, c);
162 if (len < 3 || len > 1029 ||
167 log_warn(c, "upstream server is trying to send a "
168 "header that's too long.");
169 proxy_error(bev, EVBUFFER_READ, c);
174 code = (hdr[0] - '0') * 10 + (hdr[1] - '0');
176 if (code < 10 || code >= 70) {
177 log_warn(c, "upstream server is trying to send an "
178 "invalid reply code: %d", code);
179 proxy_error(bev, EVBUFFER_READ, c);
183 start_reply(c, code, hdr + 3);
185 if (c->code < 20 || c->code > 29) {
186 proxy_error(bev, EVBUFFER_EOF, c);
191 bufferevent_write_buffer(c->bev, src);
195proxy_write(struct bufferevent *bev, void *d)
197 struct evbuffer *dst = EVBUFFER_OUTPUT(bev);
199 /* request successfully sent */
200 if (EVBUFFER_LENGTH(dst) == 0)
201 bufferevent_disable(bev, EV_WRITE);
205proxy_error(struct bufferevent *bev, short error, void *d)
207 struct client *c = d;
210 * If we're here it means that some kind of non-recoverable
214 bufferevent_free(bev);
217 tls_free(c->proxyctx);
223 /* EOF and no header */
225 start_reply(c, PROXY_ERROR, "protocol error");
229 c->type = REQUEST_DONE;
230 client_write(c->bev, c);
234proxy_enqueue_req(struct client *c)
236 struct proxy *p = c->proxy;
237 struct evbuffer *evb;
238 char iribuf[GEMINI_URL_LEN];
240 c->proxybev = bufferevent_new(c->pfd, proxy_read, proxy_write,
242 if (c->proxybev == NULL)
243 fatal("can't allocate bufferevent: %s", strerror(errno));
246 event_set(&c->proxybev->ev_read, c->pfd, EV_READ,
247 proxy_tls_readcb, c->proxybev);
248 event_set(&c->proxybev->ev_write, c->pfd, EV_WRITE,
249 proxy_tls_writecb, c->proxybev);
252 evbuffer_unfreeze(c->proxybev->input, 0);
253 evbuffer_unfreeze(c->proxybev->output, 1);
257 serialize_iri(&c->iri, iribuf, sizeof(iribuf));
259 evb = EVBUFFER_OUTPUT(c->proxybev);
260 evbuffer_add_printf(evb, "%s\r\n", iribuf);
262 bufferevent_enable(c->proxybev, EV_READ|EV_WRITE);
266proxy_handshake(int fd, short event, void *d)
268 struct client *c = d;
270 if (event == EV_TIMEOUT) {
271 start_reply(c, PROXY_ERROR, "timeout");
275 switch (tls_handshake(c->proxyctx)) {
276 case TLS_WANT_POLLIN:
277 event_set(&c->proxyev, fd, EV_READ, proxy_handshake, c);
278 event_add(&c->proxyev, &handshake_timeout);
280 case TLS_WANT_POLLOUT:
281 event_set(&c->proxyev, fd, EV_WRITE, proxy_handshake, c);
282 event_add(&c->proxyev, &handshake_timeout);
285 log_warn(c, "handshake with proxy failed: %s",
286 tls_error(c->proxyctx));
287 start_reply(c, PROXY_ERROR, "handshake failed");
292 proxy_enqueue_req(c);
296proxy_setup_tls(struct client *c)
298 struct proxy *p = c->proxy;
299 struct tls_config *conf = NULL;
302 if ((conf = tls_config_new()) == NULL)
306 tls_config_insecure_noverifyname(conf);
308 tls_config_insecure_noverifycert(conf);
309 tls_config_set_protocols(conf, p->protocols);
311 if (p->cert != NULL) {
314 r = tls_config_set_cert_mem(conf, p->cert, p->certlen);
318 r = tls_config_set_key_mem(conf, p->key, p->keylen);
323 if ((c->proxyctx = tls_client()) == NULL)
326 if (tls_configure(c->proxyctx, conf) == -1)
329 if ((hn = p->sni) == NULL)
331 if (tls_connect_socket(c->proxyctx, c->pfd, hn) == -1)
335 event_set(&c->proxyev, c->pfd, EV_READ|EV_WRITE, proxy_handshake, c);
336 event_add(&c->proxyev, &handshake_timeout);
338 tls_config_free(conf);
342 tls_config_free(conf);
343 if (c->proxyctx != NULL) {
344 tls_free(c->proxyctx);
351proxy_init(struct client *c)
353 struct proxy *p = c->proxy;
355 if (!p->notls && proxy_setup_tls(c) == -1)
358 proxy_enqueue_req(c);
360 c->type = REQUEST_PROXY;