2 * tlsproxy is a transparent TLS proxy for HTTPS connections.
4 * Copyright (C) 2011 Simon Ruderich
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 /* socket(), bind(), accept(), listen() */
23 #include <sys/types.h>
24 #include <sys/socket.h>
28 #include <arpa/inet.h>
37 /* Maximum line of the request line. Longer request lines are aborted with an
38 * error. The standard doesn't specify a maximum line length but this should
39 * be a good limit to make processing simpler. */
40 #define MAX_REQUEST_LINE 4096
43 /* Proxy hostname and port if specified on the command line. */
44 static char *use_proxy_host;
45 static char *use_proxy_port;
48 static void handle_connection(int socket);
49 static int read_http_request(FILE *client_fd, char *request, size_t length);
50 static void send_close_bad_request(FILE *client_fd);
51 static void send_close_forwarding_failure(FILE *client_fd);
53 static void transfer_data(int client, int server);
54 static int read_from_write_to(int from, int to);
56 static int connect_to_host(const char *hostname, const char *port);
58 static int parse_request(const char *buffer, char *host, char *port,
62 int main(int argc, char **argv) {
64 int client_socket, server_socket;
65 struct sockaddr_in6 server_in;
67 if (2 != argc && 5 != argc) {
68 printf("Usage: %s [-proxy hostname port] port\n", argv[0]);
72 port = atoi(argv[5 == argc ? 4 : 1]);
73 if (0 >= port || 0xffff < port) {
74 printf("Usage: %s [-proxy hostname port] port\n", argv[0]);
76 printf("Invalid port: %s!\n", argv[5 == argc ? 4 : 1]);
81 use_proxy_host = strdup(argv[2]);
82 use_proxy_port = strdup(argv[3]);
83 if (NULL == use_proxy_host || NULL == use_proxy_port) {
88 printf("Using proxy: %s:%s.\n", use_proxy_host, use_proxy_port);
92 server_socket = socket(PF_INET6, SOCK_STREAM, 0);
93 if (-1 == server_socket) {
99 /* Fast rebinding for debug mode, could cause invalid packets. */
101 int socket_option = 1;
102 setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR,
103 &socket_option, sizeof(socket_option));
107 /* Bind to the listen socket. */
108 memset(&server_in, 0, sizeof(server_in));
109 server_in.sin6_family = AF_INET6; /* IPv6 (and IPv4) */
110 server_in.sin6_addr = in6addr_any; /* bind to any address */
111 server_in.sin6_port = htons((uint16_t)port); /* port to bind to */
112 if (-1 == bind(server_socket, (struct sockaddr *)&server_in,
113 sizeof(server_in))) {
117 /* And accept connections. */
118 if (-1 == listen(server_socket, 5)) {
124 printf("Listening for connections on port %d.\n", port);
128 /* Accept new connection. */
129 client_socket = accept(server_socket, NULL, NULL);
130 if (-1 == client_socket) {
135 handle_connection(client_socket);
141 static void handle_connection(int client_socket) {
143 FILE *client_fd, *server_fd;
145 char buffer[MAX_REQUEST_LINE];
146 char host[MAX_REQUEST_LINE];
152 client_fd = fdopen(client_socket, "a+");
153 if (NULL == client_fd) {
155 close(client_socket);
160 printf("New connection:\n");
163 /* Read request line (CONNECT ..) and headers (they are discarded). */
164 result = read_http_request(client_fd, buffer, sizeof(buffer));
168 } else if (result == -2) {
170 send_close_bad_request(client_fd);
175 printf(" request: %s", buffer);
178 if (0 != parse_request(buffer, host, port, &version_minor)) {
179 send_close_bad_request(client_fd);
181 printf(" bad request\n");
187 printf(" %s:%s (HTTP 1.%d)\n", host, port, version_minor);
190 /* Connect to proxy server or directly to server. */
191 if (NULL != use_proxy_host && NULL != use_proxy_port) {
192 server_socket = connect_to_host(use_proxy_host, use_proxy_port);
194 server_socket = connect_to_host(host, port);
197 if (-1 == server_socket) {
198 send_close_forwarding_failure(client_fd);
201 server_fd = fdopen(server_socket, "a+");
202 if (NULL == server_fd) {
203 send_close_forwarding_failure(client_fd);
207 /* Connect to proxy if requested (command line option). */
208 if (NULL != use_proxy_host && NULL != use_proxy_port) {
209 fprintf(server_fd, "CONNECT %s:%s HTTP/1.0\r\n", host, port);
210 fprintf(server_fd, "\r\n");
212 /* Read response line from proxy server. */
213 result = read_http_request(server_fd, buffer, sizeof(buffer));
216 send_close_forwarding_failure(client_fd);
218 } else if (result == -2) {
221 send_close_forwarding_failure(client_fd);
225 /* Check response of proxy server. */
226 if (0 != strncmp(buffer, "HTTP/1.0 200", 12)) {
228 printf(" bad proxy response\n");
231 send_close_forwarding_failure(client_fd);
237 printf(" connection to server established\n");
240 /* We've established a connection, tell the client. */
241 fprintf(client_fd, "HTTP/1.0 200 Connection established\r\n");
242 fprintf(client_fd, "\r\n");
245 /* And transfer all data between client and server transparently. */
246 transfer_data(client_socket, server_socket);
252 /* Read HTTP request line and headers (ignored).
254 * On success 0 is returned, -1 on client error (we close client descriptor in
255 * this case), -2 on unexpected EOF.
257 static int read_http_request(FILE *client_fd, char *request, size_t length) {
258 char buffer[MAX_REQUEST_LINE];
260 if (NULL == fgets(request, (int)length, client_fd)) {
261 if (ferror(client_fd)) {
262 perror("fgets(), request");
270 while (NULL != fgets(buffer, MAX_REQUEST_LINE, client_fd)) {
272 if (0 == strcmp(buffer, "\n") || 0 == strcmp(buffer, "\r\n")) {
276 if (ferror(client_fd)) {
277 perror("fgets(), header");
285 static void send_close_bad_request(FILE *client_fd) {
286 fprintf(client_fd, "HTTP/1.0 400 Bad Request\r\n");
287 fprintf(client_fd, "\r\n");
290 static void send_close_forwarding_failure(FILE *client_fd) {
291 fprintf(client_fd, "HTTP/1.0 503 Forwarding failure\r\n");
292 fprintf(client_fd, "\r\n");
297 /* Transfer data between client and server sockets until one closes the
299 static void transfer_data(int client, int server) {
300 struct pollfd fds[2];
302 fds[0].events = POLLIN | POLLPRI | POLLHUP | POLLERR;
305 fds[1].events = POLLIN | POLLPRI | POLLHUP | POLLERR;
309 int result = poll(fds, 2, 0);
315 /* Data available from client. */
316 if (fds[0].revents & POLLIN || fds[0].revents & POLLPRI) {
317 if (0 != read_from_write_to(client, server)) {
318 /* EOF (or other error) */
322 /* Data available from server. */
323 if (fds[1].revents & POLLIN || fds[1].revents & POLLPRI) {
324 if (0 != read_from_write_to(server, client)) {
325 /* EOF (or other error) */
330 /* Client closed connection. */
331 if (fds[0].revents & POLLERR || fds[0].revents & POLLHUP) {
334 /* Server closed connection. */
335 if (fds[1].revents & POLLERR || fds[1].revents & POLLHUP) {
341 /* Read available data from socket from and write it to socket to. At maximum
342 * 4096 bytes are read/written. */
343 static int read_from_write_to(int from, int to) {
345 ssize_t size_written;
348 size_read = read(from, buffer, sizeof(buffer));
354 if (0 == size_read) {
358 size_written = write(to, buffer, (size_t)size_read);
359 if (0 > size_written) {
363 if (size_read != size_written) {
364 printf("only written %ld of %ld bytes!\n", size_read, size_written);
372 static int connect_to_host(const char *hostname, const char *port) {
373 struct addrinfo gai_hints;
374 struct addrinfo *gai_result;
378 struct addrinfo *server;
380 if (NULL == hostname || NULL == port) {
384 /* Get IP of hostname server. */
385 memset(&gai_hints, 0, sizeof(gai_hints));
386 gai_hints.ai_family = AF_UNSPEC;
387 gai_hints.ai_socktype = SOCK_STREAM;
388 gai_hints.ai_protocol = 0;
389 gai_hints.ai_flags = AI_NUMERICSERV /* given port is numeric */
390 | AI_ADDRCONFIG /* supported by this computer */
391 | AI_V4MAPPED; /* support IPv4 through IPv6 */
392 gai_return = getaddrinfo(hostname, port, &gai_hints, &gai_result);
393 if (0 != gai_return) {
394 perror("getaddrinfo()");
398 /* Now try to connect to each server returned by getaddrinfo(), use the
399 * first successful connect. */
400 for (server = gai_result; NULL != server; server = server->ai_next) {
401 server_socket = socket(server->ai_family,
403 server->ai_protocol);
404 if (-1 == server_socket) {
405 perror("socket(), trying next");
409 if (-1 != connect(server_socket, server->ai_addr,
410 server->ai_addrlen)) {
413 perror("connect(), trying next");
415 close(server_socket);
417 /* Make sure we free the result from getaddrinfo(). */
418 freeaddrinfo(gai_result);
420 if (NULL == server) {
421 fprintf(stderr, "no server found, aborting\n");
425 return server_socket;
429 /* Parse HTTP CONNECT request string and save its parameters.
431 * The following format is expected: "CONNECT host:port HTTP/1.y".
433 * request and host must have the same size! port must be at least 6 bytes
436 static int parse_request(const char *request, char *host, char *port,
437 int *version_minor) {
438 int port_unused; /* just used to verify the port is numeric */
441 /* scanf() doesn't check spaces. */
442 if (0 != strncmp(request, "CONNECT ", 8)) {
445 /* Check request and extract data, "host:port" is not yet separated. */
446 if (2 != sscanf(request, "CONNECT %s HTTP/1.%d",
447 host, version_minor)) {
450 /* Make sure ":port" is there. */
451 if (NULL == (position = strchr(host, ':'))) {
454 /* Make sure port is numeric. */
455 if (1 != sscanf(position + 1, "%d", &port_unused)) {
458 /* Store it in *port. */
459 strncpy(port, position + 1, 5);
461 /* And remove port from host. */