Replies: 6 comments 2 replies
-
You can use the |
Beta Was this translation helpful? Give feedback.
-
Hm, yes, but how i can use dns name in proxy section, with ip its worknig nice?
|
Beta Was this translation helpful? Give feedback.
-
You could try this rough and ready PoC patch. The main caveats are
diff --git a/src/nxt_sockaddr.c b/src/nxt_sockaddr.c
index 32941893..2f729532 100644
--- a/src/nxt_sockaddr.c
+++ b/src/nxt_sockaddr.c
@@ -2,20 +2,24 @@
/*
* Copyright (C) Igor Sysoev
* Copyright (C) NGINX, Inc.
*/
+#include <stdbool.h>
+#include <arpa/inet.h>
+
#include <nxt_main.h>
#if (NXT_INET6)
static u_char *nxt_inet6_ntop(u_char *addr, u_char *buf, u_char *end);
#endif
static nxt_sockaddr_t *nxt_sockaddr_unix_parse(nxt_mp_t *mp, nxt_str_t *addr);
static nxt_sockaddr_t *nxt_sockaddr_inet6_parse(nxt_mp_t *mp, nxt_str_t *addr);
static nxt_sockaddr_t *nxt_sockaddr_inet_parse(nxt_mp_t *mp, nxt_str_t *addr);
+static nxt_sockaddr_t *nxt_sockaddr_host_parse(nxt_mp_t *mp, nxt_str_t *addr);
nxt_sockaddr_t *
nxt_sockaddr_cache_alloc(nxt_event_engine_t *engine, nxt_listen_socket_t *ls)
{
@@ -538,10 +542,24 @@ nxt_sockaddr_parse(nxt_mp_t *mp, nxt_str_t *addr)
return sa;
}
+static inline bool is_hostname(const u_char *str)
+{
+ while (*str) {
+ if ((*str != '.' && *str != ':') && (*str < '0' || *str > '9')) {
+ return true;
+ }
+
+ str++;
+ }
+
+ return false;
+}
+
+
nxt_sockaddr_t *
nxt_sockaddr_parse_optport(nxt_mp_t *mp, nxt_str_t *addr)
{
nxt_sockaddr_t *sa;
@@ -554,12 +572,15 @@ nxt_sockaddr_parse_optport(nxt_mp_t *mp, nxt_str_t *addr)
sa = nxt_sockaddr_unix_parse(mp, addr);
} else if (addr->start[0] == '[' || nxt_inet6_probe(addr)) {
sa = nxt_sockaddr_inet6_parse(mp, addr);
- } else {
+ } else if (!is_hostname(addr->start)) {
sa = nxt_sockaddr_inet_parse(mp, addr);
+
+ } else {
+ sa = nxt_sockaddr_host_parse(mp, addr);
}
if (nxt_fast_path(sa != NULL)) {
nxt_sockaddr_text(sa);
}
@@ -768,10 +789,69 @@ nxt_sockaddr_inet_parse(nxt_mp_t *mp, nxt_str_t *addr)
return sa;
}
+static nxt_sockaddr_t *
+nxt_sockaddr_host_parse(nxt_mp_t *mp, nxt_str_t *addr)
+{
+ int err;
+ char ipaddr[NXT_INET6_ADDR_STR_LEN];
+ char ipport[NXT_INET6_ADDR_STR_LEN + 8];
+ char *host, *p;
+ nxt_str_t str;
+ nxt_sockaddr_t *sa;
+ struct addrinfo *res;
+
+ host = strndup((char *)addr->start, addr->length);
+ p = memchr(host, ':', strlen(host));
+ if (p != NULL) {
+ *p = '\0';
+
+ if (strlen(p + 1) > 5) {
+ nxt_thread_log_error(NXT_LOG_ERR, "invalid port in \"%V\"", addr);
+ goto out_free;
+ }
+ }
+
+ err = getaddrinfo(host, NULL, NULL, &res);
+ if (err) {
+ goto out_free;
+ }
+
+ if (res->ai_family == AF_INET) {
+ inet_ntop(AF_INET, &((struct sockaddr_in *)res->ai_addr)->sin_addr,
+ ipaddr, NXT_INET6_ADDR_STR_LEN);
+
+ sprintf(ipport, "%s%s%s", ipaddr, p ? ":" : "", p ? p + 1 : "");
+
+ /* Do this manually as nxt_length() is using sizeof(ipport) */
+ str.start = (u_char *)ipport;
+ str.length = strlen(ipport);
+ sa = nxt_sockaddr_inet_parse(mp, &str);
+#if (NXT_INET6)
+ } else {
+ inet_ntop(AF_INET6, &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr,
+ ipaddr, NXT_INET6_ADDR_STR_LEN);
+
+ sprintf(ipport, "[%s]%s%s", ipaddr, p ? ":" : "", p ? p + 1 : "");
+
+ str.start = (u_char *)ipport;
+ str.length = strlen(ipport);
+ sa = nxt_sockaddr_inet6_parse(mp, &str);
+#endif
+ }
+
+ freeaddrinfo(res);
+
+out_free:
+ free(host);
+
+ return sa;
+}
+
+
in_addr_t
nxt_inet_addr(u_char *buf, size_t length)
{
u_char c, *end;
in_addr_t addr; |
Beta Was this translation helpful? Give feedback.
-
I'd like to add the following, it's a topic of DNS resolver.
|
Beta Was this translation helpful? Give feedback.
-
1 Obviously if you want to use a hostname then you need functioning DNS... 2 Seems totally overkill for this. 3 Yes see caveats. Anyway it was just a quick hack to show what the minimal thing, that would likely work most of the time, would look like. |
Beta Was this translation helpful? Give feedback.
-
thx a lot! |
Beta Was this translation helpful? Give feedback.
-
Hello, how i can redirect to another container, when getting location path contained "api_v2", "api_v3" ?
Trying with redirect to application with proxy, but its not supported
Beta Was this translation helpful? Give feedback.
All reactions