You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by sp...@apache.org on 2022/10/09 02:06:51 UTC

[apisix] branch master updated: feat: proxy-mirror plugin resolve host (#7861)

This is an automated email from the ASF dual-hosted git repository.

spacewander pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix.git


The following commit(s) were added to refs/heads/master by this push:
     new 96eb1bb8b feat: proxy-mirror plugin resolve host (#7861)
96eb1bb8b is described below

commit 96eb1bb8bf8b0d27bedfa90abe5875799cd15c04
Author: Strangevy <st...@foxmail.com>
AuthorDate: Sun Oct 9 10:06:44 2022 +0800

    feat: proxy-mirror plugin resolve host (#7861)
---
 apisix/plugins/proxy-mirror.lua | 27 ++++++++++++-
 t/plugin/proxy-mirror.t         | 90 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 115 insertions(+), 2 deletions(-)

diff --git a/apisix/plugins/proxy-mirror.lua b/apisix/plugins/proxy-mirror.lua
index fcbfba036..74ec93fd1 100644
--- a/apisix/plugins/proxy-mirror.lua
+++ b/apisix/plugins/proxy-mirror.lua
@@ -15,6 +15,8 @@
 -- limitations under the License.
 --
 local core          = require("apisix.core")
+local url           = require("net.url")
+
 local math_random = math.random
 local has_mod, apisix_ngx_client = pcall(require, "resty.apisix.client")
 
@@ -59,9 +61,30 @@ function _M.check_schema(conf)
 end
 
 
+local function resolver_host(prop_host)
+    local url_decoded = url.parse(prop_host)
+    local decoded_host = url_decoded.host
+    if not core.utils.parse_ipv4(decoded_host) and not core.utils.parse_ipv6(decoded_host) then
+        local ip, err = core.resolver.parse_domain(decoded_host)
+
+        if not ip then
+            core.log.error("dns resolver resolves domain: ", decoded_host," error: ", err,
+                            " will continue to use the host: ", decoded_host)
+            return prop_host
+        end
+
+        local host = url_decoded.scheme .. '://' .. ip ..
+            (url_decoded.port and ':' .. url_decoded.port or '')
+        core.log.info(prop_host, " is resolved to: ", host)
+        return host
+    end
+    return prop_host
+end
+
+
 local function enable_mirror(ctx, conf)
-    ctx.var.upstream_mirror_uri =
-        conf.host .. (conf.path or ctx.var.uri) .. ctx.var.is_args .. (ctx.var.args or '')
+    ctx.var.upstream_mirror_uri = resolver_host(conf.host) .. (conf.path or ctx.var.uri) ..
+                                ctx.var.is_args .. (ctx.var.args or '')
 
     if has_mod then
         apisix_ngx_client.enable_mirror()
diff --git a/t/plugin/proxy-mirror.t b/t/plugin/proxy-mirror.t
index a95c238d9..eb5db12eb 100644
--- a/t/plugin/proxy-mirror.t
+++ b/t/plugin/proxy-mirror.t
@@ -696,3 +696,93 @@ passed
 passed
 passed
 passed
+
+
+
+=== TEST 23: set mirror requests host to domain
+--- config
+       location /t {
+           content_by_lua_block {
+               local t = require("lib.test_admin").test
+               local code, body = t('/apisix/admin/routes/1',
+                    ngx.HTTP_PUT,
+                    [[{
+                        "plugins": {
+                            "proxy-mirror": {
+                               "host": "http://httpbin.org",
+                               "path": "/get"
+                            }
+                        },
+                        "upstream": {
+                            "nodes": {
+                                "127.0.0.1:1980": 1
+                            },
+                            "type": "roundrobin"
+                        },
+                        "uri": "/hello"
+                   }]]
+                   )
+
+               if code >= 300 then
+                   ngx.status = code
+               end
+               ngx.say(body)
+           }
+       }
+--- response_body
+passed
+
+
+
+=== TEST 24: hit route resolver domain
+--- request
+GET /hello
+--- response_body
+hello world
+--- error_log_like eval
+qr/http:\/\/httpbin\.org is resolved to: http:\/\/((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})(\.((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})){3}/
+
+
+
+=== TEST 25: set as a domain name that cannot be found.
+--- config
+       location /t {
+           content_by_lua_block {
+               local t = require("lib.test_admin").test
+               local code, body = t('/apisix/admin/routes/1',
+                    ngx.HTTP_PUT,
+                    [[{
+                        "plugins": {
+                            "proxy-mirror": {
+                               "host": "http://not-find-domian.notfind",
+                               "path": "/get"
+                            }
+                        },
+                        "upstream": {
+                            "nodes": {
+                                "127.0.0.1:1980": 1
+                            },
+                            "type": "roundrobin"
+                        },
+                        "uri": "/hello"
+                   }]]
+                   )
+
+               if code >= 300 then
+                   ngx.status = code
+               end
+               ngx.say(body)
+           }
+       }
+--- response_body
+passed
+
+
+
+=== TEST 26: hit route resolver error domain
+--- request
+GET /hello
+--- response_body
+hello world
+--- error_log
+dns resolver resolves domain: not-find-domian.notfind error: