Skip to content

Commit

Permalink
Move the proxy DNS resolver code into gateway/src/resty/resolver/http…
Browse files Browse the repository at this point in the history
….lua

To avoid having to call a resolver when used with proxies, all DNS resolution
should happen inside the connect() method
  • Loading branch information
tkan145 committed Feb 5, 2024
1 parent 4801707 commit f301699
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
7 changes: 4 additions & 3 deletions gateway/src/resty/http/proxy.lua
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ local function connect(request, skip_https_connect)
proxy_uri.port = default_port(proxy_uri)
end

-- Resolve the proxy IP/Port
local proxy_host, proxy_port = httpc:resolve(proxy_uri.host, proxy_uri.port)
local proxy_url = format("%s://%s:%s", proxy_uri.scheme, proxy_host, proxy_port)
local proxy_url = format("%s://%s:%s", proxy_uri.scheme, proxy_uri.host, proxy_uri.port)
local proxy_auth = request.proxy_auth

if scheme == 'http' then
-- Used by http_ng module to send request to 3scale backend through proxy.

-- http proxy needs absolute URL as the request path, lua-resty-http 1.17.1 will
-- construct a path_prefix based on host and port so we only set request path here
--
Expand Down Expand Up @@ -110,6 +110,7 @@ local function connect(request, skip_https_connect)
ngx.log(ngx.DEBUG, 'targeting server ', host, ':', port)
else
-- Connect direct
-- Mostly used by http_ng module to connect 3scale backend module.
local ok, err = httpc:connect(options)
if not ok then return nil, err end

Expand Down
17 changes: 17 additions & 0 deletions gateway/src/resty/resolver/http.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
local resty_http = require 'resty.http'
local resty_resolver = require 'resty.resolver'
local round_robin = require 'resty.balancer.round_robin'
local url_helper = require('resty.url_helper')
local format = string.format

local setmetatable = setmetatable

Expand Down Expand Up @@ -53,6 +55,21 @@ function _M.connect(self, options, ...)
ip, real_port = self:resolve(options.host, options.port)
options.host = ip
options.port = real_port
else
local uri, err = url_helper.parse_url(proxy)
if not uri then
return nil, 'invalid proxy ' .. err
end

-- Resolve the proxy IP/Port
local proxy_host, proxy_port = self:resolve(uri.host, uri.port)
local proxy_url = format("%s://%s:%s", uri.scheme, proxy_host, proxy_port)

if proxy_opts.http_proxy then
options.proxy_opts.http_proxy = proxy_url
elseif proxy_opts.https_proxy then
options.proxy_opts.https_proxy = proxy_url
end
end

local ok, err = resty_http.connect(self, options, ...)
Expand Down

0 comments on commit f301699

Please sign in to comment.