Skip to content

Commit 6998e01

Browse files
authored
http_client: implement NO_PROXY support (#3272)
Signed-off-by: Yu Yi <[email protected]>
1 parent 22346a7 commit 6998e01

File tree

5 files changed

+60
-3
lines changed

5 files changed

+60
-3
lines changed

include/fluent-bit/flb_config.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,16 @@ struct flb_config {
154154
*/
155155
char *http_proxy;
156156

157+
/*
158+
* A comma-separated list of host names that shouldn't go through
159+
* any proxy is set in (only an asterisk, * matches all hosts).
160+
* As a convention (https://curl.se/docs/manual.html), this value can be set
161+
* and respected by `NO_PROXY` environment variable when `HTTP_PROXY` is used.
162+
* Example: NO_PROXY="127.0.0.1,localhost,kubernetes.default.svc"
163+
* Note: only `,` is allowed as seperator between URLs.
164+
*/
165+
char *no_proxy;
166+
157167
/* Chunk I/O Buffering */
158168
void *cio;
159169
char *storage_path;

include/fluent-bit/flb_str.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,13 @@ static inline char *flb_strndup(const char *s, size_t n)
5858
return str;
5959
}
6060

61+
/* emptyval checks whether a string has a non-null value "". */
62+
static inline int flb_str_emptyval(const char *s)
63+
{
64+
if (s != NULL && strcmp(s, "") == 0) {
65+
return FLB_TRUE;
66+
}
67+
return FLB_FALSE;
68+
}
69+
6170
#endif

include/fluent-bit/flb_upstream.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,6 @@ int flb_upstream_set_property(struct flb_config *config,
105105
int flb_upstream_is_async(struct flb_upstream *u);
106106
void flb_upstream_thread_safe(struct flb_upstream *u);
107107
struct mk_list *flb_upstream_get_config_map(struct flb_config *config);
108-
108+
int flb_should_proxy_for_host(const char *host, const char *proxy, const char *no_proxy);
109109

110110
#endif

src/flb_config.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,15 @@ struct flb_config *flb_config_init()
161161
#endif
162162

163163
config->http_proxy = getenv("HTTP_PROXY");
164-
if (config->http_proxy != NULL && strcmp(config->http_proxy, "") == 0) {
164+
if (flb_str_emptyval(config->http_proxy) == FLB_TRUE) {
165165
/* Proxy should not be set when the `HTTP_PROXY` is set to "" */
166166
config->http_proxy = NULL;
167167
}
168+
config->no_proxy = getenv("NO_PROXY");
169+
if (flb_str_emptyval(config->no_proxy) == FLB_TRUE || config->http_proxy == NULL) {
170+
/* NoProxy should not be set when the `NO_PROXYY` is set to "" or there is no Proxy. */
171+
config->no_proxy = NULL;
172+
}
168173

169174
config->cio = NULL;
170175
config->storage_path = NULL;

src/flb_upstream.c

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ struct flb_upstream *flb_upstream_create(struct flb_config *config,
185185
flb_net_setup_init(&u->net);
186186

187187
/* Set upstream to the http_proxy if it is specified. */
188-
if (config->http_proxy) {
188+
if (flb_should_proxy_for_host(host, config->http_proxy, config->no_proxy) == FLB_TRUE) {
189189
flb_debug("[upstream] config->http_proxy: %s", config->http_proxy);
190190
ret = flb_utils_proxy_url_split(config->http_proxy, &proxy_protocol,
191191
&proxy_username, &proxy_password,
@@ -237,6 +237,39 @@ struct flb_upstream *flb_upstream_create(struct flb_config *config,
237237
return u;
238238
}
239239

240+
/*
241+
* Checks whehter a destinate URL should be proxied.
242+
*/
243+
int flb_should_proxy_for_host(const char *host, const char *proxy, const char *no_proxy)
244+
{
245+
/* No HTTP_PROXY, should not set up proxy for the upstream `host`. */
246+
if (proxy == NULL) {
247+
return FLB_FALSE;
248+
}
249+
250+
/* No NO_PROXY with HTTP_PROXY set, should set up proxy for the upstream `host`. */
251+
if (no_proxy == NULL) {
252+
return FLB_TRUE;
253+
}
254+
255+
/* NO_PROXY=`*`, it matches all hosts. */
256+
if (strcmp(no_proxy, "*") == 0) {
257+
return FLB_FALSE;
258+
}
259+
260+
/* check the URL list in the NO_PROXY */
261+
char *no_proxy_url = strtok(no_proxy, ",");
262+
while (no_proxy_url != NULL) {
263+
if (strcmp(host, no_proxy_url) == 0) {
264+
return FLB_FALSE;
265+
}
266+
no_proxy_url = strtok(NULL, ",");
267+
}
268+
269+
return FLB_TRUE;
270+
}
271+
272+
240273

241274
/* Create an upstream context using a valid URL (protocol, host and port) */
242275
struct flb_upstream *flb_upstream_create_url(struct flb_config *config,

0 commit comments

Comments
 (0)