fix phase2 gateway matching and release artifact

This commit is contained in:
hectorzhao
2026-06-28 22:16:49 +08:00
parent a86de6545f
commit 175166a5dc
17 changed files with 2670 additions and 35 deletions
+84 -19
View File
@@ -209,17 +209,22 @@ end
local function match_caller(gateway_id, gateway_json)
local mode = field(gateway_json, 'callerMatchMode') or 'ANY'
if mode == 'ANY' then
return true
return true, 0
end
if mode == 'PREFIXES' then
local prefixes = redis.call('LRANGE', prefix .. ':customer_gateway:' .. gateway_id .. ':caller_prefixes', 0, -1)
local best_len = 0
for _, caller_prefix in ipairs(prefixes) do
if starts_with(caller, caller_prefix) then
return true
local prefix_len = string.len(caller_prefix)
if starts_with(caller, caller_prefix) and prefix_len > best_len then
best_len = prefix_len
end
end
if best_len > 0 then
return true, best_len
end
end
return false
return false, 0
end
local function match_callee(gateway_id, gateway_json)
@@ -334,6 +339,23 @@ local function legacy_policy_route(gateway_id, customer_id)
return nil
end
local function route_customer_gateway(gateway_id, gateway_json, customer_id, real_callee, business_prefix_id, business_prefix)
local line_group_id = field(gateway_json, 'lineGroupId')
if line_group_id and line_group_id ~= '' then
local vendor_route, route_reason = first_vendor_route(line_group_id, real_callee)
if vendor_route then
return result('allow', 'OK', gateway_id, active, line_group_id, 'single_gateway', customer_id, vendor_route[1], vendor_route[2], vendor_route[3], vendor_route[4], real_callee, business_prefix_id, business_prefix, vendor_route[5], vendor_route[6])
end
return result('reject', route_reason, gateway_id, active, line_group_id, 'single_gateway', customer_id, 'none', 'none', 'none', 'none', real_callee, business_prefix_id, business_prefix, caller, real_callee)
end
local legacy = legacy_policy_route(gateway_id, customer_id)
if legacy then
return legacy
end
return result('reject', 'LINE_GROUP_MISSING', gateway_id, active, 'none', 'single_gateway', customer_id, 'none', 'none', 'none', 'none', real_callee, business_prefix_id, business_prefix, caller, real_callee)
end
local candidates = redis.call('LRANGE', prefix .. ':auth:ip:' .. source_ip .. ':gateways', 0, -1)
if #candidates == 0 then
local legacy_gateway_id = redis.call('GET', prefix .. ':auth:ip:' .. source_ip)
@@ -350,6 +372,21 @@ local saw_gateway = false
local saw_customer = false
local saw_caller = false
local saw_business_prefix_required = false
local saw_business_prefix_match = false
local selected_business_prefix_gateway = nil
local selected_business_prefix_gateway_json = nil
local selected_business_prefix_customer_id = nil
local selected_business_prefix_callee = nil
local selected_business_prefix_id = 'none'
local selected_business_prefix = 'none'
local selected_business_prefix_length = -1
local selected_caller_gateway = nil
local selected_caller_gateway_json = nil
local selected_caller_customer_id = nil
local selected_caller_length = -1
local selected_fallback_gateway = nil
local selected_fallback_gateway_json = nil
local selected_fallback_customer_id = nil
for _, gateway_id in ipairs(candidates) do
local gateway_json = redis.call('GET', prefix .. ':customer_gateway:' .. gateway_id)
@@ -358,27 +395,42 @@ for _, gateway_id in ipairs(candidates) do
local customer_id, customer_reason, customer_marker = customer_ok(gateway_id, gateway_json)
if customer_id then
saw_customer = true
if match_caller(gateway_id, gateway_json) then
saw_caller = true
if (field(gateway_json, 'calleeMatchMode') or 'ANY') == 'BUSINESS_PREFIXES' then
saw_business_prefix_required = true
local caller_ok, caller_prefix_length = match_caller(gateway_id, gateway_json)
local callee_mode = field(gateway_json, 'calleeMatchMode') or 'ANY'
if callee_mode == 'BUSINESS_PREFIXES' then
saw_business_prefix_required = true
if caller_ok then
saw_caller = true
end
local real_callee, business_prefix_id, business_prefix = match_callee(gateway_id, gateway_json)
if real_callee then
local line_group_id = field(gateway_json, 'lineGroupId')
if line_group_id and line_group_id ~= '' then
local vendor_route, route_reason = first_vendor_route(line_group_id, real_callee)
if vendor_route then
return result('allow', 'OK', gateway_id, active, line_group_id, 'single_gateway', customer_id, vendor_route[1], vendor_route[2], vendor_route[3], vendor_route[4], real_callee, business_prefix_id, business_prefix, vendor_route[5], vendor_route[6])
saw_business_prefix_match = true
if caller_ok then
local business_prefix_length = string.len(business_prefix or '')
if business_prefix_length > selected_business_prefix_length then
selected_business_prefix_gateway = gateway_id
selected_business_prefix_gateway_json = gateway_json
selected_business_prefix_customer_id = customer_id
selected_business_prefix_callee = real_callee
selected_business_prefix_id = business_prefix_id
selected_business_prefix = business_prefix
selected_business_prefix_length = business_prefix_length
end
return result('reject', route_reason, gateway_id, active, line_group_id, 'single_gateway', customer_id, 'none', 'none', 'none', 'none', real_callee, business_prefix_id, business_prefix, caller, real_callee)
end
local legacy = legacy_policy_route(gateway_id, customer_id)
if legacy then
return legacy
end
elseif caller_ok then
saw_caller = true
if caller_prefix_length > 0 then
if caller_prefix_length > selected_caller_length then
selected_caller_gateway = gateway_id
selected_caller_gateway_json = gateway_json
selected_caller_customer_id = customer_id
selected_caller_length = caller_prefix_length
end
return result('reject', 'LINE_GROUP_MISSING', gateway_id, active, 'none', 'single_gateway', customer_id, 'none', 'none', 'none', 'none', real_callee, business_prefix_id, business_prefix, caller, real_callee)
elseif not selected_fallback_gateway then
selected_fallback_gateway = gateway_id
selected_fallback_gateway_json = gateway_json
selected_fallback_customer_id = customer_id
end
end
else
@@ -387,6 +439,19 @@ for _, gateway_id in ipairs(candidates) do
end
end
if selected_business_prefix_gateway then
return route_customer_gateway(selected_business_prefix_gateway, selected_business_prefix_gateway_json, selected_business_prefix_customer_id, selected_business_prefix_callee, selected_business_prefix_id, selected_business_prefix)
end
if saw_business_prefix_match then
return result('reject', 'CALLER_PREFIX_NOT_MATCHED', candidates[1] or 'none', active, 'none', 'none', 'caller_prefix_not_matched', 'none', 'none', 'none', 'none', raw_callee, 'none', 'none', caller, raw_callee)
end
if selected_caller_gateway then
return route_customer_gateway(selected_caller_gateway, selected_caller_gateway_json, selected_caller_customer_id, raw_callee, 'none', 'none')
end
if selected_fallback_gateway then
return route_customer_gateway(selected_fallback_gateway, selected_fallback_gateway_json, selected_fallback_customer_id, raw_callee, 'none', 'none')
end
if not saw_gateway then
return result('reject', 'GATEWAY_MISSING', candidates[1] or 'none', active, 'none', 'none', 'gateway_missing', 'none', 'none', 'none', 'none', raw_callee, 'none', 'none', caller, raw_callee)
end