1709c21c4SNick Mathewson 2709c21c4SNick Mathewson #include <stdio.h> 3709c21c4SNick Mathewson #include <assert.h> 4709c21c4SNick Mathewson #include <stdlib.h> 5709c21c4SNick Mathewson #include <string.h> 6709c21c4SNick Mathewson #include <errno.h> 7709c21c4SNick Mathewson 8709c21c4SNick Mathewson #include <sys/socket.h> 9709c21c4SNick Mathewson #include <netinet/in.h> 10709c21c4SNick Mathewson 11709c21c4SNick Mathewson #include <event2/bufferevent_ssl.h> 12709c21c4SNick Mathewson #include <event2/bufferevent.h> 13709c21c4SNick Mathewson #include <event2/buffer.h> 14709c21c4SNick Mathewson #include <event2/listener.h> 15709c21c4SNick Mathewson #include <event2/util.h> 16709c21c4SNick Mathewson 17709c21c4SNick Mathewson #include <openssl/ssl.h> 18709c21c4SNick Mathewson #include <openssl/err.h> 19709c21c4SNick Mathewson #include <openssl/rand.h> 20709c21c4SNick Mathewson 21709c21c4SNick Mathewson static struct event_base *base; 22709c21c4SNick Mathewson static struct sockaddr_storage listen_on_addr; 23709c21c4SNick Mathewson static struct sockaddr_storage connect_to_addr; 24709c21c4SNick Mathewson static int connect_to_addrlen; 25709c21c4SNick Mathewson static int use_wrapper = 1; 26709c21c4SNick Mathewson 27709c21c4SNick Mathewson static SSL_CTX *ssl_ctx = NULL; 28709c21c4SNick Mathewson 29*c119e4a1SNick Mathewson #define MAX_OUTPUT (512*1024) 30*c119e4a1SNick Mathewson 31*c119e4a1SNick Mathewson static void drained_writecb(struct bufferevent *bev, void *ctx); 32*c119e4a1SNick Mathewson static void eventcb(struct bufferevent *bev, short what, void *ctx); 33*c119e4a1SNick Mathewson 34709c21c4SNick Mathewson static void 35709c21c4SNick Mathewson readcb(struct bufferevent *bev, void *ctx) 36709c21c4SNick Mathewson { 37709c21c4SNick Mathewson struct bufferevent *partner = ctx; 38709c21c4SNick Mathewson struct evbuffer *src, *dst; 39*c119e4a1SNick Mathewson size_t len; 40709c21c4SNick Mathewson src = bufferevent_get_input(bev); 41*c119e4a1SNick Mathewson len = evbuffer_get_length(src); 42709c21c4SNick Mathewson if (!partner) { 43*c119e4a1SNick Mathewson evbuffer_drain(src, len); 44709c21c4SNick Mathewson return; 45709c21c4SNick Mathewson } 46709c21c4SNick Mathewson dst = bufferevent_get_output(partner); 47709c21c4SNick Mathewson evbuffer_add_buffer(dst, src); 48*c119e4a1SNick Mathewson 49*c119e4a1SNick Mathewson if (evbuffer_get_length(dst) >= MAX_OUTPUT) { 50*c119e4a1SNick Mathewson /* We're giving the other side data faster than it can 51*c119e4a1SNick Mathewson * pass it on. Stop reading here until we have drained the 52*c119e4a1SNick Mathewson * other side to MAX_OUTPUT/2 bytes. */ 53*c119e4a1SNick Mathewson bufferevent_setcb(partner, readcb, drained_writecb, 54*c119e4a1SNick Mathewson eventcb, bev); 55*c119e4a1SNick Mathewson bufferevent_setwatermark(partner, EV_WRITE, MAX_OUTPUT/2, 56*c119e4a1SNick Mathewson MAX_OUTPUT); 57*c119e4a1SNick Mathewson bufferevent_disable(bev, EV_READ); 58*c119e4a1SNick Mathewson } 59*c119e4a1SNick Mathewson } 60*c119e4a1SNick Mathewson 61*c119e4a1SNick Mathewson static void 62*c119e4a1SNick Mathewson drained_writecb(struct bufferevent *bev, void *ctx) 63*c119e4a1SNick Mathewson { 64*c119e4a1SNick Mathewson struct bufferevent *partner = ctx; 65*c119e4a1SNick Mathewson 66*c119e4a1SNick Mathewson /* We were choking the other side until we drained our outbuf a bit. 67*c119e4a1SNick Mathewson * Now it seems drained. */ 68*c119e4a1SNick Mathewson bufferevent_setcb(bev, readcb, NULL, eventcb, partner); 69*c119e4a1SNick Mathewson bufferevent_setwatermark(bev, EV_WRITE, 0, 0); 70*c119e4a1SNick Mathewson if (partner) 71*c119e4a1SNick Mathewson bufferevent_enable(partner, EV_READ); 72709c21c4SNick Mathewson } 73709c21c4SNick Mathewson 74709c21c4SNick Mathewson static void 75709c21c4SNick Mathewson close_on_finished_writecb(struct bufferevent *bev, void *ctx) 76709c21c4SNick Mathewson { 77709c21c4SNick Mathewson struct evbuffer *b = bufferevent_get_output(bev); 78*c119e4a1SNick Mathewson 79709c21c4SNick Mathewson if (evbuffer_get_length(b) == 0) { 80709c21c4SNick Mathewson bufferevent_free(bev); 81709c21c4SNick Mathewson } 82709c21c4SNick Mathewson } 83709c21c4SNick Mathewson 84709c21c4SNick Mathewson static void 85709c21c4SNick Mathewson eventcb(struct bufferevent *bev, short what, void *ctx) 86709c21c4SNick Mathewson { 87709c21c4SNick Mathewson struct bufferevent *partner = ctx; 88709c21c4SNick Mathewson 89709c21c4SNick Mathewson if (what & (BEV_EVENT_EOF|BEV_EVENT_ERROR)) { 90709c21c4SNick Mathewson if (what & BEV_EVENT_ERROR) 91709c21c4SNick Mathewson perror("maybe an error"); 92709c21c4SNick Mathewson 93709c21c4SNick Mathewson if (partner) { 94709c21c4SNick Mathewson /* Flush all pending data */ 95709c21c4SNick Mathewson readcb(bev, ctx); 96709c21c4SNick Mathewson 97*c119e4a1SNick Mathewson if (evbuffer_get_length( 98*c119e4a1SNick Mathewson bufferevent_get_output(partner))) { 99*c119e4a1SNick Mathewson /* We still have to flush data from the other 100*c119e4a1SNick Mathewson * side, but when that's done, close the other 101*c119e4a1SNick Mathewson * side. */ 102709c21c4SNick Mathewson bufferevent_setcb(partner, 103709c21c4SNick Mathewson NULL, close_on_finished_writecb, 104709c21c4SNick Mathewson eventcb, NULL); 105*c119e4a1SNick Mathewson bufferevent_disable(partner, EV_READ); 106*c119e4a1SNick Mathewson } else { 107*c119e4a1SNick Mathewson /* We have nothing left to say to the other 108*c119e4a1SNick Mathewson * side; close it. */ 109*c119e4a1SNick Mathewson bufferevent_free(partner); 110*c119e4a1SNick Mathewson } 111709c21c4SNick Mathewson } 112709c21c4SNick Mathewson bufferevent_free(bev); 113709c21c4SNick Mathewson } 114709c21c4SNick Mathewson } 115709c21c4SNick Mathewson 116709c21c4SNick Mathewson static void 117709c21c4SNick Mathewson syntax(void) 118709c21c4SNick Mathewson { 119709c21c4SNick Mathewson fputs("Syntax:\n", stderr); 120709c21c4SNick Mathewson fputs(" le-proxy [-s] [-W] <listen-on-addr> <connect-to-addr>\n", stderr); 121709c21c4SNick Mathewson fputs("Example:\n", stderr); 122709c21c4SNick Mathewson fputs(" le-proxy 127.0.0.1:8888 1.2.3.4:80\n", stderr); 123709c21c4SNick Mathewson 124709c21c4SNick Mathewson exit(1); 125709c21c4SNick Mathewson } 126709c21c4SNick Mathewson 127709c21c4SNick Mathewson static void 128709c21c4SNick Mathewson accept_cb(struct evconnlistener *listener, evutil_socket_t fd, 129709c21c4SNick Mathewson struct sockaddr *a, int slen, void *p) 130709c21c4SNick Mathewson { 131709c21c4SNick Mathewson struct bufferevent *b_out, *b_in; 132709c21c4SNick Mathewson /* Create two linked bufferevent objects: one to connect, one for the 133709c21c4SNick Mathewson * new connection */ 134709c21c4SNick Mathewson b_in = bufferevent_socket_new(base, fd, 135709c21c4SNick Mathewson BEV_OPT_CLOSE_ON_FREE|BEV_OPT_DEFER_CALLBACKS); 136709c21c4SNick Mathewson 137709c21c4SNick Mathewson if (!ssl_ctx || use_wrapper) 138709c21c4SNick Mathewson b_out = bufferevent_socket_new(base, -1, 139709c21c4SNick Mathewson BEV_OPT_CLOSE_ON_FREE|BEV_OPT_DEFER_CALLBACKS); 140709c21c4SNick Mathewson else { 141709c21c4SNick Mathewson SSL *ssl = SSL_new(ssl_ctx); 142709c21c4SNick Mathewson b_out = bufferevent_openssl_socket_new(base, -1, ssl, 143709c21c4SNick Mathewson BUFFEREVENT_SSL_CONNECTING, 144709c21c4SNick Mathewson BEV_OPT_CLOSE_ON_FREE|BEV_OPT_DEFER_CALLBACKS); 145709c21c4SNick Mathewson } 146709c21c4SNick Mathewson 147709c21c4SNick Mathewson assert(b_in && b_out); 148709c21c4SNick Mathewson 149709c21c4SNick Mathewson if (bufferevent_socket_connect(b_out, 150709c21c4SNick Mathewson (struct sockaddr*)&connect_to_addr, connect_to_addrlen)<0) { 151709c21c4SNick Mathewson perror("bufferevent_socket_connect"); 152709c21c4SNick Mathewson bufferevent_free(b_out); 153709c21c4SNick Mathewson bufferevent_free(b_in); 154709c21c4SNick Mathewson return; 155709c21c4SNick Mathewson } 156709c21c4SNick Mathewson 157709c21c4SNick Mathewson if (ssl_ctx && use_wrapper) { 158709c21c4SNick Mathewson struct bufferevent *b_ssl; 159709c21c4SNick Mathewson SSL *ssl = SSL_new(ssl_ctx); 160709c21c4SNick Mathewson b_ssl = bufferevent_openssl_filter_new(base, 161709c21c4SNick Mathewson b_out, ssl, BUFFEREVENT_SSL_CONNECTING, 162709c21c4SNick Mathewson BEV_OPT_CLOSE_ON_FREE|BEV_OPT_DEFER_CALLBACKS); 163709c21c4SNick Mathewson if (!b_ssl) { 164709c21c4SNick Mathewson perror("Bufferevent_openssl_new"); 165709c21c4SNick Mathewson bufferevent_free(b_out); 166709c21c4SNick Mathewson bufferevent_free(b_in); 167709c21c4SNick Mathewson } 168709c21c4SNick Mathewson b_out = b_ssl; 169709c21c4SNick Mathewson } 170709c21c4SNick Mathewson 171709c21c4SNick Mathewson bufferevent_setcb(b_in, readcb, NULL, eventcb, b_out); 172709c21c4SNick Mathewson bufferevent_setcb(b_out, readcb, NULL, eventcb, b_in); 173709c21c4SNick Mathewson 174709c21c4SNick Mathewson bufferevent_enable(b_in, EV_READ|EV_WRITE); 175709c21c4SNick Mathewson bufferevent_enable(b_out, EV_READ|EV_WRITE); 176709c21c4SNick Mathewson } 177709c21c4SNick Mathewson 178709c21c4SNick Mathewson int 179709c21c4SNick Mathewson main(int argc, char **argv) 180709c21c4SNick Mathewson { 181709c21c4SNick Mathewson int i; 182709c21c4SNick Mathewson int socklen; 183709c21c4SNick Mathewson 184709c21c4SNick Mathewson int use_ssl = 0; 185709c21c4SNick Mathewson struct evconnlistener *listener; 186709c21c4SNick Mathewson 187709c21c4SNick Mathewson if (argc < 3) 188709c21c4SNick Mathewson syntax(); 189709c21c4SNick Mathewson 190709c21c4SNick Mathewson for (i=1; i < argc; ++i) { 191709c21c4SNick Mathewson if (!strcmp(argv[i], "-s")) { 192709c21c4SNick Mathewson use_ssl = 1; 193709c21c4SNick Mathewson } else if (!strcmp(argv[i], "-W")) { 194709c21c4SNick Mathewson use_wrapper = 0; 195709c21c4SNick Mathewson } else if (argv[i][0] == '-') { 196709c21c4SNick Mathewson syntax(); 197709c21c4SNick Mathewson } else 198709c21c4SNick Mathewson break; 199709c21c4SNick Mathewson } 200709c21c4SNick Mathewson 201709c21c4SNick Mathewson if (i+2 != argc) 202709c21c4SNick Mathewson syntax(); 203709c21c4SNick Mathewson 204709c21c4SNick Mathewson memset(&listen_on_addr, 0, sizeof(listen_on_addr)); 205709c21c4SNick Mathewson socklen = sizeof(listen_on_addr); 206709c21c4SNick Mathewson if (evutil_parse_sockaddr_port(argv[i], 207709c21c4SNick Mathewson (struct sockaddr*)&listen_on_addr, &socklen)<0) { 208709c21c4SNick Mathewson int p = atoi(argv[i]); 209709c21c4SNick Mathewson struct sockaddr_in *sin = (struct sockaddr_in*)&listen_on_addr; 210709c21c4SNick Mathewson if (p < 1 || p > 65535) 211709c21c4SNick Mathewson syntax(); 212709c21c4SNick Mathewson sin->sin_port = htons(p); 213709c21c4SNick Mathewson sin->sin_addr.s_addr = htonl(0x7f000001); 214709c21c4SNick Mathewson sin->sin_family = AF_INET; 215709c21c4SNick Mathewson socklen = sizeof(struct sockaddr_in); 216709c21c4SNick Mathewson } 217709c21c4SNick Mathewson 218709c21c4SNick Mathewson memset(&connect_to_addr, 0, sizeof(connect_to_addr)); 219709c21c4SNick Mathewson connect_to_addrlen = sizeof(connect_to_addr); 220709c21c4SNick Mathewson if (evutil_parse_sockaddr_port(argv[i+1], 221709c21c4SNick Mathewson (struct sockaddr*)&connect_to_addr, &connect_to_addrlen)<0) 222709c21c4SNick Mathewson syntax(); 223709c21c4SNick Mathewson 224709c21c4SNick Mathewson base = event_base_new(); 225709c21c4SNick Mathewson if (!base) { 226709c21c4SNick Mathewson perror("event_base_new()"); 227709c21c4SNick Mathewson return 1; 228709c21c4SNick Mathewson } 229709c21c4SNick Mathewson 230709c21c4SNick Mathewson if (use_ssl) { 231709c21c4SNick Mathewson int r; 232709c21c4SNick Mathewson SSL_library_init(); 233709c21c4SNick Mathewson ERR_load_crypto_strings(); 234709c21c4SNick Mathewson SSL_load_error_strings(); 235709c21c4SNick Mathewson OpenSSL_add_all_algorithms(); 236709c21c4SNick Mathewson r = RAND_poll(); 237709c21c4SNick Mathewson if (r == 0) { 238709c21c4SNick Mathewson fprintf(stderr, "RAND_poll() failed.\n"); 239709c21c4SNick Mathewson return 1; 240709c21c4SNick Mathewson } 241709c21c4SNick Mathewson ssl_ctx = SSL_CTX_new(SSLv23_method()); 242709c21c4SNick Mathewson } 243709c21c4SNick Mathewson 244709c21c4SNick Mathewson listener = evconnlistener_new_bind(base, accept_cb, NULL, 245709c21c4SNick Mathewson LEV_OPT_CLOSE_ON_FREE|LEV_OPT_CLOSE_ON_EXEC|LEV_OPT_REUSEABLE, 246709c21c4SNick Mathewson -1, (struct sockaddr*)&listen_on_addr, socklen); 247709c21c4SNick Mathewson 248709c21c4SNick Mathewson event_base_dispatch(base); 249709c21c4SNick Mathewson 250709c21c4SNick Mathewson return 0; 251709c21c4SNick Mathewson } 252