forked from apache/apisix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapisix.lua
531 lines (421 loc) · 15 KB
/
apisix.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
-- Copyright (C) Yuansheng Wang
local require = require
local core = require("apisix.core")
local plugin = require("apisix.plugin")
local service_fetch = require("apisix.http.service").get
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
local load_balancer
local parsed_domain = core.lrucache.new({
ttl = 300, count = 512
})
local _M = {version = 0.3}
function _M.http_init()
require("resty.core")
if require("ffi").os == "Linux" then
require("ngx.re").opt("jit_stack_size", 200 * 1024)
end
require("jit.opt").start("minstitch=2", "maxtrace=4000",
"maxrecord=8000", "sizemcode=64",
"maxmcode=4000", "maxirconst=1000")
--
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)
core.id.init()
end
function _M.http_init_worker()
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
require("apisix.balancer").init_worker()
load_balancer = require("apisix.balancer").run
require("apisix.admin.init").init_worker()
router.http_init_worker()
require("apisix.http.service").init_worker()
plugin.init_worker()
require("apisix.consumer").init_worker()
if core.config == require("apisix.core.config_yaml") then
core.config.init_worker()
end
require("apisix.debug").init_worker()
end
local function run_plugin(phase, plugins, api_ctx)
api_ctx = api_ctx or ngx.ctx.api_ctx
if not api_ctx then
return
end
plugins = plugins or api_ctx.plugins
if not plugins then
return api_ctx
end
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
if phase ~= "log" then
for i = 1, #plugins, 2 do
local phase_fun = plugins[i][phase]
if phase_fun then
local code, body = phase_fun(plugins[i + 1], api_ctx)
if code or body then
core.response.exit(code, body)
end
end
end
return api_ctx
end
for i = 1, #plugins, 2 do
local phase_fun = plugins[i][phase]
if phase_fun then
phase_fun(plugins[i + 1], api_ctx)
end
end
return api_ctx
end
function _M.http_ssl_phase()
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
local ok, err = router.router_ssl.match_and_set(api_ctx)
if not ok then
if err then
core.log.warn("failed to fetch ssl config: ", err)
end
end
end
local function parse_domain_in_up(up, ver)
local local_conf = core.config.local_conf()
local dns_resolver = local_conf and local_conf.apisix and
local_conf.apisix.dns_resolver
local new_nodes = core.table.new(0, 8)
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
local ip_info = core.utils.dns_parse(dns_resolver, host)
core.log.info("parse addr: ", core.json.delay_encode(ip_info),
" resolver: ", core.json.delay_encode(dns_resolver),
" addr: ", addr)
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)
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)
local local_conf = core.config.local_conf()
local dns_resolver = local_conf and local_conf.apisix and
local_conf.apisix.dns_resolver
local new_nodes = core.table.new(0, 8)
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
local ip_info = core.utils.dns_parse(dns_resolver, host)
core.log.info("parse addr: ", core.json.delay_encode(ip_info),
" resolver: ", core.json.delay_encode(dns_resolver),
" addr: ", addr)
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)
end
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
function _M.http_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)
-- load and run global rule
if router.global_rules and router.global_rules.values
and #router.global_rules.values > 0 then
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)
run_plugin("access", plugins, api_ctx)
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
router.router_http.match(api_ctx)
core.log.info("matched 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
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
route, changed = plugin.merge_service_route(service, route)
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
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
parsed_domain(upstream, api_ctx.conf_version,
parse_domain_in_up, upstream)
end
end
elseif route.has_domain then
route = parsed_domain(route, api_ctx.conf_version,
parse_domain_in_route, route)
end
local plugins = core.tablepool.fetch("plugins", 32, 0)
api_ctx.plugins = plugin.filter(route, plugins)
run_plugin("rewrite", plugins, api_ctx)
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
run_plugin("access", plugins, api_ctx)
end
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
if route.value.service_id then
-- core.log.info("matched route: ", core.json.delay_encode(route.value))
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
route, changed = plugin.merge_service_route(service, route)
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
local plugins = core.tablepool.fetch("plugins", 32, 0)
api_ctx.plugins = plugin.filter(route, plugins)
run_plugin("rewrite", plugins, api_ctx)
run_plugin("access", plugins, api_ctx)
end
function _M.http_header_filter_phase()
run_plugin("header_filter")
end
function _M.http_body_filter_phase()
run_plugin("body_filter")
end
function _M.http_log_phase()
local api_ctx = run_plugin("log")
if api_ctx then
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)
end
end
function _M.http_balancer_phase()
local api_ctx = ngx.ctx.api_ctx
if not api_ctx then
core.log.error("invalid api_ctx")
return core.response.exit(500)
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
do
local router
function _M.http_admin()
if not router then
router = admin_init.get()
end
-- core.log.info("uri: ", get_var("uri"), " method: ", get_method())
local ok = router:dispatch(get_var("uri"), {method = get_method()})
if not ok then
ngx_exit(404)
end
end
end -- do
function _M.stream_init()
core.log.info("enter stream_init")
end
function _M.stream_init_worker()
core.log.info("enter stream_init_worker")
router.stream_init_worker()
plugin.init_worker()
load_balancer = require("apisix.balancer").run
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
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)
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)
run_plugin("log")
end
return _M