move handling of upstream list to new module upstream.{c,h}
Michael
This commit is contained in:
1 parent
56ba3d45bd
commit
fd987e97f0
7 files changed
+265
-198
No files matched your search
+1
-184
@@ -45,6 +45,7 @@
|
||||
#include "vector.h"
|
||||
#include "reverse-proxy.h"
|
||||
#include "transparent-proxy.h"
|
||||
#include "upstream.h"
|
||||
|
||||
/*
|
||||
* Maximum length of a HTTP line
|
||||
@@ -300,190 +301,6 @@ static int extract_ssl_url (const char *url, struct request_s *request)
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef UPSTREAM_SUPPORT
|
||||
/**
|
||||
* Construct an upstream struct from input data.
|
||||
*/
|
||||
static struct upstream *upstream_build (const char *host, int port, const char *domain)
|
||||
{
|
||||
char *ptr;
|
||||
struct upstream *up;
|
||||
|
||||
up = (struct upstream *) safemalloc (sizeof (struct upstream));
|
||||
if (!up) {
|
||||
log_message (LOG_ERR,
|
||||
"Unable to allocate memory in upstream_build()");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
up->host = up->domain = NULL;
|
||||
up->ip = up->mask = 0;
|
||||
|
||||
if (domain == NULL) {
|
||||
if (!host || host[0] == '\0' || port < 1) {
|
||||
log_message (LOG_WARNING,
|
||||
"Nonsense upstream rule: invalid host or port");
|
||||
goto fail;
|
||||
}
|
||||
|
||||
up->host = safestrdup (host);
|
||||
up->port = port;
|
||||
|
||||
log_message (LOG_INFO, "Added upstream %s:%d for [default]",
|
||||
host, port);
|
||||
} else if (host == NULL) {
|
||||
if (!domain || domain[0] == '\0') {
|
||||
log_message (LOG_WARNING,
|
||||
"Nonsense no-upstream rule: empty domain");
|
||||
goto fail;
|
||||
}
|
||||
|
||||
ptr = strchr (domain, '/');
|
||||
if (ptr) {
|
||||
struct in_addr addrstruct;
|
||||
|
||||
*ptr = '\0';
|
||||
if (inet_aton (domain, &addrstruct) != 0) {
|
||||
up->ip = ntohl (addrstruct.s_addr);
|
||||
*ptr++ = '/';
|
||||
|
||||
if (strchr (ptr, '.')) {
|
||||
if (inet_aton (ptr, &addrstruct) != 0)
|
||||
up->mask =
|
||||
ntohl (addrstruct.s_addr);
|
||||
} else {
|
||||
up->mask =
|
||||
~((1 << (32 - atoi (ptr))) - 1);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
up->domain = safestrdup (domain);
|
||||
}
|
||||
|
||||
log_message (LOG_INFO, "Added no-upstream for %s", domain);
|
||||
} else {
|
||||
if (!host || host[0] == '\0' || port < 1 || !domain
|
||||
|| domain == '\0') {
|
||||
log_message (LOG_WARNING,
|
||||
"Nonsense upstream rule: invalid parameters");
|
||||
goto fail;
|
||||
}
|
||||
|
||||
up->host = safestrdup (host);
|
||||
up->port = port;
|
||||
up->domain = safestrdup (domain);
|
||||
|
||||
log_message (LOG_INFO, "Added upstream %s:%d for %s",
|
||||
host, port, domain);
|
||||
}
|
||||
|
||||
return up;
|
||||
|
||||
fail:
|
||||
safefree (up->host);
|
||||
safefree (up->domain);
|
||||
safefree (up);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
* Add an entry to the upstream list
|
||||
*/
|
||||
void upstream_add (const char *host, int port, const char *domain)
|
||||
{
|
||||
struct upstream *up;
|
||||
|
||||
up = upstream_build (host, port, domain);
|
||||
if (up == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!up->domain && !up->ip) { /* always add default to end */
|
||||
struct upstream *tmp = config.upstream_list;
|
||||
|
||||
while (tmp) {
|
||||
if (!tmp->domain && !tmp->ip) {
|
||||
log_message (LOG_WARNING,
|
||||
"Duplicate default upstream");
|
||||
goto upstream_cleanup;
|
||||
}
|
||||
|
||||
if (!tmp->next) {
|
||||
up->next = NULL;
|
||||
tmp->next = up;
|
||||
return;
|
||||
}
|
||||
|
||||
tmp = tmp->next;
|
||||
}
|
||||
}
|
||||
|
||||
up->next = config.upstream_list;
|
||||
config.upstream_list = up;
|
||||
|
||||
return;
|
||||
|
||||
upstream_cleanup:
|
||||
safefree (up->host);
|
||||
safefree (up->domain);
|
||||
safefree (up);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
* Check if a host is in the upstream list
|
||||
*/
|
||||
static struct upstream *upstream_get (char *host)
|
||||
{
|
||||
struct upstream *up = config.upstream_list;
|
||||
|
||||
in_addr_t my_ip = INADDR_NONE;
|
||||
|
||||
while (up) {
|
||||
if (up->domain) {
|
||||
if (strcasecmp (host, up->domain) == 0)
|
||||
break; /* exact match */
|
||||
|
||||
if (up->domain[0] == '.') {
|
||||
char *dot = strchr (host, '.');
|
||||
|
||||
if (!dot && !up->domain[1])
|
||||
break; /* local host matches "." */
|
||||
|
||||
while (dot && strcasecmp (dot, up->domain))
|
||||
dot = strchr (dot + 1, '.');
|
||||
|
||||
if (dot)
|
||||
break; /* subdomain match */
|
||||
}
|
||||
} else if (up->ip) {
|
||||
if (my_ip == INADDR_NONE)
|
||||
my_ip = ntohl (inet_addr (host));
|
||||
|
||||
if ((my_ip & up->mask) == up->ip)
|
||||
break;
|
||||
} else {
|
||||
break; /* No domain or IP, default upstream */
|
||||
}
|
||||
|
||||
up = up->next;
|
||||
}
|
||||
|
||||
if (up && (!up->host || !up->port))
|
||||
up = NULL;
|
||||
|
||||
if (up)
|
||||
log_message (LOG_INFO, "Found proxy %s:%d for %s",
|
||||
up->host, up->port, host);
|
||||
else
|
||||
log_message (LOG_INFO, "No proxy for %s", host);
|
||||
|
||||
return up;
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Create a connection for HTTP connections.
|
||||
*/
|
||||
|
||||
Reference in new issue
Block a user