2018-05-18 21:36:43 +00:00
|
|
|
local balancer_resty = require("balancer.resty")
|
|
|
|
local ck = require("resty.cookie")
|
2019-04-29 13:20:30 +00:00
|
|
|
local ngx_balancer = require("ngx.balancer")
|
|
|
|
local split = require("util.split")
|
2020-01-22 20:19:16 +00:00
|
|
|
local same_site = require("util.same_site")
|
2018-05-18 21:36:43 +00:00
|
|
|
|
2020-06-06 15:07:06 +00:00
|
|
|
local ngx = ngx
|
|
|
|
local pairs = pairs
|
|
|
|
local ipairs = ipairs
|
|
|
|
local string = string
|
|
|
|
local tonumber = tonumber
|
|
|
|
local setmetatable = setmetatable
|
|
|
|
|
2019-08-30 16:07:24 +00:00
|
|
|
local _M = balancer_resty:new()
|
2019-02-19 22:27:08 +00:00
|
|
|
local DEFAULT_COOKIE_NAME = "route"
|
2021-07-29 21:23:19 +00:00
|
|
|
local COOKIE_VALUE_DELIMITER = "|"
|
2018-05-18 21:36:43 +00:00
|
|
|
|
2019-02-19 22:27:08 +00:00
|
|
|
function _M.cookie_name(self)
|
|
|
|
return self.cookie_session_affinity.name or DEFAULT_COOKIE_NAME
|
|
|
|
end
|
|
|
|
|
2019-08-30 16:07:24 +00:00
|
|
|
function _M.new(self)
|
2018-05-18 21:36:43 +00:00
|
|
|
local o = {
|
2019-08-30 16:07:24 +00:00
|
|
|
alternative_backends = nil,
|
|
|
|
cookie_session_affinity = nil,
|
2021-07-29 21:23:19 +00:00
|
|
|
traffic_shaping_policy = nil,
|
|
|
|
backend_key = nil
|
2018-05-18 21:36:43 +00:00
|
|
|
}
|
2019-08-30 16:07:24 +00:00
|
|
|
|
2018-05-18 21:36:43 +00:00
|
|
|
setmetatable(o, self)
|
|
|
|
self.__index = self
|
2019-08-30 16:07:24 +00:00
|
|
|
|
|
|
|
return o
|
2019-08-30 09:40:29 +00:00
|
|
|
end
|
|
|
|
|
2021-07-29 21:23:19 +00:00
|
|
|
function _M.get_cookie_parsed(self)
|
2019-08-30 09:40:29 +00:00
|
|
|
local cookie, err = ck:new()
|
|
|
|
if not cookie then
|
|
|
|
ngx.log(ngx.ERR, err)
|
|
|
|
end
|
|
|
|
|
2021-07-29 21:23:19 +00:00
|
|
|
local result = {
|
|
|
|
upstream_key = nil,
|
|
|
|
backend_key = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
local raw_value = cookie:get(self:cookie_name())
|
|
|
|
if not raw_value then
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
|
|
|
local parsed_value, len = split.split_string(raw_value, COOKIE_VALUE_DELIMITER)
|
|
|
|
if len == 0 then
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
|
|
|
result.upstream_key = parsed_value[1]
|
|
|
|
if len > 1 then
|
|
|
|
result.backend_key = parsed_value[2]
|
|
|
|
end
|
|
|
|
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
|
|
|
function _M.get_cookie(self)
|
|
|
|
return self:get_cookie_parsed().upstream_key
|
2018-05-18 21:36:43 +00:00
|
|
|
end
|
|
|
|
|
2019-08-30 09:40:29 +00:00
|
|
|
function _M.set_cookie(self, value)
|
2018-05-18 21:36:43 +00:00
|
|
|
local cookie, err = ck:new()
|
|
|
|
if not cookie then
|
|
|
|
ngx.log(ngx.ERR, err)
|
|
|
|
end
|
|
|
|
|
2019-02-19 22:27:08 +00:00
|
|
|
local cookie_path = self.cookie_session_affinity.path
|
2018-11-19 14:15:24 +00:00
|
|
|
if not cookie_path then
|
|
|
|
cookie_path = ngx.var.location_path
|
|
|
|
end
|
|
|
|
|
2020-01-22 20:19:16 +00:00
|
|
|
local cookie_samesite = self.cookie_session_affinity.samesite
|
|
|
|
if cookie_samesite then
|
|
|
|
local cookie_conditional_samesite_none = self.cookie_session_affinity.conditional_samesite_none
|
|
|
|
if cookie_conditional_samesite_none
|
|
|
|
and cookie_samesite == "None"
|
|
|
|
and not same_site.same_site_none_compatible(ngx.var.http_user_agent) then
|
|
|
|
cookie_samesite = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-01 22:23:40 +00:00
|
|
|
local cookie_secure = self.cookie_session_affinity.secure
|
|
|
|
if cookie_secure == nil then
|
|
|
|
cookie_secure = ngx.var.https == "on"
|
|
|
|
end
|
|
|
|
|
2018-10-29 07:21:10 +00:00
|
|
|
local cookie_data = {
|
2019-02-19 22:27:08 +00:00
|
|
|
key = self:cookie_name(),
|
2021-07-29 21:23:19 +00:00
|
|
|
value = value .. COOKIE_VALUE_DELIMITER .. self.backend_key,
|
2018-11-19 14:15:24 +00:00
|
|
|
path = cookie_path,
|
2018-05-18 21:36:43 +00:00
|
|
|
httponly = true,
|
2020-02-08 18:54:52 +00:00
|
|
|
samesite = cookie_samesite,
|
2021-09-01 22:23:40 +00:00
|
|
|
secure = cookie_secure,
|
2018-10-29 07:21:10 +00:00
|
|
|
}
|
|
|
|
|
2019-02-19 22:27:08 +00:00
|
|
|
if self.cookie_session_affinity.expires and self.cookie_session_affinity.expires ~= "" then
|
2020-06-06 15:07:06 +00:00
|
|
|
cookie_data.expires = ngx.cookie_time(ngx.time() +
|
|
|
|
tonumber(self.cookie_session_affinity.expires))
|
2018-10-29 07:21:10 +00:00
|
|
|
end
|
|
|
|
|
2019-02-19 22:27:08 +00:00
|
|
|
if self.cookie_session_affinity.maxage and self.cookie_session_affinity.maxage ~= "" then
|
|
|
|
cookie_data.max_age = tonumber(self.cookie_session_affinity.maxage)
|
2018-10-29 07:21:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
local ok
|
|
|
|
ok, err = cookie:set(cookie_data)
|
2018-05-18 21:36:43 +00:00
|
|
|
if not ok then
|
|
|
|
ngx.log(ngx.ERR, err)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-07-29 21:23:19 +00:00
|
|
|
function _M.is_affinitized(self)
|
|
|
|
return self:get_cookie_parsed().backend_key == self.backend_key
|
|
|
|
end
|
|
|
|
|
2019-04-29 13:20:30 +00:00
|
|
|
function _M.get_last_failure()
|
|
|
|
return ngx_balancer.get_last_failure()
|
|
|
|
end
|
|
|
|
|
2019-06-06 20:40:59 +00:00
|
|
|
local function get_failed_upstreams()
|
|
|
|
local indexed_upstream_addrs = {}
|
|
|
|
local upstream_addrs = split.split_upstream_var(ngx.var.upstream_addr) or {}
|
|
|
|
|
|
|
|
for _, addr in ipairs(upstream_addrs) do
|
|
|
|
indexed_upstream_addrs[addr] = true
|
|
|
|
end
|
|
|
|
|
|
|
|
return indexed_upstream_addrs
|
2019-06-06 16:51:38 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
local function should_set_cookie(self)
|
2019-12-28 22:56:13 +00:00
|
|
|
local host = ngx.var.host
|
|
|
|
if ngx.var.server_name == '_' then
|
|
|
|
host = ngx.var.server_name
|
|
|
|
end
|
|
|
|
|
|
|
|
if self.cookie_session_affinity.locations then
|
|
|
|
local locs = self.cookie_session_affinity.locations[host]
|
2019-08-21 23:17:42 +00:00
|
|
|
if locs == nil then
|
|
|
|
-- Based off of wildcard hostname in ../certificate.lua
|
2019-12-28 22:56:13 +00:00
|
|
|
local wildcard_host, _, err = ngx.re.sub(host, "^[^\\.]+\\.", "*.", "jo")
|
2019-08-21 23:17:42 +00:00
|
|
|
if err then
|
|
|
|
ngx.log(ngx.ERR, "error: ", err);
|
2019-08-26 15:22:07 +00:00
|
|
|
elseif wildcard_host then
|
|
|
|
locs = self.cookie_session_affinity.locations[wildcard_host]
|
2019-08-21 23:17:42 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-06 16:51:38 +00:00
|
|
|
if locs ~= nil then
|
|
|
|
for _, path in pairs(locs) do
|
|
|
|
if ngx.var.location_path == path then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2018-05-30 21:14:28 +00:00
|
|
|
function _M.balance(self)
|
2019-06-06 16:51:38 +00:00
|
|
|
local upstream_from_cookie
|
2019-04-29 13:20:30 +00:00
|
|
|
|
2019-09-24 08:46:02 +00:00
|
|
|
local key = self:get_cookie()
|
2019-04-29 13:20:30 +00:00
|
|
|
if key then
|
|
|
|
upstream_from_cookie = self.instance:find(key)
|
|
|
|
end
|
|
|
|
|
2019-06-06 16:51:38 +00:00
|
|
|
local last_failure = self.get_last_failure()
|
2020-06-06 15:07:06 +00:00
|
|
|
local should_pick_new_upstream = last_failure ~= nil and
|
|
|
|
self.cookie_session_affinity.change_on_failure or upstream_from_cookie == nil
|
2019-04-29 13:20:30 +00:00
|
|
|
|
2019-06-06 16:51:38 +00:00
|
|
|
if not should_pick_new_upstream then
|
|
|
|
return upstream_from_cookie
|
2019-04-29 13:20:30 +00:00
|
|
|
end
|
|
|
|
|
2019-07-08 17:51:24 +00:00
|
|
|
local new_upstream
|
|
|
|
|
2019-08-30 09:40:29 +00:00
|
|
|
new_upstream, key = self:pick_new_upstream(get_failed_upstreams())
|
2019-06-06 16:51:38 +00:00
|
|
|
if not new_upstream then
|
|
|
|
ngx.log(ngx.WARN, string.format("failed to get new upstream; using upstream %s", new_upstream))
|
|
|
|
elseif should_set_cookie(self) then
|
2019-09-24 08:46:02 +00:00
|
|
|
self:set_cookie(key)
|
2018-05-18 21:36:43 +00:00
|
|
|
end
|
|
|
|
|
2019-04-29 13:20:30 +00:00
|
|
|
return new_upstream
|
2018-05-18 21:36:43 +00:00
|
|
|
end
|
|
|
|
|
2019-02-19 22:27:08 +00:00
|
|
|
function _M.sync(self, backend)
|
2019-08-30 09:40:29 +00:00
|
|
|
-- reload balancer nodes
|
2019-02-19 22:27:08 +00:00
|
|
|
balancer_resty.sync(self, backend)
|
|
|
|
|
2019-08-30 16:07:24 +00:00
|
|
|
self.traffic_shaping_policy = backend.trafficShapingPolicy
|
|
|
|
self.alternative_backends = backend.alternativeBackends
|
2019-02-19 22:27:08 +00:00
|
|
|
self.cookie_session_affinity = backend.sessionAffinityConfig.cookieSessionAffinity
|
2021-07-29 21:23:19 +00:00
|
|
|
self.backend_key = ngx.md5(ngx.md5(backend.name) .. backend.name)
|
2019-02-19 22:27:08 +00:00
|
|
|
end
|
|
|
|
|
2018-05-18 21:36:43 +00:00
|
|
|
return _M
|