Skip to content

Commit

Permalink
feat: proxy-mirror plugin resolve host (apache#7861) (apache#8356)
Browse files Browse the repository at this point in the history
Co-authored-by: Strangevy <strangevy@foxmail.com>
Fixes apache#8351
  • Loading branch information
spacewander authored Nov 21, 2022
1 parent 1bcc931 commit e3bdf27
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 2 deletions.
27 changes: 25 additions & 2 deletions apisix/plugins/proxy-mirror.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -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()
Expand Down
90 changes: 90 additions & 0 deletions t/plugin/proxy-mirror.t
Original file line number Diff line number Diff line change
Expand Up @@ -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:

0 comments on commit e3bdf27

Please sign in to comment.