1 /* tinyproxy - A fast light-weight HTTP proxy
2 * Copyright (C) 2002 Petr Lampa <lampa@fit.vutbr.cz>
3 * Copyright (C) 2008 Robert James Kaes <rjk@wormbytes.ca>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 * This section of code is used for the transparent proxy option. You will
22 * need to configure your firewall to redirect all connections for HTTP
23 * traffic to tinyproxy for this to work properly.
26 #include "transparent-proxy.h"
29 #include "html-error.h"
36 * Build a URL from parts.
38 static int build_url (char **url, const char *host, int port, const char *path)
43 assert (host != NULL);
44 assert (port > 0 && port < 32768);
45 assert (path != NULL);
47 len = strlen (host) + strlen (path) + 14;
48 *url = (char *) safemalloc (len);
52 return snprintf (*url, len, "http://%s:%d%s", host, port, path);
56 do_transparent_proxy (struct conn_s *connptr, hashmap_t hashofheaders,
57 struct request_s *request, struct config_s *conf,
62 size_t ulen = strlen (*url);
64 length = hashmap_entry_by_key (hashofheaders, "host", (void **) &data);
66 struct sockaddr_in dest_addr;
69 (connptr->client_fd, (struct sockaddr *) &dest_addr,
72 "process_request: cannot get destination IP for %d",
74 indicate_http_error (connptr, 400, "Bad Request",
75 "detail", "Unknown destination",
80 request->host = (char *) safemalloc (17);
81 strlcpy (request->host, inet_ntoa (dest_addr.sin_addr), 17);
83 request->port = ntohs (dest_addr.sin_port);
85 request->path = (char *) safemalloc (ulen + 1);
86 strlcpy (request->path, *url, ulen + 1);
88 build_url (url, request->host, request->port, request->path);
89 log_message (LOG_INFO,
90 "process_request: trans IP %s %s for %d",
91 request->method, *url, connptr->client_fd);
93 request->host = (char *) safemalloc (length + 1);
94 if (sscanf (data, "%[^:]:%hu", request->host, &request->port) !=
96 strlcpy (request->host, data, length + 1);
97 request->port = HTTP_PORT;
100 request->path = (char *) safemalloc (ulen + 1);
101 strlcpy (request->path, *url, ulen + 1);
103 build_url (url, request->host, request->port, request->path);
104 log_message (LOG_INFO,
105 "process_request: trans Host %s %s for %d",
106 request->method, *url, connptr->client_fd);
108 if (conf->ipAddr && strcmp (request->host, conf->ipAddr) == 0) {
109 log_message (LOG_ERR,
110 "process_request: destination IP is localhost %d",
112 indicate_http_error (connptr, 400, "Bad Request",
114 "You tried to connect to the machine "
115 "the proxy is running on", "url", *url,