2008-06-09 06:50:23 +08:00
|
|
|
/* tinyproxy - A fast light-weight HTTP proxy
|
|
|
|
* Copyright (C) 2002 Petr Lampa <lampa@fit.vutbr.cz>
|
|
|
|
* Copyright (C) 2008 Robert James Kaes <rjk@wormbytes.ca>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This section of code is used for the transparent proxy option. You will
|
|
|
|
* need to configure your firewall to redirect all connections for HTTP
|
|
|
|
* traffic to tinyproxy for this to work properly.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "transparent-proxy.h"
|
|
|
|
#include "conns.h"
|
|
|
|
#include "heap.h"
|
|
|
|
#include "html-error.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "reqs.h"
|
2009-10-02 17:51:42 +08:00
|
|
|
#include "text.h"
|
2009-12-08 06:42:30 +08:00
|
|
|
#include "conf.h"
|
2008-06-09 06:50:23 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Build a URL from parts.
|
|
|
|
*/
|
2009-09-15 03:41:25 +08:00
|
|
|
static int build_url (char **url, const char *host, int port, const char *path)
|
2008-06-09 06:50:23 +08:00
|
|
|
{
|
2009-09-15 03:41:25 +08:00
|
|
|
int len;
|
2008-06-09 06:50:23 +08:00
|
|
|
|
2009-09-15 03:41:25 +08:00
|
|
|
assert (url != NULL);
|
|
|
|
assert (host != NULL);
|
|
|
|
assert (port > 0 && port < 32768);
|
|
|
|
assert (path != NULL);
|
2008-06-09 06:50:23 +08:00
|
|
|
|
2009-09-15 03:41:25 +08:00
|
|
|
len = strlen (host) + strlen (path) + 14;
|
2018-02-25 12:18:46 +08:00
|
|
|
*url = (char *) saferealloc (*url, len);
|
2009-09-15 03:41:25 +08:00
|
|
|
if (*url == NULL)
|
|
|
|
return -1;
|
2008-06-09 06:50:23 +08:00
|
|
|
|
2009-09-15 03:41:25 +08:00
|
|
|
return snprintf (*url, len, "http://%s:%d%s", host, port, path);
|
2008-06-09 06:50:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2020-09-12 20:49:10 +08:00
|
|
|
do_transparent_proxy (struct conn_s *connptr, orderedmap hashofheaders,
|
2008-12-08 21:39:44 +08:00
|
|
|
struct request_s *request, struct config_s *conf,
|
2010-08-17 04:36:20 +08:00
|
|
|
char **url)
|
2008-06-09 06:50:23 +08:00
|
|
|
{
|
2009-09-15 03:41:25 +08:00
|
|
|
socklen_t length;
|
|
|
|
char *data;
|
2010-08-17 04:36:20 +08:00
|
|
|
size_t ulen = strlen (*url);
|
2013-11-23 07:15:48 +08:00
|
|
|
ssize_t i;
|
2008-06-09 06:50:23 +08:00
|
|
|
|
2020-09-12 20:49:10 +08:00
|
|
|
data = orderedmap_find (hashofheaders, "host");
|
|
|
|
if (!data) {
|
2020-09-06 23:22:11 +08:00
|
|
|
union sockaddr_union dest_addr;
|
|
|
|
const void *dest_inaddr;
|
|
|
|
char namebuf[INET6_ADDRSTRLEN+1];
|
|
|
|
int af;
|
2020-03-18 20:31:13 +08:00
|
|
|
length = sizeof(dest_addr);
|
2008-12-01 23:01:11 +08:00
|
|
|
|
2009-09-15 03:41:25 +08:00
|
|
|
if (getsockname
|
2020-09-06 23:22:11 +08:00
|
|
|
(connptr->client_fd, (void *) &dest_addr,
|
2020-03-18 20:31:13 +08:00
|
|
|
&length) < 0 || length > sizeof(dest_addr)) {
|
2020-09-06 23:22:11 +08:00
|
|
|
addr_err:;
|
2009-09-15 03:41:25 +08:00
|
|
|
log_message (LOG_ERR,
|
|
|
|
"process_request: cannot get destination IP for %d",
|
|
|
|
connptr->client_fd);
|
|
|
|
indicate_http_error (connptr, 400, "Bad Request",
|
|
|
|
"detail", "Unknown destination",
|
2010-08-17 04:36:20 +08:00
|
|
|
"url", *url, NULL);
|
2009-09-15 03:41:25 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2009-10-02 17:51:42 +08:00
|
|
|
|
2020-09-06 23:22:11 +08:00
|
|
|
af = length == sizeof(dest_addr.v4) ? AF_INET : AF_INET6;
|
|
|
|
if (af == AF_INET) dest_inaddr = &dest_addr.v4.sin_addr;
|
|
|
|
else dest_inaddr = &dest_addr.v6.sin6_addr;
|
2009-10-02 17:51:42 +08:00
|
|
|
|
2020-09-06 23:22:11 +08:00
|
|
|
if (!inet_ntop(af, dest_inaddr, namebuf, sizeof namebuf))
|
|
|
|
goto addr_err;
|
|
|
|
|
|
|
|
request->host = safestrdup (namebuf);
|
|
|
|
request->port = ntohs (af == AF_INET ? dest_addr.v4.sin_port
|
|
|
|
: dest_addr.v6.sin6_port);
|
2009-10-02 17:51:42 +08:00
|
|
|
|
|
|
|
request->path = (char *) safemalloc (ulen + 1);
|
2010-08-17 04:36:20 +08:00
|
|
|
strlcpy (request->path, *url, ulen + 1);
|
2009-10-02 17:51:42 +08:00
|
|
|
|
2010-08-17 04:36:20 +08:00
|
|
|
build_url (url, request->host, request->port, request->path);
|
2009-09-15 03:41:25 +08:00
|
|
|
log_message (LOG_INFO,
|
|
|
|
"process_request: trans IP %s %s for %d",
|
2010-08-17 04:36:20 +08:00
|
|
|
request->method, *url, connptr->client_fd);
|
2009-09-15 03:41:25 +08:00
|
|
|
} else {
|
2020-09-12 20:49:10 +08:00
|
|
|
length = strlen (data);
|
2009-09-23 10:19:06 +08:00
|
|
|
request->host = (char *) safemalloc (length + 1);
|
2009-09-15 03:41:25 +08:00
|
|
|
if (sscanf (data, "%[^:]:%hu", request->host, &request->port) !=
|
|
|
|
2) {
|
2009-10-02 17:51:42 +08:00
|
|
|
strlcpy (request->host, data, length + 1);
|
2009-09-15 03:41:25 +08:00
|
|
|
request->port = HTTP_PORT;
|
|
|
|
}
|
2009-10-02 17:51:42 +08:00
|
|
|
|
|
|
|
request->path = (char *) safemalloc (ulen + 1);
|
2010-08-17 04:36:20 +08:00
|
|
|
strlcpy (request->path, *url, ulen + 1);
|
2009-10-02 17:51:42 +08:00
|
|
|
|
2010-08-17 04:36:20 +08:00
|
|
|
build_url (url, request->host, request->port, request->path);
|
2009-09-15 03:41:25 +08:00
|
|
|
log_message (LOG_INFO,
|
|
|
|
"process_request: trans Host %s %s for %d",
|
2010-08-17 04:36:20 +08:00
|
|
|
request->method, *url, connptr->client_fd);
|
2008-12-08 21:39:44 +08:00
|
|
|
}
|
2013-11-23 07:15:48 +08:00
|
|
|
|
|
|
|
if (conf->listen_addrs == NULL) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < vector_length(conf->listen_addrs); i++) {
|
|
|
|
const char *addr;
|
|
|
|
|
|
|
|
addr = (char *)vector_getentry(conf->listen_addrs, i, NULL);
|
|
|
|
|
|
|
|
if (addr && strcmp(request->host, addr) == 0) {
|
|
|
|
log_message(LOG_ERR,
|
|
|
|
"transparent: destination IP %s is local "
|
|
|
|
"on socket fd %d",
|
|
|
|
request->host, connptr->client_fd);
|
|
|
|
indicate_http_error(connptr, 400, "Bad Request",
|
|
|
|
"detail",
|
|
|
|
"You tried to connect to the "
|
|
|
|
"machine the proxy is running on",
|
|
|
|
"url", *url, NULL);
|
|
|
|
return 0;
|
|
|
|
}
|
2008-12-08 21:39:44 +08:00
|
|
|
}
|
2008-12-01 23:01:11 +08:00
|
|
|
|
2009-09-15 03:41:25 +08:00
|
|
|
return 1;
|
2008-06-09 06:50:23 +08:00
|
|
|
}
|