Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: proxy-mirror plugin resolve host #7861

Merged
merged 5 commits into from
Oct 9, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: