2019-10-31 09:27:28 +08:00
|
|
|
--
|
|
|
|
-- Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
-- contributor license agreements. See the NOTICE file distributed with
|
|
|
|
-- this work for additional information regarding copyright ownership.
|
|
|
|
-- The ASF licenses this file to You under the Apache License, Version 2.0
|
|
|
|
-- (the "License"); you may not use this file except in compliance with
|
|
|
|
-- the License. You may obtain a copy of the License at
|
|
|
|
--
|
|
|
|
-- http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
--
|
|
|
|
-- Unless required by applicable law or agreed to in writing, software
|
|
|
|
-- distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
-- See the License for the specific language governing permissions and
|
|
|
|
-- limitations under the License.
|
|
|
|
--
|
2019-09-18 17:41:44 +08:00
|
|
|
local require = require
|
|
|
|
local core = require("apisix.core")
|
|
|
|
local plugin = require("apisix.plugin")
|
2019-07-12 09:57:34 +08:00
|
|
|
local service_fetch = require("apisix.http.service").get
|
2019-09-18 17:41:44 +08:00
|
|
|
local admin_init = require("apisix.admin.init")
|
|
|
|
local get_var = require("resty.ngxvar").fetch
|
|
|
|
local router = require("apisix.router")
|
|
|
|
local ipmatcher = require("resty.ipmatcher")
|
|
|
|
local ngx = ngx
|
|
|
|
local get_method = ngx.req.get_method
|
|
|
|
local ngx_exit = ngx.exit
|
|
|
|
local math = math
|
|
|
|
local error = error
|
|
|
|
local ipairs = ipairs
|
|
|
|
local pairs = pairs
|
|
|
|
local tostring = tostring
|
2019-07-17 23:52:15 +08:00
|
|
|
local load_balancer
|
2019-04-11 12:02:16 +08:00
|
|
|
|
2019-09-18 17:41:44 +08:00
|
|
|
|
2020-03-06 10:30:32 +08:00
|
|
|
local parsed_domain
|
2020-03-02 16:04:15 +08:00
|
|
|
|
|
|
|
|
2019-09-18 17:41:44 +08:00
|
|
|
local _M = {version = 0.3}
|
2019-04-10 11:08:25 +08:00
|
|
|
|
2019-05-20 10:36:23 +08:00
|
|
|
|
2020-03-06 10:30:32 +08:00
|
|
|
function _M.http_init()
|
2019-04-11 15:00:59 +08:00
|
|
|
require("resty.core")
|
2019-06-03 14:16:27 +08:00
|
|
|
|
|
|
|
if require("ffi").os == "Linux" then
|
|
|
|
require("ngx.re").opt("jit_stack_size", 200 * 1024)
|
|
|
|
end
|
|
|
|
|
2019-04-11 15:00:59 +08:00
|
|
|
require("jit.opt").start("minstitch=2", "maxtrace=4000",
|
|
|
|
"maxrecord=8000", "sizemcode=64",
|
|
|
|
"maxmcode=4000", "maxirconst=1000")
|
2019-06-10 16:32:24 +08:00
|
|
|
|
|
|
|
--
|
|
|
|
local seed, err = core.utils.get_seed_from_urandom()
|
|
|
|
if not seed then
|
|
|
|
core.log.warn('failed to get seed from urandom: ', err)
|
|
|
|
seed = ngx.now() * 1000 + ngx.worker.pid()
|
|
|
|
end
|
|
|
|
math.randomseed(seed)
|
2020-03-06 10:30:32 +08:00
|
|
|
|
2019-06-10 16:32:24 +08:00
|
|
|
core.id.init()
|
2019-04-10 11:08:25 +08:00
|
|
|
end
|
|
|
|
|
2019-05-20 10:36:23 +08:00
|
|
|
|
2019-07-01 17:24:38 +08:00
|
|
|
function _M.http_init_worker()
|
2019-07-17 23:52:15 +08:00
|
|
|
local we = require("resty.worker.events")
|
|
|
|
local ok, err = we.configure({shm = "worker-events", interval = 0.1})
|
|
|
|
if not ok then
|
|
|
|
error("failed to init worker event: " .. err)
|
|
|
|
end
|
|
|
|
|
2019-09-12 13:27:18 +08:00
|
|
|
require("apisix.balancer").init_worker()
|
|
|
|
load_balancer = require("apisix.balancer").run
|
2019-07-12 09:57:34 +08:00
|
|
|
require("apisix.admin.init").init_worker()
|
|
|
|
|
2019-09-12 13:27:18 +08:00
|
|
|
router.http_init_worker()
|
2019-07-12 09:57:34 +08:00
|
|
|
require("apisix.http.service").init_worker()
|
2019-09-16 10:58:27 +08:00
|
|
|
plugin.init_worker()
|
2019-06-06 15:59:30 +08:00
|
|
|
require("apisix.consumer").init_worker()
|
2019-09-09 17:42:20 +08:00
|
|
|
|
|
|
|
if core.config == require("apisix.core.config_yaml") then
|
|
|
|
core.config.init_worker()
|
|
|
|
end
|
2019-10-09 11:26:54 +08:00
|
|
|
|
|
|
|
require("apisix.debug").init_worker()
|
2020-02-19 20:30:34 +08:00
|
|
|
|
|
|
|
local local_conf = core.config.local_conf()
|
|
|
|
local dns_resolver_valid = local_conf and local_conf.apisix and
|
|
|
|
local_conf.apisix.dns_resolver_valid
|
|
|
|
|
|
|
|
parsed_domain = core.lrucache.new({
|
|
|
|
ttl = dns_resolver_valid, count = 512, invalid_stale = true,
|
|
|
|
})
|
2019-05-23 16:35:47 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2019-06-16 07:32:25 +08:00
|
|
|
local function run_plugin(phase, plugins, api_ctx)
|
2019-05-23 16:35:47 +08:00
|
|
|
api_ctx = api_ctx or ngx.ctx.api_ctx
|
2019-05-23 21:57:51 +08:00
|
|
|
if not api_ctx then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2019-06-16 07:32:25 +08:00
|
|
|
plugins = plugins or api_ctx.plugins
|
|
|
|
if not plugins then
|
2019-05-29 10:29:40 +08:00
|
|
|
return api_ctx
|
|
|
|
end
|
|
|
|
|
2019-07-24 09:01:06 +08:00
|
|
|
if phase == "balancer" then
|
|
|
|
local balancer_name = api_ctx.balancer_name
|
|
|
|
local balancer_plugin = api_ctx.balancer_plugin
|
|
|
|
if balancer_name and balancer_plugin then
|
|
|
|
local phase_fun = balancer_plugin[phase]
|
|
|
|
phase_fun(balancer_plugin, api_ctx)
|
|
|
|
return api_ctx
|
|
|
|
end
|
|
|
|
|
|
|
|
for i = 1, #plugins, 2 do
|
|
|
|
local phase_fun = plugins[i][phase]
|
|
|
|
if phase_fun and
|
|
|
|
(not balancer_name or balancer_name == plugins[i].name) then
|
|
|
|
phase_fun(plugins[i + 1], api_ctx)
|
|
|
|
if api_ctx.balancer_name == plugins[i].name then
|
|
|
|
api_ctx.balancer_plugin = plugins[i]
|
|
|
|
return api_ctx
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return api_ctx
|
|
|
|
end
|
|
|
|
|
2019-05-29 10:29:40 +08:00
|
|
|
if phase ~= "log" then
|
2019-06-16 07:32:25 +08:00
|
|
|
for i = 1, #plugins, 2 do
|
|
|
|
local phase_fun = plugins[i][phase]
|
2019-05-29 10:29:40 +08:00
|
|
|
if phase_fun then
|
2019-06-16 07:32:25 +08:00
|
|
|
local code, body = phase_fun(plugins[i + 1], api_ctx)
|
2019-05-29 10:29:40 +08:00
|
|
|
if code or body then
|
|
|
|
core.response.exit(code, body)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return api_ctx
|
2019-05-23 16:35:47 +08:00
|
|
|
end
|
|
|
|
|
2019-06-16 07:32:25 +08:00
|
|
|
for i = 1, #plugins, 2 do
|
|
|
|
local phase_fun = plugins[i][phase]
|
2019-05-28 10:47:53 +08:00
|
|
|
if phase_fun then
|
2019-06-16 07:32:25 +08:00
|
|
|
phase_fun(plugins[i + 1], api_ctx)
|
2019-05-23 16:35:47 +08:00
|
|
|
end
|
|
|
|
end
|
2019-05-29 10:29:40 +08:00
|
|
|
|
|
|
|
return api_ctx
|
2019-04-10 11:08:25 +08:00
|
|
|
end
|
|
|
|
|
2019-05-20 10:36:23 +08:00
|
|
|
|
2019-07-01 17:24:38 +08:00
|
|
|
function _M.http_ssl_phase()
|
2019-06-22 10:44:07 +08:00
|
|
|
local ngx_ctx = ngx.ctx
|
|
|
|
local api_ctx = ngx_ctx.api_ctx
|
|
|
|
|
|
|
|
if api_ctx == nil then
|
|
|
|
api_ctx = core.tablepool.fetch("api_ctx", 0, 32)
|
|
|
|
ngx_ctx.api_ctx = api_ctx
|
|
|
|
end
|
|
|
|
|
2019-10-10 22:28:58 +08:00
|
|
|
local ok, err = router.router_ssl.match_and_set(api_ctx)
|
2019-06-22 10:44:07 +08:00
|
|
|
if not ok then
|
|
|
|
if err then
|
2019-10-10 22:28:58 +08:00
|
|
|
core.log.warn("failed to fetch ssl config: ", err)
|
2019-06-22 10:44:07 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2019-09-18 17:41:44 +08:00
|
|
|
local function parse_domain_in_up(up, ver)
|
2020-03-06 10:30:32 +08:00
|
|
|
local local_conf = core.config.local_conf()
|
|
|
|
local dns_resolver = local_conf and local_conf.apisix and
|
|
|
|
local_conf.apisix.dns_resolver
|
2019-09-18 17:41:44 +08:00
|
|
|
local new_nodes = core.table.new(0, 8)
|
2020-03-06 10:30:32 +08:00
|
|
|
|
2019-09-18 17:41:44 +08:00
|
|
|
for addr, weight in pairs(up.value.nodes) do
|
|
|
|
local host, port = core.utils.parse_addr(addr)
|
|
|
|
if not ipmatcher.parse_ipv4(host) and
|
|
|
|
not ipmatcher.parse_ipv6(host) then
|
2020-02-19 20:30:34 +08:00
|
|
|
local ip_info, err = core.utils.dns_parse(dns_resolver, host)
|
|
|
|
if not ip_info then
|
|
|
|
return nil, err
|
|
|
|
end
|
|
|
|
|
|
|
|
core.log.info("parse addr: ", core.json.delay_encode(ip_info))
|
|
|
|
core.log.info("resolver: ", core.json.delay_encode(dns_resolver))
|
|
|
|
core.log.info("host: ", host)
|
|
|
|
if ip_info.address then
|
2019-09-18 17:41:44 +08:00
|
|
|
new_nodes[ip_info.address .. ":" .. port] = weight
|
|
|
|
core.log.info("dns resolver domain: ", host, " to ",
|
|
|
|
ip_info.address)
|
2020-02-19 20:30:34 +08:00
|
|
|
else
|
|
|
|
return nil, "failed to parse domain in route"
|
2019-09-18 17:41:44 +08:00
|
|
|
end
|
|
|
|
else
|
|
|
|
new_nodes[addr] = weight
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
up.dns_value = core.table.clone(up.value)
|
|
|
|
up.dns_value.nodes = new_nodes
|
|
|
|
core.log.info("parse upstream which contain domain: ",
|
|
|
|
core.json.delay_encode(up))
|
|
|
|
return up
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
local function parse_domain_in_route(route, ver)
|
2020-03-06 10:30:32 +08:00
|
|
|
local local_conf = core.config.local_conf()
|
|
|
|
local dns_resolver = local_conf and local_conf.apisix and
|
|
|
|
local_conf.apisix.dns_resolver
|
2019-09-18 17:41:44 +08:00
|
|
|
local new_nodes = core.table.new(0, 8)
|
2020-03-06 10:30:32 +08:00
|
|
|
|
2019-09-18 17:41:44 +08:00
|
|
|
for addr, weight in pairs(route.value.upstream.nodes) do
|
|
|
|
local host, port = core.utils.parse_addr(addr)
|
|
|
|
if not ipmatcher.parse_ipv4(host) and
|
|
|
|
not ipmatcher.parse_ipv6(host) then
|
2020-02-19 20:30:34 +08:00
|
|
|
local ip_info, err = core.utils.dns_parse(dns_resolver, host)
|
|
|
|
if not ip_info then
|
|
|
|
return nil, err
|
|
|
|
end
|
|
|
|
|
|
|
|
core.log.info("parse addr: ", core.json.delay_encode(ip_info))
|
|
|
|
core.log.info("resolver: ", core.json.delay_encode(dns_resolver))
|
|
|
|
core.log.info("host: ", host)
|
2019-09-18 17:41:44 +08:00
|
|
|
if ip_info and ip_info.address then
|
|
|
|
new_nodes[ip_info.address .. ":" .. port] = weight
|
|
|
|
core.log.info("dns resolver domain: ", host, " to ",
|
|
|
|
ip_info.address)
|
2020-02-19 20:30:34 +08:00
|
|
|
else
|
|
|
|
return nil, "failed to parse domain in route"
|
2019-09-18 17:41:44 +08:00
|
|
|
end
|
2020-02-19 20:30:34 +08:00
|
|
|
|
2019-09-18 17:41:44 +08:00
|
|
|
else
|
|
|
|
new_nodes[addr] = weight
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
route.dns_value = core.table.deepcopy(route.value)
|
|
|
|
route.dns_value.upstream.nodes = new_nodes
|
|
|
|
core.log.info("parse route which contain domain: ",
|
|
|
|
core.json.delay_encode(route))
|
|
|
|
return route
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2019-07-01 17:24:38 +08:00
|
|
|
function _M.http_access_phase()
|
2019-04-11 12:02:16 +08:00
|
|
|
local ngx_ctx = ngx.ctx
|
|
|
|
local api_ctx = ngx_ctx.api_ctx
|
|
|
|
|
2019-08-21 23:10:34 +08:00
|
|
|
if not api_ctx then
|
2019-05-29 10:29:40 +08:00
|
|
|
api_ctx = core.tablepool.fetch("api_ctx", 0, 32)
|
2019-06-22 10:44:07 +08:00
|
|
|
ngx_ctx.api_ctx = api_ctx
|
2019-04-11 12:02:16 +08:00
|
|
|
end
|
|
|
|
|
2019-05-28 10:47:53 +08:00
|
|
|
core.ctx.set_vars_meta(api_ctx)
|
2019-07-28 19:37:31 +08:00
|
|
|
|
2019-09-10 15:27:40 +08:00
|
|
|
-- load and run global rule
|
2019-09-27 04:15:56 +08:00
|
|
|
if router.global_rules and router.global_rules.values
|
|
|
|
and #router.global_rules.values > 0 then
|
2019-09-10 15:27:40 +08:00
|
|
|
local plugins = core.tablepool.fetch("plugins", 32, 0)
|
|
|
|
for _, global_rule in ipairs(router.global_rules.values) do
|
|
|
|
api_ctx.conf_type = "global_rule"
|
|
|
|
api_ctx.conf_version = global_rule.modifiedIndex
|
|
|
|
api_ctx.conf_id = global_rule.value.id
|
|
|
|
|
|
|
|
core.table.clear(plugins)
|
|
|
|
api_ctx.plugins = plugin.filter(global_rule, plugins)
|
|
|
|
run_plugin("rewrite", plugins, api_ctx)
|
2019-09-22 06:05:58 +08:00
|
|
|
run_plugin("access", plugins, api_ctx)
|
2019-09-10 15:27:40 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
core.tablepool.release("plugins", plugins)
|
|
|
|
api_ctx.plugins = nil
|
|
|
|
api_ctx.conf_type = nil
|
|
|
|
api_ctx.conf_version = nil
|
|
|
|
api_ctx.conf_id = nil
|
|
|
|
end
|
|
|
|
|
2019-08-04 02:06:42 +08:00
|
|
|
router.router_http.match(api_ctx)
|
2019-05-08 10:38:45 +08:00
|
|
|
|
2019-08-20 22:07:59 +08:00
|
|
|
core.log.info("matched route: ",
|
2019-06-16 09:56:03 +08:00
|
|
|
core.json.delay_encode(api_ctx.matched_route, true))
|
2019-05-27 17:14:06 +08:00
|
|
|
|
|
|
|
local route = api_ctx.matched_route
|
2019-05-29 16:20:02 +08:00
|
|
|
if not route then
|
2020-02-18 15:54:30 +08:00
|
|
|
return core.response.exit(404,
|
|
|
|
{error_msg = "failed to match any routes"})
|
2019-05-29 16:20:02 +08:00
|
|
|
end
|
|
|
|
|
2019-08-21 23:10:34 +08:00
|
|
|
if route.value.service_protocol == "grpc" then
|
|
|
|
return ngx.exec("@grpc_pass")
|
|
|
|
end
|
|
|
|
|
|
|
|
if route.value.service_id then
|
|
|
|
local service = service_fetch(route.value.service_id)
|
|
|
|
if not service then
|
|
|
|
core.log.error("failed to fetch service configuration by ",
|
|
|
|
"id: ", route.value.service_id)
|
|
|
|
return core.response.exit(404)
|
|
|
|
end
|
|
|
|
|
|
|
|
local changed
|
2019-09-22 06:05:58 +08:00
|
|
|
route, changed = plugin.merge_service_route(service, route)
|
2019-08-21 23:10:34 +08:00
|
|
|
api_ctx.matched_route = route
|
|
|
|
|
|
|
|
if changed then
|
|
|
|
api_ctx.conf_type = "route&service"
|
|
|
|
api_ctx.conf_version = route.modifiedIndex .. "&"
|
|
|
|
.. service.modifiedIndex
|
|
|
|
api_ctx.conf_id = route.value.id .. "&"
|
|
|
|
.. service.value.id
|
|
|
|
else
|
|
|
|
api_ctx.conf_type = "service"
|
|
|
|
api_ctx.conf_version = service.modifiedIndex
|
|
|
|
api_ctx.conf_id = service.value.id
|
|
|
|
end
|
|
|
|
else
|
|
|
|
api_ctx.conf_type = "route"
|
|
|
|
api_ctx.conf_version = route.modifiedIndex
|
|
|
|
api_ctx.conf_id = route.value.id
|
|
|
|
end
|
|
|
|
|
2020-01-14 14:37:24 +08:00
|
|
|
local enable_websocket
|
2019-09-18 17:41:44 +08:00
|
|
|
local up_id = route.value.upstream_id
|
|
|
|
if up_id then
|
|
|
|
local upstreams_etcd = core.config.fetch_created_obj("/upstreams")
|
|
|
|
if upstreams_etcd then
|
|
|
|
local upstream = upstreams_etcd:get(tostring(up_id))
|
|
|
|
if upstream.has_domain then
|
2020-02-19 20:30:34 +08:00
|
|
|
local _, err = parsed_domain(upstream, api_ctx.conf_version,
|
|
|
|
parse_domain_in_up, upstream)
|
|
|
|
if err then
|
|
|
|
core.log.error("failed to parse domain in upstream: ", err)
|
|
|
|
return core.response.exit(500)
|
|
|
|
end
|
2019-09-18 17:41:44 +08:00
|
|
|
end
|
2020-01-14 14:37:24 +08:00
|
|
|
|
|
|
|
if upstream.value.enable_websocket then
|
|
|
|
enable_websocket = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
else
|
|
|
|
if route.has_domain then
|
|
|
|
route = parsed_domain(route, api_ctx.conf_version,
|
|
|
|
parse_domain_in_route, route)
|
|
|
|
end
|
|
|
|
|
|
|
|
if route.value.upstream and route.value.upstream.enable_websocket then
|
|
|
|
enable_websocket = true
|
2019-09-18 17:41:44 +08:00
|
|
|
end
|
2020-01-14 14:37:24 +08:00
|
|
|
end
|
2019-09-18 17:41:44 +08:00
|
|
|
|
2020-01-14 14:37:24 +08:00
|
|
|
if enable_websocket then
|
|
|
|
api_ctx.var.upstream_upgrade = api_ctx.var.http_upgrade
|
|
|
|
api_ctx.var.upstream_connection = api_ctx.var.http_connection
|
2019-09-18 17:41:44 +08:00
|
|
|
end
|
|
|
|
|
2019-08-21 23:10:34 +08:00
|
|
|
local plugins = core.tablepool.fetch("plugins", 32, 0)
|
|
|
|
api_ctx.plugins = plugin.filter(route, plugins)
|
|
|
|
|
|
|
|
run_plugin("rewrite", plugins, api_ctx)
|
2019-09-22 06:05:58 +08:00
|
|
|
if api_ctx.consumer then
|
|
|
|
local changed
|
|
|
|
route, changed = plugin.merge_consumer_route(route, api_ctx.consumer)
|
|
|
|
if changed then
|
|
|
|
core.table.clear(api_ctx.plugins)
|
|
|
|
api_ctx.plugins = plugin.filter(route, api_ctx.plugins)
|
|
|
|
end
|
|
|
|
end
|
2019-08-21 23:10:34 +08:00
|
|
|
run_plugin("access", plugins, api_ctx)
|
|
|
|
end
|
|
|
|
|
2019-09-10 15:27:40 +08:00
|
|
|
|
2019-08-21 23:10:34 +08:00
|
|
|
function _M.grpc_access_phase()
|
|
|
|
local ngx_ctx = ngx.ctx
|
|
|
|
local api_ctx = ngx_ctx.api_ctx
|
|
|
|
|
|
|
|
if not api_ctx then
|
|
|
|
api_ctx = core.tablepool.fetch("api_ctx", 0, 32)
|
|
|
|
ngx_ctx.api_ctx = api_ctx
|
|
|
|
end
|
|
|
|
|
|
|
|
core.ctx.set_vars_meta(api_ctx)
|
|
|
|
|
|
|
|
router.router_http.match(api_ctx)
|
|
|
|
|
|
|
|
core.log.info("route: ",
|
|
|
|
core.json.delay_encode(api_ctx.matched_route, true))
|
|
|
|
|
|
|
|
local route = api_ctx.matched_route
|
|
|
|
if not route then
|
|
|
|
return core.response.exit(404)
|
|
|
|
end
|
|
|
|
|
2019-05-27 17:14:06 +08:00
|
|
|
if route.value.service_id then
|
2019-06-15 07:09:26 +08:00
|
|
|
-- core.log.info("matched route: ", core.json.delay_encode(route.value))
|
2019-05-27 17:14:06 +08:00
|
|
|
local service = service_fetch(route.value.service_id)
|
2019-06-14 17:56:13 +08:00
|
|
|
if not service then
|
2019-06-15 07:09:26 +08:00
|
|
|
core.log.error("failed to fetch service configuration by ",
|
2019-06-14 17:56:13 +08:00
|
|
|
"id: ", route.value.service_id)
|
2019-06-15 22:34:26 +08:00
|
|
|
return core.response.exit(404)
|
2019-06-14 17:56:13 +08:00
|
|
|
end
|
|
|
|
|
2019-05-27 17:14:06 +08:00
|
|
|
local changed
|
2019-09-22 06:05:58 +08:00
|
|
|
route, changed = plugin.merge_service_route(service, route)
|
2019-06-05 11:33:37 +08:00
|
|
|
api_ctx.matched_route = route
|
2019-05-27 17:14:06 +08:00
|
|
|
|
|
|
|
if changed then
|
|
|
|
api_ctx.conf_type = "route&service"
|
|
|
|
api_ctx.conf_version = route.modifiedIndex .. "&"
|
|
|
|
.. service.modifiedIndex
|
|
|
|
api_ctx.conf_id = route.value.id .. "&"
|
|
|
|
.. service.value.id
|
|
|
|
else
|
2019-06-05 11:33:37 +08:00
|
|
|
api_ctx.conf_type = "service"
|
2019-06-14 17:56:13 +08:00
|
|
|
api_ctx.conf_version = service.modifiedIndex
|
|
|
|
api_ctx.conf_id = service.value.id
|
2019-05-27 17:14:06 +08:00
|
|
|
end
|
|
|
|
|
2019-05-20 16:50:43 +08:00
|
|
|
else
|
|
|
|
api_ctx.conf_type = "route"
|
2019-05-27 17:14:06 +08:00
|
|
|
api_ctx.conf_version = route.modifiedIndex
|
|
|
|
api_ctx.conf_id = route.value.id
|
2019-05-20 16:50:43 +08:00
|
|
|
end
|
|
|
|
|
2019-06-16 07:32:25 +08:00
|
|
|
local plugins = core.tablepool.fetch("plugins", 32, 0)
|
|
|
|
api_ctx.plugins = plugin.filter(route, plugins)
|
2019-05-08 11:30:16 +08:00
|
|
|
|
2019-06-16 07:32:25 +08:00
|
|
|
run_plugin("rewrite", plugins, api_ctx)
|
|
|
|
run_plugin("access", plugins, api_ctx)
|
2019-04-10 11:08:25 +08:00
|
|
|
end
|
|
|
|
|
2019-06-13 12:01:36 +08:00
|
|
|
|
2019-07-01 17:24:38 +08:00
|
|
|
function _M.http_header_filter_phase()
|
2019-05-23 16:35:47 +08:00
|
|
|
run_plugin("header_filter")
|
2019-04-10 11:08:25 +08:00
|
|
|
end
|
|
|
|
|
2019-09-10 15:27:40 +08:00
|
|
|
|
2019-08-21 23:10:34 +08:00
|
|
|
function _M.http_body_filter_phase()
|
|
|
|
run_plugin("body_filter")
|
|
|
|
end
|
2019-06-13 12:01:36 +08:00
|
|
|
|
2019-09-10 15:27:40 +08:00
|
|
|
|
2019-07-01 17:24:38 +08:00
|
|
|
function _M.http_log_phase()
|
2020-03-25 09:48:12 +08:00
|
|
|
local api_ctx = ngx.ctx.api_ctx
|
|
|
|
if not api_ctx then
|
|
|
|
return
|
|
|
|
end
|
2019-06-22 10:44:07 +08:00
|
|
|
|
2020-03-25 09:48:12 +08:00
|
|
|
if router.global_rules and router.global_rules.values
|
2020-03-26 07:23:19 +08:00
|
|
|
and #router.global_rules.values > 0
|
|
|
|
then
|
2020-03-25 09:48:12 +08:00
|
|
|
local plugins = core.tablepool.fetch("plugins", 32, 0)
|
|
|
|
for _, global_rule in ipairs(router.global_rules.values) do
|
|
|
|
core.table.clear(plugins)
|
2020-03-26 07:23:19 +08:00
|
|
|
plugins = plugin.filter(global_rule, plugins)
|
|
|
|
run_plugin("log", plugins, api_ctx)
|
2019-06-16 07:32:25 +08:00
|
|
|
end
|
2020-03-25 09:48:12 +08:00
|
|
|
core.tablepool.release("plugins", plugins)
|
2019-05-29 10:29:40 +08:00
|
|
|
end
|
2020-03-25 09:48:12 +08:00
|
|
|
|
2020-03-26 07:23:19 +08:00
|
|
|
run_plugin("log", nil, api_ctx)
|
|
|
|
|
2020-03-25 09:48:12 +08:00
|
|
|
if api_ctx.uri_parse_param then
|
|
|
|
core.tablepool.release("uri_parse_param", api_ctx.uri_parse_param)
|
|
|
|
end
|
|
|
|
|
|
|
|
core.ctx.release_vars(api_ctx)
|
|
|
|
if api_ctx.plugins then
|
|
|
|
core.tablepool.release("plugins", api_ctx.plugins)
|
|
|
|
end
|
|
|
|
|
|
|
|
core.tablepool.release("api_ctx", api_ctx)
|
2019-04-10 11:08:25 +08:00
|
|
|
end
|
|
|
|
|
2019-06-13 12:01:36 +08:00
|
|
|
|
2019-07-01 17:24:38 +08:00
|
|
|
function _M.http_balancer_phase()
|
2019-05-17 15:58:15 +08:00
|
|
|
local api_ctx = ngx.ctx.api_ctx
|
2019-06-16 07:32:25 +08:00
|
|
|
if not api_ctx then
|
|
|
|
core.log.error("invalid api_ctx")
|
|
|
|
return core.response.exit(500)
|
2019-05-17 15:58:15 +08:00
|
|
|
end
|
|
|
|
|
2019-07-24 09:01:06 +08:00
|
|
|
-- first time
|
|
|
|
if not api_ctx.balancer_name then
|
|
|
|
run_plugin("balancer", nil, api_ctx)
|
|
|
|
if api_ctx.balancer_name then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if api_ctx.balancer_name and api_ctx.balancer_name ~= "default" then
|
|
|
|
return run_plugin("balancer", nil, api_ctx)
|
|
|
|
end
|
|
|
|
|
|
|
|
api_ctx.balancer_name = "default"
|
2019-05-23 22:38:08 +08:00
|
|
|
load_balancer(api_ctx.matched_route, api_ctx)
|
2019-05-17 15:58:15 +08:00
|
|
|
end
|
|
|
|
|
2019-12-24 14:44:42 +08:00
|
|
|
local function cors_admin()
|
|
|
|
local local_conf = core.config.local_conf()
|
|
|
|
if local_conf.apisix and not local_conf.apisix.enable_admin_cors then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local method = get_method()
|
|
|
|
if method == "OPTIONS" then
|
|
|
|
core.response.set_header("Access-Control-Allow-Origin", "*",
|
2020-02-18 15:54:30 +08:00
|
|
|
"Access-Control-Allow-Methods",
|
|
|
|
"POST, GET, PUT, OPTIONS, DELETE, PATCH",
|
|
|
|
"Access-Control-Max-Age", "3600",
|
|
|
|
"Access-Control-Allow-Headers", "*",
|
|
|
|
"Access-Control-Allow-Credentials", "true",
|
|
|
|
"Content-Length", "0",
|
|
|
|
"Content-Type", "text/plain")
|
2019-12-24 14:44:42 +08:00
|
|
|
ngx_exit(200)
|
|
|
|
end
|
|
|
|
|
|
|
|
core.response.set_header("Access-Control-Allow-Origin", "*",
|
|
|
|
"Access-Control-Allow-Credentials", "true",
|
|
|
|
"Access-Control-Expose-Headers", "*",
|
|
|
|
"Access-Control-Max-Age", "3600")
|
|
|
|
end
|
2019-06-13 12:01:36 +08:00
|
|
|
|
|
|
|
do
|
|
|
|
local router
|
|
|
|
|
2019-07-01 17:24:38 +08:00
|
|
|
function _M.http_admin()
|
2019-06-13 12:01:36 +08:00
|
|
|
if not router then
|
|
|
|
router = admin_init.get()
|
|
|
|
end
|
|
|
|
|
2019-12-24 14:44:42 +08:00
|
|
|
-- add cors rsp header
|
|
|
|
cors_admin()
|
|
|
|
|
2019-06-13 12:01:36 +08:00
|
|
|
-- core.log.info("uri: ", get_var("uri"), " method: ", get_method())
|
2019-08-20 22:07:59 +08:00
|
|
|
local ok = router:dispatch(get_var("uri"), {method = get_method()})
|
2019-06-13 12:01:36 +08:00
|
|
|
if not ok then
|
|
|
|
ngx_exit(404)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end -- do
|
|
|
|
|
|
|
|
|
2020-03-06 10:30:32 +08:00
|
|
|
function _M.stream_init()
|
2019-09-12 13:27:18 +08:00
|
|
|
core.log.info("enter stream_init")
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function _M.stream_init_worker()
|
|
|
|
core.log.info("enter stream_init_worker")
|
|
|
|
router.stream_init_worker()
|
2019-09-16 10:58:27 +08:00
|
|
|
plugin.init_worker()
|
|
|
|
|
2019-09-12 13:27:18 +08:00
|
|
|
load_balancer = require("apisix.balancer").run
|
2020-02-19 20:30:34 +08:00
|
|
|
|
|
|
|
local local_conf = core.config.local_conf()
|
|
|
|
local dns_resolver_valid = local_conf and local_conf.apisix and
|
|
|
|
local_conf.apisix.dns_resolver_valid
|
|
|
|
|
|
|
|
parsed_domain = core.lrucache.new({
|
|
|
|
ttl = dns_resolver_valid, count = 512, invalid_stale = true,
|
|
|
|
})
|
2019-09-12 13:27:18 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function _M.stream_preread_phase()
|
|
|
|
core.log.info("enter stream_preread_phase")
|
|
|
|
|
|
|
|
local ngx_ctx = ngx.ctx
|
|
|
|
local api_ctx = ngx_ctx.api_ctx
|
|
|
|
|
|
|
|
if not api_ctx then
|
|
|
|
api_ctx = core.tablepool.fetch("api_ctx", 0, 32)
|
|
|
|
ngx_ctx.api_ctx = api_ctx
|
|
|
|
end
|
|
|
|
|
|
|
|
core.ctx.set_vars_meta(api_ctx)
|
|
|
|
|
|
|
|
router.router_stream.match(api_ctx)
|
|
|
|
|
|
|
|
core.log.info("matched route: ",
|
|
|
|
core.json.delay_encode(api_ctx.matched_route, true))
|
|
|
|
|
|
|
|
local matched_route = api_ctx.matched_route
|
|
|
|
if not matched_route then
|
|
|
|
return ngx_exit(1)
|
|
|
|
end
|
|
|
|
|
2019-09-16 10:58:27 +08:00
|
|
|
local plugins = core.tablepool.fetch("plugins", 32, 0)
|
|
|
|
api_ctx.plugins = plugin.stream_filter(matched_route, plugins)
|
|
|
|
-- core.log.info("valid plugins: ", core.json.delay_encode(plugins, true))
|
|
|
|
|
|
|
|
run_plugin("preread", plugins, api_ctx)
|
2019-09-12 13:27:18 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function _M.stream_balancer_phase()
|
|
|
|
core.log.info("enter stream_balancer_phase")
|
|
|
|
local api_ctx = ngx.ctx.api_ctx
|
|
|
|
if not api_ctx then
|
|
|
|
core.log.error("invalid api_ctx")
|
|
|
|
return ngx_exit(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- first time
|
|
|
|
if not api_ctx.balancer_name then
|
|
|
|
run_plugin("balancer", nil, api_ctx)
|
|
|
|
if api_ctx.balancer_name then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if api_ctx.balancer_name and api_ctx.balancer_name ~= "default" then
|
|
|
|
return run_plugin("balancer", nil, api_ctx)
|
|
|
|
end
|
|
|
|
|
|
|
|
api_ctx.balancer_name = "default"
|
|
|
|
load_balancer(api_ctx.matched_route, api_ctx)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function _M.stream_log_phase()
|
|
|
|
core.log.info("enter stream_log_phase")
|
|
|
|
-- core.ctx.release_vars(api_ctx)
|
2019-09-16 10:58:27 +08:00
|
|
|
run_plugin("log")
|
2019-09-12 13:27:18 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2019-04-10 11:08:25 +08:00
|
|
|
return _M
|