From: Simon Ruderich Date: Sun, 27 Feb 2011 01:47:01 +0000 (+0100) Subject: tlsproxy.c: Add -proxy command line option to use another TLS proxy. X-Git-Tag: 0.1~44 X-Git-Url: https://ruderich.org/simon/gitweb/?a=commitdiff_plain;h=d68c5b70dc46a20fc73251ccecf751ad8f1ee943;p=tlsproxy%2Ftlsproxy.git tlsproxy.c: Add -proxy command line option to use another TLS proxy. --- diff --git a/tlsproxy.c b/tlsproxy.c index a7b4157..39b68df 100644 --- a/tlsproxy.c +++ b/tlsproxy.c @@ -40,6 +40,11 @@ #define MAX_REQUEST_LINE 4096 +/* Proxy hostname and port if specified on the command line. */ +static char *use_proxy_host; +static char *use_proxy_port; + + static void handle_connection(int socket); static int read_http_request(FILE *client_fd, char *request, size_t length); static void send_close_bad_request(FILE *client_fd); @@ -59,19 +64,31 @@ int main(int argc, char **argv) { int client_socket, server_socket; struct sockaddr_in6 server_in; - if (2 != argc) { - printf("Usage: %s port\n", argv[0]); + if (2 != argc && 5 != argc) { + printf("Usage: %s [-proxy hostname port] port\n", argv[0]); return EXIT_FAILURE; } - port = atoi(argv[1]); + port = atoi(argv[5 == argc ? 4 : 1]); if (0 >= port || 0xffff < port) { - printf("Usage: %s port\n", argv[0]); + printf("Usage: %s [-proxy hostname port] port\n", argv[0]); printf("\n"); - printf("Invalid port: %s!\n", argv[1]); + printf("Invalid port: %s!\n", argv[5 == argc ? 4 : 1]); return EXIT_FAILURE; } + if (5 == argc) { + use_proxy_host = strdup(argv[2]); + use_proxy_port = strdup(argv[3]); + if (NULL == use_proxy_host || NULL == use_proxy_port) { + perror("strdup()"); + return EXIT_FAILURE; + } +#ifdef DEBUG + printf("Using proxy: %s:%s.\n", use_proxy_host, use_proxy_port); +#endif + } + server_socket = socket(PF_INET6, SOCK_STREAM, 0); if (-1 == server_socket) { perror("socket()"); @@ -166,7 +183,13 @@ static void handle_connection(int client_socket) { printf(" %s:%s (HTTP 1.%d)\n", host, port, version_minor); #endif - server_socket = connect_to_host(host, port); + /* Connect to proxy server or directly to server. */ + if (NULL != use_proxy_host && NULL != use_proxy_port) { + server_socket = connect_to_host(use_proxy_host, use_proxy_port); + } else { + server_socket = connect_to_host(host, port); + } + if (-1 == server_socket) { send_close_forwarding_failure(client_fd); return; @@ -177,6 +200,39 @@ static void handle_connection(int client_socket) { return; } + /* Connect to proxy if requested (command line option). */ + if (NULL != use_proxy_host && NULL != use_proxy_port) { + fprintf(server_fd, "CONNECT %s:%s HTTP/1.0\r\n", host, port); + fprintf(server_fd, "\r\n"); + + /* Read response line from proxy server. */ + result = read_http_request(server_fd, buffer, sizeof(buffer)); + if (result == -1) { + /* Read error. */ + send_close_forwarding_failure(client_fd); + return; + } else if (result == -2) { + /* EOF */ + fclose(server_fd); + send_close_forwarding_failure(client_fd); + return; + } + + /* Check response of proxy server. */ + if (0 != strncmp(buffer, "HTTP/1.0 200", 12)) { +#ifdef DEBUG + printf(" bad proxy response\n"); +#endif + fclose(server_fd); + send_close_forwarding_failure(client_fd); + return; + } + } + +#ifdef DEBUG + printf(" connection to server established\n"); +#endif + /* We've established a connection, tell the client. */ fprintf(client_fd, "HTTP/1.0 200 Connection established\r\n"); fprintf(client_fd, "\r\n");